Skip to content

Commit

Permalink
Merge in defaults from subcommand parser for the parser help.
Browse files Browse the repository at this point in the history
  • Loading branch information
dereuromark committed Jan 10, 2017
1 parent 3249ea6 commit e0a4b9a
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/Console/ConsoleOptionParser.php
Expand Up @@ -580,6 +580,9 @@ public function addSubcommand($name, array $options = [])
'parser' => null
];
$options += $defaults;

$options = $this->_mergeSubcommandHelpToParserDescription($options);

$command = new ConsoleInputSubcommand($options);
}
$this->_subcommands[$name] = $command;
Expand All @@ -588,6 +591,39 @@ public function addSubcommand($name, array $options = [])
return $this;
}

/**
* @param array $options Options
* @return array
*/
protected function _mergeSubcommandHelpToParserDescription($options)
{
if (!$options['help']) {
return $options;
}

if ($options['parser'] && is_object($options['parser'])) {
if ($options['parser']->getDescription() !== null) {
return $options;
}

$options['parser']->setDescription($options['help']);

return $options;
}

if ($options['parser'] && is_array($options['parser'])) {
if (isset($options['parser']['description'])) {
return $options;
}
}

$options['parser'] = [
'description' => $options['help']
] + (array)$options['parser'];

return $options;
}

/**
* Remove a subcommand from the option parser.
*
Expand Down
44 changes: 44 additions & 0 deletions tests/TestCase/Console/ConsoleOptionParserTest.php
Expand Up @@ -603,6 +603,8 @@ public function testHelpSubcommandHelp()

$result = $parser->help('method');
$expected = <<<TEXT
This is another command
<info>Usage:</info>
cake mycommand method [--connection] [-h] [-0]
Expand All @@ -612,6 +614,48 @@ public function testHelpSubcommandHelp()
--help, -h Display this help.
--zero, -0 Zero.
TEXT;
$this->assertTextEquals($expected, $result, 'Help is not correct.');
}

/**
* test that help() with a command param shows the help for a subcommand
*
* @return void
*/
public function testHelpSubcommandHelpArray()
{
$subParser = [
'options' => [
'foo' => [
'short' => 'f',
'help' => 'Foo.',
'boolean' => true,
]
],
];

$parser = new ConsoleOptionParser('mycommand', false);
$parser->addSubcommand('method', [
'help' => 'This is a subcommand',
'parser' => $subParser
])
->addOption('test', ['help' => 'A test option.']);

$result = $parser->help('method');
$expected = <<<TEXT
This is a subcommand
<info>Usage:</info>
cake mycommand method [-f] [-h] [-q] [-v]
<info>Options:</info>
--foo, -f Foo.
--help, -h Display this help.
--quiet, -q Enable quiet output.
--verbose, -v Enable verbose output.
TEXT;
$this->assertTextEquals($expected, $result, 'Help is not correct.');
}
Expand Down

0 comments on commit e0a4b9a

Please sign in to comment.