From ebe464b6f0c77ecf12466061616bc036d330521d Mon Sep 17 00:00:00 2001 From: vrana Date: Fri, 1 Mar 2013 16:07:16 -0800 Subject: [PATCH] Respect British spelling also in arguments Summary: These are the errors I really do. Test Plan: $ arc diff --no-lint (Assuming '--no-lint' is the British spelling of '--nolint'.) $ arc diff --reviewer a (Assuming '--reviewer' is the British spelling of '--reviewers'.) New unit test. Reviewers: epriestley Reviewed By: epriestley CC: aran, Korvin Differential Revision: https://secure.phabricator.com/D5185 --- src/configuration/ArcanistConfiguration.php | 27 ++++++++- .../__tests__/ArcanistBritishTestCase.php | 56 ++++++++++++++++--- src/workflow/ArcanistBaseWorkflow.php | 16 +++++- 3 files changed, 86 insertions(+), 13 deletions(-) diff --git a/src/configuration/ArcanistConfiguration.php b/src/configuration/ArcanistConfiguration.php index 0cf66ed1..23d58c66 100644 --- a/src/configuration/ArcanistConfiguration.php +++ b/src/configuration/ArcanistConfiguration.php @@ -209,11 +209,34 @@ class ArcanistConfiguration { ->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(); } diff --git a/src/configuration/__tests__/ArcanistBritishTestCase.php b/src/configuration/__tests__/ArcanistBritishTestCase.php index 3fe34688..511614ff 100644 --- a/src/configuration/__tests__/ArcanistBritishTestCase.php +++ b/src/configuration/__tests__/ArcanistBritishTestCase.php @@ -2,44 +2,44 @@ 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, @@ -57,4 +57,42 @@ final class ArcanistBritishTestCase extends ArcanistTestCase { } + 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}"); + } + } diff --git a/src/workflow/ArcanistBaseWorkflow.php b/src/workflow/ArcanistBaseWorkflow.php index 6cf14902..5a7883f5 100644 --- a/src/workflow/ArcanistBaseWorkflow.php +++ b/src/workflow/ArcanistBaseWorkflow.php @@ -636,8 +636,20 @@ abstract class ArcanistBaseWorkflow extends Phobject { } 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); -- 2.43.0