diff options
author | Vy Nguyen <vyng@google.com> | 2024-10-15 10:14:48 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-10-15 10:14:48 -0400 |
commit | 4d788814061a1003f577e293f2cd74b30223e050 (patch) | |
tree | e30abaf663ebb3c5a2a2e8fc38a8b7151092c936 /lldb/source/Commands/CommandObjectMultiword.cpp | |
parent | 043f066a647334195da41d7f5fd2a8400d7e4c91 (diff) | |
download | llvm-4d788814061a1003f577e293f2cd74b30223e050.zip llvm-4d788814061a1003f577e293f2cd74b30223e050.tar.gz llvm-4d788814061a1003f577e293f2cd74b30223e050.tar.bz2 |
[LLDB]Provide clearer error message for invalid commands. (#111891)
Sometimes users (esp. gdb-longtime users) accidentally use GDB syntax,
such as `breakpoint foo`, and they would get an error message from LLDB
saying simply `Invalid command "breakpoint foo"`, which is not very
helpful.
This change provides additional suggestions to help correcting the
mistake.
Diffstat (limited to 'lldb/source/Commands/CommandObjectMultiword.cpp')
-rw-r--r-- | lldb/source/Commands/CommandObjectMultiword.cpp | 46 |
1 files changed, 36 insertions, 10 deletions
diff --git a/lldb/source/Commands/CommandObjectMultiword.cpp b/lldb/source/Commands/CommandObjectMultiword.cpp index 4efa565..ab3369c 100644 --- a/lldb/source/Commands/CommandObjectMultiword.cpp +++ b/lldb/source/Commands/CommandObjectMultiword.cpp @@ -194,28 +194,54 @@ 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 { + // Try to offer some alternatives to help correct the command. + error_msg.assign( + llvm::Twine("\"" + sub_command + "\" is not a valid subcommand of \"" + + GetCommandName() + "\"." + GetSubcommandsHintText() + + " Use \"help " + GetCommandName() + "\" to find out more.") + .str()); } error_msg.append("\n"); result.AppendRawError(error_msg.c_str()); } +std::string CommandObjectMultiword::GetSubcommandsHintText() { + if (m_subcommand_dict.empty()) + return ""; + const size_t maxCount = 5; + size_t i = 0; + std::string buffer = " Valid subcommand"; + buffer.append(m_subcommand_dict.size() > 1 ? "s are:" : " is"); + CommandMap::iterator pos; + for (pos = m_subcommand_dict.begin(); + pos != m_subcommand_dict.end() && i < maxCount; ++pos, ++i) { + buffer.append(" "); + buffer.append(pos->first); + buffer.append(","); + } + if (i < m_subcommand_dict.size()) + buffer.append(" and others"); + else + buffer.pop_back(); + + buffer.append("."); + return buffer; +} + void CommandObjectMultiword::GenerateHelpText(Stream &output_stream) { // First time through here, generate the help text for the object and push it // to the return result object as well |