Skip to content

Commit 8e36450

Browse files
committed
fix: remian args is not string list
1 parent 7a99f46 commit 8e36450

File tree

2 files changed

+20
-47
lines changed

2 files changed

+20
-47
lines changed

src/Flags.php

+11-42
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,11 @@
1717
use Toolkit\PFlag\Flag\Option;
1818
use Toolkit\Stdlib\Str;
1919
use function array_shift;
20+
use function array_values;
2021
use function count;
2122
use function implode;
2223
use function is_array;
23-
use function is_numeric;
2424
use function is_string;
25-
use function ltrim;
2625
use function sprintf;
2726
use function str_split;
2827
use function strlen;
@@ -282,33 +281,6 @@ private function appendRawArgs(array $args): void
282281
}
283282
}
284283

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-
312284
/**
313285
* @param string $shorts eg: 'abc' from '-abc'
314286
*/
@@ -413,11 +385,14 @@ public function bindingArguments(): self
413385
}
414386
}
415387

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);
418394
}
419395

420-
$this->remainArgs = $args;
421396
return $this;
422397
}
423398

@@ -474,7 +449,6 @@ public function addArg(
474449
mixed $default = null,
475450
array $moreInfo = []
476451
): static {
477-
/** @var Argument $arg */
478452
$arg = Argument::new($name, $desc, $type, $required, $default);
479453

480454
$this->addArgument($arg);
@@ -489,13 +463,12 @@ public function addArg(
489463
*
490464
* @return self
491465
* @see argRules for an rule
492-
*
493466
*/
494467
public function addArgByRule(string $name, array|string $rule): static
495468
{
496469
$index = count($this->arguments);
497470
$define = $this->parseRule($rule, $name, $index, false);
498-
/** @var Argument $arg */
471+
499472
$arg = Argument::newByArray($name, $define);
500473

501474
parent::addArgByRule($name, $rule);
@@ -578,7 +551,7 @@ public function hasArg(int|string $nameOrIndex): bool
578551

579552
$index = $this->name2index[$nameOrIndex];
580553
} else {
581-
$index = (int)$nameOrIndex;
554+
$index = $nameOrIndex;
582555
}
583556

584557
return isset($this->arguments[$index]);
@@ -696,7 +669,7 @@ public function getArgIndex(int|string $nameOrIndex): int
696669
return $this->name2index[$nameOrIndex] ?? -1;
697670
}
698671

699-
$index = (int)$nameOrIndex;
672+
$index = $nameOrIndex;
700673
return isset($this->arguments[$index]) ? $index : -1;
701674
}
702675

@@ -772,7 +745,6 @@ public function addOpt(
772745
mixed $default = null,
773746
array $moreInfo = []
774747
): static {
775-
/** @var Option $opt */
776748
$opt = Option::new($name, $desc, $type, $required, $default);
777749
$opt->setAliases($moreInfo['aliases'] ?? []);
778750
$opt->setShortcut($shortcut);
@@ -793,7 +765,6 @@ public function addOpt(
793765
public function addOptByRule(string $name, array|string $rule): static
794766
{
795767
$define = $this->parseRule($rule, $name);
796-
/** @var Option $option */
797768
$option = Option::newByArray($define['name'], $define);
798769

799770
if (is_array($rule) && isset($rule['aliases'])) {
@@ -955,9 +926,7 @@ public function setTrustedOpt(string $name, mixed $value): void
955926
*/
956927
public function getOptDefine(string $name): array
957928
{
958-
$opt = $this->mustGetOption($name);
959-
960-
return $opt->toArray();
929+
return $this->mustGetOption($name)->toArray();
961930
}
962931

963932
/**

src/SFlags.php

+9-5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Toolkit\PFlag\Exception\FlagParseException;
1515
use Toolkit\Stdlib\OS;
1616
use Toolkit\Stdlib\Str;
17+
use function array_values;
1718
use function count;
1819
use function current;
1920
use function explode;
@@ -567,11 +568,13 @@ public function bindingArguments(): void
567568
}
568569
}
569570

570-
if ($this->strictMatchArgs && $args) {
571-
throw new FlagException(sprintf('unknown arguments (error: "%s").', implode(', ', $args)));
572-
}
571+
if ($args) {
572+
if ($this->strictMatchArgs) {
573+
throw new FlagException(sprintf('unknown arguments (error: "%s").', implode(', ', $args)));
574+
}
573575

574-
$this->remainArgs = $args;
576+
$this->remainArgs = array_values($args);
577+
}
575578
}
576579

577580
/**
@@ -951,6 +954,7 @@ public function getArg(int|string $nameOrIndex, mixed $default = null): mixed
951954

952955
/**
953956
* @param int|string $nameOrIndex
957+
* @param string $errMsg
954958
*
955959
* @return mixed
956960
*/
@@ -993,7 +997,7 @@ protected function mustGetArgIndex(int|string $nameOrIndex): int
993997
public function getArgIndex(int|string $nameOrIndex): int
994998
{
995999
if (!is_string($nameOrIndex)) {
996-
$index = (int)$nameOrIndex;
1000+
$index = $nameOrIndex;
9971001
return isset($this->argDefines[$index]) ? $index : -1;
9981002
}
9991003

0 commit comments

Comments
 (0)