Skip to content

Commit 228d42a

Browse files
committed
refactor: refact the flag rule parse logic
1 parent 2db776c commit 228d42a

File tree

13 files changed

+506
-353
lines changed

13 files changed

+506
-353
lines changed

.github/workflows/php.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ jobs:
5454
# Docs: https://getcomposer.org/doc/articles/scripts.md
5555

5656
- name: Run test suite
57-
run: phpunit -v
57+
run: |
58+
phpunit -v
59+
php example flags-demo.php
60+
php example sflags-demo.php
5861
5962
# git status && git log -1 && git fetch --tags --force
6063
# git status && git log -1 && git fetch --prune --unshallow

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,11 @@ use Toolkit\PFlag\Validator\EnumValidator;
7676
$fs->addOpt('age', 'a', 'this is a int option', FlagType::INT);
7777

7878
// - use string rule
79-
$fs->addOptByRule('name,n', 'string;true;;this is a string option');
79+
$fs->addOptByRule('name,n', 'string;this is a string option;true');
8080
// -- add multi option at once.
8181
$fs->addOptsByRules([
82-
'tag,t' => 'strings;no;;array option, allow set multi times',
83-
'f' => 'bool;no;;this is an bool option',
82+
'tag,t' => 'strings;array option, allow set multi times',
83+
'f' => 'bool;this is an bool option',
8484
]);
8585
// - use array rule
8686
/** @see Flags::DEFINE_ITEM for array rule */
@@ -108,7 +108,7 @@ use Toolkit\PFlag\FlagType;
108108
// - quick add
109109
$fs->addArg('strArg1', 'the is string arg and is required', 'string', true);
110110
// - use string rule
111-
$fs->addArgByRule('intArg2', 'int;no;89;this is a int arg and with default value');
111+
$fs->addArgByRule('intArg2', 'int;this is a int arg and with default value;no;89');
112112
// - use Argument object
113113
$arg = Argument::new('arrArg');
114114
// OR $arg->setType(FlagType::ARRAY);
@@ -201,7 +201,7 @@ $flags = ['--name', 'inhere', '--age', '99', '--tag', 'php', '-t', 'go', '--tag'
201201

202202
$optRules = [
203203
'name', // string
204-
'age' => 'int;required', // set required
204+
'age' => 'int;;required', // set required
205205
'tag,t' => FlagType::ARRAY,
206206
'f' => FlagType::BOOL,
207207
];
@@ -249,7 +249,7 @@ $scriptFile = array_shift($rawFlags);
249249
$optRules = [
250250
// some option rules
251251
'name', // string
252-
'age' => 'int;required', // set required
252+
'age' => 'int;;required', // set required
253253
'tag,t' => FlagType::ARRAY,
254254
'f' => FlagType::BOOL,
255255
];
@@ -323,7 +323,7 @@ $arrArg = $fs->getArg('arrArg'); // array{"arr0", "arr1"}
323323

324324
The options/arguments rules
325325

326-
- string value is rule(`type;required;default;desc`).
326+
- string value is rule(`type;desc;required;default;shorts`).
327327
- array is define item `SFlags::DEFINE_ITEM`
328328
- supportted type see `FlagType::*`
329329

@@ -337,7 +337,7 @@ $rules = [
337337
'f' => 'bool',
338338
'long' => FlagType::STRING,
339339
'tags' => 'array', // can also: ints, strings
340-
'name' => 'type;required;default;the description message', // with default, desc, required
340+
'name' => 'type;the description message;required;default', // with desc, default, required
341341
]
342342
```
343343

example/flags-demo.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@
3737
$fs->addOpt('age', 'a', 'this is a int option', FlagType::INT);
3838

3939
// - use string rule
40-
$fs->addOptByRule('name,n', 'string;true;;this is a string option');
40+
$fs->addOptByRule('name,n', 'string;this is a string option;true;');
4141
// -- add multi option at once.
4242
$fs->addOptsByRules([
43-
'tag,t' => 'strings;no;;array option, allow set multi times',
44-
'f' => 'bool;no;;this is an bool option',
43+
'tag,t' => 'strings;array option, allow set multi times',
44+
'f' => 'bool;this is an bool option',
4545
]);
4646
// - use array rule
4747
/** @see Flags::DEFINE_ITEM for array rule */
@@ -63,7 +63,7 @@
6363
// - quick add
6464
$fs->addArg('strArg1', 'the is string arg and is required', 'string', true);
6565
// - use string rule
66-
$fs->addArgByRule('intArg2', 'int;no;89;this is a int arg and with default value');
66+
$fs->addArgByRule('intArg2', 'int;this is a int arg and with default value;no;89');
6767
// - use Argument object
6868
$arg = Argument::new('arrArg');
6969
// OR $arg->setType(FlagType::ARRAY);

example/sflags-demo.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@
2222

2323
$optRules = [
2424
// some option rules
25-
'name' => 'string;;;this is an string option', // string
26-
'age' => 'int;required;;this is an int option', // set required
27-
'tag,t' => 'strings;no;;array option, allow set multi times',
28-
'f' => 'bool;no;;this is an bool option',
25+
'name' => 'string;this is an string option', // string
26+
'age' => 'int;this is an int option;required', // set required
27+
'tag,t' => 'strings;array option, allow set multi times',
28+
'f' => 'bool;this is an bool option',
2929
];
3030
$argRules = [
3131
// some argument rules
3232
'string',
3333
// set name
34-
'arrArg' => 'strings;[a,b];;this is an array arg, allow multi value',
34+
'arrArg' => 'strings;this is an array arg, allow multi value;;[a,b]',
3535
];
3636

3737
$fs = SFlags::new();

0 commit comments

Comments
 (0)