Skip to content

Commit

Permalink
Use the help() method for unknown subcommands
Browse files Browse the repository at this point in the history
  • Loading branch information
HavokInspiration committed Aug 1, 2017
1 parent 876c100 commit 57db7a6
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 19 deletions.
27 changes: 16 additions & 11 deletions src/Console/ConsoleOptionParser.php
Expand Up @@ -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();
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 3 additions & 1 deletion src/Console/Shell.php
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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));
}

Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Console/ConsoleOptionParserTest.php
Expand Up @@ -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`.
Expand Down
12 changes: 6 additions & 6 deletions tests/TestCase/Console/ShellTest.php
Expand Up @@ -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();
Expand All @@ -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');
Expand Down Expand Up @@ -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');
Expand All @@ -1036,7 +1036,7 @@ public function testRunCommandWithMissingMethodInSubcommands()
->method('startup');

$parser->expects($this->once())
->method('getCommandError');
->method('help');

$shell->runCommand(['slice', 'cakes', '--verbose']);
}
Expand All @@ -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');
Expand All @@ -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');
Expand Down

0 comments on commit 57db7a6

Please sign in to comment.