6
6
[ ![ Php Version Support] ( https://img.shields.io/packagist/php-v/toolkit/pflag )] ( https://packagist.org/packages/toolkit/pflag )
7
7
[ ![ Latest Stable Version] ( http://img.shields.io/packagist/v/toolkit/pflag.svg )] ( https://packagist.org/packages/toolkit/pflag )
8
8
9
- Command line flag parse library
9
+ Generic PHP command line flags parse library
10
10
11
11
## Install
12
12
@@ -16,13 +16,101 @@ Command line flag parse library
16
16
composer require toolkit/pflag
17
17
```
18
18
19
- ## Use Flags
19
+ ## Flags
20
20
21
- > TODO
21
+ Flags - is an cli flags(options&argument) parser and manager.
22
22
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
+ ```
24
68
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) `
26
114
27
115
### Examples
28
116
@@ -96,7 +184,7 @@ $fs = SFlags::new();
96
184
$fs->parseDefined($rawFlags, $optRules, $argRules);
97
185
```
98
186
99
- Run demo:
187
+ ** Run demo:**
100
188
101
189
``` bash
102
190
php example/sflags-demo.php --name inhere --age 99 --tag go -t php -t java -f arg0 arr0 arr1
@@ -124,15 +212,21 @@ array(2) {
124
212
}
125
213
```
126
214
127
- ### Get Value
215
+ ** Show help**
216
+
217
+ ``` bash
218
+ $ php example/sflags-demo.php --help
219
+ ```
220
+
221
+ ## Get Value
128
222
129
223
** Options**
130
224
131
225
``` 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"}
136
230
```
137
231
138
232
** Arguments**
@@ -145,7 +239,7 @@ $arrArg = $fs->getArg(1); // array{"arr0", "arr1"}
145
239
$arrArg = $fs->getArg('arrArg'); // array{"arr0", "arr1"}
146
240
```
147
241
148
- ### Flag Rule
242
+ ## Flag Rule
149
243
150
244
The options/arguments rules
151
245
0 commit comments