Skip to content

Commit 7f5b5c4

Browse files
committed
Adding exception for unknown option usage.
1 parent d3c95bc commit 7f5b5c4

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

cake/console/console_option_parser.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,9 @@ protected function _parseShortOption($option, $params) {
420420
* @return array Params with $option added in.
421421
*/
422422
protected function _parseOptionName($name, $params) {
423+
if (!isset($this->_options[$name])) {
424+
throw new InvalidArgumentException(sprintf(__('Unknown option `%s`'), $name));
425+
}
423426
$definition = $this->_options[$name];
424427
$nextValue = $this->_nextToken();
425428
if (!$definition['boolean'] && !empty($nextValue) && $nextValue{0} != '-') {

cake/tests/cases/console/console_option_parser.test.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,22 @@ function testOptionWithBooleanParam() {
159159
$parser = new ConsoleOptionParser();
160160
$parser->addOption('no-commit', array('boolean' => true))
161161
->addOption('table', array('short' => 't'));
162-
162+
163163
$result = $parser->parse(array('--table', 'posts', '--no-commit', 'arg1', 'arg2'));
164164
$expected = array(array('table' => 'posts', 'no-commit' => true), array('arg1', 'arg2'));
165165
$this->assertEquals($expected, $result, 'Boolean option did not parse correctly.');
166-
166+
}
167+
168+
/**
169+
* test parsing options that do not exist.
170+
*
171+
* @expectedException InvalidArgumentException
172+
*/
173+
function testOptionThatDoesNotExist() {
174+
$parser = new ConsoleOptionParser();
175+
$parser->addOption('no-commit', array('boolean' => true));
176+
177+
$result = $parser->parse(array('--fail', 'other'));
167178
}
168179

169180
/**

0 commit comments

Comments
 (0)