Skip to content

Commit 9f8c1d9

Browse files
committed
update: update flags help render logic. update readme
1 parent 6bc9201 commit 9f8c1d9

File tree

9 files changed

+347
-120
lines changed

9 files changed

+347
-120
lines changed

README.md

Lines changed: 93 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -16,84 +16,136 @@ Generic PHP command line flags parse library
1616
composer require toolkit/pflag
1717
```
1818

19-
## Flags
19+
## Flags Usage
2020

2121
Flags - is an cli flags(options&argument) parser and manager.
2222

23-
### Parse CLI Input
23+
> example codes please see [example/flags-demo.php](example/flags-demo.php)
2424
25-
write the codes to an php file(see [example/flags-demo.php](example/flags-demo.php))
25+
### Create Flags
2626

2727
```php
2828
use Toolkit\PFlag\Flags;
29-
use Toolkit\PFlag\FlagType;
3029

31-
// run demo:
32-
// php example/sflags-demo.php --name inhere --age 99 --tag go -t php -t java -f arg0 arr0 arr1
33-
$flags = $_SERVER['argv'];
34-
// NOTICE: must shift first element.
35-
$scriptFile = array_shift($flags);
30+
require dirname(__DIR__) . '/test/bootstrap.php';
3631

3732
$fs = Flags::new();
33+
// with some config
3834
$fs->setScriptFile($scriptFile);
35+
/** @see Flags::$settings */
36+
$fs->setSettings([
37+
'descNlOnOptLen' => 26
38+
]);
39+
```
40+
41+
### Define options
42+
43+
```php
44+
use Toolkit\PFlag\Flag\Option;
45+
use Toolkit\PFlag\FlagType;
46+
use Toolkit\PFlag\Validator\EnumValidator;
3947

4048
// add options
49+
// - quick add
4150
$fs->addOpt('age', 'a', 'this is a int option', FlagType::INT);
51+
52+
// - use string rule
4253
$fs->addOptByRule('name,n', 'string;true;;this is a string option');
54+
// -- add multi option at once.
4355
$fs->addOptsByRules([
4456
'tag,t' => 'strings;no;;array option, allow set multi times',
4557
'f' => 'bool;no;;this is an bool option',
4658
]);
59+
// - use array rule
60+
/** @see Flags::DEFINE_ITEM for array rule */
61+
$fs->addOptByRule('name-is-very-lang', [
62+
'type' => FlagType::STRING,
63+
'desc' => 'option name is to lang, desc will print on newline',
64+
'shorts' => ['d','e','f'],
65+
// TIP: add validator limit input value.
66+
'validator' => EnumValidator::new(['one', 'two', 'three']),
67+
]);
68+
69+
// - use Option
70+
$opt = Option::new('str1', "this is string option, \ndesc has multi line, \nhaha...");
71+
$opt->setDefault('defVal');
72+
$fs->addOption($opt);
73+
```
74+
75+
### Define Arguments
76+
77+
```php
78+
use Toolkit\PFlag\Flag\Argument;
79+
use Toolkit\PFlag\FlagType;
4780

4881
// add arguments
49-
$fs->addArg('strArg', 'the first arg, is string', 'string', true);
50-
$fs->addArg('arrArg', 'the second arg, is array', 'strings');
82+
// - quick add
83+
$fs->addArg('strArg1', 'the is string arg and is required', 'string', true);
84+
// - use string rule
85+
$fs->addArgByRule('intArg2', 'int;no;89;this is a int arg and with default value');
86+
// - use Argument object
87+
$arg = Argument::new('arrArg');
88+
// OR $arg->setType(FlagType::ARRAY);
89+
$arg->setType(FlagType::STRINGS);
90+
$arg->setDesc("this is an array arg,\n allow multi value,\n must define at last");
91+
$fs->addArgument($arg);
92+
```
93+
94+
### Parse Input
95+
96+
```php
97+
use Toolkit\PFlag\Flags;
98+
use Toolkit\PFlag\FlagType;
5199

52-
// call parse
53100
if (!$fs->parse($flags)) {
101+
// on render help
54102
return;
55103
}
56104

