Adding exception for unknown option usage.

This commit is contained in:
mark_story 2010-10-09 20:13:27 -04:00
parent d3c95bc2c4
commit 7f5b5c4fbd
2 changed files with 16 additions and 2 deletions

View file

@ -420,6 +420,9 @@ class ConsoleOptionParser {
* @return array Params with $option added in. * @return array Params with $option added in.
*/ */
protected function _parseOptionName($name, $params) { protected function _parseOptionName($name, $params) {
if (!isset($this->_options[$name])) {
throw new InvalidArgumentException(sprintf(__('Unknown option `%s`'), $name));
}
$definition = $this->_options[$name]; $definition = $this->_options[$name];
$nextValue = $this->_nextToken(); $nextValue = $this->_nextToken();
if (!$definition['boolean'] && !empty($nextValue) && $nextValue{0} != '-') { if (!$definition['boolean'] && !empty($nextValue) && $nextValue{0} != '-') {

View file

@ -159,11 +159,22 @@ class ConsoleOptionParserTest extends CakeTestCase {
$parser = new ConsoleOptionParser(); $parser = new ConsoleOptionParser();
$parser->addOption('no-commit', array('boolean' => true)) $parser->addOption('no-commit', array('boolean' => true))
->addOption('table', array('short' => 't')); ->addOption('table', array('short' => 't'));
$result = $parser->parse(array('--table', 'posts', '--no-commit', 'arg1', 'arg2')); $result = $parser->parse(array('--table', 'posts', '--no-commit', 'arg1', 'arg2'));
$expected = array(array('table' => 'posts', 'no-commit' => true), array('arg1', 'arg2')); $expected = array(array('table' => 'posts', 'no-commit' => true), array('arg1', 'arg2'));
$this->assertEquals($expected, $result, 'Boolean option did not parse correctly.'); $this->assertEquals($expected, $result, 'Boolean option did not parse correctly.');
}
/**
* test parsing options that do not exist.
*
* @expectedException InvalidArgumentException
*/
function testOptionThatDoesNotExist() {
$parser = new ConsoleOptionParser();
$parser->addOption('no-commit', array('boolean' => true));
$result = $parser->parse(array('--fail', 'other'));
} }
/** /**