@@ -16,84 +16,136 @@ Generic PHP command line flags parse library
1616composer require toolkit/pflag
1717```
1818
19- ## Flags
19+ ## Flags Usage
2020
2121Flags - 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
2828use 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
53100if (!$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
69124Output:
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
99151SFlags - 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
0 commit comments