Skip to content

Commit

Permalink
Fixing issues where option values started with '-'.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Aug 6, 2011
1 parent b739127 commit 5d3c470
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
19 changes: 18 additions & 1 deletion lib/Cake/Console/ConsoleOptionParser.php
Expand Up @@ -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) {
Expand All @@ -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.
Expand Down
15 changes: 15 additions & 0 deletions lib/Cake/Test/Case/Console/ConsoleOptionParserTest.php
Expand Up @@ -267,6 +267,21 @@ public function testOptionWithChoices() {
$result = $parser->parse(array('--name', 'jimmy'));
}

/**
* 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.
*
Expand Down

0 comments on commit 5d3c470

Please sign in to comment.