Skip to content

Commit f6e0e5e

Browse files
committed
feat: add some new common methods
1 parent 52d1b32 commit f6e0e5e

File tree

3 files changed

+122
-15
lines changed

3 files changed

+122
-15
lines changed

src/Contract/ParserInterface.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,23 @@ public function addArg(
100100
public function parse(?array $flags = null): bool;
101101

102102
/**
103+
* Whether defined the option
104+
*
103105
* @param string $name
104106
*
105107
* @return bool
106108
*/
107109
public function hasOpt(string $name): bool;
108110

111+
/**
112+
* Whether input argument
113+
*
114+
* @param string $name
115+
*
116+
* @return bool
117+
*/
118+
public function hasInputOpt(string $name): bool;
119+
109120
/**
110121
* Get an option value by name
111122
*
@@ -134,12 +145,30 @@ public function setOpt(string $name, $value): void;
134145
public function setTrustedOpt(string $name, $value): void;
135146

136147
/**
148+
* Whether defined the argument
149+
*
137150
* @param string|int $nameOrIndex
138151
*
139152
* @return bool
140153
*/
141154
public function hasArg($nameOrIndex): bool;
142155

156+
/**
157+
* Whether input argument
158+
*
159+
* @param string|int $nameOrIndex
160+
*
161+
* @return bool
162+
*/
163+
public function hasInputArg($nameOrIndex): bool;
164+
165+
/**
166+
* @param string|int $nameOrIndex
167+
*
168+
* @return int Will return -1 if arg not exists
169+
*/
170+
public function getArgIndex($nameOrIndex): int;
171+
143172
/**
144173
* Get an argument value by name
145174
*

src/Flags.php

Lines changed: 66 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ public function getArgument($nameOrIndex): ?Argument
634634
{
635635
if (is_string($nameOrIndex)) {
636636
if (!isset($this->name2index[$nameOrIndex])) {
637-
throw new FlagException("flag argument '$nameOrIndex' is undefined");
637+
return null;
638638
}
639639

640640
$index = $this->name2index[$nameOrIndex];
@@ -645,6 +645,38 @@ public function getArgument($nameOrIndex): ?Argument
645645
return $this->arguments[$index] ?? null;
646646
}
647647

648+
/**
649+
* @param string|int $nameOrIndex
650+
*
651+
* @return int
652+
*/
653+
public function getArgIndex($nameOrIndex): int
654+
{
655+
if (is_string($nameOrIndex)) {
656+
return $this->name2index[$nameOrIndex] ?? -1;
657+
}
658+
659+
$index = (int)$nameOrIndex;
660+
return isset($this->arguments[$index]) ? $index : -1;
661+
}
662+
663+
/**
664+
* Whether input argument
665+
*
666+
* @param string|int $nameOrIndex
667+
*
668+
* @return bool
669+
*/
670+
public function hasInputArg($nameOrIndex): bool
671+
{
672+
$arg = $this->getArgument($nameOrIndex);
673+
if (!$arg) {
674+
return false;
675+
}
676+
677+
return $arg->hasValue();
678+
}
679+
648680
/**
649681
* @return array
650682
*/
@@ -820,16 +852,6 @@ public function hasOpt(string $name): bool
820852
return isset($this->options[$name]);
821853
}
822854

823-
/**
824-
* @param string $name
825-
*
826-
* @return bool
827-
*/
828-
public function hasMatched(string $name): bool
829-
{
830-
return isset($this->matched[$name]);
831-
}
832-
833855
/**
834856
* @param string $name
835857
* @param null|mixed $default
@@ -839,7 +861,7 @@ public function hasMatched(string $name): bool
839861
public function getOpt(string $name, $default = null)
840862
{
841863
$opt = $this->getDefinedOption($name);
842-
if (!$opt) { // not exist option
864+
if (!$opt) { // not exist
843865
throw new FlagException("flag option '$name' is undefined");
844866
}
845867

@@ -857,7 +879,7 @@ public function getOpt(string $name, $default = null)
857879
public function setOpt(string $name, $value): void
858880
{
859881
$opt = $this->getDefinedOption($name);
860-
if (!$opt) { // not exist option
882+
if (!$opt) { // not exist
861883
throw new FlagException("flag option '$name' is undefined");
862884
}
863885

@@ -885,7 +907,27 @@ public function setTrustedOpt(string $name, $value): void
885907
*/
886908
public function getOption(string $name): ?Option
887909
{
888-
return $this->matched[$name] ?? null;
910+
return $this->options[$name] ?? null;
911+
}
912+
913+
/**
914+
* @param string $name
915+
*
916+
* @return bool
917+
*/
918+
public function hasInputOpt(string $name): bool
919+
{
920+
return isset($this->matched[$name]);
921+
}
922+
923+
/**
924+
* @param string $name
925+
*
926+
* @return bool
927+
*/
928+
public function hasMatched(string $name): bool
929+
{
930+
return isset($this->matched[$name]);
889931
}
890932

891933
/**
@@ -977,4 +1019,14 @@ public function getMatchedOptions(): array
9771019
{
9781020
return $this->matched;
9791021
}
1022+
1023+
/**
1024+
* @param string $name
1025+
*
1026+
* @return Option|null
1027+
*/
1028+
public function getMatchedOption(string $name): ?Option
1029+
{
1030+
return $this->matched[$name] ?? null;
1031+
}
9801032
}

src/SFlags.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -927,7 +927,7 @@ public function getArg($nameOrIndex, $default = null)
927927
*
928928
* @return int Will return -1 if arg not exists
929929
*/
930-
protected function getArgIndex($nameOrIndex): int
930+
public function getArgIndex($nameOrIndex): int
931931
{
932932
if (!is_string($nameOrIndex)) {
933933
$index = (int)$nameOrIndex;
@@ -982,6 +982,20 @@ public function getOptsHelpData(): array
982982
return $helpData;
983983
}
984984

985+
/**
986+
* Whether input argument
987+
*
988+
* @param string|int $nameOrIndex
989+
*
990+
* @return bool
991+
*/
992+
public function hasInputArg($nameOrIndex): bool
993+
{
994+
$index = $this->getArgIndex($nameOrIndex);
995+
996+
return isset($this->args[$index]);
997+
}
998+
985999
/**
9861000
* @param string|int $nameOrIndex
9871001
*
@@ -1002,6 +1016,18 @@ public function getArgDefines(): array
10021016
return $this->argDefines;
10031017
}
10041018

1019+
/**
1020+
* Whether input argument
1021+
*
1022+
* @param string $name
1023+
*
1024+
* @return bool
1025+
*/
1026+
public function hasInputOpt(string $name): bool
1027+
{
1028+
return isset($this->opts[$name]);
1029+
}
1030+
10051031
/**
10061032
* @param string $name
10071033
*

0 commit comments

Comments
 (0)