diff --git a/src/Console/ConsoleOptionParser.php b/src/Console/ConsoleOptionParser.php index 454f4be8870..f83f5a979ae 100644 --- a/src/Console/ConsoleOptionParser.php +++ b/src/Console/ConsoleOptionParser.php @@ -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; @@ -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. * diff --git a/tests/TestCase/Console/ConsoleOptionParserTest.php b/tests/TestCase/Console/ConsoleOptionParserTest.php index e076b279b20..9df72231207 100644 --- a/tests/TestCase/Console/ConsoleOptionParserTest.php +++ b/tests/TestCase/Console/ConsoleOptionParserTest.php @@ -603,6 +603,8 @@ public function testHelpSubcommandHelp() $result = $parser->help('method'); $expected = <<Usage: cake mycommand method [--connection] [-h] [-0] @@ -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 = <<Usage: +cake mycommand method [-f] [-h] [-q] [-v] + +Options: + +--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.'); }