Skip to content
Permalink
Browse files

When an unknown option is used, display a helpful message rather than…

… the entire command help message
  • Loading branch information...
HavokInspiration committed Jul 31, 2017
1 parent 822c923 commit 56c729479761e536a3331588474ad86d733e41c5
Showing with 30 additions and 2 deletions.
  1. +30 −1 src/Console/ConsoleOptionParser.php
  2. +0 −1 src/Console/Shell.php
@@ -818,6 +818,35 @@ public function getCommandError($command)
return implode("\n", $out);
}
/**
* Get the message output in the console stating that the option can not be found and tries to guess what the user
* wanted to say. Output a list of available options as well.
*
* @param string $option Unknown option name trying to be used.
* @return string The message to be displayed in the console.
*/
public function getOptionError($option)
{
$availableOptions = array_keys($this->_options);
$bestGuess = $this->findClosestItem($option, $availableOptions);
$out = [];
$out[] = sprintf('Unknown option `%s`.', $option);
$out[] = '';
if ($bestGuess !== null) {
$out[] = sprintf('Did you mean `%s` ?', $bestGuess);
$out[] = '';
}
$out[] = 'Available options are :';
$out[] = '';
foreach ($availableOptions as $availableOption) {
$out[] = ' - ' . $availableOption;
}
return implode("\n", $out);
}
/**
* Tries to guess the item name the user originally wanted using the levenshtein algorithm.
*
@@ -908,7 +937,7 @@ protected function _parseShortOption($option, $params)
protected function _parseOption($name, $params)
{
if (!isset($this->_options[$name])) {
throw new ConsoleException(sprintf('Unknown option `%s`', $name));
throw new ConsoleException($this->getOptionError($name));
}
$option = $this->_options[$name];
$isBoolean = $option->isBoolean();
@@ -459,7 +459,6 @@ public function runCommand($argv, $autoMethod = false, $extra = [])
list($this->params, $this->args) = $this->OptionParser->parse($argv);
} catch (ConsoleException $e) {
$this->err('Error: ' . $e->getMessage());
$this->out($this->OptionParser->help($command));
return false;
}

0 comments on commit 56c7294

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