blob: 07d80ebd5de98549b0c393b0fd59bf2499e937b5 [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
52
53bool
Greg Clayton238c0a12010-09-18 01:14:36 +000054CommandObjectHelp::Execute (Args& command, CommandReturnObject &result)
Chris Lattner24943d22010-06-08 16:52:24 +000055{
56 CommandObject::CommandMap::iterator pos;
57 CommandObject *cmd_obj;
58 const int argc = command.GetArgumentCount ();
Greg Clayton63094e02010-06-23 01:19:29 +000059
Chris Lattner24943d22010-06-08 16:52:24 +000060 // 'help' doesn't take any options or arguments, other than command names. If argc is 0, we show the user
61 // 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 +000062 if (argc == 0)
63 {
64 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Greg Clayton238c0a12010-09-18 01:14:36 +000065 m_interpreter.GetHelp (result); // General help, for ALL commands.
Chris Lattner24943d22010-06-08 16:52:24 +000066 }
67 else
68 {
69 // Get command object for the first command argument. Only search built-in command dictionary.
Jim Inghamd40f8a62010-07-06 22:46:59 +000070 StringList matches;
Greg Clayton238c0a12010-09-18 01:14:36 +000071 cmd_obj = m_interpreter.GetCommandObject (command.GetArgumentAtIndex (0), &matches);
Caroline Ticee6866a32010-10-28 23:17:48 +000072 bool is_alias_command = m_interpreter.AliasExists (command.GetArgumentAtIndex (0));
73 std::string alias_name = command.GetArgumentAtIndex(0);
Greg Clayton63094e02010-06-23 01:19:29 +000074
Chris Lattner24943d22010-06-08 16:52:24 +000075 if (cmd_obj != NULL)
76 {
77 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);
84 if (! sub_cmd_obj->IsMultiwordObject ())
85 {
86 all_okay = false;
87 }
88 else
89 {
90 pos = ((CommandObjectMultiword *) sub_cmd_obj)->m_subcommand_dict.find (sub_command);
91 if (pos != ((CommandObjectMultiword *) sub_cmd_obj)->m_subcommand_dict.end())
Greg Clayton63094e02010-06-23 01:19:29 +000092 sub_cmd_obj = pos->second.get();
Chris Lattner24943d22010-06-08 16:52:24 +000093 else
Greg Clayton63094e02010-06-23 01:19:29 +000094 all_okay = false;
Chris Lattner24943d22010-06-08 16:52:24 +000095 }
96 }
Greg Clayton63094e02010-06-23 01:19:29 +000097
Chris Lattner24943d22010-06-08 16:52:24 +000098 if (!all_okay || (sub_cmd_obj == NULL))
99 {
100 std::string cmd_string;
101 command.GetCommandString (cmd_string);
102 result.AppendErrorWithFormat
Greg Clayton63094e02010-06-23 01:19:29 +0000103 ("'%s' is not a known command.\nTry 'help' to see a current list of commands.\n",
104 cmd_string.c_str());
Chris Lattner24943d22010-06-08 16:52:24 +0000105 result.SetStatus (eReturnStatusFailed);
106 }
107 else
108 {
109 Stream &output_strm = result.GetOutputStream();
110 if (sub_cmd_obj->GetOptions() != NULL)
111 {
Caroline Ticefb355112010-10-01 17:46:38 +0000112 if (sub_cmd_obj->WantsRawCommandString())
113 {
114 std::string help_text (sub_cmd_obj->GetHelp());
115 help_text.append (" This command takes 'raw' input (no need to quote stuff).");
116 m_interpreter.OutputFormattedHelpText (output_strm, "", "", help_text.c_str(), 1);
117 }
118 else
119 m_interpreter.OutputFormattedHelpText (output_strm, "", "", sub_cmd_obj->GetHelp(), 1);
Chris Lattner24943d22010-06-08 16:52:24 +0000120 output_strm.Printf ("\nSyntax: %s\n", sub_cmd_obj->GetSyntax());
Greg Clayton238c0a12010-09-18 01:14:36 +0000121 sub_cmd_obj->GetOptions()->GenerateOptionUsage (m_interpreter, output_strm, sub_cmd_obj);
Chris Lattner24943d22010-06-08 16:52:24 +0000122 const char *long_help = sub_cmd_obj->GetHelpLong();
123 if ((long_help != NULL)
124 && (strlen (long_help) > 0))
Greg Clayton63094e02010-06-23 01:19:29 +0000125 output_strm.Printf ("\n%s", long_help);
Johnny Chen8e468592010-10-08 17:21:27 +0000126 // Mark this help command with a success status.
127 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner24943d22010-06-08 16:52:24 +0000128 }
129 else if (sub_cmd_obj->IsMultiwordObject())
130 {
Caroline Ticefb355112010-10-01 17:46:38 +0000131 if (sub_cmd_obj->WantsRawCommandString())
132 {
133 std::string help_text (sub_cmd_obj->GetHelp());
134 help_text.append (" This command takes 'raw' input (no need to quote stuff).");
135 m_interpreter.OutputFormattedHelpText (output_strm, "", "", help_text.c_str(), 1);
136 }
137 else
138 m_interpreter.OutputFormattedHelpText (output_strm, "", "", sub_cmd_obj->GetHelp(), 1);
Greg Clayton238c0a12010-09-18 01:14:36 +0000139 ((CommandObjectMultiword *) sub_cmd_obj)->GenerateHelpText (result);
Chris Lattner24943d22010-06-08 16:52:24 +0000140 }
141 else
142 {
Greg Clayton63094e02010-06-23 01:19:29 +0000143 const char *long_help = sub_cmd_obj->GetHelpLong();
144 if ((long_help != NULL)
145 && (strlen (long_help) > 0))
Caroline Ticeabb507a2010-09-08 21:06:11 +0000146 output_strm.Printf ("\n%s", long_help);
Caroline Ticefb355112010-10-01 17:46:38 +0000147 else if (sub_cmd_obj->WantsRawCommandString())
148 {
149 std::string help_text (sub_cmd_obj->GetHelp());
150 help_text.append (" This command takes 'raw' input (no need to quote stuff).");
151 m_interpreter.OutputFormattedHelpText (output_strm, "", "", help_text.c_str(), 1);
152 }
Greg Clayton63094e02010-06-23 01:19:29 +0000153 else
Greg Clayton238c0a12010-09-18 01:14:36 +0000154 m_interpreter.OutputFormattedHelpText (output_strm, "", "", sub_cmd_obj->GetHelp(), 1);
Greg Clayton63094e02010-06-23 01:19:29 +0000155 output_strm.Printf ("\nSyntax: %s\n", sub_cmd_obj->GetSyntax());
Johnny Chen8e468592010-10-08 17:21:27 +0000156 // Mark this help command with a success status.
157 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner24943d22010-06-08 16:52:24 +0000158 }
159 }
Caroline Ticee6866a32010-10-28 23:17:48 +0000160
161 if (is_alias_command)
162 {
163 StreamString sstr;
164 m_interpreter.GetAliasHelp (alias_name.c_str(), cmd_obj->GetCommandName(), sstr);
165 result.GetOutputStream().Printf ("\n'%s' is an abbreviation for %s\n", alias_name.c_str(), sstr.GetData());
166 }
Chris Lattner24943d22010-06-08 16:52:24 +0000167 }
Jim Inghamd40f8a62010-07-06 22:46:59 +0000168 else if (matches.GetSize() > 0)
169 {
170 Stream &output_strm = result.GetOutputStream();
171 output_strm.Printf("Help requested with ambiguous command name, possible completions:\n");
Greg Clayton54e7afa2010-07-09 20:39:50 +0000172 const uint32_t match_count = matches.GetSize();
173 for (uint32_t i = 0; i < match_count; i++)
Jim Inghamd40f8a62010-07-06 22:46:59 +0000174 {
175 output_strm.Printf("\t%s\n", matches.GetStringAtIndex(i));
176 }
177 }
Chris Lattner24943d22010-06-08 16:52:24 +0000178 else
179 {
Caroline Ticefb355112010-10-01 17:46:38 +0000180 // Maybe the user is asking for help about a command argument rather than a command.
181 const CommandArgumentType arg_type = CommandObject::LookupArgumentName (command.GetArgumentAtIndex (0));
182 if (arg_type != eArgTypeLastArg)
183 {
184 Stream &output_strm = result.GetOutputStream ();
185 CommandObject::GetArgumentHelp (output_strm, arg_type, m_interpreter);
186 result.SetStatus (eReturnStatusSuccessFinishNoResult);
187 }
188 else
189 {
190 result.AppendErrorWithFormat
191 ("'%s' is not a known command.\nTry 'help' to see a current list of commands.\n",
192 command.GetArgumentAtIndex(0));
193 result.SetStatus (eReturnStatusFailed);
194 }
Chris Lattner24943d22010-06-08 16:52:24 +0000195 }
196 }
Greg Clayton63094e02010-06-23 01:19:29 +0000197
Chris Lattner24943d22010-06-08 16:52:24 +0000198 return result.Succeeded();
199}
200
201int
202CommandObjectHelp::HandleCompletion
203(
204 Args &input,
205 int &cursor_index,
206 int &cursor_char_position,
207 int match_start_point,
208 int max_return_elements,
Jim Ingham802f8b02010-06-30 05:02:46 +0000209 bool &word_complete,
Chris Lattner24943d22010-06-08 16:52:24 +0000210 StringList &matches
211)
212{
213 // Return the completions of the commands in the help system:
214 if (cursor_index == 0)
215 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000216 return m_interpreter.HandleCompletionMatches (input,
217 cursor_index,
218 cursor_char_position,
219 match_start_point,
220 max_return_elements,
221 word_complete,
222 matches);
Chris Lattner24943d22010-06-08 16:52:24 +0000223 }
224 else
225 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000226 CommandObject *cmd_obj = m_interpreter.GetCommandObject (input.GetArgumentAtIndex(0));
Chris Lattner24943d22010-06-08 16:52:24 +0000227 input.Shift();
228 cursor_index--;
Greg Clayton238c0a12010-09-18 01:14:36 +0000229 return cmd_obj->HandleCompletion (input,
230 cursor_index,
231 cursor_char_position,
232 match_start_point,
233 max_return_elements,
234 word_complete,
235 matches);
Chris Lattner24943d22010-06-08 16:52:24 +0000236 }
237}