Skip to content

Commit fb565c0

Browse files
committed
fix add arg error
1 parent b77ca90 commit fb565c0

File tree

6 files changed

+224
-34
lines changed

6 files changed

+224
-34
lines changed

src/Contract/ParserInterface.php

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ interface ParserInterface
1616
{
1717
/**
1818
* @return array
19+
* @psalm-return list<string>
1920
*/
2021
public function getFlags(): array;
2122

2223
/**
2324
* @return array
25+
* @psalm-return list<string>
2426
*/
2527
public function getRawArgs(): array;
2628

@@ -30,7 +32,55 @@ public function getRawArgs(): array;
3032
public function isNotEmpty(): bool;
3133

3234
/**
33-
* @param array|null $flags
35+
* Add option
36+
*
37+
* @param string $name
38+
* @param string $shortcut
39+
* @param string $desc
40+
* @param string $type The argument data type. default is: string. {@see FlagType}
41+
* @param bool $required
42+
* @param mixed $default
43+
* @param array $moreInfo
44+
*
45+
* @psalm-param array{alias: string, showType: string} $moreInfo
46+
*
47+
* @return self
48+
*/
49+
public function addOpt(
50+
string $name,
51+
string $shortcut,
52+
string $desc,
53+
string $type = '',
54+
bool $required = false,
55+
$default = null,
56+
array $moreInfo = []
57+
): self;
58+
59+
/**
60+
* Add an argument
61+
*
62+
* @param string $name
63+
* @param string $desc
64+
* @param string $type The argument data type. default is: string. {@see FlagType}
65+
* @param bool $required
66+
* @param mixed $default
67+
* @param array $moreInfo
68+
*
69+
* @psalm-param array{alias: string, showType: string} $moreInfo
70+
*
71+
* @return self
72+
*/
73+
public function addArg(
74+
string $name,
75+
string $desc,
76+
string $type = '',
77+
bool $required = false,
78+
$default = null,
79+
array $moreInfo = []
80+
): self;
81+
82+
/**
83+
* @param array|null $flags If NULL, will parse the $_SERVER['argv]
3484
*
3585
* @return bool
3686
*/
@@ -72,6 +122,7 @@ public function getArg($nameOrIndex, $default = null);
72122

73123
/**
74124
* @return array
125+
* @psalm-return array<string, mixed>
75126
*/
76127
public function getOpts(): array;
77128

src/Flags.php

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

1212
use RuntimeException;
13+
use Toolkit\PFlag\Contract\ParserInterface;
1314
use Toolkit\PFlag\Exception\FlagException;
1415
use Toolkit\PFlag\Exception\FlagParseException;
1516
use Toolkit\PFlag\Flag\Argument;
@@ -452,18 +453,23 @@ public function isNotEmpty(): bool
452453
* @param string $type The argument data type. default is: string. {@see FlagType}
453454
* @param bool $required
454455
* @param null|mixed $default
456+
* @param array $moreInfo
457+
*
458+
* @return ParserInterface|self
455459
*/
456460
public function addArg(
457461
string $name,
458462
string $desc,
459463
string $type = '',
460464
bool $required = false,
461-
$default = null
462-
): void {
465+
$default = null,
466+
array $moreInfo = []
467+
): ParserInterface {
463468
/** @var Argument $arg */
464469
$arg = Argument::new($name, $desc, $type, $required, $default);
465470

466471
$this->addArgument($arg);
472+
return $this;
467473
}
468474

469475
/**
@@ -633,29 +639,36 @@ protected function resetArguments(): void
633639
**************************************************************************/
634640

635641
/**
642+
* Add option
643+
*
636644
* @param string $name
637-
* @param string $shorts
645+
* @param string $shortcut
638646
* @param string $desc
639647
* @param string $type The argument data type. default is: string. {@see FlagType}
640648
* @param bool $required
641649
* @param mixed $default
642-
* @param string $alias
650+
* @param array $moreInfo
651+
*
652+
* @psalm-param array{alias: string, showType: string} $moreInfo
653+
*
654+
* @return ParserInterface|self
643655
*/
644656
public function addOpt(
645657
string $name,
646-
string $shorts,
658+
string $shortcut,
647659
string $desc,
648660
string $type = '',
649661
bool $required = false,
650662
$default = null,
651-
string $alias = ''
652-
): void {
663+
array $moreInfo = []
664+
): ParserInterface {
653665
/** @var Option $opt */
654666
$opt = Option::new($name, $desc, $type, $required, $default);
655-
$opt->setAlias($alias);
656-
$opt->setShortcut($shorts);
667+
$opt->setAlias($moreInfo['alias'] ?? '');
668+
$opt->setShortcut($shortcut);
657669

658670
$this->addOption($opt);
671+
return $this;
659672
}
660673

661674
/**

src/SFlags.php

Lines changed: 40 additions & 22 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 Toolkit\PFlag\Contract\ParserInterface;
1314
use Toolkit\PFlag\Exception\FlagException;
1415
use Toolkit\PFlag\Exception\FlagParseException;
1516
use Toolkit\Stdlib\OS;
@@ -379,7 +380,7 @@ protected function setOptValue(string $option, $value): void
379380
}
380381

381382
/**
382-
* @param string $name The option name
383+
* @param string $name The option name
383384
* @param mixed $value
384385
* @param array $define {@see DEFINE_ITEM}
385386
*/
@@ -525,12 +526,15 @@ public function resetResults(): void
525526
***************************************************************/
526527

527528
/**
528-
* @param string $name
529-
* @param string $shortcut
530-
* @param string $desc
531-
* @param string $type The argument data type. default is: string. {@see FlagType}
532-
* @param bool $required
533-
* @param mixed $default
529+
* @param string $name
530+
* @param string $shortcut
531+
* @param string $desc
532+
* @param string $type The argument data type. default is: string. {@see FlagType}
533+
* @param bool $required
534+
* @param mixed $default
535+
* @param array $moreInfo
536+
*
537+
* @psalm-param array{alias: string, showType: string} $moreInfo
534538
*
535539
* @return SFlags
536540
*/
@@ -540,8 +544,9 @@ public function addOpt(
540544
string $desc,
541545
string $type = '',
542546
bool $required = false,
543-
$default = null
544-
): self {
547+
$default = null,
548+
array $moreInfo = []
549+
): ParserInterface {
545550
$define = self::DEFINE_ITEM;
546551

547552
$define['name'] = $name;
@@ -552,16 +557,23 @@ public function addOpt(
552557
$define['default'] = $default;
553558
$define['shorts'] = $shortcut ? Str::explode($shortcut, ',') : [];
554559

560+
if (isset($moreInfo['showType'])) {
561+
$define['showType'] = $moreInfo['showType'];
562+
}
563+
555564
$this->addOptDefine($define);
556565
return $this;
557566
}
558567

559568
/**
560-
* @param string $name
561-
* @param string $desc
562-
* @param string $type The argument data type. default is: string. {@see FlagType}
563-
* @param bool $required
564-
* @param null|mixed $default
569+
* @param string $name
570+
* @param string $desc
571+
* @param string $type The argument data type. default is: string. {@see FlagType}
572+
* @param bool $required
573+
* @param mixed $default
574+
* @param array $moreInfo
575+
*
576+
* @psalm-param array{alias: string, showType: string} $moreInfo
565577
*
566578
* @return SFlags
567579
*/
@@ -570,17 +582,23 @@ public function addArg(
570582
string $desc,
571583
string $type = '',
572584
bool $required = false,
573-
$default = null
574-
): self {
585+
$default = null,
586+
array $moreInfo = []
587+
): ParserInterface {
575588
$define = self::DEFINE_ITEM;
576589

577-
$define['name'] = $name;
578-
$define['desc'] = $desc;
579-
$define['type'] = $type ?: FlagType::STRING;
590+
$define['name'] = $name;
591+
$define['desc'] = $desc;
592+
$define['index'] = count($this->argDefines);
593+
$define['type'] = $type ?: FlagType::STRING;
580594

581595
$define['required'] = $required;
582596
$define['default'] = $default;
583597

598+
if (isset($moreInfo['showType'])) {
599+
$define['showType'] = $moreInfo['showType'];
600+
}
601+
584602
$this->addArgDefine($define);
585603
return $this;
586604
}
@@ -662,19 +680,19 @@ protected function addOptDefine(array $define): void
662680
protected function parseArgRules(array $rules): void
663681
{
664682
// check and collect arguments
665-
$index = 0;
666683
foreach ($rules as $name => $rule) {
667684
if (!$rule) {
668685
throw new FlagException('flag argument rule cannot be empty');
669686
}
670687

688+
$name = is_string($name) ? $name : '';
689+
$index = count($this->argDefines);
690+
671691
// parse rule
672-
$name = is_string($name) ? $name : '';
673692
$define = $this->parseRule($rule, $name, $index, false);
674693

675694
// add define
676695
$this->addArgDefine($define);
677-
$index++;
678696
}
679697
}
680698

src/Validator/LenValidator.php

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Toolkit\PFlag\Validator;
4+
5+
use Toolkit\PFlag\Exception\FlagException;
6+
use function count;
7+
use function is_array;
8+
use function is_string;
9+
use function sprintf;
10+
use function strlen;
11+
use function trim;
12+
13+
/**
14+
* class LenValidator
15+
*/
16+
class LenValidator extends AbstractValidator
17+
{
18+
/**
19+
* @var int|null
20+
*/
21+
protected $min;
22+
23+
/**
24+
* @var int|null
25+
*/
26+
protected $max;
27+
28+
/**
29+
* @param int|null $min
30+
* @param int|null $max
31+
*
32+
* @return static
33+
*/
34+
public static function new(int $min = null, int $max = null): self
35+
{
36+
return new static($min, $max);
37+
}
38+
39+
/**
40+
* Class constructor.
41+
*
42+
* @param int|null $min
43+
* @param int|null $max
44+
*/
45+
public function __construct(int $min = null, int $max = null)
46+
{
47+
$this->min = $min;
48+
$this->max = $max;
49+
}
50+
51+
/**
52+
* @param mixed $value
53+
* @param string $name
54+
*
55+
* @return bool
56+
*/
57+
public function checkInput($value, string $name): bool
58+
{
59+
if (is_string($value)) {
60+
$len = strlen(trim($value));
61+
} elseif (is_array($value)) {
62+
$len = count($value);
63+
} else {
64+
return false;
65+
}
66+
67+
// if ($this->min !== null && $this->max !== null) {
68+
// return sprintf('Len: %d - %d', $this->min, $this->max);
69+
// }
70+
//
71+
// if ($this->min !== null) {
72+
// return sprintf('Len: >= %d', $this->min);
73+
// }
74+
//
75+
// if ($this->max !== null) {
76+
// return sprintf('Len: <= %d', $this->max);
77+
// }
78+
79+
// if (empty($value)) {
80+
// throw new FlagException("flag '$name' value cannot be empty");
81+
// }
82+
83+
return true;
84+
}
85+
86+
/**
87+
* @return string
88+
*/
89+
public function __toString(): string
90+
{
91+
if ($this->min !== null && $this->max !== null) {
92+
return sprintf('Len: %d - %d', $this->min, $this->max);
93+
}
94+
95+
if ($this->min !== null) {
96+
return sprintf('Len: >= %d', $this->min);
97+
}
98+
99+
if ($this->max !== null) {
100+
return sprintf('Len: <= %d', $this->max);
101+
}
102+
103+
// not limit
104+
return '';
105+
}
106+
}

0 commit comments

Comments
 (0)