57-
vdump(
58-
$fs->getOpts(),
59-
$fs->getArgs()
60-
);
105+
vdump($fs->getOpts(), $fs->getArgs());
61106
```
62107

108+
**Show help**
109+
110+
```bash
111+
$ php example/flags-demo.php --help
112+
```
113+
114+
Output:
115+
116+
![flags-demo](example/images/flags-demo.png)
117+
63118
**Run demo:**
64119

65120
```bash
66-
php example/sflags-demo.php --name inhere --age 99 --tag go -t php -t java -f arg0 arr0 arr1
121+
$ php example/flags-demo.php --name inhere --age 99 --tag go -t php -t java -d one -f arg0 80 arr0 arr1
67122
```
68123

69124
Output:
70125

71126
```text
72-
array(4) {
127+
array(6) {
128+
["str1"]=> string(6) "defVal"
73129
["name"]=> string(6) "inhere"
74130
["age"]=> int(99)
75131
["tag"]=> array(3) {
76132
[0]=> string(2) "go"
77133
[1]=> string(3) "php"
78134
[2]=> string(4) "java"
79135
}
136+
["name-is-very-lang"]=> string(3) "one"
80137
["f"]=> bool(true)
81138
}
82-
array(2) {
139+
array(3) {
83140
[0]=> string(4) "arg0"
84-
[1]=> array(2) {
141+
[1]=> int(80)
142+
[2]=> array(2) {
85143
[0]=> string(4) "arr0"
86144
[1]=> string(4) "arr1"
87145
}
88146
}
89147
```
90148

91-
**Show help**
92-
93-
```bash
94-
$ php example/flags-demo.php --help
95-
```
96-
97149
## SFlags
98150

99151
SFlags - is an simple flags(options&argument) parser and manager.
@@ -263,7 +315,7 @@ $rules = [
263315

264316
**For options**
265317

266-
- option allow set alias/shorts
318+
- option allow set shorts
267319

268320
> TIP: name `long,s` - first is the option name. remaining is short names.
269321
@@ -274,21 +326,22 @@ $rules = [
274326

275327
**Definition item**
276328

277-
The const `SFlags::DEFINE_ITEM`:
329+
The const `Flags::DEFINE_ITEM`:
278330

279331
```php
280-
public const DEFINE_ITEM = [
281-
'name' => '',
282-
'desc' => '',
283-
'type' => FlagType::STRING,
284-
// 'index' => 0, // only for argument
285-
'required' => false,
286-
'default' => null,
287-
'shorts' => [], // only for option
288-
// value validator
289-
'validator' => null,
290-
// 'category' => null
291-
];
332+
public const DEFINE_ITEM = [
333+
'name' => '',
334+
'desc' => '',
335+
'type' => FlagType::STRING,
336+
'showType' => '', // use for show help
337+
// 'index' => 0, // only for argument
338+
'required' => false,
339+
'default' => null,
340+
'shorts' => [], // only for option
341+
// value validator
342+
'validator' => null,
343+
// 'category' => null
344+
];
292345
```
293346

294347
## Unit tests

example/flag-demo.php

Lines changed: 0 additions & 45 deletions
This file was deleted.

example/flags-demo.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php declare(strict_types=1);
2+
3+
/**
4+
* This file is part of toolkit/pflag.
5+
*
6+
* @link https://github.com/php-toolkit
7+
* @author https://github.com/inhere
8+
* @license MIT
9+
*/
10+
11+
use Toolkit\PFlag\Flag\Argument;
12+
use Toolkit\PFlag\Flag\Option;
13+
use Toolkit\PFlag\Flags;
14+
use Toolkit\PFlag\FlagType;
15+
use Toolkit\PFlag\Validator\EnumValidator;
16+
17+
require dirname(__DIR__) . '/test/bootstrap.php';
18+
19+
// run demo:
20+
// php example/flag-demo.php -h
21+
// php example/sflags-demo.php --name inhere --age 99 --tag go -t php -t java -f arg0 arr0 arr1
22+
$flags = $_SERVER['argv'];
23+
// NOTICE: must shift first element.
24+
$scriptFile = array_shift($flags);
25+
26+
$fs = Flags::new();
27+
$fs->setScriptFile($scriptFile);
28+
/** @see Flags::$settings */
29+
$fs->setSettings([
30+
'descNlOnOptLen' => 26
31+
]);
32+
33+
// add options
34+
// - quick add
35+
$fs->addOpt('age', 'a', 'this is a int option', FlagType::INT);
36+
37+
// - use string rule
38+
$fs->addOptByRule('name,n', 'string;true;;this is a string option');
39+
// -- add multi option at once.
40+
$fs->addOptsByRules([
41+
'tag,t' => 'strings;no;;array option, allow set multi times',
42+
'f' => 'bool;no;;this is an bool option',
43+
]);
44+
// - use array rule
45+
/** @see Flags::DEFINE_ITEM for array rule */
46+
$fs->addOptByRule('name-is-very-lang', [
47+
'type' => FlagType::STRING,
48+
'desc' => 'option name is to lang, desc will print on newline',
49+
'shorts' => ['d','e'],
50+
// TIP: add validator limit input value.
51+
'validator' => EnumValidator::new(['one', 'two', 'three']),
52+
]);
53+
54+
// - use Option
55+
$opt = Option::new('str1', "this is string option, \ndesc has multi line, \nhaha...");
56+
$opt->setDefault('defVal');
57+
$fs->addOption($opt);
58+
59+
// add arguments
60+
// - quick add
61+
$fs->addArg('strArg1', 'the is string arg and is required', 'string', true);
62+
// - use string rule
63+
$fs->addArgByRule('intArg2', 'int;no;89;this is a int arg and with default value');
64+
// - use Argument object
65+
$arg = Argument::new('arrArg');
66+
// OR $arg->setType(FlagType::ARRAY);
67+
$arg->setType(FlagType::STRINGS);
68+
$arg->setDesc("this is an array arg,\n allow multi value,\n must define at last");
69+
$fs->addArgument($arg);
70+
71+
// edump($fs);
72+
if (!$fs->parse($flags)) {
73+
// on render help
74+
return;
75+
}
76+
77+
vdump($fs->getOpts(), $fs->getArgs());

example/images/flags-demo.png

83.3 KB
Loading

0 commit comments

Comments
 (0)