From 816fec79b604f871c434c35d2ab0a98853f6678d Mon Sep 17 00:00:00 2001 From: Mark Story Date: Thu, 21 Sep 2017 20:55:36 -0400 Subject: [PATCH] Make argNames parameter a simple list. This makes it easier to integrate with the ConsoleOptionParser. --- src/Console/Arguments.php | 15 ++++++++------- tests/TestCase/Console/ArgumentsTest.php | 4 ++-- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/Console/Arguments.php b/src/Console/Arguments.php index 2f72c8f05ad..24db98030e6 100644 --- a/src/Console/Arguments.php +++ b/src/Console/Arguments.php @@ -46,7 +46,8 @@ class Arguments * * @param string[] $args Positional arguments * @param array $options Named arguments - * @param array $argNames Map of argument names and their indexes. + * @param string[] $argNames List of argument names. Order is expected to be + * the same as $args. */ public function __construct(array $args, array $options, array $argNames) { @@ -99,12 +100,12 @@ public function hasArgumentAt($index) */ public function hasArgument($name) { - if (!isset($this->argNames[$name])) { + $offset = array_search($name, $this->argNames, true); + if ($offset === false) { return false; } - $index = $this->argNames[$name]; - return isset($this->args[$index]); + return isset($this->args[$offset]); } /** @@ -115,11 +116,11 @@ public function hasArgument($name) */ public function getArgument($name) { - if (!isset($this->argNames[$name])) { + $offset = array_search($name, $this->argNames, true); + if ($offset === false) { return null; } - $index = $this->argNames[$name]; - return $this->args[$index]; + return $this->args[$offset]; } } diff --git a/tests/TestCase/Console/ArgumentsTest.php b/tests/TestCase/Console/ArgumentsTest.php index 838d730ace0..5c1297df3fc 100644 --- a/tests/TestCase/Console/ArgumentsTest.php +++ b/tests/TestCase/Console/ArgumentsTest.php @@ -71,7 +71,7 @@ public function testHasArgumentAt() public function testHasArgument() { $values = ['big', 'brown', 'bear']; - $names = ['size' => 0, 'color' => 1, 'species' => 2, 'odd' => 3]; + $names = ['size', 'color', 'species', 'odd']; $args = new Arguments($values, [], $names); $this->assertTrue($args->hasArgument('size')); $this->assertTrue($args->hasArgument('color')); @@ -88,7 +88,7 @@ public function testHasArgument() public function testGetArgument() { $values = ['big', 'brown', 'bear']; - $names = ['size' => 0, 'color' => 1, 'species' => 2, 'odd' => 3]; + $names = ['size', 'color', 'species', 'odd']; $args = new Arguments($values, [], $names); $this->assertSame($values[0], $args->getArgument('size')); $this->assertSame($values[1], $args->getArgument('color'));