Skip to content

Commit 0695802

Browse files
committed
refact: refacting the flag and sflag common logic
1 parent 71bad8a commit 0695802

14 files changed

+1195
-786
lines changed

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ $flags = ['--name', 'inhere', '--age', '99', '--tag', 'php', '-t', 'go', '--tag'
3333

3434
$optRules = [
3535
'name', // string
36-
'age' => 'int,required', // set required
36+
'age' => 'int;required', // set required
3737
'tag,t' => FlagType::ARRAY,
3838
'f' => FlagType::BOOL,
3939
];
@@ -81,7 +81,7 @@ $scriptFile = array_shift($rawFlags);
8181
$optRules = [
8282
// some option rules
8383
'name', // string
84-
'age' => 'int,required', // set required
84+
'age' => 'int;required', // set required
8585
'tag,t' => FlagType::ARRAY,
8686
'f' => FlagType::BOOL,
8787
];
@@ -92,7 +92,8 @@ $argRules = [
9292
'arrArg' => 'array',
9393
];
9494

95-
$fs = SFlags::new()->parseDefined($rawFlags, $optRules, $argRules);
95+
$fs = SFlags::new();
96+
$fs->parseDefined($rawFlags, $optRules, $argRules);
9697
```
9798

9899
Run demo:
@@ -148,7 +149,7 @@ $arrArg = $fs->getArg('arrArg'); // array{"arr0", "arr1"}
148149

149150
The options/arguments rules
150151

151-
- string value is rule(`type,required,default,desc`).
152+
- string value is rule(`type;required;default;desc`).
152153
- array is define item `SFlags::DEFINE_ITEM`
153154
- supportted type see `FlagType::*`
154155

@@ -161,8 +162,8 @@ $rules = [
161162
'long,s' => 'int',
162163
'f' => 'bool',
163164
'long' => FlagType::STRING,
164-
'tags' => 'array', // can also: int[], string[]
165-
'name' => 'type,required,default,the description message', // with default, desc, required
165+
'tags' => 'array', // can also: ints, strings
166+
'name' => 'type;required;default;the description message', // with default, desc, required
166167
]
167168
```
168169

example/flag-demo.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php declare(strict_types=1);
2+
3+
/**
4+
* This file is part of toolkit/pflag.
5+
*
6+
* @link https://github.com/php-toolkit
7+
* @author https://github.com/inhere
8+
* @license MIT
9+
*/
10+
11+
use Toolkit\PFlag\Flags;
12+
use Toolkit\PFlag\FlagType;
13+
14+
require dirname(__DIR__) . '/test/bootstrap.php';
15+
16+
// run demo:
17+
// php example/sflags-demo.php --name inhere --age 99 --tag go -t php -t java -f arg0 arr0 arr1
18+
$flags = $_SERVER['argv'];
19+
// NOTICE: must shift first element.
20+
$scriptFile = array_shift($flags);
21+
22+
$fs = Flags::new();
23+
$fs->setScriptFile($scriptFile);
24+
25+
// add options
26+
$fs->addOpt('age', 'a', 'this is a int option', FlagType::INT);
27+
$fs->addOptByRule('name,n', 'string;true;;this is a string option');
28+
$fs->addOptsByRules([
29+
'tag,t' => 'strings;no;;array option, allow set multi times',
30+
'f' => 'bool;no;;this is an bool option',
31+
]);
32+
33+
// add arguments
34+
$fs->addArg('strArg', 'the first arg, is string', 'string', true);
35+
$fs->addArg('arrArg', 'the second arg, is array', 'strings');
36+
37+
// edump($fs);
38+
if (!$fs->parse($flags)) {
39+
return;
40+
}
41+
42+
vdump(
43+
// $fs->getRawArgs(),
44+
$fs->getOpts(),
45+
$fs->getArgs()
46+
);

example/sflags-demo.php

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php declare(strict_types=1);
2+
23
/**
34
* This file is part of toolkit/pflag.
45
*
@@ -7,40 +8,42 @@
78
* @license MIT
89
*/
910

10-
use Toolkit\PFlag\FlagType;
1111
use Toolkit\PFlag\SFlags;
1212

1313
require dirname(__DIR__) . '/test/bootstrap.php';
1414

1515
// run demo:
1616
// php example/sflags-demo.php --name inhere --age 99 --tag go -t php -t java -f arg0 arr0 arr1
17-
$rawFlags = $_SERVER['argv'];
17+
$flags = $_SERVER['argv'];
1818
// NOTICE: must shift first element.
19-
$scriptFile = array_shift($rawFlags);
19+
$scriptFile = array_shift($flags);
2020

2121
$optRules = [
2222
// some option rules
23-
'name', // string
24-
'age' => 'int,required', // set required
25-
'tag,t' => FlagType::ARRAY,
26-
'f' => FlagType::BOOL,
23+
'name' => 'string;;;this is an string option', // string
24+
'age' => 'int;required;;this is an int option', // set required
25+
'tag,t' => 'strings;no;;array option, allow set multi times',
26+
'f' => 'bool;no;;this is an bool option',
2727
];
2828
$argRules = [
2929
// some argument rules
3030
'string',
3131
// set name
32-
'arrArg' => 'array',
32+
'arrArg' => 'strings;[a,b];;this is an array arg, allow multi value',
3333
];
3434

3535
$fs = SFlags::new();
36-
// $fs = $fs->parseDefined($rawFlags, $optRules, $argRules);
36+
$fs->setScriptFile($scriptFile);
3737

3838
$fs->setOptRules($optRules);
3939
$fs->setArgRules($argRules);
40-
$fs->parse($rawFlags);
40+
41+
if (!$fs->parse($flags)) {
42+
return;
43+
}
4144

4245
vdump(
43-
// $fs->getRawArgs(),
46+
// $fs->getRawArgs(),
4447
$fs->getOpts(),
4548
$fs->getArgs()
4649
);

0 commit comments

Comments
 (0)