Skip to content

Commit 19ecbcf

Browse files
committed
update some flag logic, add more tests
1 parent 4f97899 commit 19ecbcf

13 files changed

+218
-108
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# PHP Flag
22

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

89
Command line flag parse library
910

composer.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@
2626
"Toolkit\\PFlagTest\\": "test/"
2727
}
2828
},
29-
"bin": [
30-
31-
],
3229
"scripts": {
3330
"test": "php vender/bin/phpunit"
3431
}

src/Contract/FlagInterface.php

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,47 @@
1616
*/
1717
interface FlagInterface
1818
{
19+
public function init(): void;
20+
1921
/**
20-
* @return bool
22+
* @return string
2123
*/
22-
public function isArray(): bool;
24+
public function getType(): string;
2325

2426
/**
25-
* @return bool
27+
* @return string
2628
*/
27-
public function isRequired(): bool;
29+
public function getName(): string;
2830

2931
/**
30-
* @return bool
32+
* @return string
3133
*/
32-
public function isOptional(): bool;
34+
public function getDesc(): string;
3335

3436
/**
3537
* Get the flag value
3638
*
3739
* @return mixed
3840
*/
3941
public function getValue();
42+
43+
/**
44+
* @param mixed $value
45+
*/
46+
public function setValue($value): void;
47+
48+
/**
49+
* @return bool
50+
*/
51+
public function isArray(): bool;
52+
53+
/**
54+
* @return bool
55+
*/
56+
public function isRequired(): bool;
57+
58+
/**
59+
* @return bool
60+
*/
61+
public function isOptional(): bool;
4062
}

src/Contract/ParserInterface.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,59 @@
99

1010
namespace Toolkit\PFlag\Contract;
1111

12+
use Toolkit\FsUtil\Parser\AbstractParser;
13+
use Toolkit\PFlag\Flags;
14+
use Toolkit\PFlag\SFlags;
15+
1216
/**
1317
* interface ParserInterface
1418
*/
1519
interface ParserInterface
1620
{
21+
/**
22+
* @return array
23+
*/
24+
public function getRawFlags(): array;
25+
26+
/**
27+
* @return array
28+
*/
29+
public function getRawArgs(): array;
30+
31+
/**
32+
* @param array $rawFlags
33+
*
34+
* @return AbstractParser|SFlags|Flags
35+
*/
36+
public function parse(array $rawFlags);
37+
38+
/**
39+
* Get an option value by name
40+
*
41+
* @param string $name
42+
* @param null|mixed $default
43+
*
44+
* @return mixed
45+
*/
46+
public function getOpt(string $name, $default = null);
47+
48+
/**
49+
* Get an argument value by name
50+
*
51+
* @param string|int $nameOrIndex
52+
* @param null|mixed $default
53+
*
54+
* @return mixed
55+
*/
56+
public function getArg($nameOrIndex, $default = null);
57+
58+
/**
59+
* @return array
60+
*/
61+
public function getOpts(): array;
62+
63+
/**
64+
* @return array
65+
*/
66+
public function getArgs(): array;
1767
}

src/Contract/ValueInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,7 @@
77
*/
88
interface ValueInterface
99
{
10+
public function setValue();
1011

12+
public function getValue();
1113
}

src/Flag/AbstractFlag.php

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use Toolkit\PFlag\Contract\FlagInterface;
1414
use Toolkit\PFlag\Exception\FlagException;
1515
use Toolkit\PFlag\FlagType;
16-
use function vdump;
1716

