66[ ![ Php Version Support] ( https://img.shields.io/packagist/php-v/toolkit/pflag )] ( https://packagist.org/packages/toolkit/pflag )
77[ ![ Latest Stable Version] ( http://img.shields.io/packagist/v/toolkit/pflag.svg )] ( https://packagist.org/packages/toolkit/pflag )
88
9- Command line flag parse library
9+ Generic PHP command line flags parse library
1010
1111## Install
1212
@@ -16,13 +16,101 @@ Command line flag parse library
1616composer require toolkit/pflag
1717```
1818
19- ## Use Flags
19+ ## Flags
2020
21- > TODO
21+ Flags - is an cli flags(options&argument) parser and manager.
2222
23- ## Use SFlags
23+ ### Parse CLI Input
24+
25+ write the codes to an php file(see [ example/flags-demo.php] ( example/flags-demo.php ) )
26+
27+ ``` php
28+ use Toolkit\PFlag\Flags;
29+ use Toolkit\PFlag\FlagType;
30+
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);
36+
37+ $fs = Flags::new();
38+ $fs->setScriptFile($scriptFile);
39+
40+ // add options
41+ $fs->addOpt('age', 'a', 'this is a int option', FlagType::INT);
42+ $fs->addOptByRule('name,n', 'string;true;;this is a string option');
43+ $fs->addOptsByRules([
44+ 'tag,t' => 'strings;no;;array option, allow set multi times',
45+ 'f' => 'bool;no;;this is an bool option',
46+ ]);
47+
48+ // add arguments
49+ $fs->addArg('strArg', 'the first arg, is string', 'string', true);
50+ $fs->addArg('arrArg', 'the second arg, is array', 'strings');
51+
52+ // call parse
53+ if (!$fs->parse($flags)) {
54+ return;
55+ }
56+
57+ vdump(
58+ $fs->getOpts(),
59+ $fs->getArgs()
60+ );
61+ ```
62+
63+ ** Run demo:**
64+
65+ ``` bash
66+ php example/sflags-demo.php --name inhere --age 99 --tag go -t php -t java -f arg0 arr0 arr1
67+ ```
2468
25- SFlags - is an simple flags(options&argument) parser
69+ Output:
70+
71+ ``` text
72+ array(4) {
73+ ["name"]=> string(6) "inhere"
74+ ["age"]=> int(99)
75+ ["tag"]=> array(3) {
76+ [0]=> string(2) "go"
77+ [1]=> string(3) "php"
78+ [2]=> string(4) "java"
79+ }
80+ ["f"]=> bool(true)
81+ }
82+ array(2) {
83+ [0]=> string(4) "arg0"
84+ [1]=> array(2) {
85+ [0]=> string(4) "arr0"
86+ [1]=> string(4) "arr1"
87+ }
88+ }
89+ ```
90+
91+ ** Show help**
92+
93+ ``` bash
94+ $ php example/flags-demo.php --help
95+ ```
96+
97+ ## SFlags
98+
99+ SFlags - is an simple flags(options&argument) parser and manager.
100+
101+ > ` SFlags ` only support add option/argument by rule string or define array.
102+
103+ ### Methods
104+
105+ Options:
106+
107+ - ` setOptRules(array $rules) `
108+ - ` addOptRule(string $name, string|array $rule) `
109+
110+ Arguments:
111+
112+ - ` setArgRules(array $rules) `
113+ - ` addArgRule(string $name, string|array $rule) `
26114
27115### Examples
28116
@@ -96,7 +184,7 @@ $fs = SFlags::new();
96184$fs->parseDefined($rawFlags, $optRules, $argRules);
97185```
98186
99- Run demo:
187+ ** Run demo:**
100188
101189``` bash
102190php example/sflags-demo.php --name inhere --age 99 --tag go -t php -t java -f arg0 arr0 arr1
@@ -124,15 +212,21 @@ array(2) {
124212}
125213```
126214
127- ### Get Value
215+ ** Show help**
216+
217+ ``` bash
218+ $ php example/sflags-demo.php --help
219+ ```
220+
221+ ## Get Value
128222
129223** Options**
130224
131225``` php
132- $force = $fs->getOption ('f'); // bool(true)
133- $age = $fs->getOption ('age'); // int(99)
134- $name = $fs->getOption ('name'); // string(inhere)
135- $tags = $fs->getOption ('tags'); // array{"php", "go", "java"}
226+ $force = $fs->getOpt ('f'); // bool(true)
227+ $age = $fs->getOpt ('age'); // int(99)
228+ $name = $fs->getOpt ('name'); // string(inhere)
229+ $tags = $fs->getOpt ('tags'); // array{"php", "go", "java"}
136230```
137231
138232** Arguments**
@@ -145,7 +239,7 @@ $arrArg = $fs->getArg(1); // array{"arr0", "arr1"}
145239$arrArg = $fs->getArg('arrArg'); // array{"arr0", "arr1"}
146240```
147241
148- ### Flag Rule
242+ ## Flag Rule
149243
150244The options/arguments rules
151245
0 commit comments