Browse Source

Merge pull request #16927 from cakephp/5.x-subcommand

remove sub command
saeideng 3 years ago
parent
commit
c0a03e06da

+ 1 - 7
src/Console/BaseCommand.php

@@ -18,7 +18,6 @@ namespace Cake\Console;
 
 use Cake\Console\Exception\ConsoleException;
 use Cake\Console\Exception\StopException;
-use Cake\Core\Exception\CakeException;
 use Cake\Utility\Inflector;
 
 /**
@@ -119,11 +118,6 @@ abstract class BaseCommand implements CommandInterface
         $parser->setDescription(static::getDescription());
 
         $parser = $this->buildOptionParser($parser);
-        if ($parser->subcommands()) {
-            throw new CakeException(
-                'You cannot add sub-commands to `Command` sub-classes. Instead make a separate command.'
-            );
-        }
 
         return $parser;
     }
@@ -204,7 +198,7 @@ abstract class BaseCommand implements CommandInterface
             $io->setOutputAs(ConsoleOutput::RAW);
         }
 
-        $io->out($parser->help(null, $format));
+        $io->out($parser->help($format));
     }
 
     /**

+ 0 - 144
src/Console/ConsoleInputSubcommand.php

@@ -1,144 +0,0 @@
-<?php
-declare(strict_types=1);
-
-/**
- * ConsoleInputSubcommand file
- *
- * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice.
- *
- * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
- * @link          https://cakephp.org CakePHP(tm) Project
- * @since         2.0.0
- * @license       https://opensource.org/licenses/mit-license.php MIT License
- */
-namespace Cake\Console;
-
-use InvalidArgumentException;
-use SimpleXMLElement;
-
-/**
- * An object to represent a single subcommand used in the command line.
- * Created when you call ConsoleOptionParser::addSubcommand()
- *
- * @see \Cake\Console\ConsoleOptionParser::addSubcommand()
- */
-class ConsoleInputSubcommand
-{
-    /**
-     * Name of the subcommand
-     *
-     * @var string
-     */
-    protected string $_name = '';
-
-    /**
-     * Help string for the subcommand
-     *
-     * @var string
-     */
-    protected string $_help = '';
-
-    /**
-     * The ConsoleOptionParser for this subcommand.
-     *
-     * @var \Cake\Console\ConsoleOptionParser|null
-     */
-    protected ?ConsoleOptionParser $_parser = null;
-
-    /**
-     * Make a new Subcommand
-     *
-     * @param array<string, mixed>|string $name The long name of the subcommand, or an array with all the properties.
-     * @param string $help The help text for this option.
-     * @param \Cake\Console\ConsoleOptionParser|array<string, mixed>|null $parser A parser for this subcommand.
-     *   Either a ConsoleOptionParser, or an array that can be used with ConsoleOptionParser::buildFromArray().
-     */
-    public function __construct(array|string $name, string $help = '', ConsoleOptionParser|array|null $parser = null)
-    {
-        if (is_array($name)) {
-            $data = $name + ['name' => null, 'help' => '', 'parser' => null];
-            if (empty($data['name'])) {
-                throw new InvalidArgumentException('"name" not provided for console option parser');
-            }
-
-            $name = $data['name'];
-            $help = $data['help'];
-            $parser = $data['parser'];
-        }
-
-        if (is_array($parser)) {
-            $parser['command'] = $name;
-            $parser = ConsoleOptionParser::buildFromArray($parser);
-        }
-
-        $this->_name = $name;
-        $this->_help = $help;
-        $this->_parser = $parser;
-    }
-
-    /**
-     * Get the value of the name attribute.
-     *
-     * @return string Value of this->_name.
-     */
-    public function name(): string
-    {
-        return $this->_name;
-    }
-
-    /**
-     * Get the raw help string for this command
-     *
-     * @return string
-     */
-    public function getRawHelp(): string
-    {
-        return $this->_help;
-    }
-
-    /**
-     * Generate the help for this this subcommand.
-     *
-     * @param int $width The width to make the name of the subcommand.
-     * @return string
-     */
-    public function help(int $width = 0): string
-    {
-        $name = $this->_name;
-        if (strlen($name) < $width) {
-            $name = str_pad($name, $width, ' ');
-        }
-
-        return $name . $this->_help;
-    }
-
-    /**
-     * Get the usage value for this option
-     *
-     * @return \Cake\Console\ConsoleOptionParser|null
-     */
-    public function parser(): ?ConsoleOptionParser
-    {
-        return $this->_parser;
-    }
-
-    /**
-     * Append this subcommand to the Parent element
-     *
-     * @param \SimpleXMLElement $parent The parent element.
-     * @return \SimpleXMLElement The parent with this subcommand appended.
-     */
-    public function xml(SimpleXMLElement $parent): SimpleXMLElement
-    {
-        $command = $parent->addChild('command');
-        $command->addAttribute('name', $this->_name);
-        $command->addAttribute('help', $this->_help);
-
-        return $parent;
-    }
-}

