blob: 3879005af3e858304cc0aa78163652926040d32e [file] [log] [blame]
Chris Lattner24943d22010-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
10#include "CommandObjectHelp.h"
11
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
16#include "lldb/Interpreter/CommandObjectMultiword.h"
17#include "lldb/Interpreter/CommandInterpreter.h"
Jim Ingham84cdc152010-06-15 19:49:27 +000018#include "lldb/Interpreter/Options.h"
Chris Lattner24943d22010-06-08 16:52:24 +000019#include "lldb/Interpreter/CommandReturnObject.h"
20
21using namespace lldb;
22using namespace lldb_private;
23
24//-------------------------------------------------------------------------
25// CommandObjectHelp
26//-------------------------------------------------------------------------
27
Greg Clayton238c0a12010-09-18 01:14:36 +000028CommandObjectHelp::CommandObjectHelp (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +000029 CommandObjectParsed (interpreter,
30 "help",
31 "Show a list of all debugger commands, or give details about specific commands.",
32 "help [<cmd-name>]"), m_options (interpreter)
Chris Lattner24943d22010-06-08 16:52:24 +000033{
Caroline Tice43b014a2010-10-04 22:28:36 +000034 CommandArgumentEntry arg;
35 CommandArgumentData command_arg;
36
37 // Define the first (and only) variant of this arg.
38 command_arg.arg_type = eArgTypeCommandName;
39 command_arg.arg_repetition = eArgRepeatStar;
40
41 // There is only one variant this argument could be; put it into the argument entry.
42 arg.push_back (command_arg);
43
44 // Push the data for the first argument into the m_arguments vector.
45 m_arguments.push_back (arg);
Chris Lattner24943d22010-06-08 16:52:24 +000046}
47
48CommandObjectHelp::~CommandObjectHelp()
49{
50}
51
Enrico Granata1ac6d1f2011-09-09 17:49:36 +000052OptionDefinition
53CommandObjectHelp::CommandOptions::g_option_table[] =
54{
55 { LLDB_OPT_SET_ALL, false, "show-aliases", 'a', no_argument, NULL, 0, eArgTypeNone, "Show aliases in the command list."},
56 { LLDB_OPT_SET_ALL, false, "hide-user-commands", 'u', no_argument, NULL, 0, eArgTypeNone, "Hide user-defined commands from the list."},
57 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
58};
59
Chris Lattner24943d22010-06-08 16:52:24 +000060bool
Jim Inghamda26bd22012-06-08 21:56:10 +000061CommandObjectHelp::DoExecute (Args& command, CommandReturnObject &result)
Chris Lattner24943d22010-06-08 16:52:24 +000062{
63 CommandObject::CommandMap::iterator pos;
64 CommandObject *cmd_obj;
65 const int argc = command.GetArgumentCount ();
Greg Clayton63094e02010-06-23 01:19:29 +000066
Enrico Granata1ac6d1f2011-09-09 17:49:36 +000067 // 'help' doesn't take any arguments, other than command names. If argc is 0, we show the user
68 // all commands (aliases and user commands if asked for). Otherwise every argument must be the name of a command or a sub-command.
Chris Lattner24943d22010-06-08 16:52:24 +000069 if (argc == 0)
70 {
Enrico Granata1ac6d1f2011-09-09 17:49:36 +000071 uint32_t cmd_types = CommandInterpreter::eCommandTypesBuiltin;
72 if (m_options.m_show_aliases)
73 cmd_types |= CommandInterpreter::eCommandTypesAliases;
74 if (m_options.m_show_user_defined)
75 cmd_types |= CommandInterpreter::eCommandTypesUserDef;
76
Chris Lattner24943d22010-06-08 16:52:24 +000077 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Enrico Granata1ac6d1f2011-09-09 17:49:36 +000078 m_interpreter.GetHelp (result, cmd_types); // General help
Chris Lattner24943d22010-06-08 16:52:24 +000079 }
80 else
81 {
82 // Get command object for the first command argument. Only search built-in command dictionary.
Jim Inghamd40f8a62010-07-06 22:46:59 +000083 StringList matches;
Greg Clayton238c0a12010-09-18 01:14:36 +000084 cmd_obj = m_interpreter.GetCommandObject (command.GetArgumentAtIndex (0), &matches);
Caroline Ticee6866a32010-10-28 23:17:48 +000085 bool is_alias_command = m_interpreter.AliasExists (command.GetArgumentAtIndex (0));
86 std::string alias_name = command.GetArgumentAtIndex(0);
Greg Clayton63094e02010-06-23 01:19:29 +000087
Chris Lattner24943d22010-06-08 16:52:24 +000088 if (cmd_obj != NULL)
89 {
Jim Inghamc2d382c2010-11-30 22:59:37 +000090 StringList matches;
Chris Lattner24943d22010-06-08 16:52:24 +000091 bool all_okay = true;
92 CommandObject *sub_cmd_obj = cmd_obj;
93 // Loop down through sub_command dictionaries until we find the command object that corresponds
94 // to the help command entered.
95 for (int i = 1; i < argc && all_okay; ++i)
96 {
97 std::string sub_command = command.GetArgumentAtIndex(i);
Jim Inghamc2d382c2010-11-30 22:59:37 +000098 matches.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +000099 if (! sub_cmd_obj->IsMultiwordObject ())
100 {
101 all_okay = false;
102 }
103 else
104 {
Jim Inghamc2d382c2010-11-30 22:59:37 +0000105 CommandObject *found_cmd;
Greg Clayton13193d52012-10-13 02:07:45 +0000106 found_cmd = sub_cmd_obj->GetSubcommandObject(sub_command.c_str(), &matches);
Jim Inghamc2d382c2010-11-30 22:59:37 +0000107 if (found_cmd == NULL)
Greg Clayton63094e02010-06-23 01:19:29 +0000108 all_okay = false;
Jim Inghamdd1f02a2010-12-01 00:42:17 +0000109 else if (matches.GetSize() > 1)
Jim Inghamc2d382c2010-11-30 22:59:37 +0000110 all_okay = false;
111 else
112 sub_cmd_obj = found_cmd;
Chris Lattner24943d22010-06-08 16:52:24 +0000113 }
114 }
Greg Clayton63094e02010-06-23 01:19:29 +0000115
Chris Lattner24943d22010-06-08 16:52:24 +0000116 if (!all_okay || (sub_cmd_obj == NULL))
117 {
118 std::string cmd_string;
119 command.GetCommandString (cmd_string);
Jim Inghamc2d382c2010-11-30 22:59:37 +0000120 if (matches.GetSize() < 2)
121 {
122 result.AppendErrorWithFormat("'%s' is not a known command.\n"
123 "Try 'help' to see a current list of commands.\n",
124 cmd_string.c_str());
125 }
126 else
127 {
128 StreamString s;
129 s.Printf ("ambiguous command %s", cmd_string.c_str());
130 size_t num_matches = matches.GetSize();
131 for (size_t match_idx = 0; match_idx < num_matches; match_idx++)
132 {
133 s.Printf ("\n\t%s", matches.GetStringAtIndex(match_idx));
134 }
135 s.Printf ("\n");
136 result.AppendError(s.GetData());
137 }
138
Chris Lattner24943d22010-06-08 16:52:24 +0000139 result.SetStatus (eReturnStatusFailed);
140 }
141 else
142 {
143 Stream &output_strm = result.GetOutputStream();
144 if (sub_cmd_obj->GetOptions() != NULL)
145 {
Caroline Ticefb355112010-10-01 17:46:38 +0000146 if (sub_cmd_obj->WantsRawCommandString())
147 {
148 std::string help_text (sub_cmd_obj->GetHelp());
149 help_text.append (" This command takes 'raw' input (no need to quote stuff).");
150 m_interpreter.OutputFormattedHelpText (output_strm, "", "", help_text.c_str(), 1);
151 }
152 else
153 m_interpreter.OutputFormattedHelpText (output_strm, "", "", sub_cmd_obj->GetHelp(), 1);
Chris Lattner24943d22010-06-08 16:52:24 +0000154 output_strm.Printf ("\nSyntax: %s\n", sub_cmd_obj->GetSyntax());
Greg Claytonf15996e2011-04-07 22:46:35 +0000155 sub_cmd_obj->GetOptions()->GenerateOptionUsage (output_strm, sub_cmd_obj);
Chris Lattner24943d22010-06-08 16:52:24 +0000156 const char *long_help = sub_cmd_obj->GetHelpLong();
157 if ((long_help != NULL)
158 && (strlen (long_help) > 0))
Greg Clayton63094e02010-06-23 01:19:29 +0000159 output_strm.Printf ("\n%s", long_help);
Johnny Chenfd8ef2e2012-01-27 18:49:33 +0000160 if (sub_cmd_obj->WantsRawCommandString() && !sub_cmd_obj->WantsCompletion())
Caroline Tice44c841d2010-12-07 19:58:26 +0000161 {
Jim Ingham79505722012-05-02 01:12:54 +0000162 // Emit the message about using ' -- ' between the end of the command options and the raw input
163 // conditionally, i.e., only if the command object does not want completion.
164 m_interpreter.OutputFormattedHelpText (output_strm, "", "",
165 "\nIMPORTANT NOTE: Because this command takes 'raw' input, if you use any command options"
166 " you must use ' -- ' between the end of the command options and the beginning of the raw input.", 1);
Caroline Tice44c841d2010-12-07 19:58:26 +0000167 }
Jim Ingham79505722012-05-02 01:12:54 +0000168 else if (sub_cmd_obj->GetNumArgumentEntries() > 0
169 && sub_cmd_obj->GetOptions()
170 && sub_cmd_obj->GetOptions()->NumCommandOptions() > 0)
171 {
172 // Also emit a warning about using "--" in case you are using a command that takes options and arguments.
173 m_interpreter.OutputFormattedHelpText (output_strm, "", "",
174 "\nThis command takes options and arguments, if your arguments look like option specifiers"
175 " you must use '--' to terminate the options before starting to give the arguments.", 1);
176 }
177
Johnny Chenfd8ef2e2012-01-27 18:49:33 +0000178 // Mark this help command with a success status.
Johnny Chen8e468592010-10-08 17:21:27 +0000179 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner24943d22010-06-08 16:52:24 +0000180 }
181 else if (sub_cmd_obj->IsMultiwordObject())
182 {
Caroline Ticefb355112010-10-01 17:46:38 +0000183 if (sub_cmd_obj->WantsRawCommandString())
184 {
185 std::string help_text (sub_cmd_obj->GetHelp());
186 help_text.append (" This command takes 'raw' input (no need to quote stuff).");
187 m_interpreter.OutputFormattedHelpText (output_strm, "", "", help_text.c_str(), 1);
188 }
189 else
190 m_interpreter.OutputFormattedHelpText (output_strm, "", "", sub_cmd_obj->GetHelp(), 1);
Greg Clayton13193d52012-10-13 02:07:45 +0000191 sub_cmd_obj->GenerateHelpText (result);
Chris Lattner24943d22010-06-08 16:52:24 +0000192 }
193 else
194 {
Greg Clayton63094e02010-06-23 01:19:29 +0000195 const char *long_help = sub_cmd_obj->GetHelpLong();
196 if ((long_help != NULL)
197 && (strlen (long_help) > 0))
Johnny Chen1d4e4d52011-10-28 23:30:28 +0000198 output_strm.Printf ("%s", long_help);
Caroline Ticefb355112010-10-01 17:46:38 +0000199 else if (sub_cmd_obj->WantsRawCommandString())
200 {
201 std::string help_text (sub_cmd_obj->GetHelp());
202 help_text.append (" This command takes 'raw' input (no need to quote stuff).");
203 m_interpreter.OutputFormattedHelpText (output_strm, "", "", help_text.c_str(), 1);
204 }
Greg Clayton63094e02010-06-23 01:19:29 +0000205 else
Greg Clayton238c0a12010-09-18 01:14:36 +0000206 m_interpreter.OutputFormattedHelpText (output_strm, "", "", sub_cmd_obj->GetHelp(), 1);
Greg Clayton63094e02010-06-23 01:19:29 +0000207 output_strm.Printf ("\nSyntax: %s\n", sub_cmd_obj->GetSyntax());
Johnny Chen8e468592010-10-08 17:21:27 +0000208 // Mark this help command with a success status.
209 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner24943d22010-06-08 16:52:24 +0000210 }
211 }
Caroline Ticee6866a32010-10-28 23:17:48 +0000212
213 if (is_alias_command)
214 {
215 StreamString sstr;
216 m_interpreter.GetAliasHelp (alias_name.c_str(), cmd_obj->GetCommandName(), sstr);
217 result.GetOutputStream().Printf ("\n'%s' is an abbreviation for %s\n", alias_name.c_str(), sstr.GetData());
218 }
Chris Lattner24943d22010-06-08 16:52:24 +0000219 }
Jim Inghamd40f8a62010-07-06 22:46:59 +0000220 else if (matches.GetSize() > 0)
221 {
222 Stream &output_strm = result.GetOutputStream();
223 output_strm.Printf("Help requested with ambiguous command name, possible completions:\n");
Greg Clayton54e7afa2010-07-09 20:39:50 +0000224 const uint32_t match_count = matches.GetSize();
225 for (uint32_t i = 0; i < match_count; i++)
Jim Inghamd40f8a62010-07-06 22:46:59 +0000226 {
227 output_strm.Printf("\t%s\n", matches.GetStringAtIndex(i));
228 }
229 }
Chris Lattner24943d22010-06-08 16:52:24 +0000230 else
231 {
Caroline Ticefb355112010-10-01 17:46:38 +0000232 // Maybe the user is asking for help about a command argument rather than a command.
233 const CommandArgumentType arg_type = CommandObject::LookupArgumentName (command.GetArgumentAtIndex (0));
234 if (arg_type != eArgTypeLastArg)
235 {
236 Stream &output_strm = result.GetOutputStream ();
237 CommandObject::GetArgumentHelp (output_strm, arg_type, m_interpreter);
238 result.SetStatus (eReturnStatusSuccessFinishNoResult);
239 }
240 else
241 {
242 result.AppendErrorWithFormat
243 ("'%s' is not a known command.\nTry 'help' to see a current list of commands.\n",
244 command.GetArgumentAtIndex(0));
245 result.SetStatus (eReturnStatusFailed);
246 }
Chris Lattner24943d22010-06-08 16:52:24 +0000247 }
248 }
Greg Clayton63094e02010-06-23 01:19:29 +0000249
Chris Lattner24943d22010-06-08 16:52:24 +0000250 return result.Succeeded();
251}
252
253int
254CommandObjectHelp::HandleCompletion
255(
256 Args &input,
257 int &cursor_index,
258 int &cursor_char_position,
259 int match_start_point,
260 int max_return_elements,
Jim Ingham802f8b02010-06-30 05:02:46 +0000261 bool &word_complete,
Chris Lattner24943d22010-06-08 16:52:24 +0000262 StringList &matches
263)
264{
265 // Return the completions of the commands in the help system:
266 if (cursor_index == 0)
267 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000268 return m_interpreter.HandleCompletionMatches (input,
269 cursor_index,
270 cursor_char_position,
271 match_start_point,
272 max_return_elements,
273 word_complete,
274 matches);
Chris Lattner24943d22010-06-08 16:52:24 +0000275 }
276 else
277 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000278 CommandObject *cmd_obj = m_interpreter.GetCommandObject (input.GetArgumentAtIndex(0));
Jim Inghameeddf872011-10-26 19:32:01 +0000279
280 // The command that they are getting help on might be ambiguous, in which case we should complete that,
281 // otherwise complete with the command the user is getting help on...
282
283 if (cmd_obj)
284 {
285 input.Shift();
286 cursor_index--;
287 return cmd_obj->HandleCompletion (input,
288 cursor_index,
289 cursor_char_position,
290 match_start_point,
291 max_return_elements,
292 word_complete,
293 matches);
294 }
295 else
296 {
297 return m_interpreter.HandleCompletionMatches (input,
298 cursor_index,
299 cursor_char_position,
300 match_start_point,
301 max_return_elements,
302 word_complete,
303 matches);
304 }
Chris Lattner24943d22010-06-08 16:52:24 +0000305 }
306}