Skip to content

Commit

Permalink
Adding addSubcommands, and support for subcommands to buildFromArray.
Browse files Browse the repository at this point in the history
This makes the API consistent across all the elements of ConsoleOptionParser.
  • Loading branch information
markstory committed Oct 14, 2010
1 parent 577603f commit 0fd8d2b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
19 changes: 19 additions & 0 deletions cake/console/console_option_parser.php
Expand Up @@ -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.
* )
* );
* }}}
Expand All @@ -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']);
}
Expand Down Expand Up @@ -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.
*
Expand Down
22 changes: 22 additions & 0 deletions cake/tests/cases/console/console_option_parser.test.php
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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'
);
Expand All @@ -537,6 +556,9 @@ function testBuildFromArray() {

$args = $parser->arguments();
$this->assertEquals(2, count($args));

$commands = $parser->subcommands();
$this->assertEquals(1, count($commands));
}

/**
Expand Down

0 comments on commit 0fd8d2b

Please sign in to comment.