+ 11 - 192
src/Console/ConsoleOptionParser.php

@@ -115,21 +115,6 @@ class ConsoleOptionParser
     protected array $_args = [];
 
     /**
-     * Subcommands for this Command.
-     *
-     * @see \Cake\Console\ConsoleOptionParser::addSubcommand()
-     * @var array<string, \Cake\Console\ConsoleInputSubcommand>
-     */
-    protected array $_subcommands = [];
-
-    /**
-     * Subcommand sorting option
-     *
-     * @var bool
-     */
-    protected bool $_subcommandSort = true;
-
-    /**
      * Command name.
      *
      * @var string
@@ -205,9 +190,6 @@ class ConsoleOptionParser
      *      ],
      *      'options' => [
      *          // list of options compatible with addOptions
-     *      ],
-     *      'subcommands' => [
-     *          // list of subcommands to add.
      *      ]
      * ];
      * ```
@@ -225,9 +207,6 @@ class ConsoleOptionParser
         if (!empty($spec['options'])) {
             $parser->addOptions($spec['options']);
         }
-        if (!empty($spec['subcommands'])) {
-            $parser->addSubcommands($spec['subcommands']);
-        }
         if (!empty($spec['description'])) {
             $parser->setDescription($spec['description']);
         }
@@ -249,7 +228,6 @@ class ConsoleOptionParser
             'command' => $this->_command,
             'arguments' => $this->_args,
             'options' => $this->_options,
-            'subcommands' => $this->_subcommands,
             'description' => $this->_description,
             'epilog' => $this->_epilog,
         ];
@@ -272,9 +250,6 @@ class ConsoleOptionParser
         if (!empty($spec['options'])) {
             $this->addOptions($spec['options']);
         }
-        if (!empty($spec['subcommands'])) {
-            $this->addSubcommands($spec['subcommands']);
-        }
         if (!empty($spec['description'])) {
             $this->setDescription($spec['description']);
         }
@@ -364,29 +339,6 @@ class ConsoleOptionParser
     }
 
     /**
-     * Enables sorting of subcommands
-     *
-     * @param bool $value Whether to sort subcommands
-     * @return $this
-     */
-    public function enableSubcommandSort(bool $value = true)
-    {
-        $this->_subcommandSort = $value;
-
-        return $this;
-    }
-
-    /**
-     * Checks whether sorting is enabled for subcommands.
-     *
-     * @return bool
-     */
-    public function isSubcommandSortEnabled(): bool
-    {
-        return $this->_subcommandSort;
-    }
-
-    /**
      * 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.
      *
@@ -555,78 +507,6 @@ class ConsoleOptionParser
     }
 
     /**
-     * Append a subcommand to the subcommand list.
-     * Subcommands are usually methods on your Shell, but can also be used to document Tasks.
-     *
-     * ### Options
-     *
-     * - `help` - Help text for the subcommand.
-     * - `parser` - A ConsoleOptionParser for the subcommand. This allows you to create method
-     *    specific option parsers. When help is generated for a subcommand, if a parser is present
-     *    it will be used.
-     *
-     * @param \Cake\Console\ConsoleInputSubcommand|string $name Name of the subcommand.
-     *   Will also accept an instance of ConsoleInputSubcommand.
-     * @param array<string, mixed> $options Array of params, see above.
-     * @return $this
-     */
-    public function addSubcommand(ConsoleInputSubcommand|string $name, array $options = [])
-    {
-        if ($name instanceof ConsoleInputSubcommand) {
-            $command = $name;
-            $name = $command->name();
-        } else {
-            $name = Inflector::underscore($name);
-            $defaults = [
-                'name' => $name,
-                'help' => '',
-                'parser' => null,
-            ];
-            $options += $defaults;
-
-            $command = new ConsoleInputSubcommand($options);
-        }
-        $this->_subcommands[$name] = $command;
-        if ($this->_subcommandSort) {
-            asort($this->_subcommands);
-        }
-
-        return $this;
-    }
-
-    /**
-     * Remove a subcommand from the option parser.
-     *
-     * @param string $name The subcommand name to remove.
-     * @return $this
-     */
-    public function removeSubcommand(string $name)
-    {
-        unset($this->_subcommands[$name]);
-
-        return $this;
-    }
-
-    /**
-     * Add multiple subcommands at once.
-     *
-     * @param array<string, mixed> $commands Array of subcommands.
-     * @return $this
-     */
-    public function addSubcommands(array $commands)
-    {
-        foreach ($commands as $name => $params) {
-            if ($params instanceof ConsoleInputSubcommand) {
-                $name = $params;
-                $params = [];
-            }
-            $this->addSubcommand($name, $params);
-        }
-
-        return $this;
-    }
-
-    /**
      * Gets the arguments defined in the parser.
      *
      * @return array<\Cake\Console\ConsoleInputArgument>
@@ -662,19 +542,7 @@ class ConsoleOptionParser
     }
 
     /**
-     * Get the array of defined subcommands
-     *
-     * @return array<string, \Cake\Console\ConsoleInputSubcommand>
-     */
-    public function subcommands(): array
-    {
-        return $this->_subcommands;
-    }
-
-    /**
-     * Parse the argv array into a set of params and args. If $command is not null
-     * and $command is equal to a subcommand that has a parser, that parser will be used
-     * to parse the $argv
+     * Parse the argv array into a set of params and args.
      *
      * @param array $argv Array of args (argv) to parse.
      * @param \Cake\Console\ConsoleIo|null $io A ConsoleIo instance or null. If null prompt options will error.
@@ -683,15 +551,6 @@ class ConsoleOptionParser
      */
     public function parse(array $argv, ?ConsoleIo $io = null): array
     {
-        $command = isset($argv[0]) ? Inflector::underscore($argv[0]) : null;
-        if (isset($this->_subcommands[$command])) {
-            array_shift($argv);
-        }
-        if (isset($this->_subcommands[$command]) && $this->_subcommands[$command]->parser()) {
-            /** @psalm-suppress PossiblyNullReference */
-            return $this->_subcommands[$command]->parser()->parse($argv, $io);
-        }
-
         $params = $args = [];
         $this->_tokens = $argv;
 
@@ -708,9 +567,6 @@ class ConsoleOptionParser
                 continue;
             }
 
