diff --git a/src/Flags.php b/src/Flags.php index 1ad3f6f..c3f8c37 100644 --- a/src/Flags.php +++ b/src/Flags.php @@ -344,6 +344,9 @@ public function resetResults(): void parent::resetResults(); $this->matched = []; + foreach ($this->arguments as $arg) { + $arg->setTrustedValue(null); + } } /************************************************************************** @@ -384,9 +387,11 @@ public function bindingArguments(): self // array: collect all remain args if ($arg->isArray()) { - foreach ($args as $value) { - $arg->setValue($value); + $arg->setValue($value); + foreach ($args as $val) { + $arg->setValue($val); } + $remains = $args = []; } else { array_shift($remains); diff --git a/src/SFlags.php b/src/SFlags.php index 12c5114..f1c08ce 100644 --- a/src/SFlags.php +++ b/src/SFlags.php @@ -566,6 +566,8 @@ public function bindingArguments(): void // array: collect all remain args if ($isArray) { + $this->collectArgValue($value, $index, true, $define); + foreach ($args as $arrValue) { $this->collectArgValue($arrValue, $index, true, $define); } diff --git a/test/FlagsParserTest.php b/test/FlagsParserTest.php index 1747093..b8b87c6 100644 --- a/test/FlagsParserTest.php +++ b/test/FlagsParserTest.php @@ -416,14 +416,48 @@ private function doCheckSetOptAndSetArg($fs): void $this->assertSame("flag argument 'not-exist-arg' is undefined", $e->getMessage()); } - public function testParse_opt_isKV(): void + public function testParse_arrayArg(): void { foreach ($this->createParsers() as $fs) { - $this->doTestParse_opt_isKV($fs); + $this->doTestParse_arrayArg($fs); } } - public function doTestParse_opt_isKV(FlagsParser $fs): void + public function doTestParse_arrayArg(FlagsParser $fs): void + { + $fs->addOptsByRules([ + 'env, e' => [ + 'type' => FlagType::STRING, + 'required' => true, + ], + ]); + $fs->addArgByRule('files', 'array;a array arg'); + + $flags = ['-e', 'dev', 'abc']; + $fs->parse($flags); + + $this->assertNotEmpty($fs->getOpts()); + $this->assertNotEmpty($fs->getArgs()); + $this->assertEquals(['abc'], $fs->getArg('files')); + $fs->resetResults(); + + $flags = ['-e', 'dev', 'abc', 'def']; + $fs->parse($flags); + + $this->assertNotEmpty($fs->getOpts()); + $this->assertNotEmpty($fs->getArgs()); + $this->assertEquals(['abc', 'def'], $fs->getArg('files')); + $fs->resetResults(); + } + + public function testParse_optValueIsKV(): void + { + foreach ($this->createParsers() as $fs) { + $this->doTestParse_optValueIsKV($fs); + } + } + + public function doTestParse_optValueIsKV(FlagsParser $fs): void { $fs->addOptsByRules([ 'env, e' => [