Skip to content

Commit 96b4050

Browse files
committed
up: support more custom setting for add arg flag
1 parent 24d17d5 commit 96b4050

File tree

7 files changed

+31
-18
lines changed

7 files changed

+31
-18
lines changed

src/Contract/ParserInterface.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,7 @@ public function addOpt(
8080
* @param string $type The argument data type. default is: string. {@see FlagType}
8181
* @param bool $required
8282
* @param mixed|null $default
83-
* @param array $moreInfo
84-
*
85-
* @psalm-param array{helpType: string} $moreInfo
83+
* @param array{helpType: string, validator: callable|ValidatorInterface} $moreInfo
8684
*
8785
* @return self
8886
*/

src/Flag/AbstractFlag.php

+13-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Toolkit\PFlag\FlagsParser;
1717
use Toolkit\PFlag\FlagType;
1818
use Toolkit\PFlag\FlagUtil;
19+
use Toolkit\PFlag\Validator\CondValidator;
1920
use Toolkit\Stdlib\Obj;
2021
use Toolkit\Stdlib\OS;
2122
use function is_array;
@@ -25,7 +26,7 @@
2526

2627
/**
2728
* Class Flag
28-
* - - definition a input flag item(option|argument)
29+
* - definition a input flag item(option|argument)
2930
*
3031
* @package Toolkit\PFlag\Flag
3132
*/
@@ -202,6 +203,11 @@ public function setValue(mixed $value): void
202203
// has validator
203204
$cb = $this->validator;
204205
if ($cb && is_scalar($value)) {
206+
/** @see CondValidator::setFs() */
207+
// if (method_exists($cb, 'setFs')) {
208+
// $cb->setFs($this);
209+
// }
210+
205211
$ok = true;
206212
$ret = $cb($value, $this->name);
207213

@@ -410,15 +416,19 @@ public function getHelpType(bool $useTypeOnEmpty = false): string
410416
*/
411417
public function setHelpType(string $helpType): void
412418
{
413-
$this->helpType = $helpType;
419+
if ($helpType) {
420+
$this->helpType = $helpType;
421+
}
414422
}
415423

416424
/**
417425
* @param callable|null $validator
418426
*/
419427
public function setValidator(?callable $validator): void
420428
{
421-
$this->validator = $validator;
429+
if ($validator) {
430+
$this->validator = $validator;
431+
}
422432
}
423433

424434
/**

src/FlagUtil.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public static function filterOptionName(string $val): string
8585
*/
8686
public static function getMaxInt(int $val1, int $val2): int
8787
{
88-
return $val1 > $val2 ? $val1 : $val2;
88+
return max($val1, $val2);
8989
}
9090

9191
/**

src/Flags.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use InvalidArgumentException;
1313
use RuntimeException;
14+
use Toolkit\PFlag\Contract\ValidatorInterface;
1415
use Toolkit\PFlag\Exception\FlagException;
1516
use Toolkit\PFlag\Exception\FlagParseException;
1617
use Toolkit\PFlag\Flag\Argument;
@@ -437,7 +438,7 @@ public function isNotEmpty(): bool
437438
* @param string $type The argument data type. default is: string. {@see FlagType}
438439
* @param bool $required
439440
* @param mixed|null $default
440-
* @param array $moreInfo
441+
* @param array{helpType: string, validator: callable|ValidatorInterface} $moreInfo
441442
*
442443
* @return static
443444
*/
@@ -450,6 +451,8 @@ public function addArg(
450451
array $moreInfo = []
451452
): static {
452453
$arg = Argument::new($name, $desc, $type, $required, $default);
454+
$arg->setHelpType($moreInfo['helpType'] ?? '');
455+
$arg->setValidator($moreInfo['validator'] ?? null);
453456

454457
$this->addArgument($arg);
455458
return $this;

src/SFlags.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace Toolkit\PFlag;
1111

1212
use InvalidArgumentException;
13+
use Toolkit\PFlag\Contract\ValidatorInterface;
1314
use Toolkit\PFlag\Exception\FlagException;
1415
use Toolkit\PFlag\Exception\FlagParseException;
1516
use Toolkit\Stdlib\OS;
@@ -186,9 +187,7 @@ public function addOpt(
186187
* @param string $type The argument data type. default is: string. {@see FlagType}
187188
* @param bool $required
188189
* @param mixed|null $default
189-
* @param array $moreInfo
190-
*
191-
* @psalm-param array{alias: string, helpType: string} $moreInfo
190+
* @param array{helpType: string, validator: callable|ValidatorInterface} $moreInfo
192191
*
193192
* @return SFlags
194193
*/
@@ -214,6 +213,10 @@ public function addArg(
214213
$define['helpType'] = $moreInfo['helpType'];
215214
}
216215

216+
if (isset($moreInfo['validator'])) {
217+
$define['validator'] = $moreInfo['validator'];
218+
}
219+
217220
$this->addArgDefine($define);
218221
return $this;
219222
}

src/Validator/CondValidator.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ abstract class CondValidator extends AbstractValidator
2727
*
2828
* @var callable(FlagsParser):bool
2929
*/
30-
protected $cond;
30+
protected $condFn;
3131

3232
/**
3333
* @param mixed $value
@@ -37,7 +37,7 @@ abstract class CondValidator extends AbstractValidator
3737
*/
3838
public function __invoke(mixed $value, string $name): bool
3939
{
40-
$condFn = $this->cond;
40+
$condFn = $this->condFn;
4141
if ($condFn && !$condFn($this->fs)) {
4242
return true;
4343
}
@@ -62,13 +62,13 @@ public function setFs(FlagsParser $fs): void
6262
}
6363

6464
/**
65-
* @param mixed $cond
65+
* @param mixed $condFn
6666
*
6767
* @return static
6868
*/
69-
public function setCond(mixed $cond): self
69+
public function setCondFn(mixed $condFn): self
7070
{
71-
$this->cond = $cond;
71+
$this->condFn = $condFn;
7272
return $this;
7373
}
7474
}

src/Validator/FuncValidator.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
class FuncValidator extends AbstractValidator
1616
{
1717
/**
18-
* @var callable
19-
* @psalm-var callable(mixed, string): bool
18+
* @var callable(mixed, string): bool
2019
*/
2120
protected $func;
2221

0 commit comments

Comments
 (0)