diff --git a/cake/console/console_option_parser.php b/cake/console/console_option_parser.php index 25d011b4439..ef8cafdfac7 100644 --- a/cake/console/console_option_parser.php +++ b/cake/console/console_option_parser.php @@ -420,6 +420,9 @@ protected function _parseShortOption($option, $params) { * @return array Params with $option added in. */ protected function _parseOptionName($name, $params) { + if (!isset($this->_options[$name])) { + throw new InvalidArgumentException(sprintf(__('Unknown option `%s`'), $name)); + } $definition = $this->_options[$name]; $nextValue = $this->_nextToken(); if (!$definition['boolean'] && !empty($nextValue) && $nextValue{0} != '-') { diff --git a/cake/tests/cases/console/console_option_parser.test.php b/cake/tests/cases/console/console_option_parser.test.php index 2dcfd0af0d8..4f1271a151d 100644 --- a/cake/tests/cases/console/console_option_parser.test.php +++ b/cake/tests/cases/console/console_option_parser.test.php @@ -159,11 +159,22 @@ function testOptionWithBooleanParam() { $parser = new ConsoleOptionParser(); $parser->addOption('no-commit', array('boolean' => true)) ->addOption('table', array('short' => 't')); - + $result = $parser->parse(array('--table', 'posts', '--no-commit', 'arg1', 'arg2')); $expected = array(array('table' => 'posts', 'no-commit' => true), array('arg1', 'arg2')); $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')); } /**