@@ -16,84 +16,136 @@ Generic PHP command line flags parse library
16
16
composer require toolkit/pflag
17
17
```
18
18
19
- ## Flags
19
+ ## Flags Usage
20
20
21
21
Flags - is an cli flags(options&argument) parser and manager.
22
22
23
- ### Parse CLI Input
23
+ > example codes please see [ example/flags-demo.php ] ( example/flags-demo.php )
24
24
25
- write the codes to an php file(see [ example/flags-demo.php ] ( example/flags-demo.php ) )
25
+ ### Create Flags
26
26
27
27
``` php
28
28
use Toolkit\PFlag\Flags;
29
- use Toolkit\PFlag\FlagType;
30
29
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';
36
31
37
32
$fs = Flags::new();
33
+ // with some config
38
34
$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;
39
47
40
48
// add options
49
+ // - quick add
41
50
$fs->addOpt('age', 'a', 'this is a int option', FlagType::INT);
51
+
52
+ // - use string rule
42
53
$fs->addOptByRule('name,n', 'string;true;;this is a string option');
54
+ // -- add multi option at once.
43
55
$fs->addOptsByRules([
44
56
'tag,t' => 'strings;no;;array option, allow set multi times',
45
57
'f' => 'bool;no;;this is an bool option',
46
58
]);
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;
47
80
48
81
// 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;
51
99
52
- // call parse
53
100
if (!$fs->parse($flags)) {
101
+ // on render help
54
102
return;
55
103
}
56
104
57
- vdump(
58
- $fs->getOpts(),
59
- $fs->getArgs()
60
- );
105
+ vdump($fs->getOpts(), $fs->getArgs());
61
106
```
62
107
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
+
63
118
** Run demo:**
64
119
65
120
``` 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
67
122
```
68
123
69
124
Output:
70
125
71
126
``` text
72
- array(4) {
127
+ array(6) {
128
+ ["str1"]=> string(6) "defVal"
73
129
["name"]=> string(6) "inhere"
74
130
["age"]=> int(99)
75
131
["tag"]=> array(3) {
76
132
[0]=> string(2) "go"
77
133
[1]=> string(3) "php"
78
134
[2]=> string(4) "java"
79
135
}
136
+ ["name-is-very-lang"]=> string(3) "one"
80
137
["f"]=> bool(true)
81
138
}
82
- array(2 ) {
139
+ array(3 ) {
83
140
[0]=> string(4) "arg0"
84
- [1]=> array(2) {
141
+ [1]=> int(80)
142
+ [2]=> array(2) {
85
143
[0]=> string(4) "arr0"
86
144
[1]=> string(4) "arr1"
87
145
}
88
146
}
89
147
```
90
148
91
- ** Show help**
92
-
93
- ``` bash
94
- $ php example/flags-demo.php --help
95
- ```
96
-
97
149
## SFlags
98
150
99
151
SFlags - is an simple flags(options&argument) parser and manager.
@@ -263,7 +315,7 @@ $rules = [
263
315
264
316
** For options**
265
317
266
- - option allow set alias/ shorts
318
+ - option allow set shorts
267
319
268
320
> TIP: name ` long,s ` - first is the option name. remaining is short names.
269
321
@@ -274,21 +326,22 @@ $rules = [
274
326
275
327
** Definition item**
276
328
277
- The const ` SFlags ::DEFINE_ITEM` :
329
+ The const ` Flags ::DEFINE_ITEM` :
278
330
279
331
``` 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
+ ];
292
345
```
293
346
294
347
## Unit tests
0 commit comments