Skip to content

Commit 28ff0ea

Browse files
committed
update readme
1 parent 60095f3 commit 28ff0ea

File tree

2 files changed

+117
-118
lines changed

2 files changed

+117
-118
lines changed

README.md

Lines changed: 6 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,6 @@ $fs->addOpt('age', 'a', 'this is a int option', FlagType::INT);
9191
// - use string rule
9292
$fs->addOptByRule('name,n', 'string;this is a string option;true');
9393

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-
10094
// - use array rule
10195
/** @see Flags::DEFINE_ITEM for array rule */
10296
$fs->addOptByRule('name-is-very-lang', [
@@ -107,6 +101,12 @@ $fs->addOptByRule('name-is-very-lang', [
107101
'validator' => EnumValidator::new(['one', 'two', 'three']),
108102
]);
109103

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+
110110
// - use Option
111111
$opt = Option::new('str1', "this is string option, \ndesc has multi line, \nhaha...");
112112
$opt->setDefault('defVal');
@@ -199,118 +199,6 @@ array(3) {
199199

200200
-----------
201201

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-
314202
## Get Value
315203

316204
Get flag value is very simple, use method `getOpt(string $name)` `getArg($nameOrIndex)`.

sflags-usage.md

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# SFlags
2+
3+
## SFlags Usage
4+
5+
SFlags - is an simple flags(options&argument) parser and manager.
6+
7+
### Examples
8+
9+
```php
10+
use Toolkit\PFlag\SFlags;
11+
12+
$flags = ['--name', 'inhere', '--age', '99', '--tag', 'php', '-t', 'go', '--tag', 'java', '-f', 'arg0'];
13+
14+
$optRules = [
15+
'name', // string
16+
'age' => 'int;an int option;required', // set required
17+
'tag,t' => FlagType::ARRAY,
18+
'f' => FlagType::BOOL,
19+
];
20+
$argRules = [
21+
// some argument rules
22+
];
23+
24+
$fs->setOptRules($optRules);
25+
$fs->setArgRules($argRules);
26+
$fs->parse($rawFlags);
27+
// or use
28+
// $fs->parseDefined($flags, $optRules, $argRules);
29+
30+
vdump($fs->getOpts(), $fs->getRawArgs());
31+
```
32+
33+
Output:
34+
35+
```text
36+
array(3) {
37+
["name"]=> string(6) "inhere"
38+
["tag"]=> array(3) {
39+
[0]=> string(3) "php"
40+
[1]=> string(2) "go"
41+
[2]=> string(4) "java"
42+
}
43+
["f"]=> bool(true)
44+
}
45+
array(1) {
46+
[0]=> string(4) "arg0"
47+
}
48+
```
49+
50+
### Parse CLI Input
51+
52+
write the codes to an php file(see [example/sflags-demo.php](example/sflags-demo.php))
53+
54+
```php
55+
use Toolkit\PFlag\SFlags;
56+
57+
$rawFlags = $_SERVER['argv'];
58+
// NOTICE: must shift first element.
59+
$scriptFile = array_shift($rawFlags);
60+
61+
$optRules = [
62+
// some option rules
63+
'name', // string
64+
'age' => 'int;an int option;required', // set required
65+
'tag,t' => FlagType::ARRAY,
66+
'f' => FlagType::BOOL,
67+
];
68+
$argRules = [
69+
// some argument rules
70+
'string',
71+
// set name
72+
'arrArg' => 'array',
73+
];
74+
75+
$fs = SFlags::new();
76+
$fs->parseDefined($rawFlags, $optRules, $argRules);
77+
```
78+
79+
**Run demo:**
80+
81+
```bash
82+
php example/sflags-demo.php --name inhere --age 99 --tag go -t php -t java -f arg0 arr0 arr1
83+
```
84+
85+
Output:
86+
87+
```text
88+
array(4) {
89+
["name"]=> string(6) "inhere"
90+
["age"]=> int(99)
91+
["tag"]=> array(3) {
92+
[0]=> string(2) "go"
93+
[1]=> string(3) "php"
94+
[2]=> string(4) "java"
95+
}
96+
["f"]=> bool(true)
97+
}
98+
array(2) {
99+
[0]=> string(4) "arg0"
100+
[1]=> array(2) {
101+
[0]=> string(4) "arr0"
102+
[1]=> string(4) "arr1"
103+
}
104+
}
105+
```
106+
107+
**Show help**
108+
109+
```bash
110+
$ php example/sflags-demo.php --help
111+
```

0 commit comments

Comments
 (0)