Skip to content

Commit

Permalink
Adding the ability for parse() to use a subcommand parser.
Browse files Browse the repository at this point in the history
Adding text on how to get help on subcommands.
  • Loading branch information
markstory committed Oct 14, 2010
1 parent 1cf2613 commit 970a6c8
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
17 changes: 14 additions & 3 deletions cake/console/console_option_parser.php
Expand Up @@ -372,14 +372,20 @@ public function subcommands() {
}

/**
* Parse the argv array into a set of params and args.
* Parse the argv array into a set of params and args. If $command is not null
* and $command is equal to a subcommand that has a parser, that parser will be used
* to parse the $argv
*
* @param array $argv Array of args (argv) to parse
* @param array $argv Array of args (argv) to parse.
* @param string $command The subcommand to use for parsing.
* @return Array array($params, $args)
* @throws InvalidArgumentException When an invalid parameter is encountered.
* RuntimeException when required arguments are not supplied.
*/
public function parse($argv) {
public function parse($argv, $command = null) {
if (isset($this->_subcommands[$command]) && $this->_subcommands[$command]->parser()) {
return $this->_subcommands[$command]->parser()->parse($argv);
}
$params = $args = array();
$this->_tokens = $argv;
while ($token = array_shift($this->_tokens)) {
Expand Down Expand Up @@ -435,6 +441,11 @@ public function help($subcommand = null) {
$out[] = $command->help($max);
}
$out[] = '';
$out[] = sprintf(
__('To see help on a subcommand use <info>`cake %s [subcommand] --help`</info>'),
$this->command()
);
$out[] = '';
}

if (!empty($this->_options)) {
Expand Down
30 changes: 30 additions & 0 deletions cake/tests/cases/console/console_option_parser.test.php
Expand Up @@ -464,6 +464,8 @@ function testHelpSubcommand() {
method This is another command
To see help on a subcommand use <info>`cake mycommand [subcommand] --help`</info>
<info>Options:</info>
--help, -h Display this help.
Expand Down Expand Up @@ -534,4 +536,32 @@ function testBuildFromArray() {
$args = $parser->arguments();
$this->assertEquals(2, count($args));
}

/**
* test that parse() takes a subcommand argument, and that the subcommand parser
* is used.
*
* @return void
*/
function testParsingWithSubParser() {
$parser = new ConsoleOptionParser();
$parser->addOption('primary')
->addArgument('one', array('required' => true, 'choices' => array('a', 'b')))
->addArgument('two', array('required' => true))
->addSubcommand('sub', array(
'parser' => array(
'options' => array(
'secondary' => array('boolean' => true),
'fourth' => array('help' => 'fourth option')
),
'arguments' => array(
'sub_arg' => array('choices' => array('c', 'd'))
)
)
));

$result = $parser->parse(array('--secondary', '--fourth', '4', 'c'), 'sub');
$expected = array(array('secondary' => true, 'fourth' => '4'), array('c'));
$this->assertEquals($expected, $result, 'Sub parser did not parse request.');
}
}

0 comments on commit 970a6c8

Please sign in to comment.