File tree Expand file tree Collapse file tree 4 files changed +61
-6
lines changed Expand file tree Collapse file tree 4 files changed +61
-6
lines changed Original file line number Diff line number Diff line change @@ -108,6 +108,14 @@ public function hasOpt(string $name): bool;
108
108
*/
109
109
public function getOpt (string $ name , $ default = null );
110
110
111
+ /**
112
+ * @param string $name
113
+ * @param mixed $value
114
+ *
115
+ * @return mixed
116
+ */
117
+ public function setOpt (string $ name , $ value ): void ;
118
+
111
119
/**
112
120
* @param string|int $nameOrIndex
113
121
*
Original file line number Diff line number Diff line change 19
19
use Toolkit \Stdlib \OS ;
20
20
use function is_array ;
21
21
use function is_bool ;
22
+ use function is_scalar ;
22
23
use function trim ;
23
24
24
25
/**
@@ -190,7 +191,8 @@ public function setValue($value): void
190
191
$ value = FlagType::fmtBasicTypeValue ($ this ->type , $ value );
191
192
192
193
// has validator
193
- if ($ cb = $ this ->validator ) {
194
+ $ cb = $ this ->validator ;
195
+ if ($ cb && is_scalar ($ value )) {
194
196
$ ok = true ;
195
197
$ ret = $ cb ($ value , $ this ->name );
196
198
@@ -206,7 +208,11 @@ public function setValue($value): void
206
208
}
207
209
208
210
if ($ this ->isArray ()) {
209
- $ this ->value [] = $ value ;
211
+ if (is_array ($ value )) {
212
+ $ this ->value = $ value ;
213
+ } else {
214
+ $ this ->value [] = $ value ;
215
+ }
210
216
} else {
211
217
$ this ->value = $ value ;
212
218
}
@@ -285,6 +291,14 @@ public function setName(string $name): void
285
291
$ this ->name = $ name ;
286
292
}
287
293
294
+ /**
295
+ * @return array|false|float|int|string|null
296
+ */
297
+ public function getTypeDefault ()
298
+ {
299
+ return FlagType::getDefault ($ this ->type );
300
+ }
301
+
288
302
/**
289
303
* @return mixed
290
304
*/
Original file line number Diff line number Diff line change @@ -783,7 +783,7 @@ protected function addMatched(Option $option): void
783
783
*/
784
784
public function hasOpt (string $ name ): bool
785
785
{
786
- return isset ($ this ->matched [$ name ]);
786
+ return isset ($ this ->options [$ name ]);
787
787
}
788
788
789
789
/**
@@ -804,11 +804,30 @@ public function hasMatched(string $name): bool
804
804
*/
805
805
public function getOpt (string $ name , $ default = null )
806
806
{
807
- if ($ opt = $ this ->getOption ($ name )) {
807
+ $ opt = $ this ->getDefinedOption ($ name );
808
+ if (!$ opt ) { // not exist option
809
+ throw new FlagException ("flag option ' $ name' is undefined " );
810
+ }
811
+
812
+ if ($ opt ->hasValue ()) {
808
813
return $ opt ->getValue ();
809
814
}
810
815
811
- return $ default ;
816
+ return $ default ?? $ opt ->getTypeDefault ();
817
+ }
818
+
819
+ /**
820
+ * @param string $name
821
+ * @param mixed $value
822
+ */
823
+ public function setOpt (string $ name , $ value ): void
824
+ {
825
+ $ opt = $ this ->getDefinedOption ($ name );
826
+ if (!$ opt ) { // not exist option
827
+ throw new FlagException ("flag option ' $ name' is undefined " );
828
+ }
829
+
830
+ $ opt ->setValue ($ value );
812
831
}
813
832
814
833
/**
Original file line number Diff line number Diff line change @@ -740,7 +740,7 @@ protected function addArgDefine(array $define): void
740
740
*/
741
741
public function hasOpt (string $ name ): bool
742
742
{
743
- return isset ($ this ->opts [$ name ]);
743
+ return isset ($ this ->optDefines [$ name ]);
744
744
}
745
745
746
746
/**
@@ -784,6 +784,20 @@ public function getOpt(string $name, $default = null)
784
784
return $ default ?? FlagType::getDefault ($ define ['type ' ]);
785
785
}
786
786
787
+ /**
788
+ * @param string $name
789
+ * @param mixed $value
790
+ */
791
+ public function setOpt (string $ name , $ value ): void
792
+ {
793
+ $ define = $ this ->optDefines [$ name ] ?? [];
794
+ if (!$ define ) { // not exist option
795
+ throw new FlagException ("flag option ' $ name' is undefined " );
796
+ }
797
+
798
+ $ this ->opts [$ name ] = FlagType::fmtBasicTypeValue ($ define ['type ' ], $ value );
799
+ }
800
+
787
801
/**
788
802
* @return array
789
803
*/
You can’t perform that action at this time.
0 commit comments