From 0fd8d2b8e407496be24c46eb6c28c9fc929bf7b7 Mon Sep 17 00:00:00 2001 From: mark_story Date: Mon, 11 Oct 2010 13:12:23 -0400 Subject: [PATCH] Adding addSubcommands, and support for subcommands to buildFromArray. This makes the API consistent across all the elements of ConsoleOptionParser. --- cake/console/console_option_parser.php | 19 ++++++++++++++++ .../console/console_option_parser.test.php | 22 +++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/cake/console/console_option_parser.php b/cake/console/console_option_parser.php index c500d79ec..80685d9f1 100644 --- a/cake/console/console_option_parser.php +++ b/cake/console/console_option_parser.php @@ -161,6 +161,9 @@ class ConsoleOptionParser { * ), * 'options' => array( * // list of options compatible with addOptions + * ), + * 'subcommands' => array( + * // list of subcommands to add. * ) * ); * }}} @@ -176,6 +179,9 @@ class ConsoleOptionParser { 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 @@ class ConsoleOptionParser { 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 b7e6c066b..f80092b4e 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 @@ class ConsoleOptionParserTest extends CakeTestCase { $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 @@ TEXT; '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 @@ TEXT; $args = $parser->arguments(); $this->assertEquals(2, count($args)); + + $commands = $parser->subcommands(); + $this->assertEquals(1, count($commands)); } /**