Skip to content

Cli: suggest nearest option when typo is made #183

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions src/Cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
namespace ShipMonk\ComposerDependencyAnalyser;

use ShipMonk\ComposerDependencyAnalyser\Exception\InvalidCliException;
use function array_keys;
use function array_slice;
use function is_dir;
use function is_file;
use function levenshtein;
use function strlen;
use function strpos;
use function substr;
use function trim;

class Cli
{
Expand Down Expand Up @@ -54,23 +58,26 @@ public function __construct(string $cwd, array $argv)
$startsWithDashDash = strpos($arg, '--') === 0;

if ($startsWithDash && !$startsWithDashDash) {
throw new InvalidCliException("Unknown option $arg, see --help");
$suggestedOption = $this->suggestOption($arg);
throw new InvalidCliException("Unknown option $arg, $suggestedOption");
}

if (!$startsWithDashDash) {
if (is_file($cwd . '/' . $arg) || is_dir($cwd . '/' . $arg)) {
throw new InvalidCliException("Cannot pass paths ($arg) to analyse as arguments, use --config instead.");
}

throw new InvalidCliException("Unknown argument $arg, see --help");
$suggestedOption = $this->suggestOption($arg);
throw new InvalidCliException("Unknown argument $arg, $suggestedOption");
}

/** @var string $noDashesArg this is never false as we know it starts with -- */
$noDashesArg = substr($arg, 2);
$optionName = $this->getKnownOptionName($noDashesArg);

if ($optionName === null) {
throw new InvalidCliException("Unknown option $arg, see --help");
$suggestedOption = $this->suggestOption($noDashesArg);
throw new InvalidCliException("Unknown option $arg, $suggestedOption");
}

if ($this->isOptionWithRequiredValue($optionName)) {
Expand Down Expand Up @@ -186,4 +193,29 @@ public function getProvidedOptions(): CliOptions
return $options;
}

/**
* Params inspired by tracy/tracy
*/
private function suggestOption(string $input): string
{
$value = trim($input, '-');
$options = array_keys(self::OPTIONS);

$bestGuess = null;
$minDistance = (strlen($value) / 4 + 1) * 10 + .1;

foreach ($options as $option) {
$distance = levenshtein($option, $value, 9, 11, 9);

if ($distance > 0 && $distance < $minDistance) {
$minDistance = $distance;
$bestGuess = $option;
}
}

return $bestGuess === null
? 'see --help'
: "did you mean --$bestGuess?";
}

}
45 changes: 45 additions & 0 deletions tests/CliTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,51 @@ public function validationDataProvider(): iterable
'Missing argument value in --composer-json=, see --help',
['bin/script.php', '--composer-json='],
];

yield 'suggestion #1' => [
'Unknown option --hep, did you mean --help?',
['bin/script.php', '--hep'],
];

yield 'suggestion #2' => [
'Unknown option --ignore-shadow-dependencies, did you mean --ignore-shadow-deps?',
['bin/script.php', '--ignore-shadow-dependencies'],
];

yield 'suggestion #3' => [
'Unknown option --composer-lock, did you mean --composer-json?',
['bin/script.php', '--composer-lock'],
];

yield 'suggestion #4' => [
'Unknown option --ignore-prod-in-dev-deps, did you mean --ignore-prod-only-in-dev-deps?',
['bin/script.php', '--ignore-prod-in-dev-deps'],
];

yield 'suggestion #5' => [
'Unknown option --ignore-dev-prod-deps, did you mean --ignore-dev-in-prod-deps?',
['bin/script.php', '--ignore-dev-prod-deps'],
];

yield 'no suggestion #1' => [
'Unknown option --vvv, see --help',
['bin/script.php', '--vvv'],
];

yield 'no suggestion #2' => [
'Unknown option --v, see --help',
['bin/script.php', '--v'],
];

yield 'no suggestion #3' => [
'Unknown option --nonsense, see --help',
['bin/script.php', '--nonsense'],
];

yield 'no suggestion #4' => [
'Unknown option --four, see --help',
['bin/script.php', '--four'],
];
}

}
Loading