Adding an exception when a short option is longer than one

character. Having long short options breaks parsing, and
doesn't make sense with how short options are used.
This commit is contained in:
Mark Story 2011-08-06 15:21:42 -04:00
parent 10c78c5420
commit b7391274fb
3 changed files with 28 additions and 1 deletions

View file

@ -89,6 +89,11 @@ class ConsoleInputOption {
$this->_default = $default;
$this->_choices = $choices;
}
if (strlen($this->_short) > 1) {
throw new ConsoleException(
__d('cake_console', 'Short options must be one letter.')
);
}
}
/**

View file

@ -41,7 +41,8 @@ App::uses('HelpFormatter', 'Console');
*
* Options can be defined with both long and short forms. By using `$parser->addOption()`
* you can define new options. The name of the option is used as its long form, and you
* can supply an additional short form, with the `short` option.
* can supply an additional short form, with the `short` option. Short options should
* only be one letter long. Using more than one letter for a short option will raise an exception.
*
* Calling options can be done using syntax similar to most *nix command line tools. Long options
* cane either include an `=` or leave it out.
@ -52,6 +53,15 @@ App::uses('HelpFormatter', 'Console');
*
* `cake myshell command -cn`
*
* Short options can be combined into groups as seen above. Each letter in a group
* will be treated as a separate option. The previous example is equivalent to:
*
* `cake myshell command -c -n`
*
* Short options can also accept values:
*
* `cake myshell command -c default`
*
* ### Positional arguments
*
* If no positional arguments are defined, all of them will be parsed. If you define positional

View file

@ -139,6 +139,18 @@ class ConsoleOptionParserTest extends CakeTestCase {
$this->assertEquals(array('test' => 'value', 'help' => false), $result[0], 'Short parameter did not parse out');
}
/**
* Test that adding an option using a two letter short value causes an exception.
* As they will not parse correctly.
*
* @expectedException ConsoleException
* @return void
*/
public function testAddOptionShortOneLetter() {
$parser = new ConsoleOptionParser('test', false);
$parser->addOption('test', array('short' => 'te'));
}
/**
* test adding and using boolean options.
*