From b7391274fb7c16224b84f2fd23a9d6298e0cdede Mon Sep 17 00:00:00 2001 From: Mark Story Date: Sat, 6 Aug 2011 15:21:42 -0400 Subject: [PATCH] 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. --- lib/Cake/Console/ConsoleInputOption.php | 5 +++++ lib/Cake/Console/ConsoleOptionParser.php | 12 +++++++++++- .../Test/Case/Console/ConsoleOptionParserTest.php | 12 ++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/Cake/Console/ConsoleInputOption.php b/lib/Cake/Console/ConsoleInputOption.php index da1c69b0ec1..67b7d61a888 100644 --- a/lib/Cake/Console/ConsoleInputOption.php +++ b/lib/Cake/Console/ConsoleInputOption.php @@ -89,6 +89,11 @@ public function __construct($name, $short = null, $help = '', $boolean = false, $this->_default = $default; $this->_choices = $choices; } + if (strlen($this->_short) > 1) { + throw new ConsoleException( + __d('cake_console', 'Short options must be one letter.') + ); + } } /** diff --git a/lib/Cake/Console/ConsoleOptionParser.php b/lib/Cake/Console/ConsoleOptionParser.php index 56a44197dc3..2ce5ed918e9 100644 --- a/lib/Cake/Console/ConsoleOptionParser.php +++ b/lib/Cake/Console/ConsoleOptionParser.php @@ -41,7 +41,8 @@ * * 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 @@ * * `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 diff --git a/lib/Cake/Test/Case/Console/ConsoleOptionParserTest.php b/lib/Cake/Test/Case/Console/ConsoleOptionParserTest.php index 1712b0d35f6..88bad6fb0df 100644 --- a/lib/Cake/Test/Case/Console/ConsoleOptionParserTest.php +++ b/lib/Cake/Test/Case/Console/ConsoleOptionParserTest.php @@ -139,6 +139,18 @@ public function testAddOptionShort() { $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. *