-            if (isset($this->_subcommands[$token])) {
-                continue;
-            }
             if (str_starts_with($token, '--')) {
                 $params = $this->_parseLongOption($token, $params);
             } elseif (str_starts_with($token, '-')) {
@@ -772,64 +628,27 @@ class ConsoleOptionParser
     /**
      * Gets formatted help for this parser object.
      *
-     * Generates help text based on the description, options, arguments, subcommands and epilog
+     * Generates help text based on the description, options, arguments and epilog
      * in the parser.
      *
-     * @param string|null $subcommand If present and a valid subcommand that has a linked parser.
-     *    That subcommands help will be shown instead.
      * @param string $format Define the output format, can be text or XML
      * @param int $width The width to format user content to. Defaults to 72
      * @return string Generated help.
      */
-    public function help(?string $subcommand = null, string $format = 'text', int $width = 72): string
+    public function help(string $format = 'text', int $width = 72): string
     {
-        if ($subcommand === null) {
-            $formatter = new HelpFormatter($this);
-            $formatter->setAlias($this->rootName);
+        $formatter = new HelpFormatter($this);
+        $formatter->setAlias($this->rootName);
 
-            if ($format === 'text') {
-                return $formatter->text($width);
-            }
-            if ($format === 'xml') {
-                return (string)$formatter->xml();
-            }
+        if ($format === 'text') {
+            return $formatter->text($width);
         }
-        $subcommand = (string)$subcommand;
-
-        if (isset($this->_subcommands[$subcommand])) {
-            $command = $this->_subcommands[$subcommand];
-            $subparser = $command->parser();
-
-            // Generate a parser as the subcommand didn't define one.
-            if (!($subparser instanceof self)) {
-                // $subparser = clone $this;
-                $subparser = new self($subcommand);
-                $subparser
-                    ->setDescription($command->getRawHelp())
-                    ->addOptions($this->options())
-                    ->addArguments($this->arguments());
-            }
-            if ($subparser->getDescription() === '') {
-                $subparser->setDescription($command->getRawHelp());
-            }
-            $subparser->setCommand($this->getCommand() . ' ' . $subcommand);
-            $subparser->setRootName($this->rootName);
-
-            return $subparser->help(null, $format, $width);
+        if ($format === 'xml') {
+            return (string)$formatter->xml();
         }
 
-        $rootCommand = $this->getCommand();
-        $message = sprintf(
-            'Unable to find the `%s %s` subcommand. See `bin/%s %s --help`.',
-            $rootCommand,
-            $subcommand,
-            $this->rootName,
-            $rootCommand
-        );
-        throw new MissingOptionException(
-            $message,
-            $subcommand,
-            array_keys($this->subcommands())
+        throw new ConsoleException(
+            sprintf('Invalid format. Output format can be text or xml.')
         );
     }
 

+ 1 - 28
src/Console/HelpFormatter.php

@@ -97,25 +97,6 @@ class HelpFormatter
         $out[] = '<info>Usage:</info>';
         $out[] = $this->_generateUsage();
         $out[] = '';
-        $subcommands = $parser->subcommands();
-        if (!empty($subcommands)) {
-            $out[] = '<info>Subcommands:</info>';
-            $out[] = '';
-            $max = $this->_getMaxLength($subcommands) + 2;
-            foreach ($subcommands as $command) {
-                $out[] = Text::wrapBlock($command->help($max), [
-                    'width' => $width,
-                    'indent' => str_repeat(' ', $max),
-                    'indentAt' => 1,
-                ]);
-            }
-            $out[] = '';
-            $out[] = sprintf(
-                'To see help on a subcommand use <info>`' . $this->_alias . ' %s [subcommand] --help`</info>',
-                $parser->getCommand()
-            );
-            $out[] = '';
-        }
 
         $options = $parser->options();
         if ($options) {
@@ -165,10 +146,6 @@ class HelpFormatter
     protected function _generateUsage(): string
     {
         $usage = [$this->_alias . ' ' . $this->_parser->getCommand()];
-        $subcommands = $this->_parser->subcommands();
-        if (!empty($subcommands)) {
-            $usage[] = '[subcommand]';
-        }
         $options = [];
         foreach ($this->_parser->options() as $option) {
             $options[] = $option->usage();
@@ -192,7 +169,7 @@ class HelpFormatter
     /**
      * Iterate over a collection and find the longest named thing.
      *
-     * @param array<\Cake\Console\ConsoleInputOption|\Cake\Console\ConsoleInputArgument|\Cake\Console\ConsoleInputSubcommand> $collection The collection to find a max length of.
+     * @param array<\Cake\Console\ConsoleInputOption|\Cake\Console\ConsoleInputArgument> $collection The collection to find a max length of.
      * @return int
      */
     protected function _getMaxLength(array $collection): int
@@ -218,10 +195,6 @@ class HelpFormatter
         $xml->addChild('command', $parser->getCommand());
         $xml->addChild('description', $parser->getDescription());
 
-        $subcommands = $xml->addChild('subcommands');
-        foreach ($parser->subcommands() as $command) {
-            $command->xml($subcommands);
-        }
         $options = $xml->addChild('options');
         foreach ($parser->options() as $option) {
             $option->xml($options);

+ 0 - 333
tests/TestCase/Console/ConsoleOptionParserTest.php

@@ -18,7 +18,6 @@ namespace Cake\Test\TestCase\Console;
 
 use Cake\Console\ConsoleInputArgument;
 use Cake\Console\ConsoleInputOption;
-use Cake\Console\ConsoleInputSubcommand;
 use Cake\Console\ConsoleIo;
 use Cake\Console\ConsoleOptionParser;
 use Cake\Console\Exception\ConsoleException;
@@ -752,112 +751,6 @@ class ConsoleOptionParserTest extends TestCase
     }
 
     /**
-     * test setting a subcommand up.
-     */
-    public function testSubcommand(): void
-    {
-        $parser = new ConsoleOptionParser('test', false);
-        $result = $parser->addSubcommand('initdb', [
-            'help' => 'Initialize the database',
-        ]);
-        $this->assertEquals($parser, $result, 'Adding a subcommand is not chainable');
-    }
-
-    /**
-     * Tests setting a subcommand up for a Shell method `initMyDb`.
-     */
-    public function testSubcommandCamelBacked(): void
-    {
-        $parser = new ConsoleOptionParser('test', false);
-        $result = $parser->addSubcommand('initMyDb', [
-            'help' => 'Initialize the database',
-        ]);
-
-        $subcommands = array_keys($result->subcommands());
-        $this->assertEquals(['init_my_db'], $subcommands, 'Adding a subcommand does not work with camel backed method names.');
-    }
-
-    /**
-     * Test addSubcommand inherits options as no
-     * parser is created.
-     */
-    public function testAddSubcommandInheritOptions(): void
-    {
-        $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->io);
-        $this->assertSame('default', $result[0]['connection']);
-
-        $result = $parser->subcommands();
-        $this->assertArrayHasKey('build', $result);
-        $this->assertNull($result['build']->parser(), 'No parser should be created');
-    }
-
-    /**
-     * test addSubcommand with an object.
-     */
-    public function testAddSubcommandObject(): void
-    {
-        $parser = new ConsoleOptionParser('test', false);
-        $parser->addSubcommand(new ConsoleInputSubcommand('test'));
-        $result = $parser->subcommands();
-        $this->assertCount(1, $result);
-        $this->assertSame('test', $result['test']->name());
-    }
-
-    /**
-     * test addSubcommand without sorting applied.
-     */
-    public function testAddSubcommandSort(): void
-    {
-        $parser = new ConsoleOptionParser('test', false);
-        $this->assertTrue($parser->isSubcommandSortEnabled());
-        $parser->enableSubcommandSort(false);
-        $this->assertFalse($parser->isSubcommandSortEnabled());
-        $parser->addSubcommand(new ConsoleInputSubcommand('betaTest'), []);
-        $parser->addSubcommand(new ConsoleInputSubcommand('alphaTest'), []);
-        $result = $parser->subcommands();
-        $this->assertCount(2, $result);
-        $firstResult = key($result);
-        $this->assertSame('betaTest', $firstResult);
-    }
-
-    /**
-     * test removeSubcommand with an object.
-     */
-    public function testRemoveSubcommand(): void
-    {
-        $parser = new ConsoleOptionParser('test', false);
-        $parser->addSubcommand(new ConsoleInputSubcommand('test'));
-        $result = $parser->subcommands();
-        $this->assertCount(1, $result);
-        $parser->removeSubcommand('test');
-        $result = $parser->subcommands();
-        $this->assertCount(0, $result, 'Remove a subcommand does not work');
-    }
-
-    /**
-     * test adding multiple subcommands
-     */
-    public function testAddSubcommands(): void
-    {
-        $parser = new ConsoleOptionParser('test', false);
-        $result = $parser->addSubcommands([
-            'initdb' => ['help' => 'Initialize the database'],
-            'create' => ['help' => 'Create something'],
-        ]);
-        $this->assertEquals($parser, $result, 'Adding a subcommands is not chainable');
-        $result = $parser->subcommands();
-        $this->assertCount(2, $result, 'Not enough subcommands');
-    }
-
-    /**
      * test that no exception is triggered for required arguments when help is being generated
      */
     public function testHelpNoExceptionForRequiredArgumentsWhenGettingHelp(): void
@@ -884,151 +777,6 @@ class ConsoleOptionParserTest extends TestCase
     }
 
     /**
-     * test that help() with a command param shows the help for a subcommand
-     */
-    public function testHelpSubcommandHelp(): void
-    {
-        $subParser = new ConsoleOptionParser('method', false);
-        $subParser->addOption('connection', ['help' => 'Db connection.']);
-        $subParser->addOption('zero', ['short' => '0', 'help' => 'Zero.']);
-
-        $parser = new ConsoleOptionParser('mycommand', false);
-        $parser->addSubcommand('method', [
-                'help' => 'This is another command',
-                'parser' => $subParser,
-            ])
-            ->addOption('test', ['help' => 'A test option.']);
-
-        $result = $parser->help('method');
-        $expected = <<<TEXT
-This is another command
-
-<info>Usage:</info>
-cake mycommand method [--connection] [-h] [-0]
-
-<info>Options:</info>
-
---connection      Db connection.
---help, -h        Display this help.
---zero, -0        Zero.
-
-TEXT;
-        $this->assertTextEquals($expected, $result, 'Help is not correct.');
-    }
-
-    /**
-     * Test addSubcommand inherits options as no
-     * parser is created.
-     */
-    public function testHelpSubcommandInheritOptions(): void
-    {
-        $parser = new ConsoleOptionParser('mycommand', false);
-        $parser->addSubcommand('build', [
-            'help' => 'Build things.',
-        ])->addSubcommand('destroy', [
-            'help' => 'Destroy things.',
-        ])->addOption('connection', [
-            'help' => 'Db connection.',
-            'short' => 'c',
-        ])->addArgument('name', ['required' => false]);
-
-        $result = $parser->help('build');
-        $expected = <<<TEXT
-Build things.
-
-<info>Usage:</info>
-cake mycommand build [-c] [-h] [-q] [-v] [<name>]
-
-<info>Options:</info>
-
---connection, -c  Db connection.
---help, -h        Display this help.
---quiet, -q       Enable quiet output.
---verbose, -v     Enable verbose output.
-
-<info>Arguments:</info>
-
-name   <comment>(optional)</comment>
-
-TEXT;
-        $this->assertTextEquals($expected, $result, 'Help is not correct.');
-    }
-
-    /**
-     * test that help() with a command param shows the help for a subcommand
-     */
-    public function testHelpSubcommandHelpArray(): void
-    {
-        $subParser = [
-            'options' => [
-                'foo' => [
-                    'short' => 'f',
-                    'help' => 'Foo.',
-                    'boolean' => true,
-                ],
-            ],
-        ];
-
-        $parser = new ConsoleOptionParser('mycommand', false);
-        $parser->addSubcommand('method', [
-                'help' => 'This is a subcommand',
-                'parser' => $subParser,
-            ])
-            ->setRootName('tool')
-            ->addOption('test', ['help' => 'A test option.']);
-
-        $result = $parser->help('method');
-        $expected = <<<TEXT
-This is a subcommand
-
-<info>Usage:</info>
-tool 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.');
-    }
-
-    /**
-     * test that help() with a command param shows the help for a subcommand
-     */
-    public function testHelpSubcommandInheritParser(): void
-    {
-        $subParser = new ConsoleOptionParser('method', false);
-        $subParser->addOption('connection', ['help' => 'Db connection.']);
-        $subParser->addOption('zero', ['short' => '0', 'help' => 'Zero.']);
-
-        $parser = new ConsoleOptionParser('mycommand', false);
-        $parser->addSubcommand('method', [
-                'help' => 'This is another command',
-                'parser' => $subParser,
-            ])
-            ->addOption('test', ['help' => 'A test option.']);
-
-        $result = $parser->help('method');
-        $expected = <<<TEXT
-This is another command
-
-<info>Usage:</info>
-cake mycommand method [--connection] [-h] [-0]
-
-<info>Options:</info>
-
---connection      Db connection.
---help, -h        Display this help.
---zero, -0        Zero.
-
-TEXT;
-        $this->assertTextEquals($expected, $result, 'Help is not correct.');
-    }
-
-    /**
      * test that help() with a custom rootName
      */
     public function testHelpWithRootName(): void
@@ -1057,45 +805,6 @@ TEXT;
     }
 
     /**
-     * test that getCommandError() with an unknown subcommand param shows a helpful message
-     */
-    public function testHelpUnknownSubcommand(): void
-    {
-        $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.'])
-            ->addSubcommand('unstash');
-
-        try {
-            $result = $parser->help('unknown');
-        } catch (MissingOptionException $e) {
-            $result = $e->getFullMessage();
-            $this->assertStringContainsString(
-                "Unable to find the `mycommand unknown` subcommand. See `bin/cake mycommand --help`.\n" .
-                "\n" .
-                "Other valid choices:\n" .
-                "\n" .
-                '- method',
-                $result
-            );
-        }
-    }
-
-    /**
      * test building a parser from an array.
      */
     public function testBuildFromArray(): void
@@ -1110,9 +819,6 @@ TEXT;
                 'name' => ['help' => 'The name'],
                 'other' => ['help' => 'The other arg'],
             ],
-            'subcommands' => [
-                'initdb' => ['help' => 'make database'],
-            ],
             'description' => 'description text',
             'epilog' => 'epilog text',
         ];
@@ -1127,9 +833,6 @@ TEXT;
 
         $args = $parser->arguments();
         $this->assertCount(2, $args);
-
-        $commands = $parser->subcommands();
-        $this->assertCount(1, $commands);
     }
 
     /**
@@ -1152,38 +855,6 @@ TEXT;
     }
 
     /**
-     * test that parse() takes a subcommand argument, and that the subcommand parser
-     * is used.
-     */
-    public function testParsingWithSubParser(): void
-    {
-        $parser = new ConsoleOptionParser('test', false);
-        $parser->addOption('primary')
-            ->addArgument('one', ['required' => true, 'choices' => ['a', 'b']])
-            ->addArgument('two', ['required' => true])
-            ->addSubcommand('sub', [
-                'parser' => [
-                    'options' => [
-                        'secondary' => ['boolean' => true],
-                        'fourth' => ['help' => 'fourth option'],
-                    ],
-                    'arguments' => [
-                        'sub_arg' => ['choices' => ['c', 'd']],
-                    ],
-                ],
-            ]);
-
-        $result = $parser->parse(['sub', '--secondary', '--fourth', '4', 'c'], $this->io);
-        $expected = [[
-            'secondary' => true,
-            'fourth' => '4',
-            'help' => false,
-            'verbose' => false,
-            'quiet' => false], ['c']];
-        $this->assertEquals($expected, $result, 'Sub parser did not parse request.');
-    }
-
-    /**
      * Tests toArray()
      */
     public function testToArray(): void
@@ -1198,9 +869,6 @@ TEXT;
                 'name' => ['help' => 'The name'],
                 'other' => ['help' => 'The other arg'],
             ],
