1cfca06d7SDimitry Andric //===-- CommandObjectHelp.cpp ---------------------------------------------===//
2f034231aSEd Maste //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f034231aSEd Maste //
7f034231aSEd Maste //===----------------------------------------------------------------------===//
8f034231aSEd Maste
9f3fbd1c0SDimitry Andric #include "CommandObjectHelp.h"
10f034231aSEd Maste #include "lldb/Interpreter/CommandInterpreter.h"
114b4fe385SDimitry Andric #include "lldb/Interpreter/CommandOptionArgumentTable.h"
12f034231aSEd Maste #include "lldb/Interpreter/CommandReturnObject.h"
13f034231aSEd Maste
14f034231aSEd Maste using namespace lldb;
15f034231aSEd Maste using namespace lldb_private;
16f034231aSEd Maste
17f034231aSEd Maste // CommandObjectHelp
18f034231aSEd Maste
GenerateAdditionalHelpAvenuesMessage(Stream * s,llvm::StringRef command,llvm::StringRef prefix,llvm::StringRef subcommand,bool include_upropos,bool include_type_lookup)1914f1b3e8SDimitry Andric void CommandObjectHelp::GenerateAdditionalHelpAvenuesMessage(
205f29bb8aSDimitry Andric Stream *s, llvm::StringRef command, llvm::StringRef prefix,
215f29bb8aSDimitry Andric llvm::StringRef subcommand, bool include_upropos,
225f29bb8aSDimitry Andric bool include_type_lookup) {
2314f1b3e8SDimitry Andric if (!s || command.empty())
2414f1b3e8SDimitry Andric return;
2514f1b3e8SDimitry Andric
2614f1b3e8SDimitry Andric std::string command_str = command.str();
2714f1b3e8SDimitry Andric std::string prefix_str = prefix.str();
2814f1b3e8SDimitry Andric std::string subcommand_str = subcommand.str();
29706b4fc4SDimitry Andric const std::string &lookup_str =
30706b4fc4SDimitry Andric !subcommand_str.empty() ? subcommand_str : command_str;
3114f1b3e8SDimitry Andric s->Printf("'%s' is not a known command.\n", command_str.c_str());
3214f1b3e8SDimitry Andric s->Printf("Try '%shelp' to see a current list of commands.\n",
3314f1b3e8SDimitry Andric prefix.str().c_str());
345f29bb8aSDimitry Andric if (include_upropos) {
35f3fbd1c0SDimitry Andric s->Printf("Try '%sapropos %s' for a list of related commands.\n",
3614f1b3e8SDimitry Andric prefix_str.c_str(), lookup_str.c_str());
37f3fbd1c0SDimitry Andric }
3814f1b3e8SDimitry Andric if (include_type_lookup) {
3914f1b3e8SDimitry Andric s->Printf("Try '%stype lookup %s' for information on types, methods, "
4014f1b3e8SDimitry Andric "functions, modules, etc.",
4114f1b3e8SDimitry Andric prefix_str.c_str(), lookup_str.c_str());
42f3fbd1c0SDimitry Andric }
43f3fbd1c0SDimitry Andric }
44f3fbd1c0SDimitry Andric
CommandObjectHelp(CommandInterpreter & interpreter)45f3fbd1c0SDimitry Andric CommandObjectHelp::CommandObjectHelp(CommandInterpreter &interpreter)
46706b4fc4SDimitry Andric : CommandObjectParsed(interpreter, "help",
47706b4fc4SDimitry Andric "Show a list of all debugger "
4814f1b3e8SDimitry Andric "commands, or give details "
4914f1b3e8SDimitry Andric "about a specific command.",
506f8fc217SDimitry Andric "help [<cmd-name>]") {
51c0981da4SDimitry Andric // A list of command names forming a path to the command we want help on.
52c0981da4SDimitry Andric // No names is allowed - in which case we dump the top-level help.
53ac9a064cSDimitry Andric AddSimpleArgumentList(eArgTypeCommand, eArgRepeatStar);
54f034231aSEd Maste }
55f034231aSEd Maste
56f3fbd1c0SDimitry Andric CommandObjectHelp::~CommandObjectHelp() = default;
57f034231aSEd Maste
585f29bb8aSDimitry Andric #define LLDB_OPTIONS_help
595f29bb8aSDimitry Andric #include "CommandOptions.inc"
60f034231aSEd Maste
6114f1b3e8SDimitry Andric llvm::ArrayRef<OptionDefinition>
GetDefinitions()6214f1b3e8SDimitry Andric CommandObjectHelp::CommandOptions::GetDefinitions() {
63e3b55780SDimitry Andric return llvm::ArrayRef(g_help_options);
6414f1b3e8SDimitry Andric }
6514f1b3e8SDimitry Andric
DoExecute(Args & command,CommandReturnObject & result)66b1c73532SDimitry Andric void CommandObjectHelp::DoExecute(Args &command, CommandReturnObject &result) {
67f034231aSEd Maste CommandObject::CommandMap::iterator pos;
68f034231aSEd Maste CommandObject *cmd_obj;
69f034231aSEd Maste const size_t argc = command.GetArgumentCount();
70f034231aSEd Maste
71f73363f1SDimitry Andric // 'help' doesn't take any arguments, other than command names. If argc is
72f73363f1SDimitry Andric // 0, we show the user all commands (aliases and user commands if asked for).
73f73363f1SDimitry Andric // Otherwise every argument must be the name of a command or a sub-command.
7414f1b3e8SDimitry Andric if (argc == 0) {
75f034231aSEd Maste uint32_t cmd_types = CommandInterpreter::eCommandTypesBuiltin;
76f034231aSEd Maste if (m_options.m_show_aliases)
77f034231aSEd Maste cmd_types |= CommandInterpreter::eCommandTypesAliases;
78c0981da4SDimitry Andric if (m_options.m_show_user_defined) {
79f034231aSEd Maste cmd_types |= CommandInterpreter::eCommandTypesUserDef;
80c0981da4SDimitry Andric cmd_types |= CommandInterpreter::eCommandTypesUserMW;
81c0981da4SDimitry Andric }
8212bd4897SEd Maste if (m_options.m_show_hidden)
8312bd4897SEd Maste cmd_types |= CommandInterpreter::eCommandTypesHidden;
84f034231aSEd Maste
85f034231aSEd Maste result.SetStatus(eReturnStatusSuccessFinishNoResult);
86f034231aSEd Maste m_interpreter.GetHelp(result, cmd_types); // General help
8714f1b3e8SDimitry Andric } else {
8814f1b3e8SDimitry Andric // Get command object for the first command argument. Only search built-in
8914f1b3e8SDimitry Andric // command dictionary.
90f034231aSEd Maste StringList matches;
91ead24645SDimitry Andric auto command_name = command[0].ref();
9214f1b3e8SDimitry Andric cmd_obj = m_interpreter.GetCommandObject(command_name, &matches);
93f034231aSEd Maste
9414f1b3e8SDimitry Andric if (cmd_obj != nullptr) {
95f034231aSEd Maste StringList matches;
96f034231aSEd Maste bool all_okay = true;
97f034231aSEd Maste CommandObject *sub_cmd_obj = cmd_obj;
9814f1b3e8SDimitry Andric // Loop down through sub_command dictionaries until we find the command
9914f1b3e8SDimitry Andric // object that corresponds to the help command entered.
100f3fbd1c0SDimitry Andric std::string sub_command;
10114f1b3e8SDimitry Andric for (auto &entry : command.entries().drop_front()) {
102cfca06d7SDimitry Andric sub_command = std::string(entry.ref());
103f034231aSEd Maste matches.Clear();
104f3fbd1c0SDimitry Andric if (sub_cmd_obj->IsAlias())
10514f1b3e8SDimitry Andric sub_cmd_obj =
10614f1b3e8SDimitry Andric ((CommandAlias *)sub_cmd_obj)->GetUnderlyingCommand().get();
10714f1b3e8SDimitry Andric if (!sub_cmd_obj->IsMultiwordObject()) {
108f034231aSEd Maste all_okay = false;
10914f1b3e8SDimitry Andric break;
11014f1b3e8SDimitry Andric } else {
111f034231aSEd Maste CommandObject *found_cmd;
11214f1b3e8SDimitry Andric found_cmd =
11314f1b3e8SDimitry Andric sub_cmd_obj->GetSubcommandObject(sub_command.c_str(), &matches);
11414f1b3e8SDimitry Andric if (found_cmd == nullptr || matches.GetSize() > 1) {
115f034231aSEd Maste all_okay = false;
11614f1b3e8SDimitry Andric break;
11714f1b3e8SDimitry Andric } else
118f034231aSEd Maste sub_cmd_obj = found_cmd;
119f034231aSEd Maste }
120f034231aSEd Maste }
121f034231aSEd Maste
12214f1b3e8SDimitry Andric if (!all_okay || (sub_cmd_obj == nullptr)) {
123f034231aSEd Maste std::string cmd_string;
124f034231aSEd Maste command.GetCommandString(cmd_string);
12514f1b3e8SDimitry Andric if (matches.GetSize() >= 2) {
126f034231aSEd Maste StreamString s;
127f034231aSEd Maste s.Printf("ambiguous command %s", cmd_string.c_str());
128f034231aSEd Maste size_t num_matches = matches.GetSize();
12914f1b3e8SDimitry Andric for (size_t match_idx = 0; match_idx < num_matches; match_idx++) {
130f034231aSEd Maste s.Printf("\n\t%s", matches.GetStringAtIndex(match_idx));
131f034231aSEd Maste }
132f034231aSEd Maste s.Printf("\n");
13314f1b3e8SDimitry Andric result.AppendError(s.GetString());
134b1c73532SDimitry Andric return;
13514f1b3e8SDimitry Andric } else if (!sub_cmd_obj) {
136f3fbd1c0SDimitry Andric StreamString error_msg_stream;
13714f1b3e8SDimitry Andric GenerateAdditionalHelpAvenuesMessage(
13814f1b3e8SDimitry Andric &error_msg_stream, cmd_string.c_str(),
13914f1b3e8SDimitry Andric m_interpreter.GetCommandPrefix(), sub_command.c_str());
14014f1b3e8SDimitry Andric result.AppendError(error_msg_stream.GetString());
141b1c73532SDimitry Andric return;
14214f1b3e8SDimitry Andric } else {
14314f1b3e8SDimitry Andric GenerateAdditionalHelpAvenuesMessage(
14414f1b3e8SDimitry Andric &result.GetOutputStream(), cmd_string.c_str(),
14514f1b3e8SDimitry Andric m_interpreter.GetCommandPrefix(), sub_command.c_str());
14614f1b3e8SDimitry Andric result.GetOutputStream().Printf(
14714f1b3e8SDimitry Andric "\nThe closest match is '%s'. Help on it follows.\n\n",
14814f1b3e8SDimitry Andric sub_cmd_obj->GetCommandName().str().c_str());
149f034231aSEd Maste }
150f034231aSEd Maste }
151f034231aSEd Maste
152f034231aSEd Maste sub_cmd_obj->GenerateHelpText(result);
15394994d37SDimitry Andric std::string alias_full_name;
15494994d37SDimitry Andric // Don't use AliasExists here, that only checks exact name matches. If
15594994d37SDimitry Andric // the user typed a shorter unique alias name, we should still tell them
15694994d37SDimitry Andric // it was an alias.
15794994d37SDimitry Andric if (m_interpreter.GetAliasFullName(command_name, alias_full_name)) {
158f034231aSEd Maste StreamString sstr;
15994994d37SDimitry Andric m_interpreter.GetAlias(alias_full_name)->GetAliasExpansion(sstr);
16014f1b3e8SDimitry Andric result.GetOutputStream().Printf("\n'%s' is an abbreviation for %s\n",
16114f1b3e8SDimitry Andric command[0].c_str(), sstr.GetData());
162f034231aSEd Maste }
16314f1b3e8SDimitry Andric } else if (matches.GetSize() > 0) {
164f034231aSEd Maste Stream &output_strm = result.GetOutputStream();
16514f1b3e8SDimitry Andric output_strm.Printf("Help requested with ambiguous command name, possible "
16614f1b3e8SDimitry Andric "completions:\n");
167f034231aSEd Maste const size_t match_count = matches.GetSize();
16814f1b3e8SDimitry Andric for (size_t i = 0; i < match_count; i++) {
169f034231aSEd Maste output_strm.Printf("\t%s\n", matches.GetStringAtIndex(i));
170f034231aSEd Maste }
17114f1b3e8SDimitry Andric } else {
17214f1b3e8SDimitry Andric // Maybe the user is asking for help about a command argument rather than
17314f1b3e8SDimitry Andric // a command.
17414f1b3e8SDimitry Andric const CommandArgumentType arg_type =
17514f1b3e8SDimitry Andric CommandObject::LookupArgumentName(command_name);
17614f1b3e8SDimitry Andric if (arg_type != eArgTypeLastArg) {
177f034231aSEd Maste Stream &output_strm = result.GetOutputStream();
178f034231aSEd Maste CommandObject::GetArgumentHelp(output_strm, arg_type, m_interpreter);
179f034231aSEd Maste result.SetStatus(eReturnStatusSuccessFinishNoResult);
18014f1b3e8SDimitry Andric } else {
181f3fbd1c0SDimitry Andric StreamString error_msg_stream;
18214f1b3e8SDimitry Andric GenerateAdditionalHelpAvenuesMessage(&error_msg_stream, command_name,
18314f1b3e8SDimitry Andric m_interpreter.GetCommandPrefix(),
18414f1b3e8SDimitry Andric "");
18514f1b3e8SDimitry Andric result.AppendError(error_msg_stream.GetString());
186f034231aSEd Maste }
187f034231aSEd Maste }
188f034231aSEd Maste }
189f034231aSEd Maste }
190f034231aSEd Maste
HandleCompletion(CompletionRequest & request)191ead24645SDimitry Andric void CommandObjectHelp::HandleCompletion(CompletionRequest &request) {
192f034231aSEd Maste // Return the completions of the commands in the help system:
193f73363f1SDimitry Andric if (request.GetCursorIndex() == 0) {
194ead24645SDimitry Andric m_interpreter.HandleCompletionMatches(request);
195ead24645SDimitry Andric return;
196ead24645SDimitry Andric }
197f73363f1SDimitry Andric CommandObject *cmd_obj =
198ead24645SDimitry Andric m_interpreter.GetCommandObject(request.GetParsedLine()[0].ref());
199f034231aSEd Maste
20014f1b3e8SDimitry Andric // The command that they are getting help on might be ambiguous, in which
201f73363f1SDimitry Andric // case we should complete that, otherwise complete with the command the
202f73363f1SDimitry Andric // user is getting help on...
203f034231aSEd Maste
20414f1b3e8SDimitry Andric if (cmd_obj) {
205ead24645SDimitry Andric request.ShiftArguments();
206ead24645SDimitry Andric cmd_obj->HandleCompletion(request);
207ead24645SDimitry Andric return;
208f034231aSEd Maste }
209ead24645SDimitry Andric m_interpreter.HandleCompletionMatches(request);
210f034231aSEd Maste }
211