Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixing issues where option values started with '-'.

  • Loading branch information...
commit 5d3c47087142cf2ddd588115d4c895bed65fd86b 1 parent b739127
Mark Story markstory authored
19 lib/Cake/Console/ConsoleOptionParser.php
View
@@ -572,7 +572,7 @@ protected function _parseOption($name, $params) {
$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 @@ protected function _parseOption($name, $params) {
}
}
+
+/**
+ * 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 lib/Cake/Test/Case/Console/ConsoleOptionParserTest.php
View
@@ -268,6 +268,21 @@ public function testOptionWithChoices() {
}
/**
+ * 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
Please sign in to comment.
Something went wrong with that request. Please try again.