Permalink
Browse files

Fixing issues where option values started with '-'.

  • Loading branch information...
1 parent b739127 commit 5d3c47087142cf2ddd588115d4c895bed65fd86b @markstory markstory committed Aug 6, 2011
Showing with 33 additions and 1 deletion.
  1. +18 −1 lib/Cake/Console/ConsoleOptionParser.php
  2. +15 −0 lib/Cake/Test/Case/Console/ConsoleOptionParserTest.php
@@ -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.
@@ -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

0 comments on commit 5d3c470

Please sign in to comment.