blob: 4a8f01cc7528fad97611ce7e19e3548b9f22912d [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- CommandObjectHelp.cpp -----------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Chris Lattner30fdc8d2010-06-08 16:52:24 +000010// C Includes
11// C++ Includes
12// Other libraries and framework includes
13// Project includes
Eugene Zelenko26cac3a2016-02-20 00:58:29 +000014#include "CommandObjectHelp.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000015#include "lldb/Interpreter/CommandInterpreter.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000016#include "lldb/Interpreter/CommandObjectMultiword.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017#include "lldb/Interpreter/CommandReturnObject.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000018#include "lldb/Interpreter/Options.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019
20using namespace lldb;
21using namespace lldb_private;
22
23//-------------------------------------------------------------------------
24// CommandObjectHelp
25//-------------------------------------------------------------------------
26
Kate Stoneb9c1b512016-09-06 20:57:50 +000027void CommandObjectHelp::GenerateAdditionalHelpAvenuesMessage(
28 Stream *s, const char *command, const char *prefix, const char *subcommand,
29 bool include_apropos, bool include_type_lookup) {
30 if (s && command && *command) {
31 s->Printf("'%s' is not a known command.\n", command);
32 s->Printf("Try '%shelp' to see a current list of commands.\n",
33 prefix ? prefix : "");
34 if (include_apropos) {
35 s->Printf("Try '%sapropos %s' for a list of related commands.\n",
36 prefix ? prefix : "", subcommand ? subcommand : command);
Enrico Granata46d4aa22016-02-29 23:22:53 +000037 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000038 if (include_type_lookup) {
39 s->Printf("Try '%stype lookup %s' for information on types, methods, "
40 "functions, modules, etc.",
41 prefix ? prefix : "", subcommand ? subcommand : command);
42 }
43 }
Enrico Granata46d4aa22016-02-29 23:22:53 +000044}
45
Kate Stone7428a182016-07-14 22:03:10 +000046CommandObjectHelp::CommandObjectHelp(CommandInterpreter &interpreter)
Kate Stoneb9c1b512016-09-06 20:57:50 +000047 : CommandObjectParsed(interpreter, "help", "Show a list of all debugger "
48 "commands, or give details "
49 "about a specific command.",
Kate Stone7428a182016-07-14 22:03:10 +000050 "help [<cmd-name>]"),
Kate Stoneb9c1b512016-09-06 20:57:50 +000051 m_options() {
52 CommandArgumentEntry arg;
53 CommandArgumentData command_arg;
Caroline Tice405fe672010-10-04 22:28:36 +000054
Kate Stoneb9c1b512016-09-06 20:57:50 +000055 // Define the first (and only) variant of this arg.
56 command_arg.arg_type = eArgTypeCommandName;
57 command_arg.arg_repetition = eArgRepeatStar;
Caroline Tice405fe672010-10-04 22:28:36 +000058
Kate Stoneb9c1b512016-09-06 20:57:50 +000059 // There is only one variant this argument could be; put it into the argument
60 // entry.
61 arg.push_back(command_arg);
Caroline Tice405fe672010-10-04 22:28:36 +000062
Kate Stoneb9c1b512016-09-06 20:57:50 +000063 // Push the data for the first argument into the m_arguments vector.
64 m_arguments.push_back(arg);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000065}
66
Eugene Zelenko26cac3a2016-02-20 00:58:29 +000067CommandObjectHelp::~CommandObjectHelp() = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000068
Zachary Turner1f0f5b52016-09-22 20:22:55 +000069static OptionDefinition g_help_options[] = {
Kate Stoneb9c1b512016-09-06 20:57:50 +000070 // clang-format off
Kate Stoneac9c3a62016-08-26 23:28:47 +000071 {LLDB_OPT_SET_ALL, false, "hide-aliases", 'a', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Hide aliases in the command list."},
72 {LLDB_OPT_SET_ALL, false, "hide-user-commands", 'u', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Hide user-defined commands from the list."},
73 {LLDB_OPT_SET_ALL, false, "show-hidden-commands", 'h', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Include commands prefixed with an underscore."},
Kate Stoneb9c1b512016-09-06 20:57:50 +000074 // clang-format on
Enrico Granata08633ee2011-09-09 17:49:36 +000075};
76
Zachary Turner1f0f5b52016-09-22 20:22:55 +000077llvm::ArrayRef<OptionDefinition>
78CommandObjectHelp::CommandOptions::GetDefinitions() {
Zachary Turner70602432016-09-22 21:06:13 +000079 return llvm::makeArrayRef(g_help_options);
Zachary Turner1f0f5b52016-09-22 20:22:55 +000080}
81
Kate Stoneb9c1b512016-09-06 20:57:50 +000082bool CommandObjectHelp::DoExecute(Args &command, CommandReturnObject &result) {
83 CommandObject::CommandMap::iterator pos;
84 CommandObject *cmd_obj;
85 const size_t argc = command.GetArgumentCount();
Enrico Granata08633ee2011-09-09 17:49:36 +000086
Kate Stoneb9c1b512016-09-06 20:57:50 +000087 // 'help' doesn't take any arguments, other than command names. If argc is 0,
88 // we show the user
89 // all commands (aliases and user commands if asked for). Otherwise every
90 // argument must be the name of a command or a sub-command.
91 if (argc == 0) {
92 uint32_t cmd_types = CommandInterpreter::eCommandTypesBuiltin;
93 if (m_options.m_show_aliases)
94 cmd_types |= CommandInterpreter::eCommandTypesAliases;
95 if (m_options.m_show_user_defined)
96 cmd_types |= CommandInterpreter::eCommandTypesUserDef;
97 if (m_options.m_show_hidden)
98 cmd_types |= CommandInterpreter::eCommandTypesHidden;
99
100 result.SetStatus(eReturnStatusSuccessFinishNoResult);
101 m_interpreter.GetHelp(result, cmd_types); // General help
102 } else {
103 // Get command object for the first command argument. Only search built-in
104 // command dictionary.
105 StringList matches;
106 cmd_obj =
107 m_interpreter.GetCommandObject(command.GetArgumentAtIndex(0), &matches);
108 bool is_alias_command =
109 m_interpreter.AliasExists(command.GetArgumentAtIndex(0));
110 std::string alias_name = command.GetArgumentAtIndex(0);
111
112 if (cmd_obj != nullptr) {
113 StringList matches;
114 bool all_okay = true;
115 CommandObject *sub_cmd_obj = cmd_obj;
116 // Loop down through sub_command dictionaries until we find the command
Zachary Turner97d2c402016-10-05 23:40:23 +0000117 // object that corresponds to the help command entered.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000118 std::string sub_command;
Zachary Turner97d2c402016-10-05 23:40:23 +0000119 for (auto &entry : command.entries().drop_front()) {
120 sub_command = entry.ref;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000121 matches.Clear();
122 if (sub_cmd_obj->IsAlias())
123 sub_cmd_obj =
124 ((CommandAlias *)sub_cmd_obj)->GetUnderlyingCommand().get();
125 if (!sub_cmd_obj->IsMultiwordObject()) {
126 all_okay = false;
Zachary Turner97d2c402016-10-05 23:40:23 +0000127 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000128 } else {
129 CommandObject *found_cmd;
130 found_cmd =
131 sub_cmd_obj->GetSubcommandObject(sub_command.c_str(), &matches);
Zachary Turner97d2c402016-10-05 23:40:23 +0000132 if (found_cmd == nullptr || matches.GetSize() > 1) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000133 all_okay = false;
Zachary Turner97d2c402016-10-05 23:40:23 +0000134 break;
135 } else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000136 sub_cmd_obj = found_cmd;
137 }
138 }
139
140 if (!all_okay || (sub_cmd_obj == nullptr)) {
141 std::string cmd_string;
142 command.GetCommandString(cmd_string);
143 if (matches.GetSize() >= 2) {
144 StreamString s;
145 s.Printf("ambiguous command %s", cmd_string.c_str());
146 size_t num_matches = matches.GetSize();
147 for (size_t match_idx = 0; match_idx < num_matches; match_idx++) {
148 s.Printf("\n\t%s", matches.GetStringAtIndex(match_idx));
149 }
150 s.Printf("\n");
Zachary Turnerc1564272016-11-16 21:15:24 +0000151 result.AppendError(s.GetString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000152 result.SetStatus(eReturnStatusFailed);
153 return false;
154 } else if (!sub_cmd_obj) {
155 StreamString error_msg_stream;
156 GenerateAdditionalHelpAvenuesMessage(
157 &error_msg_stream, cmd_string.c_str(),
158 m_interpreter.GetCommandPrefix(), sub_command.c_str());
Zachary Turnerc1564272016-11-16 21:15:24 +0000159 result.AppendError(error_msg_stream.GetString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000160 result.SetStatus(eReturnStatusFailed);
161 return false;
162 } else {
163 GenerateAdditionalHelpAvenuesMessage(
164 &result.GetOutputStream(), cmd_string.c_str(),
165 m_interpreter.GetCommandPrefix(), sub_command.c_str());
166 result.GetOutputStream().Printf(
167 "\nThe closest match is '%s'. Help on it follows.\n\n",
Zachary Turnera4496982016-10-05 21:14:38 +0000168 sub_cmd_obj->GetCommandName().str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000169 }
170 }
171
172 sub_cmd_obj->GenerateHelpText(result);
173
174 if (is_alias_command) {
175 StreamString sstr;
Zachary Turnera483f572016-10-05 21:14:49 +0000176 m_interpreter.GetAlias(alias_name)->GetAliasExpansion(sstr);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000177 result.GetOutputStream().Printf("\n'%s' is an abbreviation for %s\n",
178 alias_name.c_str(), sstr.GetData());
179 }
180 } else if (matches.GetSize() > 0) {
181 Stream &output_strm = result.GetOutputStream();
182 output_strm.Printf("Help requested with ambiguous command name, possible "
183 "completions:\n");
184 const size_t match_count = matches.GetSize();
185 for (size_t i = 0; i < match_count; i++) {
186 output_strm.Printf("\t%s\n", matches.GetStringAtIndex(i));
187 }
188 } else {
189 // Maybe the user is asking for help about a command argument rather than
190 // a command.
191 const CommandArgumentType arg_type =
192 CommandObject::LookupArgumentName(command.GetArgumentAtIndex(0));
193 if (arg_type != eArgTypeLastArg) {
194 Stream &output_strm = result.GetOutputStream();
195 CommandObject::GetArgumentHelp(output_strm, arg_type, m_interpreter);
196 result.SetStatus(eReturnStatusSuccessFinishNoResult);
197 } else {
198 StreamString error_msg_stream;
199 GenerateAdditionalHelpAvenuesMessage(&error_msg_stream,
200 command.GetArgumentAtIndex(0),
201 m_interpreter.GetCommandPrefix());
Zachary Turnerc1564272016-11-16 21:15:24 +0000202 result.AppendError(error_msg_stream.GetString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000203 result.SetStatus(eReturnStatusFailed);
204 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000205 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000206 }
207
208 return result.Succeeded();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000209}
210
Kate Stoneb9c1b512016-09-06 20:57:50 +0000211int CommandObjectHelp::HandleCompletion(Args &input, int &cursor_index,
212 int &cursor_char_position,
213 int match_start_point,
214 int max_return_elements,
215 bool &word_complete,
216 StringList &matches) {
217 // Return the completions of the commands in the help system:
218 if (cursor_index == 0) {
219 return m_interpreter.HandleCompletionMatches(
220 input, cursor_index, cursor_char_position, match_start_point,
221 max_return_elements, word_complete, matches);
222 } else {
223 CommandObject *cmd_obj =
224 m_interpreter.GetCommandObject(input.GetArgumentAtIndex(0));
225
226 // The command that they are getting help on might be ambiguous, in which
227 // case we should complete that,
228 // otherwise complete with the command the user is getting help on...
229
230 if (cmd_obj) {
231 input.Shift();
232 cursor_index--;
233 return cmd_obj->HandleCompletion(
234 input, cursor_index, cursor_char_position, match_start_point,
235 max_return_elements, word_complete, matches);
236 } else {
237 return m_interpreter.HandleCompletionMatches(
238 input, cursor_index, cursor_char_position, match_start_point,
239 max_return_elements, word_complete, matches);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000240 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000241 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000242}