Skip to content

Commit 6246541

Browse files
committedJan 25, 2022
fix: collect array argument error
1 parent df8d27b commit 6246541

File tree

3 files changed

+46
-5
lines changed

3 files changed

+46
-5
lines changed
 

‎src/Flags.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,9 @@ public function resetResults(): void
344344
parent::resetResults();
345345

346346
$this->matched = [];
347+
foreach ($this->arguments as $arg) {
348+
$arg->setTrustedValue(null);
349+
}
347350
}
348351

349352
/**************************************************************************
@@ -384,9 +387,11 @@ public function bindingArguments(): self
384387

385388
// array: collect all remain args
386389
if ($arg->isArray()) {
387-
foreach ($args as $value) {
388-
$arg->setValue($value);
390+
$arg->setValue($value);
391+
foreach ($args as $val) {
392+
$arg->setValue($val);
389393
}
394+
390395
$remains = $args = [];
391396
} else {
392397
array_shift($remains);

‎src/SFlags.php

+2
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,8 @@ public function bindingArguments(): void
566566

567567
// array: collect all remain args
568568
if ($isArray) {
569+
$this->collectArgValue($value, $index, true, $define);
570+
569571
foreach ($args as $arrValue) {
570572
$this->collectArgValue($arrValue, $index, true, $define);
571573
}

‎test/FlagsParserTest.php

+37-3
Original file line numberDiff line numberDiff line change
@@ -416,14 +416,48 @@ private function doCheckSetOptAndSetArg($fs): void
416416
$this->assertSame("flag argument 'not-exist-arg' is undefined", $e->getMessage());
417417
}
418418

419-
public function testParse_opt_isKV(): void
419+
public function testParse_arrayArg(): void
420420
{
421421
foreach ($this->createParsers() as $fs) {
422-
$this->doTestParse_opt_isKV($fs);
422+
$this->doTestParse_arrayArg($fs);
423423
}
424424
}
425425

426-
public function doTestParse_opt_isKV(FlagsParser $fs): void
426+
public function doTestParse_arrayArg(FlagsParser $fs): void
427+
{
428+
$fs->addOptsByRules([
429+
'env, e' => [
430+
'type' => FlagType::STRING,
431+
'required' => true,
432+
],
433+
]);
434+
$fs->addArgByRule('files', 'array;a array arg');
435+
436+
$flags = ['-e', 'dev', 'abc'];
437+
$fs->parse($flags);
438+
439+
$this->assertNotEmpty($fs->getOpts());
440+
$this->assertNotEmpty($fs->getArgs());
441+
$this->assertEquals(['abc'], $fs->getArg('files'));
442+
$fs->resetResults();
443+
444+
$flags = ['-e', 'dev', 'abc', 'def'];
445+
$fs->parse($flags);
446+
447+
$this->assertNotEmpty($fs->getOpts());
448+
$this->assertNotEmpty($fs->getArgs());
449+
$this->assertEquals(['abc', 'def'], $fs->getArg('files'));
450+
$fs->resetResults();
451+
}
452+
453+
public function testParse_optValueIsKV(): void
454+
{
455+
foreach ($this->createParsers() as $fs) {
456+
$this->doTestParse_optValueIsKV($fs);
457+
}
458+
}
459+
460+
public function doTestParse_optValueIsKV(FlagsParser $fs): void
427461
{
428462
$fs->addOptsByRules([
429463
'env, e' => [

0 commit comments

Comments
 (0)
Please sign in to comment.