Skip to content

Commit 9ece2c6

Browse files
committed
feat: add new method getMustArg for get required arg
1 parent 28ff0ea commit 9ece2c6

File tree

4 files changed

+99
-27
lines changed

4 files changed

+99
-27
lines changed

src/Contract/ParserInterface.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,15 @@ public function getArgDefine($nameOrIndex): array;
197197
*/
198198
public function getArg($nameOrIndex, $default = null);
199199

200+
/**
201+
* Get an argument value by name, will throw exception on not input
202+
*
203+
* @param string|int $nameOrIndex
204+
*
205+
* @return mixed
206+
*/
207+
public function getMustArg($nameOrIndex, string $errMsg = '');
208+
200209
/**
201210
* Set trusted argument value, will not format and validate value.
202211
*

src/Flags.php

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

1010
namespace Toolkit\PFlag;
1111

12+
use InvalidArgumentException;
1213
use RuntimeException;
1314
use Toolkit\PFlag\Contract\ParserInterface;
1415
use Toolkit\PFlag\Exception\FlagException;
@@ -589,11 +590,7 @@ public function hasArg($nameOrIndex): bool
589590
*/
590591
public function setArg($nameOrIndex, $value): void
591592
{
592-
$arg = $this->getArgument($nameOrIndex);
593-
if (!$arg) { // not exist
594-
throw new FlagException("flag argument '$nameOrIndex' is undefined");
595-
}
596-
593+
$arg = $this->mustGetArgument($nameOrIndex);
597594
$arg->setValue($value);
598595
}
599596

@@ -603,14 +600,31 @@ public function setArg($nameOrIndex, $value): void
603600
*/
604601
public function setTrustedArg(string $name, $value): void
605602
{
606-
$arg = $this->getArgument($name);
607-
if (!$arg) { // not exist
608-
throw new FlagException("flag argument '$name' is undefined");
609-
}
603+
$arg = $this->mustGetArgument($name);
610604

611605
$arg->setTrustedValue($value);
612606
}
613607

