Adding support for help generating help for subcommands.

Adding tests for subcommand help generation.
This commit is contained in:
mark_story 2010-10-10 01:54:51 -04:00
parent 3be24d0b0b
commit ab794b300e
2 changed files with 55 additions and 2 deletions

View file

@ -133,6 +133,20 @@ class ConsoleOptionParser {
}
}
/**
* Get or set the command name for shell/task
*
* @param string $text The text to set, or null if you want to read
* @return mixed If reading, the value of the command. If setting $this will be returned
*/
public function command($text = null) {
if ($text !== null) {
$this->_command = $text;
return $this;
}
return $this->_command;
}
/**
* Get or set the description text for shell/task
*
@ -301,9 +315,19 @@ class ConsoleOptionParser {
* Generates help text based on the description, options, arguments and epilog
* in the parser.
*
* @param string $subcommand If present and a valid subcommand that has a linked parser.
* That subcommands help will be shown instead.
* @return string
*/
public function help() {
public function help($subcommand = null) {
if (
isset($this->_subcommands[$subcommand]) &&
$this->_subcommands[$subcommand]['parser'] instanceof self
) {
$subparser = $this->_subcommands[$subcommand]['parser'];
$subparser->command($this->command() . ' ' . $subparser->command());
return $subparser->help();
}
$out = array();
if (!empty($this->_description)) {
$out[] = $this->_description;

View file

@ -363,6 +363,35 @@ method This is another command
TEXT;
$this->assertEquals($expected, $result, 'Help is not correct.');
}
/**
* test that help() with a command param shows the help for a subcommand
*
* @return void
*/
function testHelpSubcommandHelp() {
$subParser = new ConsoleOptionParser('method', false);
$subParser->addOption('connection', array('help' => 'Db connection.'));
$parser = new ConsoleOptionParser('mycommand', false);
$parser->addSubcommand('method', array(
'help' => 'This is another command',
'parser' => $subParser
))
->addOption('test', array('help' => 'A test option.'));
$result = $parser->help('method');
$expected = <<<TEXT
<info>Usage:</info>
cake mycommand method [-h] [--connection]
<info>Options:</info>
--help, -h Display this help.
--connection Db connection.
TEXT;
$this->assertEquals($expected, $result, 'Help is not correct.');
}
}