@@ -91,12 +91,6 @@ $fs->addOpt('age', 'a', 'this is a int option', FlagType::INT);
91
91
// - use string rule
92
92
$fs->addOptByRule('name,n', 'string;this is a string option;true');
93
93
94
- // -- add multi option at once.
95
- $fs->addOptsByRules([
96
- 'tag,t' => 'strings;array option, allow set multi times',
97
- 'f' => 'bool;this is an bool option',
98
- ]);
99
-
100
94
// - use array rule
101
95
/** @see Flags::DEFINE_ITEM for array rule */
102
96
$fs->addOptByRule('name-is-very-lang', [
@@ -107,6 +101,12 @@ $fs->addOptByRule('name-is-very-lang', [
107
101
'validator' => EnumValidator::new(['one', 'two', 'three']),
108
102
]);
109
103
104
+ // -- add multi option at once.
105
+ $fs->addOptsByRules([
106
+ 'tag,t' => 'strings;array option, allow set multi times',
107
+ 'f' => 'bool;this is an bool option',
108
+ ]);
109
+
110
110
// - use Option
111
111
$opt = Option::new('str1', "this is string option, \ndesc has multi line, \nhaha...");
112
112
$opt->setDefault('defVal');
@@ -199,118 +199,6 @@ array(3) {
199
199
200
200
-----------
201
201
202
- ## SFlags Usage
203
-
204
- SFlags - is an simple flags(options&argument) parser and manager.
205
-
206
- ### Examples
207
-
208
- ``` php
209
- use Toolkit\PFlag\SFlags;
210
-
211
- $flags = ['--name', 'inhere', '--age', '99', '--tag', 'php', '-t', 'go', '--tag', 'java', '-f', 'arg0'];
212
-
213
- $optRules = [
214
- 'name', // string
215
- 'age' => 'int;an int option;required', // set required
216
- 'tag,t' => FlagType::ARRAY,
217
- 'f' => FlagType::BOOL,
218
- ];
219
- $argRules = [
220
- // some argument rules
221
- ];
222
-
223
- $fs->setOptRules($optRules);
224
- $fs->setArgRules($argRules);
225
- $fs->parse($rawFlags);
226
- // or use
227
- // $fs->parseDefined($flags, $optRules, $argRules);
228
-
229
- vdump($fs->getOpts(), $fs->getRawArgs());
230
- ```
231
-
232
- Output:
233
-
234
- ``` text
235
- array(3) {
236
- ["name"]=> string(6) "inhere"
237
- ["tag"]=> array(3) {
238
- [0]=> string(3) "php"
239
- [1]=> string(2) "go"
240
- [2]=> string(4) "java"
241
- }
242
- ["f"]=> bool(true)
243
- }
244
- array(1) {
245
- [0]=> string(4) "arg0"
246
- }
247
- ```
248
-
249
- ### Parse CLI Input
250
-
251
- write the codes to an php file(see [ example/sflags-demo.php] ( example/sflags-demo.php ) )
252
-
253
- ``` php
254
- use Toolkit\PFlag\SFlags;
255
-
256
- $rawFlags = $_SERVER['argv'];
257
- // NOTICE: must shift first element.
258
- $scriptFile = array_shift($rawFlags);
259
-
260
- $optRules = [
261
- // some option rules
262
- 'name', // string
263
- 'age' => 'int;an int option;required', // set required
264
- 'tag,t' => FlagType::ARRAY,
265
- 'f' => FlagType::BOOL,
266
- ];
267
- $argRules = [
268
- // some argument rules
269
- 'string',
270
- // set name
271
- 'arrArg' => 'array',
272
- ];
273
-
274
- $fs = SFlags::new();
275
- $fs->parseDefined($rawFlags, $optRules, $argRules);
276
- ```
277
-
278
- ** Run demo:**
279
-
280
- ``` bash
281
- php example/sflags-demo.php --name inhere --age 99 --tag go -t php -t java -f arg0 arr0 arr1
282
- ```
283
-
284
- Output:
285
-
286
- ``` text
287
- array(4) {
288
- ["name"]=> string(6) "inhere"
289
- ["age"]=> int(99)
290
- ["tag"]=> array(3) {
291
- [0]=> string(2) "go"
292
- [1]=> string(3) "php"
293
- [2]=> string(4) "java"
294
- }
295
- ["f"]=> bool(true)
296
- }
297
- array(2) {
298
- [0]=> string(4) "arg0"
299
- [1]=> array(2) {
300
- [0]=> string(4) "arr0"
301
- [1]=> string(4) "arr1"
302
- }
303
- }
304
- ```
305
-
306
- ** Show help**
307
-
308
- ``` bash
309
- $ php example/sflags-demo.php --help
310
- ```
311
-
312
- -----------
313
-
314
202
## Get Value
315
203
316
204
Get flag value is very simple, use method ` getOpt(string $name) ` ` getArg($nameOrIndex) ` .
0 commit comments