Skip to content

Commit

Permalink
fix: collect array argument error
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jan 25, 2022
1 parent df8d27b commit 6246541
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
9 changes: 7 additions & 2 deletions src/Flags.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,9 @@ public function resetResults(): void
parent::resetResults();

$this->matched = [];
foreach ($this->arguments as $arg) {
$arg->setTrustedValue(null);
}
}

/**************************************************************************
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/SFlags.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
40 changes: 37 additions & 3 deletions test/FlagsParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand Down

0 comments on commit 6246541

Please sign in to comment.