17
17
use Toolkit \PFlag \Flag \Option ;
18
18
use Toolkit \Stdlib \Str ;
19
19
use function array_shift ;
20
+ use function array_values ;
20
21
use function count ;
21
22
use function implode ;
22
23
use function is_array ;
23
- use function is_numeric ;
24
24
use function is_string ;
25
- use function ltrim ;
26
25
use function sprintf ;
27
26
use function str_split ;
28
27
use function strlen ;
@@ -282,33 +281,6 @@ private function appendRawArgs(array $args): void
282
281
}
283
282
}
284
283
285
- /**
286
- * check and get option Name
287
- *
288
- * invalid:
289
- * - empty string
290
- * - no prefix '-' (is argument)
291
- * - invalid option name as argument. eg: '- '
292
- *
293
- * @param string $val
294
- *
295
- * @return string
296
- */
297
- private function filterOptionName (string $ val ): string
298
- {
299
- // is not an option.
300
- if ('' === $ val || $ val [0 ] !== '- ' ) {
301
- return '' ;
302
- }
303
-
304
- $ name = ltrim ($ val , '- ' );
305
- if (is_numeric ($ name )) {
306
- return '' ;
307
- }
308
-
309
- return $ name ;
310
- }
311
-
312
284
/**
313
285
* @param string $shorts eg: 'abc' from '-abc'
314
286
*/
@@ -413,11 +385,14 @@ public function bindingArguments(): self
413
385
}
414
386
}
415
387
416
- if ($ this ->strictMatchArgs && $ args ) {
417
- throw new FlagException (sprintf ('unknown arguments (error: "%s"). ' , implode (' ' , $ args )));
388
+ if ($ args ) {
389
+ if ($ this ->strictMatchArgs ) {
390
+ throw new FlagException (sprintf ('unknown arguments (error: "%s"). ' , implode (' ' , $ args )));
391
+ }
392
+
393
+ $ this ->remainArgs = array_values ($ args );
418
394
}
419
395
420
- $ this ->remainArgs = $ args ;
421
396
return $ this ;
422
397
}
423
398
@@ -474,7 +449,6 @@ public function addArg(
474
449
mixed $ default = null ,
475
450
array $ moreInfo = []
476
451
): static {
477
- /** @var Argument $arg */
478
452
$ arg = Argument::new ($ name , $ desc , $ type , $ required , $ default );
479
453
480
454
$ this ->addArgument ($ arg );
@@ -489,13 +463,12 @@ public function addArg(
489
463
*
490
464
* @return self
491
465
* @see argRules for an rule
492
- *
493
466
*/
494
467
public function addArgByRule (string $ name , array |string $ rule ): static
495
468
{
496
469
$ index = count ($ this ->arguments );
497
470
$ define = $ this ->parseRule ($ rule , $ name , $ index , false );
498
- /** @var Argument $arg */
471
+
499
472
$ arg = Argument::newByArray ($ name , $ define );
500
473
501
474
parent ::addArgByRule ($ name , $ rule );
@@ -578,7 +551,7 @@ public function hasArg(int|string $nameOrIndex): bool
578
551
579
552
$ index = $ this ->name2index [$ nameOrIndex ];
580
553
} else {
581
- $ index = ( int ) $ nameOrIndex ;
554
+ $ index = $ nameOrIndex ;
582
555
}
583
556
584
557
return isset ($ this ->arguments [$ index ]);
@@ -696,7 +669,7 @@ public function getArgIndex(int|string $nameOrIndex): int
696
669
return $ this ->name2index [$ nameOrIndex ] ?? -1 ;
697
670
}
698
671
699
- $ index = ( int ) $ nameOrIndex ;
672
+ $ index = $ nameOrIndex ;
700
673
return isset ($ this ->arguments [$ index ]) ? $ index : -1 ;
701
674
}
702
675
@@ -772,7 +745,6 @@ public function addOpt(
772
745
mixed $ default = null ,
773
746
array $ moreInfo = []
774
747
): static {
775
- /** @var Option $opt */
776
748
$ opt = Option::new ($ name , $ desc , $ type , $ required , $ default );
777
749
$ opt ->setAliases ($ moreInfo ['aliases ' ] ?? []);
778
750
$ opt ->setShortcut ($ shortcut );
@@ -793,7 +765,6 @@ public function addOpt(
793
765
public function addOptByRule (string $ name , array |string $ rule ): static
794
766
{
795
767
$ define = $ this ->parseRule ($ rule , $ name );
796
- /** @var Option $option */
797
768
$ option = Option::newByArray ($ define ['name ' ], $ define );
798
769
799
770
if (is_array ($ rule ) && isset ($ rule ['aliases ' ])) {
@@ -955,9 +926,7 @@ public function setTrustedOpt(string $name, mixed $value): void
955
926
*/
956
927
public function getOptDefine (string $ name ): array
957
928
{
958
- $ opt = $ this ->mustGetOption ($ name );
959
-
960
- return $ opt ->toArray ();
929
+ return $ this ->mustGetOption ($ name )->toArray ();
961
930
}
962
931
963
932
/**
0 commit comments