]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Respect British spelling also in arguments
authorvrana <jakubv@fb.com>
Sat, 2 Mar 2013 00:07:16 +0000 (16:07 -0800)
committervrana <jakubv@fb.com>
Sat, 2 Mar 2013 00:53:26 +0000 (16:53 -0800)
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
src/configuration/__tests__/ArcanistBritishTestCase.php
src/workflow/ArcanistBaseWorkflow.php

index 0cf66ed129c696bee79023bbebb068d6b4e7c68d..23d58c668d1fde72957a4aecff34ad219d3db92b 100644 (file)
@@ -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();
     }
 
index 3fe34688c3c25656cb8b8d0f4e96cca23219ebff..511614ffa95c6bf437bedfbf2fd1fe11ff1b4666 100644 (file)
@@ -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}");
+  }
+
 }
index 6cf149028ece03eb82f45c68416723b780e15634..5a7883f519fb9f4f9fa195f3248914ca86f8ff22 100644 (file)
@@ -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);