[Lldb-commits] [lldb] [LLDB]Provide clearer error message for invalid commands. (PR #111891)

Jonas Devlieghere via lldb-commits lldb-commits at lists.llvm.org
Thu Oct 10 15:20:09 PDT 2024


================
@@ -194,28 +194,50 @@ void CommandObjectMultiword::Execute(const char *args_string,
 
   std::string error_msg;
   const size_t num_subcmd_matches = matches.GetSize();
-  if (num_subcmd_matches > 0)
+  if (num_subcmd_matches > 0) {
     error_msg.assign("ambiguous command ");
-  else
-    error_msg.assign("invalid command ");
-
-  error_msg.append("'");
-  error_msg.append(std::string(GetCommandName()));
-  error_msg.append(" ");
-  error_msg.append(std::string(sub_command));
-  error_msg.append("'.");
+    error_msg.append("'");
+    error_msg.append(std::string(GetCommandName()));
+    error_msg.append(" ");
+    error_msg.append(std::string(sub_command));
+    error_msg.append("'.");
 
-  if (num_subcmd_matches > 0) {
     error_msg.append(" Possible completions:");
     for (const std::string &match : matches) {
       error_msg.append("\n\t");
       error_msg.append(match);
     }
+  } else {
+    // Rather than simply complaining about the invalid (sub) command,
+    // try to offer some alternatives.
+    // This is especially useful for cases where the user types something
+    // seamingly trivial, such as `breakpoint foo`.
+    error_msg.assign(
+        llvm::Twine("'" + sub_command + "' is not a valid subcommand of \"" +
+                    GetCommandName() + "\". Valid subcommands are " +
+                    GetTopSubcommands(/*count=*/5) + ". Use \"help " +
+                    GetCommandName() + "\" to find out more.")
+            .str());
   }
   error_msg.append("\n");
   result.AppendRawError(error_msg.c_str());
 }
 
+std::string CommandObjectMultiword::GetTopSubcommands(int count) {
+  if (m_subcommand_dict.empty())
+    return "<NONE>";
+  std::string buffer = "{";
+  CommandMap::iterator pos;
+  for (pos = m_subcommand_dict.begin();
+       pos != m_subcommand_dict.end() && count > 0; ++pos, --count) {
+    buffer.append("'");
+    buffer.append(pos->first);
+    buffer.append("',");
+  }
+  buffer.append("...}");
----------------
JDevlieghere wrote:

Can this return something that feels more like natural language? By that I mean dropping the opening and closing braces, adding a space after the comma and maybe even dropping the single quotes. 

IIUC, this will also always print `...` even if there are say two subcommands and count was 3, which seems needlessly confusing. We should only append this if there are actually more commands that weren't printed. 

https://github.com/llvm/llvm-project/pull/111891


More information about the lldb-commits mailing list