diff --git a/src/Console/ConsoleInputSubcommand.php b/src/Console/ConsoleInputSubcommand.php index 68e9ad0ebc1..1572f8134c9 100644 --- a/src/Console/ConsoleInputSubcommand.php +++ b/src/Console/ConsoleInputSubcommand.php @@ -32,14 +32,14 @@ class ConsoleInputSubcommand * * @var string */ - protected $_name; + protected $_name = ''; /** * Help string for the subcommand * * @var string */ - protected $_help; + protected $_help = ''; /** * The ConsoleOptionParser for this subcommand. @@ -83,6 +83,16 @@ public function name() return $this->_name; } + /** + * Get the raw help string for this command + * + * @return string + */ + public function getRawHelp() + { + return $this->_help; + } + /** * Generate the help for this this subcommand. * diff --git a/src/Console/ConsoleOptionParser.php b/src/Console/ConsoleOptionParser.php index ea3387c71e1..5c7e44ad5bb 100644 --- a/src/Console/ConsoleOptionParser.php +++ b/src/Console/ConsoleOptionParser.php @@ -583,8 +583,6 @@ public function addSubcommand($name, array $options = []) ]; $options += $defaults; - $options = $this->_mergeSubcommandHelpToParserDescription($options); - $command = new ConsoleInputSubcommand($options); } $this->_subcommands[$name] = $command; @@ -593,33 +591,6 @@ 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'] instanceof self) { - if ($options['parser']->getDescription() !== null) { - return $options; - } - - $options['parser']->setDescription($options['help']); - - return $options; - } - - $options['parser'] = [ - 'description' => $options['help'] - ] + (array)$options['parser']; - - return $options; - } - /** * Remove a subcommand from the option parser. * @@ -750,12 +721,16 @@ public function parse($argv) */ public function help($subcommand = null, $format = 'text', $width = 72) { - if (isset($this->_subcommands[$subcommand]) && - $this->_subcommands[$subcommand]->parser() instanceof self - ) { - $subparser = $this->_subcommands[$subcommand]->parser(); - $subparser->setCommand($this->getCommand() . ' ' . $subparser->getCommand()); - + if (isset($this->_subcommands[$subcommand])) { + $command = $this->_subcommands[$subcommand]; + $subparser = $command->parser(); + if (!($subparser instanceof self)) { + $subparser = clone $this; + } + if (strlen($subparser->getDescription()) === 0) { + $subparser->setDescription($command->getRawHelp()); + } + $subparser->setCommand($this->getCommand() . ' ' . $subcommand); return $subparser->help(null, $format, $width); } diff --git a/tests/TestCase/Console/ConsoleOptionParserTest.php b/tests/TestCase/Console/ConsoleOptionParserTest.php index f00d08fd12c..14a77a96e6f 100644 --- a/tests/TestCase/Console/ConsoleOptionParserTest.php +++ b/tests/TestCase/Console/ConsoleOptionParserTest.php @@ -591,6 +591,30 @@ public function testSubcommand() $this->assertEquals($parser, $result, 'Adding a subcommand is not chainable'); } + /** + * Test addSubcommand inherits options as no + * parser is created. + * + * @return void + */ + public function testAddSubcommandInheritOptions() + { + $parser = new ConsoleOptionParser('test', false); + $parser->addSubcommand('build', [ + 'help' => 'Build things' + ])->addOption('connection', [ + 'short' => 'c', + 'default' => 'default' + ])->addArgument('name', ['required' => false]); + + $result = $parser->parse(['build']); + $this->assertEquals('default', $result[0]['connection']); + + $result = $parser->subcommands(); + $this->assertArrayHasKey('build', $result); + $this->assertFalse($result['build']->parser(), 'No parser should be created'); + } + /** * test addSubcommand with an object. *