Skip to content

Commit

Permalink
Adding choices to usage and generated help text.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Oct 14, 2010
1 parent b398877 commit ef027f6
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
6 changes: 6 additions & 0 deletions cake/console/console_input_argument.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ public function help($width = 0) {
if (!$this->isRequired()) {
$optional = ' <comment>(optional)</comment>';
}
if (!empty($this->_choices)) {
$optional .= sprintf(' <comment>(choices: %s)</comment>', implode('|', $this->_choices));
}
return sprintf('%s%s%s', $name, $this->_help, $optional);
}

Expand All @@ -84,6 +87,9 @@ public function help($width = 0) {
*/
public function usage() {
$name = $this->_name;
if (!empty($this->_choices)) {
$name = implode('|', $this->_choices);
}
if (!$this->isRequired()) {
$name = '[' . $name . ']';
}
Expand Down
6 changes: 6 additions & 0 deletions cake/console/console_input_option.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ public function help($width = 0) {
if (!empty($this->_default) && $this->_default !== true) {
$default = sprintf(__(' <comment>(default: %s)</comment>'), $this->_default);
}
if (!empty($this->_choices)) {
$default .= sprintf(' <comment>(choices: %s)</comment>', implode('|', $this->_choices));
}
if (!empty($this->_short)) {
$short = ', -' . $this->_short;
}
Expand All @@ -96,6 +99,9 @@ public function usage() {
if (!empty($this->_default) && $this->_default !== true) {
$default = ' ' . $this->_default;
}
if (!empty($this->_choices)) {
$default = ' ' . implode('|', $this->_choices);
}
return sprintf('[%s%s]', $name, $default);
}

Expand Down
34 changes: 34 additions & 0 deletions cake/tests/cases/console/console_option_parser.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,40 @@ function testHelpWithOptionsAndArguments() {
model The model to make.
other_longer Another argument. <comment>(optional)</comment>
TEXT;
$this->assertEquals($expected, $result, 'Help does not match');
}

/**
* test help() with options and arguments that have choices.
*
* @return void
*/
function testHelpWithChoices() {
$parser = new ConsoleOptionParser('mycommand', false);
$parser->addOption('test', array('help' => 'A test option.', 'choices' => array('one', 'two')))
->addArgument('type', array(
'help' => 'Resource type.',
'choices' => array('aco', 'aro'),
'required' => true
))
->addArgument('other_longer', array('help' => 'Another argument.'));

$result = $parser->help();
$expected = <<<TEXT
<info>Usage:</info>
cake mycommand [-h] [--test one|two] aco|aro [other_longer]
<info>Options:</info>
--help, -h Display this help.
--test A test option. <comment>(choices: one|two)</comment>
<info>Arguments:</info>
type Resource type. <comment>(choices: aco|aro)</comment>
other_longer Another argument. <comment>(optional)</comment>
TEXT;
$this->assertEquals($expected, $result, 'Help does not match');
}
Expand Down

0 comments on commit ef027f6

Please sign in to comment.