Skip to content

Commit

Permalink
Check arguments.
Browse files Browse the repository at this point in the history
  • Loading branch information
euromark committed Nov 26, 2014
1 parent 22220f5 commit 3399d2f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
10 changes: 10 additions & 0 deletions src/Console/ConsoleInputArgument.php
Expand Up @@ -82,6 +82,16 @@ public function name() {
return $this->_name;
}

/**
* Checks if this argument is equal to another argument.
*
* @param \Cake\Console\ConsoleInputArgument $argument ConsoleInputArgument to compare to.
* @return bool
*/
public function isEqualTo(ConsoleInputArgument $argument) {
return $this->usage() === $argument->usage();
}

/**
* Generate the help for this argument.
*
Expand Down
6 changes: 5 additions & 1 deletion src/Console/ConsoleOptionParser.php
Expand Up @@ -235,7 +235,6 @@ public function merge($spec) {
$spec = $spec->toArray();
}
if (!empty($spec['arguments'])) {
$this->_args = array();
$this->addArguments($spec['arguments']);
}
if (!empty($spec['options'])) {
Expand Down Expand Up @@ -394,6 +393,11 @@ public function addArgument($name, $params = []) {
unset($options['index']);
$arg = new ConsoleInputArgument($options);
}
foreach ($this->_args as $k => $a) {
if ($a->isEqualTo($arg)) {
return $this;
}
}
$this->_args[$index] = $arg;
ksort($this->_args);
return $this;
Expand Down
10 changes: 8 additions & 2 deletions tests/TestCase/Console/ConsoleOptionParserTest.php
Expand Up @@ -701,11 +701,14 @@ public function testToArray() {
*/
public function testMerge() {
$parser = new ConsoleOptionParser('test');
$parser->addOption('test', array('short' => 't', 'boolean' => true));
$parser->addOption('test', array('short' => 't', 'boolean' => true))
->addArgument('one', array('required' => true, 'choices' => array('a', 'b')))
->addArgument('two', array('required' => true));

$parserTwo = new ConsoleOptionParser('test');
$parserTwo->addOption('file', array('short' => 'f', 'boolean' => true))
->addOption('output', array('short' => 'o', 'boolean' => true));
->addOption('output', array('short' => 'o', 'boolean' => true))
->addArgument('one', array('required' => true, 'choices' => array('a', 'b')));

$parser->merge($parserTwo);
$result = $parser->toArray();
Expand All @@ -715,6 +718,9 @@ public function testMerge() {
$this->assertTrue(isset($options['test']));
$this->assertTrue(isset($options['file']));
$this->assertTrue(isset($options['output']));

$this->assertEquals(2, count($result['arguments']));
$this->assertEquals(6, count($result['options']));
}

}

0 comments on commit 3399d2f

Please sign in to comment.