From 3399d2fd84834fa40e68f8440fca7c96e8bbd8c3 Mon Sep 17 00:00:00 2001 From: euromark Date: Wed, 26 Nov 2014 21:33:00 +0100 Subject: [PATCH] Check arguments. --- src/Console/ConsoleInputArgument.php | 10 ++++++++++ src/Console/ConsoleOptionParser.php | 6 +++++- tests/TestCase/Console/ConsoleOptionParserTest.php | 10 ++++++++-- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/Console/ConsoleInputArgument.php b/src/Console/ConsoleInputArgument.php index fc402660dff..65d2fdc43b5 100644 --- a/src/Console/ConsoleInputArgument.php +++ b/src/Console/ConsoleInputArgument.php @@ -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. * diff --git a/src/Console/ConsoleOptionParser.php b/src/Console/ConsoleOptionParser.php index e4b109a8cbd..c5d04b32fbf 100644 --- a/src/Console/ConsoleOptionParser.php +++ b/src/Console/ConsoleOptionParser.php @@ -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'])) { @@ -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; diff --git a/tests/TestCase/Console/ConsoleOptionParserTest.php b/tests/TestCase/Console/ConsoleOptionParserTest.php index 01607722b83..12b8d123e41 100644 --- a/tests/TestCase/Console/ConsoleOptionParserTest.php +++ b/tests/TestCase/Console/ConsoleOptionParserTest.php @@ -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(); @@ -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'])); } }