Skip to content

Commit 944a443

Browse files
committed
update: update some logic for defaut value
1 parent 962dba1 commit 944a443

File tree

7 files changed

+102
-23
lines changed

7 files changed

+102
-23
lines changed

src/Flag/AbstractFlag.php

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

1718
/**
1819
* Class Flag
@@ -101,19 +102,11 @@ public function __construct(string $name, string $desc = '', bool $required = fa
101102

102103
public function init(): void
103104
{
104-
if ($this->isArray()) {
105-
$this->type = FlagType::ARRAY;
105+
if ($this->default !== null) {
106+
$this->default = FlagType::fmtBasicTypeValue($this->type, $this->default);
106107
}
107108
}
108109

109-
/**
110-
* @return bool
111-
*/
112-
public function hasDefault(): bool
113-
{
114-
return $this->default !== null;
115-
}
116-
117110
/**
118111
* @return mixed
119112
*/
@@ -159,6 +152,22 @@ public function setValidator(callable $validator): void
159152
$this->validator = $validator;
160153
}
161154

155+
/**
156+
* @return bool
157+
*/
158+
public function hasValue(): bool
159+
{
160+
return $this->value !== null;
161+
}
162+
163+
/**
164+
* @return bool
165+
*/
166+
public function hasDefault(): bool
167+
{
168+
return $this->default !== null;
169+
}
170+
162171
/******************************************************************
163172
*
164173
*****************************************************************/
@@ -185,6 +194,10 @@ public function getType(): string
185194
*/
186195
public function setType(string $type): void
187196
{
197+
if (!$type) {
198+
return;
199+
}
200+
188201
if (!FlagType::isValid($type)) {
189202
$name = $this->name;
190203
throw new FlagException("cannot define invalid flag type: $type(name: $name)");

src/Flag/Argument.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ class Argument extends AbstractFlag
3333
*/
3434
public function setType(string $type): void
3535
{
36+
if (!$type) {
37+
return;
38+
}
39+
3640
if (!FlagType::isValid($type)) {
3741
$name = $this->getName();
3842
$mark = $name ? "(name: $name)" : "(#$this->index)";

src/Flag/Option.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,11 @@ public function getShortcut(): string
7474
}
7575

7676
/**
77-
* @param string $shortcut eg: 'a|b'
77+
* @param string $shortcut eg: 'a,b' Or '-a,-b'
7878
*/
7979
public function setShortcut(string $shortcut): void
8080
{
81-
$shortcuts = preg_split('{(\|)-?}', ltrim($shortcut, '-'));
81+
$shortcuts = preg_split('{(,)-?}', ltrim($shortcut, '-'));
8282
$shortcuts = array_filter($shortcuts);
8383

8484
$this->setShorts($shortcuts);

src/FlagType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace Toolkit\PFlag;
1111

1212
use Toolkit\Cli\Helper\FlagHelper;
13+
use function is_scalar;
1314
use function is_string;
1415

1516
/**
@@ -100,6 +101,10 @@ public static function isArray(string $type): bool
100101
*/
101102
public static function fmtBasicTypeValue(string $type, $value)
102103
{
104+
if (!is_scalar($value)) {
105+
return $value;
106+
}
107+
103108
// format value by type
104109
switch ($type) {
105110
case self::INT:

src/Traits/FlagArgumentsTrait.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use Toolkit\PFlag\Exception\FlagException;
1313
use Toolkit\PFlag\Flag\Argument;
14+
use Toolkit\PFlag\FlagType;
1415
use function count;
1516
use function is_string;
1617

@@ -49,24 +50,24 @@ trait FlagArgumentsTrait
4950
/**
5051
* @param string $name
5152
* @param string $desc
53+
* @param string $type The argument data type. default is: string. {@see FlagType}
5254
* @param bool $required
53-
* @param string $type The argument data type. (eg: 'string', 'array', 'mixed')
5455
* @param null|mixed $default
5556
* @param string $alias
5657
*/
5758
public function addArg(
5859
string $name,
59-
string $desc = '',
60-
bool $required = false,
60+
string $desc,
6161
string $type = '',
62+
bool $required = false,
6263
$default = null,
6364
string $alias = ''
6465
): void {
65-
$argObj = Argument::new($name, $desc, $required, $default);
66-
$argObj->setType($type);
67-
$argObj->setAlias($alias);
66+
/** @var Argument $arg */
67+
$arg = Argument::new($name, $desc, $required, $default);
68+
$arg->setType($type);
6869

69-
$this->addArgument($argObj);
70+
$this->addArgument($arg);
7071
}
7172

7273
/**
@@ -79,7 +80,9 @@ public function addArgument(Argument $argument): void
7980

8081
$index = count($this->arguments);
8182
$argument->setIndex($index);
83+
$argument->init();
8284

85+
$name = $argument->getName();
8386
$mark = $argument->getNameMark();
8487

8588
// NOTICE: only allow one array argument and must be at last.
@@ -94,10 +97,10 @@ public function addArgument(Argument $argument): void
9497
$this->arrayArg = $this->arrayArg || $isArray;
9598
$this->optionalArg = $this->optionalArg || !$required;
9699

97-
// record index
98-
$this->name2index[$argument->getName()] = count($this->arguments);
99100
// append
100101
$this->arguments[] = $argument;
102+
// record index
103+
$this->name2index[$name] = $index;
101104
}
102105

103106
/**

src/Traits/FlagOptionsTrait.php

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,24 @@ trait FlagOptionsTrait
3838
* @param string $name
3939
* @param string $shorts
4040
* @param string $desc
41+
* @param string $type The argument data type. default is: string. {@see FlagType}
4142
* @param bool $required
4243
* @param mixed $default
44+
* @param string $alias
4345
*/
44-
public function addOpt(string $name, string $shorts, string $desc, bool $required = false, $default = null): void
45-
{
46+
public function addOpt(
47+
string $name,
48+
string $shorts,
49+
string $desc,
50+
string $type = '',
51+
bool $required = false,
52+
$default = null,
53+
string $alias = ''
54+
): void {
55+
/** @var Option $opt */
4656
$opt = Option::new($name, $desc, $required, $default);
57+
$opt->setType($type);
58+
$opt->setAlias($alias);
4759
$opt->setShortcut($shorts);
4860

4961
$this->addOption($opt);
@@ -70,6 +82,8 @@ public function addOption(Option $option): void
7082
}
7183
}
7284

85+
$option->init();
86+
7387
// add to defined
7488
$this->defined[$name] = $option;
7589
}
@@ -84,6 +98,25 @@ public function addOptions(array $options): void
8498
}
8599
}
86100

101+
/**
102+
* @param string $name
103+
* @param null|mixed $default
104+
*
105+
* @return mixed|null
106+
*/
107+
public function getOpt(string $name, $default = null)
108+
{
109+
if ($arg = $this->getOption($name)) {
110+
return $arg->getValue();
111+
}
112+
113+
if ($default === null && ($arg = $this->getDefinedOption($name))) {
114+
return $arg->hasDefault() ? $arg->getDefault() : $default;
115+
}
116+
117+
return $default;
118+
}
119+
87120
/**
88121
* @param string $name
89122
*
@@ -94,6 +127,16 @@ public function getOption(string $name): ?Option
94127
return $this->matched[$name] ?? null;
95128
}
96129

130+
/**
131+
* @param string $name
132+
*
133+
* @return Option|null
134+
*/
135+
public function getDefinedOption(string $name): ?Option
136+
{
137+
return $this->defined[$name] ?? null;
138+
}
139+
97140
/**
98141
* @param string $name
99142
*

test/FlagsTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
use Toolkit\PFlag\Exception\FlagException;
1313
use Toolkit\PFlag\Flags;
1414
use Toolkit\PFlag\Flag\Option;
15+
use Toolkit\PFlag\FlagType;
16+
use function vdump;
1517

1618
/**
1719
* Class FlagsTest
@@ -24,12 +26,21 @@ public function testParse(): void
2426
{
2527
$fs = Flags::new();
2628
$fs->addOption(Option::new('name'));
29+
$fs->addOpt('age', '', 'age desc', FlagType::INT);
30+
$fs->addOpt('int1', '', 'opt1 desc', FlagType::INT, false, '89');
31+
// vdump($fs->getDefinedOption('int1'));
32+
2733
self::assertTrue($fs->hasDefined('name'));
2834
self::assertFalse($fs->hasMatched('name'));
2935

3036
$args = ['--name', 'inhere', 'arg0', 'arg1'];
3137
$fs->parse($args);
38+
3239
self::assertTrue($fs->hasMatched('name'));
40+
$this->assertNotEmpty( $fs->getOption('name'));
41+
$this->assertSame('inhere', $fs->getOpt('name'));
42+
$this->assertSame(0, $fs->getOpt('age', 0));
43+
$this->assertSame(89, $fs->getOpt('int1'));
3344

3445
$fs->reset();
3546
$args = ['--name', 'inhere', '-s', 'sv', '-f'];

0 commit comments

Comments
 (0)