Skip to content

Commit

Permalink
Adding an exception when a short option is longer than one
Browse files Browse the repository at this point in the history
character. Having long short options breaks parsing, and
doesn't make sense with how short options are used.
  • Loading branch information
markstory committed Aug 6, 2011
1 parent 10c78c5 commit b739127
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
5 changes: 5 additions & 0 deletions lib/Cake/Console/ConsoleInputOption.php
Expand Up @@ -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.')
);
}
}

/**
Expand Down
12 changes: 11 additions & 1 deletion lib/Cake/Console/ConsoleOptionParser.php
Expand Up @@ -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.
Expand All @@ -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
Expand Down
12 changes: 12 additions & 0 deletions lib/Cake/Test/Case/Console/ConsoleOptionParserTest.php
Expand Up @@ -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.
*
Expand Down

0 comments on commit b739127

Please sign in to comment.