Skip to content

Commit 962dba1

Browse files
committed
refactor: refacting some parse logic
1 parent dddfd34 commit 962dba1

17 files changed

+714
-418
lines changed

README.md

Lines changed: 120 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
[![License](https://img.shields.io/packagist/l/toolkit/pflag.svg?style=flat-square)](LICENSE)
44
[![Php Version](https://img.shields.io/badge/php-%3E=7.2.0-brightgreen.svg?maxAge=2592000)](https://packagist.org/packages/toolkit/pflag)
5-
[![GitHub tag (latest SemVer)](https://img.shields.io/github/tag/toolkit/pflag)](https://github.com/toolkit/pflag)
6-
[![Actions Status](https://github.com/toolkit/pflag/workflows/Unit-Tests/badge.svg)](https://github.com/toolkit/pflag/actions)
5+
[![GitHub tag (latest SemVer)](https://img.shields.io/github/tag/php-toolkit/pflag)](https://github.com/php-toolkit/pflag)
6+
[![Actions Status](https://github.com/php-toolkit/pflag/workflows/Unit-Tests/badge.svg)](https://github.com/php-toolkit/pflag/actions)
77

88
Command line flag parse library
99

@@ -15,9 +15,125 @@ Command line flag parse library
1515
composer require toolkit/pflag
1616
```
1717

18-
## Usage
18+
## Use Flags
1919

20-
TODO
20+
> TODO
21+
22+
## Use SFlags
23+
24+
SFlags - is an simple flags(options&argument) parser
25+
26+
### Examples
27+
28+
```php
29+
use Toolkit\PFlag\SFlags;
30+
31+
$flags = ['--name', 'inhere', '--age', '99', '--tag', 'php', '-t', 'go', '--tag', 'java', '-f', 'arg0'];
32+
33+
$optRules = [
34+
'name', // string
35+
'age' => 'int,required', // set required
36+
'tag,t' => FlagType::ARRAY,
37+
'f' => FlagType::BOOL,
38+
];
39+
$argRules = [
40+
// some argument rules
41+
];
42+
43+
$fs->setOptRules($optRules);
44+
$fs->setArgRules($argRules);
45+
$fs->parse($rawFlags);
46+
// or use
47+
// $fs->parseDefined($flags, $optRules, $argRules);
48+
49+
vdump($fs->getRawArgs(), $fs->getOpts());
50+
```
51+
52+
Output:
53+
54+
```text
55+
array(1) {
56+
[0]=> string(4) "arg0"
57+
}
58+
array(3) {
59+
["name"]=> string(6) "inhere"
60+
["tag"]=> array(3) {
61+
[0]=> string(3) "php"
62+
[1]=> string(2) "go"
63+
[2]=> string(4) "java"
64+
}
65+
["f"]=> bool(true)
66+
}
67+
```
68+
69+
### Parse CLI Input
70+
71+
write the codes to an php file(see [example/sflags-demo.php](example/sflags-demo.php))
72+
73+
```php
74+
use Toolkit\PFlag\SFlags;
75+
76+
$rawFlags = $_SERVER['argv'];
77+
// NOTICE: must shift first element.
78+
$scriptFile = array_shift($rawFlags);
79+
80+
$optRules = [
81+
// some option rules
82+
'name', // string
83+
'age' => 'int,required', // set required
84+
'tag,t' => FlagType::ARRAY,
85+
'f' => FlagType::BOOL,
86+
];
87+
$argRules = [
88+
// some argument rules
89+
'string',
90+
'array',
91+
];
92+
93+
$fs = SFlags::new()->parseDefined($rawFlags, $optRules, $argRules);
94+
```
95+
96+
Run demo:
97+
98+
```bash
99+
php example/sflags-demo.php --name inhere --age 99 --tag go -t php -t java -f arg0 arr0 arr1
100+
```
101+
102+
Output:
103+
104+
```text
105+
$ php example/sflags-demo.php --name inhere --age 99 --tag go -t php -t java -f arg0 arr0 arr1 21-08-24 - 19:38:01
106+
CALL ON /Users/inhere/.kite/vendor/toolkit/pflag/example/sflags-demo.php(44):
107+
array(2) {
108+
[0]=> string(4) "arg0"
109+
[1]=> array(2) {
110+
[0]=> string(4) "arr0"
111+
[1]=> string(4) "arr1"
112+
}
113+
}
114+
array(4) {
115+
["name"]=> string(6) "inhere"
116+
["age"]=> int(99)
117+
["tag"]=> array(3) {
118+
[0]=> string(2) "go"
119+
[1]=> string(3) "php"
120+
[2]=> string(4) "java"
121+
}
122+
["f"]=> bool(true)
123+
}
124+
125+
```
126+
127+
### Get Value
128+
129+
**Options**
130+
131+
```php
132+
$force = $fs->getOption('f'); // bool(true)
133+
$age = $fs->getOption('age'); // int(99)
134+
$name = $fs->getOption('name'); // string(inhere)
135+
$tags = $fs->getOption('tags'); // array{"php", "go", "java"}
136+
```
21137

22138
## License
23139

example/demo.php

Lines changed: 0 additions & 19 deletions
This file was deleted.

example/sflags-demo.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php declare(strict_types=1);
2+
/**
3+
* This file is part of toolkit/pflag.
4+
*
5+
* @link https://github.com/php-toolkit
6+
* @author https://github.com/inhere
7+
* @license MIT
8+
*/
9+
10+
use Toolkit\PFlag\FlagType;
11+
use Toolkit\PFlag\SFlags;
12+
13+
require dirname(__DIR__) . '/test/bootstrap.php';
14+
15+
// run demo:
16+
// php example/sflags-demo.php --name inhere --age 99 --tag go -t php -t java -f arg0 arr0 arr1
17+
$rawFlags = $_SERVER['argv'];
18+
// NOTICE: must shift first element.
19+
$scriptFile = array_shift($rawFlags);
20+
21+
$optRules = [
22+
// some option rules
23+
'name', // string
24+
'age' => 'int,required', // set required
25+
'tag,t' => FlagType::ARRAY,
26+
'f' => FlagType::BOOL,
27+
];
28+
$argRules = [
29+
// some argument rules
30+
'string',
31+
'array',
32+
];
33+
34+
$fs = SFlags::new();
35+
// $fs = $fs->parseDefined($rawFlags, $optRules, $argRules);
36+
37+
$fs->setOptRules($optRules);
38+
$fs->setArgRules($argRules);
39+
$fs->parse($rawFlags);
40+
41+
vdump(
42+
// $fs->getRawArgs(),
43+
$fs->getArgs(),
44+
$fs->getOpts()
45+
);

src/AbstractParser.php

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Toolkit\PFlag;
4+
5+
use Toolkit\Cli\Helper\FlagHelper;
6+
use Toolkit\PFlag\Contract\ParserInterface;
7+
use Toolkit\Stdlib\Obj;
8+
use Toolkit\Stdlib\Obj\Traits\NameAliasTrait;
9+
use Toolkit\Stdlib\Obj\Traits\QuickInitTrait;
10+
use function explode;
11+
use function strpos;
12+
13+
/**
14+
* class AbstractParser
15+
*/
16+
abstract class AbstractParser implements ParserInterface
17+
{
18+
use QuickInitTrait;
19+
use NameAliasTrait;
20+
21+
/**
22+
* @var bool
23+
*/
24+
protected $parsed = false;
25+
26+
/**
27+
* The raw input flags
28+
*
29+
* @var array
30+
*/
31+
protected $rawFlags = [];
32+
33+
/**
34+
* The remaining raw args, after option parsed from {@see $rawFlags}
35+
*
36+
* @var array
37+
*/
38+
protected $rawArgs = [];
39+
40+
/**
41+
* The required option names.
42+
*
43+
* @var array
44+
*/
45+
protected $requiredOpts = [];
46+
47+
// -------------------- settings for parse --------------------
48+
49+
/**
50+
* Special short style
51+
* gnu: `-abc` will expand: `-a -b -c`
52+
* posix: `-abc` will expand: `-a=bc`
53+
*
54+
* @var string
55+
*/
56+
protected $shortStyle = 'posix';
57+
58+
/**
59+
* Whether stop parse option on first argument
60+
*
61+
* @var bool
62+
*/
63+
protected $stopOnFistArg = true;
64+
65+
protected $errOnUndefined = false;
66+
67+
/**
68+
* Whether stop parse option on found undefined option
69+
*
70+
* @var bool
71+
*/
72+
protected $stopOnUndefined = true;
73+
74+
protected $skipUndefined = false;
75+
76+
protected $ignoreUnknown = false;
77+
78+
/**
79+
* Class constructor.
80+
*
81+
* @param array $config
82+
*/
83+
public function __construct(array $config = [])
84+
{
85+
Obj::init($this, $config);
86+
}
87+
88+
/**
89+
* @return array
90+
*/
91+
protected function parseRawArgs(): array
92+
{
93+
$args = [];
94+
95+
// parse arguments
96+
foreach ($this->rawArgs as $arg) {
97+
// value specified inline (<arg>=<value>)
98+
if (strpos($arg, '=') > 0) {
99+
[$name, $value] = explode('=', $arg, 2);
100+
101+
// ensure is valid name.
102+
if (FlagHelper::isValidName($name)) {
103+
$args[$name] = $value;
104+
} else {
105+
$args[] = $arg;
106+
}
107+
} else {
108+
$args[] = $arg;
109+
}
110+
}
111+
112+
return $args;
113+
}
114+
115+
/**
116+
* @return array
117+
*/
118+
public function getRequiredOpts(): array
119+
{
120+
return $this->requiredOpts;
121+
}
122+
123+
/**
124+
* @return array
125+
*/
126+
public function getRawArgs(): array
127+
{
128+
return $this->rawArgs;
129+
}
130+
131+
/**
132+
* @return array
133+
*/
134+
public function getRawFlags(): array
135+
{
136+
return $this->rawFlags;
137+
}
138+
139+
/**
140+
* @return bool
141+
*/
142+
public function isParsed(): bool
143+
{
144+
return $this->parsed;
145+
}
146+
147+
/**
148+
* @return bool
149+
*/
150+
public function isStopOnFistArg(): bool
151+
{
152+
return $this->stopOnFistArg;
153+
}
154+
155+
/**
156+
* @param bool $stopOnFistArg
157+
*/
158+
public function setStopOnFistArg(bool $stopOnFistArg): void
159+
{
160+
$this->stopOnFistArg = $stopOnFistArg;
161+
}
162+
}

0 commit comments

Comments
 (0)