->setReplaceCost(3)
->setTransposeCost(2);
+ return self::correctSpelling($command, $options, $matrix, $max_distance);
+ }
+
+ public static function correctArgumentSpelling($command, array $options) {
+ $max_distance = 1;
+
+ // We are stricter with arguments - we allow only one inserted or deleted
+ // character. It is mainly to handle cases like --no-lint versus --nolint
+ // or --reviewer versus --reviewers.
+ $matrix = id(new PhutilEditDistanceMatrix())
+ ->setInsertCost(1)
+ ->setDeleteCost(1)
+ ->setReplaceCost(10);
+
+ return self::correctSpelling($command, $options, $matrix, $max_distance);
+ }
+
+ public static function correctSpelling(
+ $input,
+ array $options,
+ PhutilEditDistanceMatrix $matrix,
+ $max_distance) {
+
$distances = array();
- $commandv = str_split($command);
+ $inputv = str_split($input);
foreach ($options as $option) {
$optionv = str_split($option);
- $matrix->setSequences($optionv, $commandv);
+ $matrix->setSequences($optionv, $inputv);
$distances[$option] = $matrix->getEditDistance();
}
final class ArcanistBritishTestCase extends ArcanistTestCase {
- public function testCompletion() {
- $this->assertCompletion(
+ public function testCommandCompletion() {
+ $this->assertCommandCompletion(
array('land'),
'alnd',
array('land', 'amend'));
- $this->assertCompletion(
+ $this->assertCommandCompletion(
array('branch'),
'brnach',
array('branch', 'browse'));
- $this->assertCompletion(
+ $this->assertCommandCompletion(
array(),
'test',
array('list', 'unit'));
- $this->assertCompletion(
+ $this->assertCommandCompletion(
array('list'),
'lists',
array('list'));
- $this->assertCompletion(
+ $this->assertCommandCompletion(
array('diff'),
'dfif',
array('diff'));
- $this->assertCompletion(
+ $this->assertCommandCompletion(
array('unit'),
'uint',
array('unit', 'lint', 'list'));
- $this->assertCompletion(
+ $this->assertCommandCompletion(
array('list', 'lint'),
'nilt',
array('unit', 'lint', 'list'));
}
- private function assertCompletion($expect, $input, $commands) {
+ private function assertCommandCompletion($expect, $input, $commands) {
$result = ArcanistConfiguration::correctCommandSpelling(
$input,
$commands,
}
+ public function testArgumentCompletion() {
+ $this->assertArgumentCompletion(
+ array('nolint'),
+ 'no-lint',
+ array('nolint', 'nounit'));
+
+ $this->assertArgumentCompletion(
+ array('reviewers'),
+ 'reviewer',
+ array('reviewers', 'cc'));
+
+ $this->assertArgumentCompletion(
+ array(),
+ 'onlint',
+ array('nolint'));
+
+ $this->assertArgumentCompletion(
+ array(),
+ 'nolind',
+ array('nolint'));
+ }
+
+ private function assertArgumentCompletion($expect, $input, $arguments) {
+ $result = ArcanistConfiguration::correctArgumentSpelling(
+ $input,
+ $arguments);
+
+ sort($result);
+ sort($expect);
+
+ $arguments = implode(', ', $arguments);
+
+ $this->assertEqual(
+ $expect,
+ $result,
+ "Correction of {$input} against: {$arguments}");
+ }
+
}
} else if (!strncmp($arg, '--', 2)) {
$arg_key = substr($arg, 2);
if (!array_key_exists($arg_key, $spec)) {
- throw new ArcanistUsageException(
- "Unknown argument '{$arg_key}'. Try 'arc help'.");
+ $corrected = ArcanistConfiguration::correctArgumentSpelling(
+ $arg_key,
+ array_keys($spec));
+ if (count($corrected) == 1) {
+ PhutilConsole::getConsole()->writeErr(
+ pht(
+ "(Assuming '%s' is the British spelling of '%s'.)",
+ '--'.$arg_key,
+ '--'.head($corrected))."\n");
+ $arg_key = head($corrected);
+ } else {
+ throw new ArcanistUsageException(
+ "Unknown argument '{$arg_key}'. Try 'arc help'.");
+ }
}
} else if (!strncmp($arg, '-', 1)) {
$arg_key = substr($arg, 1);