Browse Source

Use the `help()` method for unknown subcommands

Yves P 8 years ago
parent
commit
57db7a68f9

+ 16 - 11
src/Console/ConsoleOptionParser.php

@@ -731,6 +731,18 @@ class ConsoleOptionParser
      */
     public function help($subcommand = null, $format = 'text', $width = 72)
     {
+        if ($subcommand === null) {
+            $formatter = new HelpFormatter($this);
+            $formatter->setAlias($this->rootName);
+
+            if ($format === 'text') {
+                return $formatter->text($width);
+            }
+            if ($format === 'xml') {
+                return $formatter->xml();
+            }
+        }
+
         if (isset($this->_subcommands[$subcommand])) {
             $command = $this->_subcommands[$subcommand];
             $subparser = $command->parser();
@@ -746,15 +758,7 @@ class ConsoleOptionParser
             return $subparser->help(null, $format, $width);
         }
 
-        $formatter = new HelpFormatter($this);
-        $formatter->setAlias($this->rootName);
-
-        if ($format === 'text') {
-            return $formatter->text($width);
-        }
-        if ($format === 'xml') {
-            return $formatter->xml();
-        }
+        return $this->getCommandError($subcommand);
     }
 
     /**
@@ -789,7 +793,7 @@ class ConsoleOptionParser
      * @param string $command Unknown command name trying to be dispatched.
      * @return string The message to be displayed in the console.
      */
-    public function getCommandError($command)
+    protected function getCommandError($command)
     {
         $rootCommand = $this->getCommand();
         $subcommands = array_keys((array)$this->subcommands());
@@ -869,7 +873,8 @@ class ConsoleOptionParser
     }
 
     /**
-     * Tries to guess the item name the user originally wanted using the levenshtein algorithm.
+     * Tries to guess the item name the user originally wanted using the some regex pattern and the levenshtein
+     * algorithm.
      *
      * @param string $needle Unknown item (either a subcommand name or an option for instance) trying to be used.
      * @param array $haystack List of items available for the type $needle belongs to.

+ 3 - 1
src/Console/Shell.php

@@ -506,7 +506,7 @@ class Shell
             return $this->main(...$this->args);
         }
 
-        $this->err($this->OptionParser->getCommandError($command));
+        $this->err($this->OptionParser->help($command));
 
         return false;
     }
@@ -548,6 +548,8 @@ class Shell
             $this->_welcome();
         }
 
+        $subcommands = $this->OptionParser->subcommands();
+        $command = isset($subcommands[$command]) ? $command : null;
         return $this->out($this->OptionParser->help($command, $format));
     }
 

+ 1 - 1
tests/TestCase/Console/ConsoleOptionParserTest.php

@@ -831,7 +831,7 @@ TEXT;
             ->addOption('test', ['help' => 'A test option.'])
             ->addSubcommand('unstash');
 
-        $result = $parser->getCommandError('unknown');
+        $result = $parser->help('unknown');
         $expected = <<<TEXT
 Unable to find the `mycommand unknown` subcommand. See `bin/cake mycommand --help`.
 

+ 6 - 6
tests/TestCase/Console/ShellTest.php

@@ -954,7 +954,7 @@ TEXT;
     public function testRunCommandWithMethodNotInSubcommands()
     {
         $parser = $this->getMockBuilder('Cake\Console\ConsoleOptionParser')
-            ->setMethods(['getCommandError'])
+            ->setMethods(['help'])
             ->setConstructorArgs(['knife'])
             ->getMock();
         $io = $this->getMockBuilder('Cake\Console\ConsoleIo')->getMock();
@@ -970,7 +970,7 @@ TEXT;
             ->will($this->returnValue($parser));
 
         $parser->expects($this->once())
-            ->method('getCommandError');
+            ->method('help');
 
         $shell->expects($this->never())->method('startup');
         $shell->expects($this->never())->method('roll');
@@ -1018,7 +1018,7 @@ TEXT;
     public function testRunCommandWithMissingMethodInSubcommands()
     {
         $parser = $this->getMockBuilder('Cake\Console\ConsoleOptionParser')
-            ->setMethods(['getCommandError'])
+            ->setMethods(['help'])
             ->setConstructorArgs(['knife'])
             ->getMock();
         $parser->addSubCommand('slice');
@@ -1036,7 +1036,7 @@ TEXT;
             ->method('startup');
 
         $parser->expects($this->once())
-            ->method('getCommandError');
+            ->method('help');
 
         $shell->runCommand(['slice', 'cakes', '--verbose']);
     }
@@ -1058,7 +1058,7 @@ TEXT;
             ->disableOriginalConstructor()
             ->getMock();
 
-        $parser->expects($this->once())->method('getCommandError');
+        $parser->expects($this->once())->method('help');
         $shell->expects($this->once())->method('getOptionParser')
             ->will($this->returnValue($parser));
         $shell->expects($this->never())->method('hr');
@@ -1083,7 +1083,7 @@ TEXT;
             ->disableOriginalConstructor()
             ->getMock();
 
-        $parser->expects($this->once())->method('getCommandError');
+        $parser->expects($this->once())->method('help');
         $shell->expects($this->once())->method('getOptionParser')
             ->will($this->returnValue($parser));
         $shell->expects($this->once())->method('err');