diff --git a/cake/console/console_option_parser.php b/cake/console/console_option_parser.php index c500d79ecf1..80685d9f185 100644 --- a/cake/console/console_option_parser.php +++ b/cake/console/console_option_parser.php @@ -161,6 +161,9 @@ public static function create($command, $defaultOptions = true) { * ), * 'options' => array( * // list of options compatible with addOptions + * ), + * 'subcommands' => array( + * // list of subcommands to add. * ) * ); * }}} @@ -176,6 +179,9 @@ public static function buildFromArray($spec) { if (!empty($spec['options'])) { $parser->addOptions($spec['options']); } + if (!empty($spec['subcommands'])) { + $parser->addSubcommands($spec['subcommands']); + } if (!empty($spec['description'])) { $parser->description($spec['description']); } @@ -362,6 +368,19 @@ public function addSubcommand($name, $params = array()) { return $this; } +/** + * Add multiple subcommands at once. + * + * @param array $commands Array of subcommands. + * @return $this + */ + public function addSubcommands(array $commands) { + foreach ($commands as $name => $params) { + $this->addSubcommand($name, $params); + } + return $this; + } + /** * Gets the arguments defined in the parser. * diff --git a/cake/tests/cases/console/console_option_parser.test.php b/cake/tests/cases/console/console_option_parser.test.php index b7e6c066b40..f80092b4e24 100644 --- a/cake/tests/cases/console/console_option_parser.test.php +++ b/cake/tests/cases/console/console_option_parser.test.php @@ -323,6 +323,22 @@ function testSubcommand() { $this->assertEquals($parser, $result, 'Adding a subcommand is not chainable'); } +/** + * test adding multiple subcommands + * + * @return void + */ + function testAddSubcommands() { + $parser = new ConsoleOptionParser(); + $result = $parser->addSubcommands(array( + 'initdb' => array('help' => 'Initialize the database'), + 'create' => array('help' => 'Create something') + )); + $this->assertEquals($parser, $result, 'Adding a subcommands is not chainable'); + $result = $parser->subcommands(); + $this->assertEquals(2, count($result), 'Not enough subcommands'); + } + /** * test getting help with defined options. * @@ -523,6 +539,9 @@ function testBuildFromArray() { 'name' => array('help' => 'The name'), 'other' => array('help' => 'The other arg') ), + 'subcommands' => array( + 'initdb' => array('help' => 'make database') + ), 'description' => 'description text', 'epilog' => 'epilog text' ); @@ -537,6 +556,9 @@ function testBuildFromArray() { $args = $parser->arguments(); $this->assertEquals(2, count($args)); + + $commands = $parser->subcommands(); + $this->assertEquals(1, count($commands)); } /**