Skip to content

Commit e7bd9a9

Browse files
committed
feat: sflag parser support value validate
1 parent 19ecbcf commit e7bd9a9

File tree

10 files changed

+303
-66
lines changed

10 files changed

+303
-66
lines changed

README.md

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ $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,
38-
'f' => FlagType::BOOL,
38+
'f' => FlagType::BOOL,
3939
];
4040
$argRules = [
4141
// some argument rules
@@ -140,10 +140,60 @@ $tags = $fs->getOption('tags'); // array{"php", "go", "java"}
140140
$arg0 = $fs->getArg(0); // string(arg0)
141141
// get an array arg
142142
$arrArg = $fs->getArg(1); // array{"arr0", "arr1"}
143-
// use name
143+
// get value by name
144144
$arrArg = $fs->getArg('arrArg'); // array{"arr0", "arr1"}
145145
```
146146

147+
### Flag Rule
148+
149+
The options/arguments rules
150+
151+
- string value is rule(`type,required,default,desc`).
152+
- array is define item `SFlags::DEFINE_ITEM`
153+
- supportted type see `FlagType::*`
154+
155+
```php
156+
$rules = [
157+
// v: only value, as name and use default type FlagType::STRING
158+
// k-v: key is name, value can be string|array
159+
'long,s',
160+
// name => rule
161+
'long,s' => 'int',
162+
'f' => 'bool',
163+
'long' => FlagType::STRING,
164+
'tags' => 'array', // can also: int[], string[]
165+
'name' => 'type,required,default,the description message', // with default, desc, required
166+
]
167+
```
168+
169+
**For options**
170+
171+
> TIP: name `long,s` - first is the option name. remaining is short names.
172+
173+
**For arguments**
174+
175+
- arguemnt no alias/shorts
176+
- array value only allow defined last
177+
178+
**Definition item**
179+
180+
The const `SFlags::DEFINE_ITEM`:
181+
182+
```php
183+
public const DEFINE_ITEM = [
184+
'name' => '',
185+
'desc' => '',
186+
'type' => FlagType::STRING,
187+
// 'index' => 0, // only for argument
188+
'required' => false,
189+
'default' => null,
190+
'aliases' => [], // only for option
191+
// value validator
192+
'validator' => null,
193+
// 'category' => null
194+
];
195+
```
196+
147197
## License
148198

149199
[MIT](LICENSE)

src/AbstractParser.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,23 @@ abstract class AbstractParser implements ParserInterface
2424
protected $parsed = false;
2525

2626
/**
27-
* The raw input flags
27+
* The input flags
2828
*
29-
* @var array
29+
* @var string[]
3030
*/
31-
protected $rawFlags = [];
31+
protected $flags = [];
3232

3333
/**
3434
* The remaining raw args, after option parsed from {@see $rawFlags}
3535
*
36-
* @var array
36+
* @var string[]
3737
*/
3838
protected $rawArgs = [];
3939

4040
/**
4141
* The required option names.
4242
*
43-
* @var array
43+
* @var string[]
4444
*/
4545
protected $requiredOpts = [];
4646

@@ -123,17 +123,17 @@ public function getRequiredOpts(): array
123123
/**
124124
* @return array
125125
*/
126-
public function getRawArgs(): array
126+
public function getFlags(): array
127127
{
128-
return $this->rawArgs;
128+
return $this->flags;
129129
}
130130

131131
/**
132132
* @return array
133133
*/
134-
public function getRawFlags(): array
134+
public function getRawArgs(): array
135135
{
136-
return $this->rawFlags;
136+
return $this->rawArgs;
137137
}
138138

139139
/**

src/Contract/ParserInterface.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,19 @@ interface ParserInterface
2121
/**
2222
* @return array
2323
*/
24-
public function getRawFlags(): array;
24+
public function getFlags(): array;
2525

2626
/**
2727
* @return array
2828
*/
2929
public function getRawArgs(): array;
3030

3131
/**
32-
* @param array $rawFlags
32+
* @param array $flags
3333
*
3434
* @return AbstractParser|SFlags|Flags
3535
*/
36-
public function parse(array $rawFlags);
36+
public function parse(array $flags);
3737

3838
/**
3939
* Get an option value by name

src/Contract/ValidatorInterface.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Toolkit\PFlag\Contract;
4+
5+
/**
6+
* interface ValidatorInterface
7+
*/
8+
interface ValidatorInterface
9+
{
10+
/**
11+
* @param mixed $value
12+
* @param string $name
13+
*
14+
* @return bool
15+
*/
16+
public function __invoke($value, string $name): bool;
17+
18+
/**
19+
* @return string
20+
*/
21+
public function __toString(): string;
22+
}

src/Exception/FlagException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99

1010
namespace Toolkit\PFlag\Exception;
1111

12-
use RuntimeException;
12+
use InvalidArgumentException;
1313

1414
/**
1515
* Class FlagException
1616
*
1717
* @package Toolkit\PFlag\Exception
1818
*/
19-
class FlagException extends RuntimeException
19+
class FlagException extends InvalidArgumentException
2020
{
2121
}

src/Flag/AbstractFlag.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ abstract class AbstractFlag implements FlagInterface
5353
*/
5454
protected $value;
5555

56+
// TODO category
57+
// protected $category = '';
58+
5659
/**
5760
* @var bool
5861
*/

src/Flags.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public function parse(array $args = null): self
100100
}
101101

102102
$this->parsed = true;
103-
$this->rawArgs = $this->rawFlags = $args;
103+
$this->rawArgs = $this->flags = $args;
104104

105105
$breakStatus = self::STATUS_OK;
106106
while (true) {

0 commit comments

Comments
 (0)