Warn the user when several commands match the input given.
authorFilipe Cabecinhas <me@filcab.net>
Wed, 16 May 2012 23:25:54 +0000 (23:25 +0000)
committerFilipe Cabecinhas <me@filcab.net>
Wed, 16 May 2012 23:25:54 +0000 (23:25 +0000)
Added a testcase.

llvm-svn: 156961

lldb/source/Interpreter/CommandInterpreter.cpp
lldb/test/functionalities/abbreviation/TestAbbreviations.py

index 5555238..9c93b53 100644 (file)
@@ -1326,6 +1326,7 @@ CommandInterpreter::HandleCommand (const char *command_line,
     StreamString revised_command_line;
     size_t actual_cmd_name_len = 0;
     std::string next_word;
+    StringList matches;
     while (!done)
     {
         char quote_char = '\0';
@@ -1346,7 +1347,7 @@ CommandInterpreter::HandleCommand (const char *command_line,
             }
             else
             {
-                cmd_obj = GetCommandObject (next_word.c_str());
+                cmd_obj = GetCommandObject (next_word.c_str(), &matches);
                 if (cmd_obj)
                 {
                     actual_cmd_name_len += next_word.length();
@@ -1392,7 +1393,26 @@ CommandInterpreter::HandleCommand (const char *command_line,
 
         if (cmd_obj == NULL)
         {
-            result.AppendErrorWithFormat ("'%s' is not a valid command.\n", next_word.c_str());
+            uint32_t num_matches = matches.GetSize();
+            if (matches.GetSize() > 1) {
+                std::string error_msg;
+                error_msg.assign ("Ambiguous command '");
+                error_msg.append(next_word.c_str());
+                error_msg.append ("'.");
+
+                error_msg.append (" Possible matches:");
+
+                for (uint32_t i = 0; i < num_matches; ++i) {
+                    error_msg.append ("\n\t");
+                    error_msg.append (matches.GetStringAtIndex(i));
+                }
+                error_msg.append ("\n");
+                result.AppendRawError (error_msg.c_str(), error_msg.size());
+            } else {
+                // We didn't have only one match, otherwise we wouldn't get here.
+                assert(num_matches == 0);
+                result.AppendErrorWithFormat ("'%s' is not a valid command.\n", next_word.c_str());
+            }
             result.SetStatus (eReturnStatusFailed);
             return false;
         }
index d89eb54..eae3ac1 100644 (file)
@@ -30,9 +30,15 @@ class AbbreviationsTestCase(TestBase):
                     COMMAND_FAILED_AS_EXPECTED, error = True,
                     substrs = ["error: 'gurp' is not a valid command."])
 
+        # Only one matching command: execute it.
         self.expect("h",
                     startstr = "The following is a list of built-in, permanent debugger commands:")
 
+        # Several matching commands: list them and error out.
+        self.expect("t",
+                    COMMAND_FAILED_AS_EXPECTED, error = True,
+                    substrs = ["Ambiguous command 't'. Possible matches:",
+                               "target", "thread", "type"])
 
         self.expect("com sou ./change_prompt.lldb",
                     patterns = ["Executing commands in '.*change_prompt.lldb'"])