Skip to content

Commit 1242dd7

Browse files
committed
feat: add new method getMustOpt for get required opt
1 parent 9ece2c6 commit 1242dd7

File tree

4 files changed

+80
-25
lines changed

4 files changed

+80
-25
lines changed

src/Contract/ParserInterface.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,16 @@ public function hasInputOpt(string $name): bool;
129129
*/
130130
public function getOpt(string $name, $default = null);
131131

132+
/**
133+
* Must get an option value by name, will throw exception on not input
134+
*
135+
* @param string $name
136+
* @param string $errMsg
137+
*
138+
* @return mixed
139+
*/
140+
public function getMustOpt(string $name, string $errMsg = '');
141+
132142
/**
133143
* @param string $name
134144
*
@@ -198,9 +208,10 @@ public function getArgDefine($nameOrIndex): array;
198208
public function getArg($nameOrIndex, $default = null);
199209

200210
/**
201-
* Get an argument value by name, will throw exception on not input
211+
* Must get an argument value by name, will throw exception on not input
202212
*
203213
* @param string|int $nameOrIndex
214+
* @param string $errMsg
204215
*
205216
* @return mixed
206217
*/

src/Flags.php

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -899,10 +899,7 @@ public function hasOpt(string $name): bool
899899
*/
900900
public function getOpt(string $name, $default = null)
901901
{
902-
$opt = $this->getOption($name);
903-
if (!$opt) { // not exist
904-
throw new FlagException("flag option '$name' is undefined");
905-
}
902+
$opt = $this->mustGetOption($name);
906903

907904
if ($opt->hasValue()) {
908905
return $opt->getValue();
@@ -913,15 +910,29 @@ public function getOpt(string $name, $default = null)
913910

914911
/**
915912
* @param string $name
916-
* @param mixed $value
913+
* @param string $errMsg
914+
*
915+
* @return mixed
917916
*/
918-
public function setOpt(string $name, $value): void
917+
public function getMustOpt(string $name, string $errMsg = '')
919918
{
920-
$opt = $this->getDefinedOption($name);
921-
if (!$opt) { // not exist
922-
throw new FlagException("flag option '$name' is undefined");
919+
$opt = $this->mustGetOption($name);
920+
921+
if ($opt->hasValue()) {
922+
return $opt->getValue();
923923
}
924924

925+
$errMsg = $errMsg ?: "The option '$name' is required";
926+
throw new InvalidArgumentException($errMsg);
927+
}
928+
929+
/**
930+
* @param string $name
931+
* @param mixed $value
932+
*/
933+
public function setOpt(string $name, $value): void
934+
{
935+
$opt = $this->mustGetOption($name);
925936
$opt->setValue($value);
926937
}
927938

@@ -931,37 +942,35 @@ public function setOpt(string $name, $value): void
931942
*/
932943
public function setTrustedOpt(string $name, $value): void
933944
{
934-
$opt = $this->getOption($name);
935-
if (!$opt) { // not exist option
936-
throw new FlagException("flag option '$name' is undefined");
937-
}
938-
945+
$opt = $this->mustGetOption($name);
939946
$opt->setTrustedValue($value);
940947
}
941948

942949
/**
943950
* @param string $name
944951
*
945-
* @return Option|null
952+
* @return array
946953
*/
947-
public function getOption(string $name): ?Option
954+
public function getOptDefine(string $name): array
948955
{
949-
return $this->options[$name] ?? null;
956+
$opt = $this->mustGetOption($name);
957+
958+
return $opt->toArray();
950959
}
951960

952961
/**
953962
* @param string $name
954963
*
955-
* @return array
964+
* @return Option
956965
*/
957-
public function getOptDefine(string $name): array
966+
protected function mustGetOption(string $name): Option
958967
{
959968
$opt = $this->getOption($name);
960969
if (!$opt) { // not exist option
961970
throw new FlagException("flag option '$name' is undefined");
962971
}
963972

964-
return $opt->toArray();
973+
return $opt;
965974
}
966975

967976
/**
@@ -1035,6 +1044,18 @@ public function getOptsHelpData(): array
10351044
*
10361045
* @return Option|null
10371046
*/
1047+
public function getOption(string $name): ?Option
1048+
{
1049+
return $this->options[$name] ?? null;
1050+
}
1051+
1052+
/**
1053+
* Alias of the getOption();
1054+
*
1055+
* @param string $name
1056+
*
1057+
* @return Option|null
1058+
*/
10381059
public function getDefinedOption(string $name): ?Option
10391060
{
10401061
return $this->options[$name] ?? null;

src/SFlags.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -807,14 +807,28 @@ public function getOpt(string $name, $default = null)
807807
return $this->opts[$name];
808808
}
809809

810-
$define = $this->optDefines[$name] ?? [];
811-
if (!$define) { // not exist option
812-
throw new FlagException("flag option '$name' is undefined");
813-
}
810+
$define = $this->getOptDefine($name);
814811

815812
return $default ?? FlagType::getDefault($define['type']);
816813
}
817814

815+
/**
816+
* @param string $name
817+
* @param string $errMsg
818+
*
819+
* @return mixed
820+
*/
821+
public function getMustOpt(string $name, string $errMsg = '')
822+
{
823+
if (isset($this->opts[$name])) {
824+
return $this->opts[$name];
825+
}
826+
827+
$this->getOptDefine($name);
828+
$errMsg = $errMsg ?: "The option '$name' is required";
829+
throw new InvalidArgumentException($errMsg);
830+
}
831+
818832
/**
819833
* @param string $name
820834
*

test/FlagsParserTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,19 @@ private function doTestGetOptAndGetArg(FlagsParser $fs): void
6868
$ok = $fs->parse(['--str-opt1', 'val1', '--int-opt', '335', '233']);
6969
$this->assertTrue($ok);
7070
$this->assertSame(335, $fs->getOpt('int-opt'));
71+
$this->assertSame(335, $fs->getMustOpt('int-opt'));
7172
$this->assertSame(233, $fs->getArg('int-arg'));
7273
$this->assertSame(233, $fs->getMustArg('int-arg'));
7374
$fs->resetResults();
7475

76+
// getMustOpt
77+
$e = $this->runAndGetException(function (FlagsParser $fs) {
78+
$fs->getMustOpt('str-opt');
79+
}, $fs);
80+
81+
$this->assertSame(InvalidArgumentException::class, get_class($e));
82+
$this->assertSame("The option 'str-opt' is required", $e->getMessage());
83+
7584
// getMustArg
7685
$e = $this->runAndGetException(function (FlagsParser $fs) {
7786
$fs->getMustArg('str-arg');

0 commit comments

Comments
 (0)