-            'subcommands' => [
-                'initdb' => ['help' => 'make database'],
-            ],
             'description' => 'description text',
             'epilog' => 'epilog text',
         ];
@@ -1215,7 +883,6 @@ TEXT;
         $this->assertArrayHasKey('other', $options);
 
         $this->assertCount(2, $result['arguments']);
-        $this->assertCount(1, $result['subcommands']);
     }
 
     /**

+ 2 - 86
tests/TestCase/Console/HelpFormatterTest.php

@@ -35,8 +35,7 @@ class HelpFormatterTest extends TestCase
         $parser = new ConsoleOptionParser('test', false);
         $parser->setDescription('This is fifteen This is fifteen This is fifteen')
             ->addOption('four', ['help' => 'this is help text this is help text'])
-            ->addArgument('four', ['help' => 'this is help text this is help text'])
-            ->addSubcommand('four', ['help' => 'this is help text this is help text']);
+            ->addArgument('four', ['help' => 'this is help text this is help text']);
 
         $formatter = new HelpFormatter($parser);
         $result = $formatter->text(30);
@@ -45,14 +44,7 @@ This is fifteen This is
 fifteen This is fifteen
 
 <info>Usage:</info>
-cake test [subcommand] [--four] [-h] [<four>]
-
-<info>Subcommands:</info>
-
-four  this is help text this
-      is help text
-
-To see help on a subcommand use <info>`cake test [subcommand] --help`</info>
+cake test [--four] [-h] [<four>]
 
 <info>Options:</info>
 
@@ -139,42 +131,6 @@ txt;
     }
 
     /**
-     * test that help() outputs subcommands.
-     */
-    public function testHelpSubcommand(): void
-    {
-        $parser = new ConsoleOptionParser('mycommand', false);
-        $parser->addSubcommand('method', ['help' => 'This is another command'])
-            ->addOption('test', ['help' => 'A test option.']);
-        $parser->addSubcommand('plugin', ['help' =>
-            'Create the directory structure, AppController class and testing setup for a new plugin. ' .
-            'Can create plugins in any of your bootstrapped plugin paths.']);
-
-        $formatter = new HelpFormatter($parser);
-        $result = $formatter->text();
-        $expected = <<<txt
-<info>Usage:</info>
-cake mycommand [subcommand] [-h] [--test]
-
-<info>Subcommands:</info>
-
-method  This is another command
-plugin  Create the directory structure, AppController class and testing
-        setup for a new plugin. Can create plugins in any of your
-        bootstrapped plugin paths.
-
-To see help on a subcommand use <info>`cake mycommand [subcommand] --help`</info>
-
-<info>Options:</info>
-
---help, -h  Display this help.
---test      A test option.
-
-txt;
-        $this->assertTextEquals($expected, $result, 'Help is not correct.');
-    }
-
-    /**
      * test getting help with defined options.
      */
     public function testHelpWithOptions(): void
