Browse Source

Fixing issues where option values started with '-'.

Mark Story 14 years ago
parent
commit
5d3c470871

+ 18 - 1
lib/Cake/Console/ConsoleOptionParser.php

@@ -572,7 +572,7 @@ class ConsoleOptionParser {
 		$option = $this->_options[$name];
 		$isBoolean = $option->isBoolean();
 		$nextValue = $this->_nextToken();
-		if (!$isBoolean && !empty($nextValue) && $nextValue{0} != '-') {
+		if (!$isBoolean && !empty($nextValue) && !$this->_optionExists($nextValue)) {
 			array_shift($this->_tokens);
 			$value = $nextValue;
 		} elseif ($isBoolean) {
@@ -586,6 +586,23 @@ class ConsoleOptionParser {
 		}
 	}
 
+
+/**
+ * Check to see if $name has an option (short/long) defined for it.
+ *
+ * @param string $name The name of the option.
+ * @return boolean
+ */
+	protected function _optionExists($name) {
+		if (substr($name, 0, 2) === '--') {
+			return isset($this->_options[substr($name, 2)]);
+		}
+		if ($name{0} === '-' && $name{1} !== '-') {
+			return isset($this->_shortOptions[$name{1}]);
+		}
+		return false;
+	}
+
 /**
  * Parse an argument, and ensure that the argument doesn't exceed the number of arguments
  * and that the argument is a valid choice.

+ 15 - 0
lib/Cake/Test/Case/Console/ConsoleOptionParserTest.php

@@ -268,6 +268,21 @@ class ConsoleOptionParserTest extends CakeTestCase {
 	}
 
 /**
+ * Ensure that option values can start with -
+ *
+ * @return void
+ */
+	public function testOptionWithValueStartingWithMinus() {
+		$parser = new ConsoleOptionParser('test', false);
+		$parser->addOption('name')
+			->addOption('age');
+
+		$result = $parser->parse(array('--name', '-foo', '--age', 'old'));
+		$expected = array('name' => '-foo', 'age' => 'old', 'help' => false);
+		$this->assertEquals($expected, $result[0], 'Option values starting with "-" are broken.');
+	}
+
+/**
  * test positional argument parsing.
  *
  * @return void