blob: 0c94ff262cb71a1aa922ddfae5f289893dc975ca [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) :
29 CommandObject (interpreter,
30 "help",
Caroline Ticec1ad82e2010-09-07 22:38:08 +000031 "Show a list of all debugger commands, or give details about specific commands.",
Greg Clayton54e7afa2010-07-09 20:39:50 +000032 "help [<cmd-name>]")
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
Chris Lattner24943d22010-06-08 16:52:24 +000052bool
Greg Clayton238c0a12010-09-18 01:14:36 +000053CommandObjectHelp::Execute (Args& command, CommandReturnObject &result)
Chris Lattner24943d22010-06-08 16:52:24 +000054{
55 CommandObject::CommandMap::iterator pos;
56 CommandObject *cmd_obj;
57 const int argc = command.GetArgumentCount ();
Greg Clayton63094e02010-06-23 01:19:29 +000058
Chris Lattner24943d22010-06-08 16:52:24 +000059 // 'help' doesn't take any options or arguments, other than command names. If argc is 0, we show the user
60 // all commands and aliases. Otherwise every argument must be the name of a command or a sub-command.
Chris Lattner24943d22010-06-08 16:52:24 +000061 if (argc == 0)
62 {
63 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Greg Clayton238c0a12010-09-18 01:14:36 +000064 m_interpreter.GetHelp (result); // General help, for ALL commands.
Chris Lattner24943d22010-06-08 16:52:24 +000065 }
66 else
67 {
68 // Get command object for the first command argument. Only search built-in command dictionary.
Jim Inghamd40f8a62010-07-06 22:46:59 +000069 StringList matches;
Greg Clayton238c0a12010-09-18 01:14:36 +000070 cmd_obj = m_interpreter.GetCommandObject (command.GetArgumentAtIndex (0), &matches);
Caroline Ticee6866a32010-10-28 23:17:48 +000071 bool is_alias_command = m_interpreter.AliasExists (command.GetArgumentAtIndex (0));
72 std::string alias_name = command.GetArgumentAtIndex(0);
Greg Clayton63094e02010-06-23 01:19:29 +000073
Chris Lattner24943d22010-06-08 16:52:24 +000074 if (cmd_obj != NULL)
75 {
Jim Inghamc2d382c2010-11-30 22:59:37 +000076 StringList matches;
Chris Lattner24943d22010-06-08 16:52:24 +000077 bool all_okay = true;
78 CommandObject *sub_cmd_obj = cmd_obj;
79 // Loop down through sub_command dictionaries until we find the command object that corresponds
80 // to the help command entered.
81 for (int i = 1; i < argc && all_okay; ++i)
82 {
83 std::string sub_command = command.GetArgumentAtIndex(i);
Jim Inghamc2d382c2010-11-30 22:59:37 +000084 matches.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +000085 if (! sub_cmd_obj->IsMultiwordObject ())
86 {
87 all_okay = false;
88 }
89 else
90 {
Jim Inghamc2d382c2010-11-30 22:59:37 +000091 CommandObject *found_cmd;
92 found_cmd = ((CommandObjectMultiword *) sub_cmd_obj)->GetSubcommandObject(sub_command.c_str(),
93 &matches);
94 if (found_cmd == NULL)
Greg Clayton63094e02010-06-23 01:19:29 +000095 all_okay = false;
Jim Inghamdd1f02a2010-12-01 00:42:17 +000096 else if (matches.GetSize() > 1)
Jim Inghamc2d382c2010-11-30 22:59:37 +000097 all_okay = false;
98 else
99 sub_cmd_obj = found_cmd;
Chris Lattner24943d22010-06-08 16:52:24 +0000100 }
101 }
Greg Clayton63094e02010-06-23 01:19:29 +0000102
Chris Lattner24943d22010-06-08 16:52:24 +0000103 if (!all_okay || (sub_cmd_obj == NULL))
104 {
105 std::string cmd_string;
106 command.GetCommandString (cmd_string);
Jim Inghamc2d382c2010-11-30 22:59:37 +0000107 if (matches.GetSize() < 2)
108 {
109 result.AppendErrorWithFormat("'%s' is not a known command.\n"
110 "Try 'help' to see a current list of commands.\n",
111 cmd_string.c_str());
112 }
113 else
114 {
115 StreamString s;
116 s.Printf ("ambiguous command %s", cmd_string.c_str());
117 size_t num_matches = matches.GetSize();
118 for (size_t match_idx = 0; match_idx < num_matches; match_idx++)
119 {
120 s.Printf ("\n\t%s", matches.GetStringAtIndex(match_idx));
121 }
122 s.Printf ("\n");
123 result.AppendError(s.GetData());
124 }
125
Chris Lattner24943d22010-06-08 16:52:24 +0000126 result.SetStatus (eReturnStatusFailed);
127 }
128 else
129 {
130 Stream &output_strm = result.GetOutputStream();
131 if (sub_cmd_obj->GetOptions() != NULL)
132 {
Caroline Ticefb355112010-10-01 17:46:38 +0000133 if (sub_cmd_obj->WantsRawCommandString())
134 {
135 std::string help_text (sub_cmd_obj->GetHelp());
136 help_text.append (" This command takes 'raw' input (no need to quote stuff).");
137 m_interpreter.OutputFormattedHelpText (output_strm, "", "", help_text.c_str(), 1);
138 }
139 else
140 m_interpreter.OutputFormattedHelpText (output_strm, "", "", sub_cmd_obj->GetHelp(), 1);
Chris Lattner24943d22010-06-08 16:52:24 +0000141 output_strm.Printf ("\nSyntax: %s\n", sub_cmd_obj->GetSyntax());
Greg Claytonf15996e2011-04-07 22:46:35 +0000142 sub_cmd_obj->GetOptions()->GenerateOptionUsage (output_strm, sub_cmd_obj);
Chris Lattner24943d22010-06-08 16:52:24 +0000143 const char *long_help = sub_cmd_obj->GetHelpLong();
144 if ((long_help != NULL)
145 && (strlen (long_help) > 0))
Greg Clayton63094e02010-06-23 01:19:29 +0000146 output_strm.Printf ("\n%s", long_help);
Johnny Chen8e468592010-10-08 17:21:27 +0000147 // Mark this help command with a success status.
Caroline Tice44c841d2010-12-07 19:58:26 +0000148 if (sub_cmd_obj->WantsRawCommandString())
149 {
150 m_interpreter.OutputFormattedHelpText (output_strm, "", "", "\nIMPORTANT NOTE: Because this command takes 'raw' input, if you use any command options you must use ' -- ' between the end of the command options and the beginning of the raw input.", 1);
151 }
Johnny Chen8e468592010-10-08 17:21:27 +0000152 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner24943d22010-06-08 16:52:24 +0000153 }
154 else if (sub_cmd_obj->IsMultiwordObject())
155 {
Caroline Ticefb355112010-10-01 17:46:38 +0000156 if (sub_cmd_obj->WantsRawCommandString())
157 {
158 std::string help_text (sub_cmd_obj->GetHelp());
159 help_text.append (" This command takes 'raw' input (no need to quote stuff).");
160 m_interpreter.OutputFormattedHelpText (output_strm, "", "", help_text.c_str(), 1);
161 }
162 else
163 m_interpreter.OutputFormattedHelpText (output_strm, "", "", sub_cmd_obj->GetHelp(), 1);
Greg Clayton238c0a12010-09-18 01:14:36 +0000164 ((CommandObjectMultiword *) sub_cmd_obj)->GenerateHelpText (result);
Chris Lattner24943d22010-06-08 16:52:24 +0000165 }
166 else
167 {
Greg Clayton63094e02010-06-23 01:19:29 +0000168 const char *long_help = sub_cmd_obj->GetHelpLong();
169 if ((long_help != NULL)
170 && (strlen (long_help) > 0))
Caroline Ticeabb507a2010-09-08 21:06:11 +0000171 output_strm.Printf ("\n%s", long_help);
Caroline Ticefb355112010-10-01 17:46:38 +0000172 else if (sub_cmd_obj->WantsRawCommandString())
173 {
174 std::string help_text (sub_cmd_obj->GetHelp());
175 help_text.append (" This command takes 'raw' input (no need to quote stuff).");
176 m_interpreter.OutputFormattedHelpText (output_strm, "", "", help_text.c_str(), 1);
177 }
Greg Clayton63094e02010-06-23 01:19:29 +0000178 else
Greg Clayton238c0a12010-09-18 01:14:36 +0000179 m_interpreter.OutputFormattedHelpText (output_strm, "", "", sub_cmd_obj->GetHelp(), 1);
Greg Clayton63094e02010-06-23 01:19:29 +0000180 output_strm.Printf ("\nSyntax: %s\n", sub_cmd_obj->GetSyntax());
Johnny Chen8e468592010-10-08 17:21:27 +0000181 // Mark this help command with a success status.
182 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner24943d22010-06-08 16:52:24 +0000183 }
184 }
Caroline Ticee6866a32010-10-28 23:17:48 +0000185
186 if (is_alias_command)
187 {
188 StreamString sstr;
189 m_interpreter.GetAliasHelp (alias_name.c_str(), cmd_obj->GetCommandName(), sstr);
190 result.GetOutputStream().Printf ("\n'%s' is an abbreviation for %s\n", alias_name.c_str(), sstr.GetData());
191 }
Chris Lattner24943d22010-06-08 16:52:24 +0000192 }
Jim Inghamd40f8a62010-07-06 22:46:59 +0000193 else if (matches.GetSize() > 0)
194 {
195 Stream &output_strm = result.GetOutputStream();
196 output_strm.Printf("Help requested with ambiguous command name, possible completions:\n");
Greg Clayton54e7afa2010-07-09 20:39:50 +0000197 const uint32_t match_count = matches.GetSize();
198 for (uint32_t i = 0; i < match_count; i++)
Jim Inghamd40f8a62010-07-06 22:46:59 +0000199 {
200 output_strm.Printf("\t%s\n", matches.GetStringAtIndex(i));
201 }
202 }
Chris Lattner24943d22010-06-08 16:52:24 +0000203 else
204 {
Caroline Ticefb355112010-10-01 17:46:38 +0000205 // Maybe the user is asking for help about a command argument rather than a command.
206 const CommandArgumentType arg_type = CommandObject::LookupArgumentName (command.GetArgumentAtIndex (0));
207 if (arg_type != eArgTypeLastArg)
208 {
209 Stream &output_strm = result.GetOutputStream ();
210 CommandObject::GetArgumentHelp (output_strm, arg_type, m_interpreter);
211 result.SetStatus (eReturnStatusSuccessFinishNoResult);
212 }
213 else
214 {
215 result.AppendErrorWithFormat
216 ("'%s' is not a known command.\nTry 'help' to see a current list of commands.\n",
217 command.GetArgumentAtIndex(0));
218 result.SetStatus (eReturnStatusFailed);
219 }
Chris Lattner24943d22010-06-08 16:52:24 +0000220 }
221 }
Greg Clayton63094e02010-06-23 01:19:29 +0000222
Chris Lattner24943d22010-06-08 16:52:24 +0000223 return result.Succeeded();
224}
225
226int
227CommandObjectHelp::HandleCompletion
228(
229 Args &input,
230 int &cursor_index,
231 int &cursor_char_position,
232 int match_start_point,
233 int max_return_elements,
Jim Ingham802f8b02010-06-30 05:02:46 +0000234 bool &word_complete,
Chris Lattner24943d22010-06-08 16:52:24 +0000235 StringList &matches
236)
237{
238 // Return the completions of the commands in the help system:
239 if (cursor_index == 0)
240 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000241 return m_interpreter.HandleCompletionMatches (input,
242 cursor_index,
243 cursor_char_position,
244 match_start_point,
245 max_return_elements,
246 word_complete,
247 matches);
Chris Lattner24943d22010-06-08 16:52:24 +0000248 }
249 else
250 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000251 CommandObject *cmd_obj = m_interpreter.GetCommandObject (input.GetArgumentAtIndex(0));
Chris Lattner24943d22010-06-08 16:52:24 +0000252 input.Shift();
253 cursor_index--;
Greg Clayton238c0a12010-09-18 01:14:36 +0000254 return cmd_obj->HandleCompletion (input,
255 cursor_index,
256 cursor_char_position,
257 match_start_point,
258 max_return_elements,
259 word_complete,
260 matches);
Chris Lattner24943d22010-06-08 16:52:24 +0000261 }
262}