Skip to content

Commit

Permalink
When an unknown option is used, display a helpful message rather than…
Browse files Browse the repository at this point in the history
… the entire command help message
  • Loading branch information
HavokInspiration committed Jul 31, 2017
1 parent 822c923 commit 56c7294
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
31 changes: 30 additions & 1 deletion src/Console/ConsoleOptionParser.php
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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();
Expand Down
1 change: 0 additions & 1 deletion src/Console/Shell.php
Expand Up @@ -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;
}
Expand Down

0 comments on commit 56c7294

Please sign in to comment.