Skip to content

Commit e131a7f

Browse files
committed
feat: add new common methods getOptDefine
1 parent 8bf74c1 commit e131a7f

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed

src/Contract/ParserInterface.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
namespace Toolkit\PFlag\Contract;
1111

12+
use Toolkit\PFlag\FlagsParser;
13+
1214
/**
1315
* interface ParserInterface
1416
*/
@@ -127,6 +129,14 @@ public function hasInputOpt(string $name): bool;
127129
*/
128130
public function getOpt(string $name, $default = null);
129131

132+
/**
133+
* @param string $name
134+
*
135+
* @return array
136+
* @see FlagsParser::DEFINE_ITEM
137+
*/
138+
public function getOptDefine(string $name): array;
139+
130140
/**
131141
* Set option value, will format and validate value.
132142
*
@@ -173,6 +183,7 @@ public function getArgIndex($nameOrIndex): int;
173183
* @param string|int $nameOrIndex
174184
*
175185
* @return array
186+
* @see FlagsParser::DEFINE_ITEM
176187
*/
177188
public function getArgDefine($nameOrIndex): array;
178189

@@ -231,12 +242,13 @@ public function getArgsHelpData(): array;
231242
*/
232243
public function getOptsHelpData(): array;
233244

245+
public function lock(): void;
246+
247+
public function unlock(): void;
248+
234249
/**
235250
* @return bool
236251
*/
237252
public function isLocked(): bool;
238253

239-
public function lock(): void;
240-
241-
public function unlock(): void;
242254
}

src/Flags.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -867,7 +867,7 @@ public function hasOpt(string $name): bool
867867
*/
868868
public function getOpt(string $name, $default = null)
869869
{
870-
$opt = $this->getDefinedOption($name);
870+
$opt = $this->getOption($name);
871871
if (!$opt) { // not exist
872872
throw new FlagException("flag option '$name' is undefined");
873873
}
@@ -899,7 +899,7 @@ public function setOpt(string $name, $value): void
899899
*/
900900
public function setTrustedOpt(string $name, $value): void
901901
{
902-
$opt = $this->getDefinedOption($name);
902+
$opt = $this->getOption($name);
903903
if (!$opt) { // not exist option
904904
throw new FlagException("flag option '$name' is undefined");
905905
}
@@ -917,6 +917,21 @@ public function getOption(string $name): ?Option
917917
return $this->options[$name] ?? null;
918918
}
919919

920+
/**
921+
* @param string $name
922+
*
923+
* @return array
924+
*/
925+
public function getOptDefine(string $name): array
926+
{
927+
$opt = $this->getOption($name);
928+
if (!$opt) { // not exist option
929+
throw new FlagException("flag option '$name' is undefined");
930+
}
931+
932+
return $opt->toArray();
933+
}
934+
920935
/**
921936
* @param string $name
922937
*

src/SFlags.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,21 @@ public function getOpt(string $name, $default = null)
813813
return $default ?? FlagType::getDefault($define['type']);
814814
}
815815

816+
/**
817+
* @param string $name
818+
*
819+
* @return array
820+
*/
821+
public function getOptDefine(string $name): array
822+
{
823+
$define = $this->optDefines[$name] ?? [];
824+
if (!$define) { // not exist
825+
throw new FlagException("flag option '$name' is undefined");
826+
}
827+
828+
return $define;
829+
}
830+
816831
/**
817832
* @param string $name
818833
* @param mixed $value

0 commit comments

Comments
 (0)