Adding addSubcommands, and support for subcommands to buildFromArray.

This makes the API consistent across all the elements of ConsoleOptionParser.
This commit is contained in:
mark_story 2010-10-11 13:12:23 -04:00
parent 577603fbd9
commit 0fd8d2b8e4
2 changed files with 41 additions and 0 deletions

View file

@ -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.
*

View file

@ -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));
}
/**