diff --git a/src/Console/ConsoleOptionParser.php b/src/Console/ConsoleOptionParser.php index 24e4a77057b..c767dd27c68 100644 --- a/src/Console/ConsoleOptionParser.php +++ b/src/Console/ConsoleOptionParser.php @@ -120,6 +120,13 @@ class ConsoleOptionParser */ protected $_subcommands = []; + /** + * Subcommand sorting option + * + * @var bool + */ + protected $_subcommandSort = true; + /** * Command name. * @@ -407,6 +414,32 @@ public function epilog($text = null) return $this->getEpilog(); } + /** + * Gets the subcommandSort Property + * + * @return bool + */ + public function getSubcommandSort() + { + return $this->_subcommandSort; + } + + /** + * Sets the subcommandSort property. + * + * @param bool $sort Whether or not the ConsoleOptionParser should sort subcommands + * + * @return $this + */ + public function setSubcommandSort($sort) + { + if ($sort !== true) { + $this->_subcommandSort = false; + } + + return $this; + } + /** * Add an option to the option parser. Options allow you to define optional or required * parameters for your console application. Options are defined by the parameters they use. @@ -576,10 +609,9 @@ public function addOptions(array $options) * * @param \Cake\Console\ConsoleInputSubcommand|string $name Name of the subcommand. Will also accept an instance of ConsoleInputSubcommand * @param array $options Array of params, see above. - * @param boolean $sort Optional sorting of the subcommand * @return $this */ - public function addSubcommand($name, array $options = [], $sort = true) + public function addSubcommand($name, array $options = []) { if ($name instanceof ConsoleInputSubcommand) { $command = $name; @@ -596,7 +628,7 @@ public function addSubcommand($name, array $options = [], $sort = true) $command = new ConsoleInputSubcommand($options); } $this->_subcommands[$name] = $command; - if ($sort) { + if ($this->_subcommandSort) { asort($this->_subcommands); } diff --git a/tests/TestCase/Console/ConsoleOptionParserTest.php b/tests/TestCase/Console/ConsoleOptionParserTest.php index ecd8a400f2b..a2a922a5f25 100644 --- a/tests/TestCase/Console/ConsoleOptionParserTest.php +++ b/tests/TestCase/Console/ConsoleOptionParserTest.php @@ -657,8 +657,11 @@ public function testAddSubcommandObject() public function testAddSubcommandSort() { $parser = new ConsoleOptionParser('test', false); - $parser->addSubcommand(new ConsoleInputSubcommand('betaTest'), [], false); - $parser->addSubcommand(new ConsoleInputSubcommand('alphaTest'), [], false); + $this->assertEquals(true, $parser->getSubcommandSort()); + $parser->setSubcommandSort(false); + $this->assertEquals(false, $parser->getSubcommandSort()); + $parser->addSubcommand(new ConsoleInputSubcommand('betaTest'), []); + $parser->addSubcommand(new ConsoleInputSubcommand('alphaTest'), []); $result = $parser->subcommands(); $this->assertCount(2, $result); $firstResult = key($result);