Skip to content
Permalink
Browse files

Use the `help()` method for unknown subcommands

  • Loading branch information...
HavokInspiration committed Aug 1, 2017
1 parent 876c100 commit 57db7a68f9f45723f30288fdb1e33c9896907a6b
@@ -731,6 +731,18 @@ public function parse($argv)
*/
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 @@ public function help($subcommand = null, $format = 'text', $width = 72)
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 @@ public function setRootName($name)
* @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 @@ protected function getShortOptionError($option)
}
/**
* 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.
@@ -506,7 +506,7 @@ public function runCommand($argv, $autoMethod = false, $extra = [])
return $this->main(...$this->args);
}
$this->err($this->OptionParser->getCommandError($command));
$this->err($this->OptionParser->help($command));
return false;
}
@@ -548,6 +548,8 @@ protected function _displayHelp($command)
$this->_welcome();
}
$subcommands = $this->OptionParser->subcommands();
$command = isset($subcommands[$command]) ? $command : null;
return $this->out($this->OptionParser->help($command, $format));
}
@@ -831,7 +831,7 @@ public function testHelpUnknownSubcommand()
->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`.
@@ -954,7 +954,7 @@ public function testRunCommandAutoMethodOff()
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 @@ public function testRunCommandWithMethodNotInSubcommands()
->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 @@ public function testRunCommandWithMethodInSubcommands()
public function testRunCommandWithMissingMethodInSubcommands()
{
$parser = $this->getMockBuilder('Cake\Console\ConsoleOptionParser')
->setMethods(['getCommandError'])
->setMethods(['help'])
->setConstructorArgs(['knife'])
->getMock();
$parser->addSubCommand('slice');
@@ -1036,7 +1036,7 @@ public function testRunCommandWithMissingMethodInSubcommands()
->method('startup');
$parser->expects($this->once())
->method('getCommandError');
->method('help');
$shell->runCommand(['slice', 'cakes', '--verbose']);
}
@@ -1058,7 +1058,7 @@ public function testRunCommandBaseClassMethod()
->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 @@ public function testRunCommandMissingMethod()
->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');

0 comments on commit 57db7a6

Please sign in to comment.
You can’t perform that action at this time.