Adding choices to usage and generated help text.

This commit is contained in:
mark_story 2010-10-10 16:07:41 -04:00
parent b398877887
commit ef027f6d0e
3 changed files with 46 additions and 0 deletions

View file

@ -74,6 +74,9 @@ class ConsoleInputArgument {
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);
}
@ -84,6 +87,9 @@ class ConsoleInputArgument {
*/
public function usage() {
$name = $this->_name;
if (!empty($this->_choices)) {
$name = implode('|', $this->_choices);
}
if (!$this->isRequired()) {
$name = '[' . $name . ']';
}

View file

@ -75,6 +75,9 @@ class ConsoleInputOption {
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;
}
@ -96,6 +99,9 @@ class ConsoleInputOption {
if (!empty($this->_default) && $this->_default !== true) {
$default = ' ' . $this->_default;
}
if (!empty($this->_choices)) {
$default = ' ' . implode('|', $this->_choices);
}
return sprintf('[%s%s]', $name, $default);
}

View file

@ -368,6 +368,40 @@ cake mycommand [-h] [--test] model [other_longer]
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');
}