blob: f73d9d23b57493b0cde9f4922a2dfc2f5caf4f9c [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
Daniel Malea93a64302012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Chris Lattner30fdc8d2010-06-08 16:52:24 +000012#include "CommandObjectHelp.h"
13
14// C Includes
15// C++ Includes
16// Other libraries and framework includes
17// Project includes
18#include "lldb/Interpreter/CommandObjectMultiword.h"
19#include "lldb/Interpreter/CommandInterpreter.h"
Jim Ingham40af72e2010-06-15 19:49:27 +000020#include "lldb/Interpreter/Options.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021#include "lldb/Interpreter/CommandReturnObject.h"
22
23using namespace lldb;
24using namespace lldb_private;
25
26//-------------------------------------------------------------------------
27// CommandObjectHelp
28//-------------------------------------------------------------------------
29
Greg Claytona7015092010-09-18 01:14:36 +000030CommandObjectHelp::CommandObjectHelp (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +000031 CommandObjectParsed (interpreter,
32 "help",
33 "Show a list of all debugger commands, or give details about specific commands.",
34 "help [<cmd-name>]"), m_options (interpreter)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000035{
Caroline Tice405fe672010-10-04 22:28:36 +000036 CommandArgumentEntry arg;
37 CommandArgumentData command_arg;
38
39 // Define the first (and only) variant of this arg.
40 command_arg.arg_type = eArgTypeCommandName;
41 command_arg.arg_repetition = eArgRepeatStar;
42
43 // There is only one variant this argument could be; put it into the argument entry.
44 arg.push_back (command_arg);
45
46 // Push the data for the first argument into the m_arguments vector.
47 m_arguments.push_back (arg);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000048}
49
50CommandObjectHelp::~CommandObjectHelp()
51{
52}
53
Enrico Granata08633ee2011-09-09 17:49:36 +000054OptionDefinition
55CommandObjectHelp::CommandOptions::g_option_table[] =
56{
Zachary Turnerd37221d2014-07-09 16:31:49 +000057 { LLDB_OPT_SET_ALL, false, "show-aliases", 'a', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone, "Show aliases in the command list."},
58 { LLDB_OPT_SET_ALL, false, "hide-user-commands", 'u', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone, "Hide user-defined commands from the list."},
59 { 0, false, NULL, 0, 0, NULL, NULL, 0, eArgTypeNone, NULL }
Enrico Granata08633ee2011-09-09 17:49:36 +000060};
61
Chris Lattner30fdc8d2010-06-08 16:52:24 +000062bool
Jim Ingham5a988412012-06-08 21:56:10 +000063CommandObjectHelp::DoExecute (Args& command, CommandReturnObject &result)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000064{
65 CommandObject::CommandMap::iterator pos;
66 CommandObject *cmd_obj;
Greg Claytonc7bece562013-01-25 18:06:21 +000067 const size_t argc = command.GetArgumentCount ();
Greg Clayton66111032010-06-23 01:19:29 +000068
Enrico Granata08633ee2011-09-09 17:49:36 +000069 // 'help' doesn't take any arguments, other than command names. If argc is 0, we show the user
70 // all commands (aliases and user commands if asked for). Otherwise every argument must be the name of a command or a sub-command.
Chris Lattner30fdc8d2010-06-08 16:52:24 +000071 if (argc == 0)
72 {
Enrico Granata08633ee2011-09-09 17:49:36 +000073 uint32_t cmd_types = CommandInterpreter::eCommandTypesBuiltin;
74 if (m_options.m_show_aliases)
75 cmd_types |= CommandInterpreter::eCommandTypesAliases;
76 if (m_options.m_show_user_defined)
77 cmd_types |= CommandInterpreter::eCommandTypesUserDef;
78
Chris Lattner30fdc8d2010-06-08 16:52:24 +000079 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Enrico Granata08633ee2011-09-09 17:49:36 +000080 m_interpreter.GetHelp (result, cmd_types); // General help
Chris Lattner30fdc8d2010-06-08 16:52:24 +000081 }
82 else
83 {
84 // Get command object for the first command argument. Only search built-in command dictionary.
Jim Ingham279a6c22010-07-06 22:46:59 +000085 StringList matches;
Greg Claytona7015092010-09-18 01:14:36 +000086 cmd_obj = m_interpreter.GetCommandObject (command.GetArgumentAtIndex (0), &matches);
Caroline Ticee7941792010-10-28 23:17:48 +000087 bool is_alias_command = m_interpreter.AliasExists (command.GetArgumentAtIndex (0));
88 std::string alias_name = command.GetArgumentAtIndex(0);
Greg Clayton66111032010-06-23 01:19:29 +000089
Chris Lattner30fdc8d2010-06-08 16:52:24 +000090 if (cmd_obj != NULL)
91 {
Jim Ingham271ad292010-11-30 22:59:37 +000092 StringList matches;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000093 bool all_okay = true;
94 CommandObject *sub_cmd_obj = cmd_obj;
95 // Loop down through sub_command dictionaries until we find the command object that corresponds
96 // to the help command entered.
Andy Gibbsa297a972013-06-19 19:04:53 +000097 for (size_t i = 1; i < argc && all_okay; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000098 {
99 std::string sub_command = command.GetArgumentAtIndex(i);
Jim Ingham271ad292010-11-30 22:59:37 +0000100 matches.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000101 if (! sub_cmd_obj->IsMultiwordObject ())
102 {
103 all_okay = false;
104 }
105 else
106 {
Jim Ingham271ad292010-11-30 22:59:37 +0000107 CommandObject *found_cmd;
Greg Clayton998255b2012-10-13 02:07:45 +0000108 found_cmd = sub_cmd_obj->GetSubcommandObject(sub_command.c_str(), &matches);
Jim Ingham271ad292010-11-30 22:59:37 +0000109 if (found_cmd == NULL)
Greg Clayton66111032010-06-23 01:19:29 +0000110 all_okay = false;
Jim Ingham97253e62010-12-01 00:42:17 +0000111 else if (matches.GetSize() > 1)
Jim Ingham271ad292010-11-30 22:59:37 +0000112 all_okay = false;
113 else
114 sub_cmd_obj = found_cmd;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000115 }
116 }
Greg Clayton66111032010-06-23 01:19:29 +0000117
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000118 if (!all_okay || (sub_cmd_obj == NULL))
119 {
120 std::string cmd_string;
121 command.GetCommandString (cmd_string);
Enrico Granata9b62d1d2013-06-12 01:50:57 +0000122 if (matches.GetSize() >= 2)
Jim Ingham271ad292010-11-30 22:59:37 +0000123 {
124 StreamString s;
125 s.Printf ("ambiguous command %s", cmd_string.c_str());
126 size_t num_matches = matches.GetSize();
127 for (size_t match_idx = 0; match_idx < num_matches; match_idx++)
128 {
129 s.Printf ("\n\t%s", matches.GetStringAtIndex(match_idx));
130 }
131 s.Printf ("\n");
132 result.AppendError(s.GetData());
Enrico Granata9b62d1d2013-06-12 01:50:57 +0000133 result.SetStatus (eReturnStatusFailed);
134 return false;
Jim Ingham271ad292010-11-30 22:59:37 +0000135 }
Enrico Granata9b62d1d2013-06-12 01:50:57 +0000136 else if (!sub_cmd_obj)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000137 {
Enrico Granata9b62d1d2013-06-12 01:50:57 +0000138 result.AppendErrorWithFormat("'%s' is not a known command.\n"
139 "Try 'help' to see a current list of commands.\n",
140 cmd_string.c_str());
141 result.SetStatus (eReturnStatusFailed);
142 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000143 }
144 else
145 {
Enrico Granata9b62d1d2013-06-12 01:50:57 +0000146 result.GetOutputStream().Printf("'%s' is not a known command.\n"
147 "Try 'help' to see a current list of commands.\n"
148 "The closest match is '%s'. Help on it follows.\n\n",
149 cmd_string.c_str(),
150 sub_cmd_obj->GetCommandName());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000151 }
152 }
Caroline Ticee7941792010-10-28 23:17:48 +0000153
Enrico Granata9b62d1d2013-06-12 01:50:57 +0000154 sub_cmd_obj->GenerateHelpText(result);
155
Caroline Ticee7941792010-10-28 23:17:48 +0000156 if (is_alias_command)
157 {
158 StreamString sstr;
159 m_interpreter.GetAliasHelp (alias_name.c_str(), cmd_obj->GetCommandName(), sstr);
160 result.GetOutputStream().Printf ("\n'%s' is an abbreviation for %s\n", alias_name.c_str(), sstr.GetData());
161 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000162 }
Jim Ingham279a6c22010-07-06 22:46:59 +0000163 else if (matches.GetSize() > 0)
164 {
165 Stream &output_strm = result.GetOutputStream();
166 output_strm.Printf("Help requested with ambiguous command name, possible completions:\n");
Greg Claytonc7bece562013-01-25 18:06:21 +0000167 const size_t match_count = matches.GetSize();
168 for (size_t i = 0; i < match_count; i++)
Jim Ingham279a6c22010-07-06 22:46:59 +0000169 {
170 output_strm.Printf("\t%s\n", matches.GetStringAtIndex(i));
171 }
172 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000173 else
174 {
Caroline Ticee139cf22010-10-01 17:46:38 +0000175 // Maybe the user is asking for help about a command argument rather than a command.
176 const CommandArgumentType arg_type = CommandObject::LookupArgumentName (command.GetArgumentAtIndex (0));
177 if (arg_type != eArgTypeLastArg)
178 {
179 Stream &output_strm = result.GetOutputStream ();
180 CommandObject::GetArgumentHelp (output_strm, arg_type, m_interpreter);
181 result.SetStatus (eReturnStatusSuccessFinishNoResult);
182 }
183 else
184 {
185 result.AppendErrorWithFormat
186 ("'%s' is not a known command.\nTry 'help' to see a current list of commands.\n",
187 command.GetArgumentAtIndex(0));
188 result.SetStatus (eReturnStatusFailed);
189 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000190 }
191 }
Greg Clayton66111032010-06-23 01:19:29 +0000192
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000193 return result.Succeeded();
194}
195
196int
197CommandObjectHelp::HandleCompletion
198(
199 Args &input,
200 int &cursor_index,
201 int &cursor_char_position,
202 int match_start_point,
203 int max_return_elements,
Jim Ingham558ce122010-06-30 05:02:46 +0000204 bool &word_complete,
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000205 StringList &matches
206)
207{
208 // Return the completions of the commands in the help system:
209 if (cursor_index == 0)
210 {
Greg Claytona7015092010-09-18 01:14:36 +0000211 return m_interpreter.HandleCompletionMatches (input,
212 cursor_index,
213 cursor_char_position,
214 match_start_point,
215 max_return_elements,
216 word_complete,
217 matches);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000218 }
219 else
220 {
Greg Claytona7015092010-09-18 01:14:36 +0000221 CommandObject *cmd_obj = m_interpreter.GetCommandObject (input.GetArgumentAtIndex(0));
Jim Ingham1d18ebf2011-10-26 19:32:01 +0000222
223 // The command that they are getting help on might be ambiguous, in which case we should complete that,
224 // otherwise complete with the command the user is getting help on...
225
226 if (cmd_obj)
227 {
228 input.Shift();
229 cursor_index--;
230 return cmd_obj->HandleCompletion (input,
231 cursor_index,
232 cursor_char_position,
233 match_start_point,
234 max_return_elements,
235 word_complete,
236 matches);
237 }
238 else
239 {
240 return m_interpreter.HandleCompletionMatches (input,
241 cursor_index,
242 cursor_char_position,
243 match_start_point,
244 max_return_elements,
245 word_complete,
246 matches);
247 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000248 }
249}