diff --git a/cake/console/console_option_parser.php b/cake/console/console_option_parser.php index 632969f539f..6719f812754 100644 --- a/cake/console/console_option_parser.php +++ b/cake/console/console_option_parser.php @@ -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)) { @@ -435,6 +441,11 @@ public function help($subcommand = null) { $out[] = $command->help($max); } $out[] = ''; + $out[] = sprintf( + __('To see help on a subcommand use `cake %s [subcommand] --help`'), + $this->command() + ); + $out[] = ''; } if (!empty($this->_options)) { diff --git a/cake/tests/cases/console/console_option_parser.test.php b/cake/tests/cases/console/console_option_parser.test.php index b89f888b66e..7751a8d018d 100644 --- a/cake/tests/cases/console/console_option_parser.test.php +++ b/cake/tests/cases/console/console_option_parser.test.php @@ -464,6 +464,8 @@ function testHelpSubcommand() { method This is another command +To see help on a subcommand use `cake mycommand [subcommand] --help` + Options: --help, -h Display this help. @@ -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.'); + } } \ No newline at end of file