@@ -319,7 +275,6 @@ xml;
 <shell>
 <command>mycommand</command>
 <description />
-<subcommands />
 <options>
 	<option name="--help" short="-h" help="Display this help." boolean="1" required="0">
 		<default>false</default>
@@ -368,7 +323,6 @@ xml;
 <shell>
 <command>mycommand</command>
 <description>Description text</description>
-<subcommands />
 <options>
 	<option name="--help" short="-h" help="Display this help." boolean="1" required="0">
 		<default>false</default>
@@ -391,42 +345,6 @@ xml;
     }
 
     /**
-     * test that help() outputs subcommands.
-     */
-    public function testXmlHelpSubcommand(): void
-    {
-        $parser = new ConsoleOptionParser('mycommand', false);
-        $parser->addSubcommand('method', ['help' => 'This is another command'])
-            ->addOption('test', ['help' => 'A test option.']);
-
-        $formatter = new HelpFormatter($parser);
-        $result = $formatter->xml();
-        $expected = <<<xml
-<?xml version="1.0"?>
-<shell>
-<command>mycommand</command>
-<description/>
-<subcommands>
-	<command name="method" help="This is another command" />
-</subcommands>
-<options>
-	<option name="--help" short="-h" help="Display this help." boolean="1" required="0">
-		<default>false</default>
-		<choices></choices>
-	</option>
-	<option name="--test" short="" help="A test option." boolean="0" required="0">
-		<default></default>
-		<choices></choices>
-	</option>
-</options>
-<arguments/>
-<epilog/>
-</shell>
-xml;
-        $this->assertXmlStringEqualsXmlString($expected, $result, 'Help does not match');
-    }
-
-    /**
      * test getting help with defined options.
      */
     public function testXmlHelpWithOptions(): void
@@ -444,7 +362,6 @@ xml;
 <shell>
 <command>mycommand</command>
 <description/>
-<subcommands/>
 <options>
 	<option name="--connection" short="-c" help="The connection to use." boolean="0" required="0">
 		<default>default</default>
@@ -483,7 +400,6 @@ xml;
 <shell>
 	<command>mycommand</command>
 	<description/>
-	<subcommands/>
 	<options>
 		<option name="--help" short="-h" help="Display this help." boolean="1" required="0">
 			<default>false</default>