Skip to content

Commit

Permalink
Make argNames parameter a simple list.
Browse files Browse the repository at this point in the history
This makes it easier to integrate with the ConsoleOptionParser.
  • Loading branch information
markstory committed Sep 22, 2017
1 parent 6a7256b commit 816fec7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
15 changes: 8 additions & 7 deletions src/Console/Arguments.php
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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]);
}

/**
Expand All @@ -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];
}
}
4 changes: 2 additions & 2 deletions tests/TestCase/Console/ArgumentsTest.php
Expand Up @@ -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'));
Expand All @@ -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'));
Expand Down

0 comments on commit 816fec7

Please sign in to comment.