608+
/**
609+
* @param string|int $nameOrIndex
610+
*
611+
* @return mixed
612+
*/
613+
public function getMustArg($nameOrIndex, string $errMsg = '')
614+
{
615+
$arg = $this->mustGetArgument($nameOrIndex);
616+
if ($arg->hasValue()) {
617+
return $arg->getValue();
618+
}
619+
620+
if (!$errMsg) {
621+
$errName = $arg->getNameMark();
622+
$errMsg = "The argument '$errName' is required";
623+
}
624+
625+
throw new InvalidArgumentException($errMsg);
626+
}
627+
614628
/**
615629
* @param string|int $nameOrIndex
616630
* @param null|mixed $default
@@ -619,10 +633,7 @@ public function setTrustedArg(string $name, $value): void
619633
*/
620634
public function getArg($nameOrIndex, $default = null)
621635
{
622-
$arg = $this->getArgument($nameOrIndex);
623-
if (!$arg) { // not exist
624-
throw new FlagException("flag argument '$nameOrIndex' is undefined");
625-
}
636+
$arg = $this->mustGetArgument($nameOrIndex);
626637

627638
if ($arg->hasValue()) {
628639
return $arg->getValue();
@@ -631,6 +642,21 @@ public function getArg($nameOrIndex, $default = null)
631642
return $default ?? $arg->getTypeDefault();
632643
}
633644

645+
/**
646+
* @param string|int $nameOrIndex
647+
*
648+
* @return Argument
649+
*/
650+
protected function mustGetArgument($nameOrIndex): Argument
651+
{
652+
$arg = $this->getArgument($nameOrIndex);
653+
if (!$arg) { // not exist
654+
throw new FlagException("flag argument '$nameOrIndex' is undefined");
655+
}
656+
657+
return $arg;
658+
}
659+
634660
/**
635661
* @param string|int $nameOrIndex
636662
*

src/SFlags.php

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class SFlags extends FlagsParser
9696
private $name2index = [];
9797

9898
/**
99-
* Parsed argument values
99+
* Parsed input argument values
100100
* - key is a self-increasing index
101101
*
102102
* ```php
@@ -895,12 +895,9 @@ public function getArgument($nameOrIndex, $default = null)
895895
*/
896896
public function setArg($nameOrIndex, $value): void
897897
{
898-
$index = $this->getArgIndex($nameOrIndex);
899-
if ($index < 0) {
900-
throw new FlagException("flag argument '$nameOrIndex' is undefined");
901-
}
902-
898+
$index = $this->mustGetArgIndex($nameOrIndex);
903899
$define = $this->argDefines[$index];
900+
904901
// set value
905902
$this->args[$index] = FlagType::fmtBasicTypeValue($define['type'], $value);
906903
}
@@ -911,10 +908,7 @@ public function setArg($nameOrIndex, $value): void
911908
*/
912909
public function setTrustedArg(string $name, $value): void
913910
{
914-
$index = $this->getArgIndex($name);
915-
if ($index < 0) {
916-
throw new FlagException("flag argument '$name' is undefined");
917-
}
911+
$index = $this->mustGetArgIndex($name);
918912

919913
$this->args[$index] = $value;
920914
}
@@ -927,10 +921,7 @@ public function setTrustedArg(string $name, $value): void
927921
*/
928922
public function getArg($nameOrIndex, $default = null)
929923
{
930-
$index = $this->getArgIndex($nameOrIndex);
931-
if ($index < 0) {
932-
throw new FlagException("flag argument '$nameOrIndex' is undefined");
933-
}
924+
$index = $this->mustGetArgIndex($nameOrIndex);
934925

935926
if (isset($this->args[$index])) {
936927
return $this->args[$index];
@@ -941,6 +932,42 @@ public function getArg($nameOrIndex, $default = null)
941932
return $default ?? FlagType::getDefault($define['type']);
942933
}
943934

935+
/**
936+
* @param string|int $nameOrIndex
937+
*
938+
* @return mixed
939+
*/
940+
public function getMustArg($nameOrIndex, string $errMsg = '')
941+
{
942+
$index = $this->mustGetArgIndex($nameOrIndex);
943+
if (isset($this->args[$index])) {
944+
return $this->args[$index];
945+
}
946+
947+
if (!$errMsg) {
948+
$define = $this->argDefines[$index];
949+
$errName = $define['name'] ? "#$index({$define['name']})" : "#$index";
950+
$errMsg = "The argument '$errName' is required";
951+
}
952+
953+
throw new InvalidArgumentException($errMsg);
954+
}
955+
956+
/**
957+
* @param string|int $nameOrIndex
958+
*
959+
* @return int
960+
*/
961+
protected function mustGetArgIndex($nameOrIndex): int
962+
{
963+
$index = $this->getArgIndex($nameOrIndex);
964+
if ($index < 0) {
965+
throw new FlagException("flag argument '$nameOrIndex' is undefined");
966+
}
967+
968+
return $index;
969+
}
970+
944971
/**
945972
* @param string|int $nameOrIndex
946973
*

test/FlagsParserTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Toolkit\PFlagTest;
44

5+
use InvalidArgumentException;
56
use Toolkit\PFlag\Exception\FlagException;
67
use Toolkit\PFlag\FlagsParser;
78
use function get_class;
@@ -68,7 +69,16 @@ private function doTestGetOptAndGetArg(FlagsParser $fs): void
6869
$this->assertTrue($ok);
6970
$this->assertSame(335, $fs->getOpt('int-opt'));
7071
$this->assertSame(233, $fs->getArg('int-arg'));
72+
$this->assertSame(233, $fs->getMustArg('int-arg'));
7173
$fs->resetResults();
74+
75+
// getMustArg
76+
$e = $this->runAndGetException(function (FlagsParser $fs) {
77+
$fs->getMustArg('str-arg');
78+
}, $fs);
79+
80+
$this->assertSame(InvalidArgumentException::class, get_class($e));
81+
$this->assertSame("The argument '#1(str-arg)' is required", $e->getMessage());
7282
}
7383

7484
public function testParse_specialArg(): void

0 commit comments

Comments
 (0)