@@ -122,11 +122,11 @@ class SFlags extends AbstractFlags
122
122
*
123
123
* ```php
124
124
* [
125
- * 'name' => self::DEFINE_ITEM,
125
+ * self::DEFINE_ITEM,
126
126
* ]
127
127
* ```
128
128
*
129
- * @var array
129
+ * @var array[]
130
130
*/
131
131
private $ argDefines = [];
132
132
@@ -704,7 +704,16 @@ public function getOption(string $name, $default = null)
704
704
*/
705
705
public function getOpt (string $ name , $ default = null )
706
706
{
707
- return $ this ->opts [$ name ] ?? $ default ;
707
+ if (isset ($ this ->opts [$ name ])) {
708
+ return $ this ->opts [$ name ];
709
+ }
710
+
711
+ $ define = $ this ->optDefines [$ name ] ?? [];
712
+ if (!$ define ) { // not exist option
713
+ throw new FlagException ("flag option ' $ name' is undefined " );
714
+ }
715
+
716
+ return $ default ?? FlagType::getDefault ($ define ['type ' ]);
708
717
}
709
718
710
719
/**
@@ -722,17 +731,9 @@ public function getOpts(): array
722
731
*/
723
732
public function hasArg ($ nameOrIndex ): bool
724
733
{
725
- if (is_string ($ nameOrIndex )) {
726
- if (!isset ($ this ->name2index [$ nameOrIndex ])) {
727
- return false ;
728
- }
729
-
730
- $ index = $ this ->name2index [$ nameOrIndex ];
731
- } else {
732
- $ index = (int )$ nameOrIndex ;
733
- }
734
+ $ index = $ this ->getArgIndex ($ nameOrIndex );
734
735
735
- return isset ($ this ->args [$ index ]);
736
+ return $ index > - 1 && isset ($ this ->args [$ index ]);
736
737
}
737
738
738
739
/**
@@ -754,17 +755,18 @@ public function getArgument($nameOrIndex, $default = null)
754
755
*/
755
756
public function getArg ($ nameOrIndex , $ default = null )
756
757
{
757
- if ( is_string ( $ nameOrIndex )) {
758
- if (! isset ( $ this -> name2index [ $ nameOrIndex ]) ) {
759
- throw new FlagException ("flag argument name ' $ nameOrIndex' is undefined " );
760
- }
758
+ $ index = $ this -> getArgIndex ( $ nameOrIndex );
759
+ if ($ index < 0 ) {
760
+ throw new FlagException ("flag argument ' $ nameOrIndex' is undefined " );
761
+ }
761
762
762
- $ index = $ this ->name2index [$ nameOrIndex ];
763
- } else {
764
- $ index = (int )$ nameOrIndex ;
763
+ if (isset ($ this ->args [$ index ])) {
764
+ return $ this ->args [$ index ];
765
765
}
766
766
767
- return $ this ->args [$ index ] ?? $ default ;
767
+ // get default with type format
768
+ $ define = $ this ->argDefines [$ index ];
769
+ return $ default ?? FlagType::getDefault ($ define ['type ' ]);
768
770
}
769
771
770
772
/**
@@ -780,19 +782,16 @@ public function getFirstArg($default = null)
780
782
/**
781
783
* @param string|int $nameOrIndex
782
784
*
783
- * @return int
785
+ * @return int Will return -1 if arg not exists
784
786
*/
785
- public function getArgIndex ($ nameOrIndex ): int
787
+ protected function getArgIndex ($ nameOrIndex ): int
786
788
{
787
789
if (!is_string ($ nameOrIndex )) {
788
- return (int )$ nameOrIndex ;
789
- }
790
-
791
- if (!isset ($ this ->name2index [$ nameOrIndex ])) {
792
- throw new FlagException ("flag argument name ' $ nameOrIndex' is undefined " );
790
+ $ index = (int )$ nameOrIndex ;
791
+ return isset ($ this ->argDefines [$ index ]) ? $ index : -1 ;
793
792
}
794
793
795
- return $ this ->name2index [$ nameOrIndex ];
794
+ return $ this ->name2index [$ nameOrIndex ] ?? - 1 ;
796
795
}
797
796
798
797
/**
@@ -842,6 +841,18 @@ public function addArgRule(string $name, $rule): self
842
841
return $ this ;
843
842
}
844
843
844
+ /**
845
+ * @param string|int $nameOrIndex
846
+ *
847
+ * @return bool
848
+ */
849
+ public function hasDefineArg ($ nameOrIndex ): bool
850
+ {
851
+ $ index = $ this ->getArgIndex ($ nameOrIndex );
852
+
853
+ return isset ($ this ->argDefines [$ index ]);
854
+ }
855
+
845
856
/**
846
857
* @return array
847
858
*/
@@ -881,6 +892,16 @@ public function addOptRule(string $name, $rule): self
881
892
return $ this ;
882
893
}
883
894
895
+ /**
896
+ * @param string $name
897
+ *
898
+ * @return bool
899
+ */
900
+ public function hasDefineOpt (string $ name ): bool
901
+ {
902
+ return isset ($ this ->optDefines [$ name ]);
903
+ }
904
+
884
905
/**
885
906
* @return array
886
907
*/
0 commit comments