1817
/**
1918
* Class Flag
@@ -68,42 +67,56 @@ abstract class AbstractFlag implements FlagInterface
6867
protected $validator;
6968

7069
/**
71-
* @param string $name
72-
* @param string $desc
73-
* @param bool $required
74-
* @param mixed|null $default
70+
* @param string $name
71+
* @param string $desc
72+
* @param string $type
73+
* @param bool $required
74+
* @param mixed $default
7575
*
7676
* @return static|Argument|Option
7777
*/
78-
public static function new(string $name, string $desc = '', bool $required = false, $default = null): self
79-
{
80-
return new static($name, $desc, $required, $default);
78+
public static function new(
79+
string $name,
80+
string $desc = '',
81+
string $type = 'string',
82+
bool $required = false,
83+
$default = null
84+
): self {
85+
return new static($name, $desc, $type, $required, $default);
8186
}
8287

8388
/**
8489
* Class constructor.
8590
*
8691
* @param string $name
8792
* @param string $desc
93+
* @param string $type
8894
* @param bool $required
8995
* @param mixed $default The default value
9096
* - for Flag::ARG_OPTIONAL mode only
9197
* - must be null for Flag::OPT_BOOLEAN
9298
*/
93-
public function __construct(string $name, string $desc = '', bool $required = false, $default = null)
94-
{
95-
$this->name = $name;
96-
99+
public function __construct(
100+
string $name,
101+
string $desc = '',
102+
string $type = 'string',
103+
bool $required = false,
104+
$default = null
105+
) {
97106
$this->default = $default;
98107
$this->required = $required;
99108

109+
$this->setName($name);
110+
$this->setType($type);
100111
$this->setDesc($desc);
101112
}
102113

103114
public function init(): void
104115
{
116+
// init default value.
105117
if ($this->default !== null) {
106118
$this->default = FlagType::fmtBasicTypeValue($this->type, $this->default);
119+
$this->value = $this->default;
107120
}
108121
}
109122

@@ -172,7 +185,6 @@ public function hasDefault(): bool
172185
*
173186
*****************************************************************/
174187

175-
176188
/**
177189
* @return string
178190
*/
@@ -199,8 +211,8 @@ public function setType(string $type): void
199211
}
200212

201213
if (!FlagType::isValid($type)) {
202-
$name = $this->name;
203-
throw new FlagException("cannot define invalid flag type: $type(name: $name)");
214+
$name = $this->getNameMark();
215+
throw new FlagException("cannot define invalid flag type: $type $name");
204216
}
205217

206218
$this->type = $type;
@@ -278,7 +290,6 @@ public function toArray(): array
278290
*/
279291
public function isArray(): bool
280292
{
281-
// return $this->hasMode(Input::ARG_IS_ARRAY);
282293
return FlagType::isArray($this->type);
283294
}
284295

src/Flag/Argument.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99

1010
namespace Toolkit\PFlag\Flag;
1111

12-
use Toolkit\PFlag\Exception\FlagException;
13-
use Toolkit\PFlag\FlagType;
1412
use function sprintf;
1513

1614
/**
@@ -28,24 +26,6 @@ class Argument extends AbstractFlag
2826
*/
2927
private $index = 0;
3028

31-
/**
32-
* @param string $type
33-
*/
34-
public function setType(string $type): void
35-
{
36-
if (!$type) {
37-
return;
38-
}
39-
40-
if (!FlagType::isValid($type)) {
41-
$name = $this->getName();
42-
$mark = $name ? "(name: $name)" : "(#$this->index)";
43-
throw new FlagException("cannot define invalid flag type: $type$mark");
44-
}
45-
46-
$this->type = $type;
47-
}
48-
4929
/**
5030
* @param string $name
5131
*/

src/Flag/Arguments.php

Lines changed: 4 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -9,50 +9,15 @@
99

1010
namespace Toolkit\PFlag\Flag;
1111

12+
use Toolkit\PFlag\Traits\FlagArgumentsTrait;
13+
1214
/**
13-
* Class InputArguments
15+
* Class Arguments
1416
* - input arguments builder
1517
*
1618
* @package Toolkit\PFlag\Flag
1719
*/
1820
class Arguments
1921
{
20-
/**
21-
* @var array
22-
*/
23-
private $arguments = [];
24-
25-
/**
26-
* @param string $name
27-
* @param int|null $mode
28-
* @param string|null $type The argument data type. (eg: 'string', 'array', 'mixed')
29-
* @param string $description
30-
* @param null $default
31-
* @param null $alias
32-
*/
33-
public function add(
34-
string $name,
35-
int $mode = null,
36-
string $type = null,
37-
string $description = '',
38-
$default = null,
39-
$alias = null
40-
): void {
41-
}
42-
43-
/**
44-
* @return array
45-
*/
46-
public function getArguments(): array
47-
{
48-
return $this->arguments;
49-
}
50-
51-
/**
52-
* @param array $arguments
53-
*/
54-
public function setArguments(array $arguments): void
55-
{
56-
$this->arguments = $arguments;
57-
}
22+
use FlagArgumentsTrait;
5823
}

src/Flag/Options.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@
99

1010
namespace Toolkit\PFlag\Flag;
1111

12+
use Toolkit\PFlag\Traits\FlagOptionsTrait;
13+
1214
/**
13-
* Class InputOptions
15+
* Class Options
1416
* - input options builder
1517
*
1618
* @package Toolkit\PFlag\Flag
1719
*/
1820
class Options
1921
{
22+
use FlagOptionsTrait;
2023
}

src/Traits/FlagArgumentsTrait.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,16 @@ trait FlagArgumentsTrait
5353
* @param string $type The argument data type. default is: string. {@see FlagType}
5454
* @param bool $required
5555
* @param null|mixed $default
56-
* @param string $alias
5756
*/
5857
public function addArg(
5958
string $name,
6059
string $desc,
6160
string $type = '',
6261
bool $required = false,
63-
$default = null,
64-
string $alias = ''
62+
$default = null
6563
): void {
6664
/** @var Argument $arg */
67-
$arg = Argument::new($name, $desc, $required, $default);
68-
$arg->setType($type);
65+
$arg = Argument::new($name, $desc, $type, $required, $default);
6966

7067
$this->addArgument($arg);
7168
}
@@ -118,6 +115,19 @@ public function getArg($nameOrIndex, $default = null)
118115
return $default;
119116
}
120117

118+
/**
119+
* @return array
120+
*/
121+
public function getArgs(): array
122+
{
123+
$args = [];
124+
foreach ($this->arguments as $argument) {
125+
$args[] = $argument->getValue();
126+
}
127+
128+
return $args;
129+
}
130+
121131
/**
122132
* @param string|int $nameOrIndex
123133
*

0 commit comments

Comments
 (0)