blob: 4e663c797ebb8d1e3cbedc6f1c67d6dccc922903 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- CommandObjectHelp.cpp -----------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006//
7//===----------------------------------------------------------------------===//
8
Eugene Zelenko26cac3a2016-02-20 00:58:29 +00009#include "CommandObjectHelp.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000010#include "lldb/Interpreter/CommandInterpreter.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000011#include "lldb/Interpreter/CommandObjectMultiword.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000012#include "lldb/Interpreter/CommandReturnObject.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000013#include "lldb/Interpreter/Options.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000014
15using namespace lldb;
16using namespace lldb_private;
17
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018// CommandObjectHelp
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019
Kate Stoneb9c1b512016-09-06 20:57:50 +000020void CommandObjectHelp::GenerateAdditionalHelpAvenuesMessage(
Jonas Devlieghered5b44032019-02-13 06:25:41 +000021 Stream *s, llvm::StringRef command, llvm::StringRef prefix,
22 llvm::StringRef subcommand, bool include_upropos,
23 bool include_type_lookup) {
Zachary Turnera49c2012016-11-16 21:34:22 +000024 if (!s || command.empty())
25 return;
26
27 std::string command_str = command.str();
28 std::string prefix_str = prefix.str();
29 std::string subcommand_str = subcommand.str();
30 const std::string &lookup_str = !subcommand_str.empty() ? subcommand_str : command_str;
31 s->Printf("'%s' is not a known command.\n", command_str.c_str());
32 s->Printf("Try '%shelp' to see a current list of commands.\n",
33 prefix.str().c_str());
Jonas Devlieghered5b44032019-02-13 06:25:41 +000034 if (include_upropos) {
Zachary Turnera49c2012016-11-16 21:34:22 +000035 s->Printf("Try '%sapropos %s' for a list of related commands.\n",
36 prefix_str.c_str(), lookup_str.c_str());
37 }
38 if (include_type_lookup) {
39 s->Printf("Try '%stype lookup %s' for information on types, methods, "
40 "functions, modules, etc.",
41 prefix_str.c_str(), lookup_str.c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +000042 }
Enrico Granata46d4aa22016-02-29 23:22:53 +000043}
44
Kate Stone7428a182016-07-14 22:03:10 +000045CommandObjectHelp::CommandObjectHelp(CommandInterpreter &interpreter)
Kate Stoneb9c1b512016-09-06 20:57:50 +000046 : CommandObjectParsed(interpreter, "help", "Show a list of all debugger "
47 "commands, or give details "
48 "about a specific command.",
Kate Stone7428a182016-07-14 22:03:10 +000049 "help [<cmd-name>]"),
Kate Stoneb9c1b512016-09-06 20:57:50 +000050 m_options() {
51 CommandArgumentEntry arg;
52 CommandArgumentData command_arg;
Caroline Tice405fe672010-10-04 22:28:36 +000053
Kate Stoneb9c1b512016-09-06 20:57:50 +000054 // Define the first (and only) variant of this arg.
55 command_arg.arg_type = eArgTypeCommandName;
56 command_arg.arg_repetition = eArgRepeatStar;
Caroline Tice405fe672010-10-04 22:28:36 +000057
Kate Stoneb9c1b512016-09-06 20:57:50 +000058 // There is only one variant this argument could be; put it into the argument
59 // entry.
60 arg.push_back(command_arg);
Caroline Tice405fe672010-10-04 22:28:36 +000061
Kate Stoneb9c1b512016-09-06 20:57:50 +000062 // Push the data for the first argument into the m_arguments vector.
63 m_arguments.push_back(arg);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000064}
65
Eugene Zelenko26cac3a2016-02-20 00:58:29 +000066CommandObjectHelp::~CommandObjectHelp() = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000067
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +000068static constexpr OptionDefinition g_help_options[] = {
Kate Stoneb9c1b512016-09-06 20:57:50 +000069 // clang-format off
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +000070 {LLDB_OPT_SET_ALL, false, "hide-aliases", 'a', OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone, "Hide aliases in the command list."},
71 {LLDB_OPT_SET_ALL, false, "hide-user-commands", 'u', OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone, "Hide user-defined commands from the list."},
72 {LLDB_OPT_SET_ALL, false, "show-hidden-commands", 'h', OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone, "Include commands prefixed with an underscore."},
Kate Stoneb9c1b512016-09-06 20:57:50 +000073 // clang-format on
Enrico Granata08633ee2011-09-09 17:49:36 +000074};
75
Zachary Turner1f0f5b52016-09-22 20:22:55 +000076llvm::ArrayRef<OptionDefinition>
77CommandObjectHelp::CommandOptions::GetDefinitions() {
Zachary Turner70602432016-09-22 21:06:13 +000078 return llvm::makeArrayRef(g_help_options);
Zachary Turner1f0f5b52016-09-22 20:22:55 +000079}
80
Kate Stoneb9c1b512016-09-06 20:57:50 +000081bool CommandObjectHelp::DoExecute(Args &command, CommandReturnObject &result) {
82 CommandObject::CommandMap::iterator pos;
83 CommandObject *cmd_obj;
84 const size_t argc = command.GetArgumentCount();
Enrico Granata08633ee2011-09-09 17:49:36 +000085
Adrian Prantl05097242018-04-30 16:49:04 +000086 // 'help' doesn't take any arguments, other than command names. If argc is
87 // 0, we show the user all commands (aliases and user commands if asked for).
88 // Otherwise every argument must be the name of a command or a sub-command.
Kate Stoneb9c1b512016-09-06 20:57:50 +000089 if (argc == 0) {
90 uint32_t cmd_types = CommandInterpreter::eCommandTypesBuiltin;
91 if (m_options.m_show_aliases)
92 cmd_types |= CommandInterpreter::eCommandTypesAliases;
93 if (m_options.m_show_user_defined)
94 cmd_types |= CommandInterpreter::eCommandTypesUserDef;
95 if (m_options.m_show_hidden)
96 cmd_types |= CommandInterpreter::eCommandTypesHidden;
97
98 result.SetStatus(eReturnStatusSuccessFinishNoResult);
99 m_interpreter.GetHelp(result, cmd_types); // General help
100 } else {
101 // Get command object for the first command argument. Only search built-in
102 // command dictionary.
103 StringList matches;
Zachary Turner14f6b2c2016-12-09 01:08:29 +0000104 auto command_name = command[0].ref;
105 cmd_obj = m_interpreter.GetCommandObject(command_name, &matches);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000106
107 if (cmd_obj != nullptr) {
108 StringList matches;
109 bool all_okay = true;
110 CommandObject *sub_cmd_obj = cmd_obj;
111 // Loop down through sub_command dictionaries until we find the command
Zachary Turner97d2c402016-10-05 23:40:23 +0000112 // object that corresponds to the help command entered.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000113 std::string sub_command;
Zachary Turner97d2c402016-10-05 23:40:23 +0000114 for (auto &entry : command.entries().drop_front()) {
115 sub_command = entry.ref;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000116 matches.Clear();
117 if (sub_cmd_obj->IsAlias())
118 sub_cmd_obj =
119 ((CommandAlias *)sub_cmd_obj)->GetUnderlyingCommand().get();
120 if (!sub_cmd_obj->IsMultiwordObject()) {
121 all_okay = false;
Zachary Turner97d2c402016-10-05 23:40:23 +0000122 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000123 } else {
124 CommandObject *found_cmd;
125 found_cmd =
126 sub_cmd_obj->GetSubcommandObject(sub_command.c_str(), &matches);
Zachary Turner97d2c402016-10-05 23:40:23 +0000127 if (found_cmd == nullptr || matches.GetSize() > 1) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000128 all_okay = false;
Zachary Turner97d2c402016-10-05 23:40:23 +0000129 break;
130 } else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000131 sub_cmd_obj = found_cmd;
132 }
133 }
134
135 if (!all_okay || (sub_cmd_obj == nullptr)) {
136 std::string cmd_string;
137 command.GetCommandString(cmd_string);
138 if (matches.GetSize() >= 2) {
139 StreamString s;
140 s.Printf("ambiguous command %s", cmd_string.c_str());
141 size_t num_matches = matches.GetSize();
142 for (size_t match_idx = 0; match_idx < num_matches; match_idx++) {
143 s.Printf("\n\t%s", matches.GetStringAtIndex(match_idx));
144 }
145 s.Printf("\n");
Zachary Turnerc1564272016-11-16 21:15:24 +0000146 result.AppendError(s.GetString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000147 result.SetStatus(eReturnStatusFailed);
148 return false;
149 } else if (!sub_cmd_obj) {
150 StreamString error_msg_stream;
151 GenerateAdditionalHelpAvenuesMessage(
152 &error_msg_stream, cmd_string.c_str(),
153 m_interpreter.GetCommandPrefix(), sub_command.c_str());
Zachary Turnerc1564272016-11-16 21:15:24 +0000154 result.AppendError(error_msg_stream.GetString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000155 result.SetStatus(eReturnStatusFailed);
156 return false;
157 } else {
158 GenerateAdditionalHelpAvenuesMessage(
159 &result.GetOutputStream(), cmd_string.c_str(),
160 m_interpreter.GetCommandPrefix(), sub_command.c_str());
161 result.GetOutputStream().Printf(
162 "\nThe closest match is '%s'. Help on it follows.\n\n",
Zachary Turnera4496982016-10-05 21:14:38 +0000163 sub_cmd_obj->GetCommandName().str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000164 }
165 }
166
167 sub_cmd_obj->GenerateHelpText(result);
Jim Ingham79d81052018-12-21 01:45:28 +0000168 std::string alias_full_name;
169 // Don't use AliasExists here, that only checks exact name matches. If
170 // the user typed a shorter unique alias name, we should still tell them
171 // it was an alias.
172 if (m_interpreter.GetAliasFullName(command_name, alias_full_name)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000173 StreamString sstr;
Jim Ingham79d81052018-12-21 01:45:28 +0000174 m_interpreter.GetAlias(alias_full_name)->GetAliasExpansion(sstr);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000175 result.GetOutputStream().Printf("\n'%s' is an abbreviation for %s\n",
Zachary Turner14f6b2c2016-12-09 01:08:29 +0000176 command[0].c_str(), sstr.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000177 }
178 } else if (matches.GetSize() > 0) {
179 Stream &output_strm = result.GetOutputStream();
180 output_strm.Printf("Help requested with ambiguous command name, possible "
181 "completions:\n");
182 const size_t match_count = matches.GetSize();
183 for (size_t i = 0; i < match_count; i++) {
184 output_strm.Printf("\t%s\n", matches.GetStringAtIndex(i));
185 }
186 } else {
187 // Maybe the user is asking for help about a command argument rather than
188 // a command.
189 const CommandArgumentType arg_type =
Zachary Turner14f6b2c2016-12-09 01:08:29 +0000190 CommandObject::LookupArgumentName(command_name);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000191 if (arg_type != eArgTypeLastArg) {
192 Stream &output_strm = result.GetOutputStream();
193 CommandObject::GetArgumentHelp(output_strm, arg_type, m_interpreter);
194 result.SetStatus(eReturnStatusSuccessFinishNoResult);
195 } else {
196 StreamString error_msg_stream;
Zachary Turner14f6b2c2016-12-09 01:08:29 +0000197 GenerateAdditionalHelpAvenuesMessage(&error_msg_stream, command_name,
198 m_interpreter.GetCommandPrefix(),
199 "");
Zachary Turnerc1564272016-11-16 21:15:24 +0000200 result.AppendError(error_msg_stream.GetString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000201 result.SetStatus(eReturnStatusFailed);
202 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000203 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000204 }
205
206 return result.Succeeded();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000207}
208
Raphael Isemann2443bbd2018-07-02 21:29:56 +0000209int CommandObjectHelp::HandleCompletion(CompletionRequest &request) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000210 // Return the completions of the commands in the help system:
Raphael Isemann2443bbd2018-07-02 21:29:56 +0000211 if (request.GetCursorIndex() == 0) {
212 return m_interpreter.HandleCompletionMatches(request);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000213 } else {
Raphael Isemann2443bbd2018-07-02 21:29:56 +0000214 CommandObject *cmd_obj =
215 m_interpreter.GetCommandObject(request.GetParsedLine()[0].ref);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000216
217 // The command that they are getting help on might be ambiguous, in which
Adrian Prantl05097242018-04-30 16:49:04 +0000218 // case we should complete that, otherwise complete with the command the
219 // user is getting help on...
Kate Stoneb9c1b512016-09-06 20:57:50 +0000220
221 if (cmd_obj) {
Raphael Isemann2443bbd2018-07-02 21:29:56 +0000222 request.GetParsedLine().Shift();
223 request.SetCursorIndex(request.GetCursorIndex() - 1);
224 return cmd_obj->HandleCompletion(request);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000225 } else {
Raphael Isemann2443bbd2018-07-02 21:29:56 +0000226 return m_interpreter.HandleCompletionMatches(request);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000227 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000228 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000229}