Skip to content
Permalink
Browse files

Adding choices to usage and generated help text.

  • Loading branch information...
markstory committed Oct 10, 2010
1 parent b398877 commit ef027f6d0e1d34d05cacbeeb6bb8c2a63f7acb98
@@ -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);
}
@@ -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 . ']';
}
@@ -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;
}
@@ -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);
}
@@ -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');
}

0 comments on commit ef027f6

Please sign in to comment.
You can’t perform that action at this time.