Browse Source

Check arguments.

euromark 11 years ago
parent
commit
3399d2fd84

+ 10 - 0
src/Console/ConsoleInputArgument.php

@@ -83,6 +83,16 @@ class ConsoleInputArgument {
 	}
 
 /**
+ * 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.
  *
  * @param int $width The width to make the name of the option.

+ 5 - 1
src/Console/ConsoleOptionParser.php

@@ -235,7 +235,6 @@ class ConsoleOptionParser {
 			$spec = $spec->toArray();
 		}
 		if (!empty($spec['arguments'])) {
-			$this->_args = array();
 			$this->addArguments($spec['arguments']);
 		}
 		if (!empty($spec['options'])) {
@@ -394,6 +393,11 @@ class ConsoleOptionParser {
 			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;

+ 8 - 2
tests/TestCase/Console/ConsoleOptionParserTest.php

@@ -701,11 +701,14 @@ TEXT;
  */
 	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 @@ TEXT;
 		$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']));
 	}
 
 }