Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- 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 Malea | d891f9b | 2012-12-05 00:20:57 +0000 | [diff] [blame^] | 10 | #include "lldb/lldb-python.h" |
| 11 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 12 | #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 Ingham | 84cdc15 | 2010-06-15 19:49:27 +0000 | [diff] [blame] | 20 | #include "lldb/Interpreter/Options.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 21 | #include "lldb/Interpreter/CommandReturnObject.h" |
| 22 | |
| 23 | using namespace lldb; |
| 24 | using namespace lldb_private; |
| 25 | |
| 26 | //------------------------------------------------------------------------- |
| 27 | // CommandObjectHelp |
| 28 | //------------------------------------------------------------------------- |
| 29 | |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 30 | CommandObjectHelp::CommandObjectHelp (CommandInterpreter &interpreter) : |
Jim Ingham | da26bd2 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 31 | 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 Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 35 | { |
Caroline Tice | 43b014a | 2010-10-04 22:28:36 +0000 | [diff] [blame] | 36 | 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 Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | CommandObjectHelp::~CommandObjectHelp() |
| 51 | { |
| 52 | } |
| 53 | |
Enrico Granata | 1ac6d1f | 2011-09-09 17:49:36 +0000 | [diff] [blame] | 54 | OptionDefinition |
| 55 | CommandObjectHelp::CommandOptions::g_option_table[] = |
| 56 | { |
| 57 | { LLDB_OPT_SET_ALL, false, "show-aliases", 'a', no_argument, NULL, 0, eArgTypeNone, "Show aliases in the command list."}, |
| 58 | { LLDB_OPT_SET_ALL, false, "hide-user-commands", 'u', no_argument, NULL, 0, eArgTypeNone, "Hide user-defined commands from the list."}, |
| 59 | { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL } |
| 60 | }; |
| 61 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 62 | bool |
Jim Ingham | da26bd2 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 63 | CommandObjectHelp::DoExecute (Args& command, CommandReturnObject &result) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 64 | { |
| 65 | CommandObject::CommandMap::iterator pos; |
| 66 | CommandObject *cmd_obj; |
| 67 | const int argc = command.GetArgumentCount (); |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 68 | |
Enrico Granata | 1ac6d1f | 2011-09-09 17:49:36 +0000 | [diff] [blame] | 69 | // '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 Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 71 | if (argc == 0) |
| 72 | { |
Enrico Granata | 1ac6d1f | 2011-09-09 17:49:36 +0000 | [diff] [blame] | 73 | 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 Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 79 | result.SetStatus (eReturnStatusSuccessFinishNoResult); |
Enrico Granata | 1ac6d1f | 2011-09-09 17:49:36 +0000 | [diff] [blame] | 80 | m_interpreter.GetHelp (result, cmd_types); // General help |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 81 | } |
| 82 | else |
| 83 | { |
| 84 | // Get command object for the first command argument. Only search built-in command dictionary. |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 85 | StringList matches; |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 86 | cmd_obj = m_interpreter.GetCommandObject (command.GetArgumentAtIndex (0), &matches); |
Caroline Tice | e6866a3 | 2010-10-28 23:17:48 +0000 | [diff] [blame] | 87 | bool is_alias_command = m_interpreter.AliasExists (command.GetArgumentAtIndex (0)); |
| 88 | std::string alias_name = command.GetArgumentAtIndex(0); |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 89 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 90 | if (cmd_obj != NULL) |
| 91 | { |
Jim Ingham | c2d382c | 2010-11-30 22:59:37 +0000 | [diff] [blame] | 92 | StringList matches; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 93 | 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. |
| 97 | for (int i = 1; i < argc && all_okay; ++i) |
| 98 | { |
| 99 | std::string sub_command = command.GetArgumentAtIndex(i); |
Jim Ingham | c2d382c | 2010-11-30 22:59:37 +0000 | [diff] [blame] | 100 | matches.Clear(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 101 | if (! sub_cmd_obj->IsMultiwordObject ()) |
| 102 | { |
| 103 | all_okay = false; |
| 104 | } |
| 105 | else |
| 106 | { |
Jim Ingham | c2d382c | 2010-11-30 22:59:37 +0000 | [diff] [blame] | 107 | CommandObject *found_cmd; |
Greg Clayton | 13193d5 | 2012-10-13 02:07:45 +0000 | [diff] [blame] | 108 | found_cmd = sub_cmd_obj->GetSubcommandObject(sub_command.c_str(), &matches); |
Jim Ingham | c2d382c | 2010-11-30 22:59:37 +0000 | [diff] [blame] | 109 | if (found_cmd == NULL) |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 110 | all_okay = false; |
Jim Ingham | dd1f02a | 2010-12-01 00:42:17 +0000 | [diff] [blame] | 111 | else if (matches.GetSize() > 1) |
Jim Ingham | c2d382c | 2010-11-30 22:59:37 +0000 | [diff] [blame] | 112 | all_okay = false; |
| 113 | else |
| 114 | sub_cmd_obj = found_cmd; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 115 | } |
| 116 | } |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 117 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 118 | if (!all_okay || (sub_cmd_obj == NULL)) |
| 119 | { |
| 120 | std::string cmd_string; |
| 121 | command.GetCommandString (cmd_string); |
Jim Ingham | c2d382c | 2010-11-30 22:59:37 +0000 | [diff] [blame] | 122 | if (matches.GetSize() < 2) |
| 123 | { |
| 124 | result.AppendErrorWithFormat("'%s' is not a known command.\n" |
| 125 | "Try 'help' to see a current list of commands.\n", |
| 126 | cmd_string.c_str()); |
| 127 | } |
| 128 | else |
| 129 | { |
| 130 | StreamString s; |
| 131 | s.Printf ("ambiguous command %s", cmd_string.c_str()); |
| 132 | size_t num_matches = matches.GetSize(); |
| 133 | for (size_t match_idx = 0; match_idx < num_matches; match_idx++) |
| 134 | { |
| 135 | s.Printf ("\n\t%s", matches.GetStringAtIndex(match_idx)); |
| 136 | } |
| 137 | s.Printf ("\n"); |
| 138 | result.AppendError(s.GetData()); |
| 139 | } |
| 140 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 141 | result.SetStatus (eReturnStatusFailed); |
| 142 | } |
| 143 | else |
| 144 | { |
| 145 | Stream &output_strm = result.GetOutputStream(); |
| 146 | if (sub_cmd_obj->GetOptions() != NULL) |
| 147 | { |
Caroline Tice | fb35511 | 2010-10-01 17:46:38 +0000 | [diff] [blame] | 148 | if (sub_cmd_obj->WantsRawCommandString()) |
| 149 | { |
| 150 | std::string help_text (sub_cmd_obj->GetHelp()); |
| 151 | help_text.append (" This command takes 'raw' input (no need to quote stuff)."); |
| 152 | m_interpreter.OutputFormattedHelpText (output_strm, "", "", help_text.c_str(), 1); |
| 153 | } |
| 154 | else |
| 155 | m_interpreter.OutputFormattedHelpText (output_strm, "", "", sub_cmd_obj->GetHelp(), 1); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 156 | output_strm.Printf ("\nSyntax: %s\n", sub_cmd_obj->GetSyntax()); |
Greg Clayton | f15996e | 2011-04-07 22:46:35 +0000 | [diff] [blame] | 157 | sub_cmd_obj->GetOptions()->GenerateOptionUsage (output_strm, sub_cmd_obj); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 158 | const char *long_help = sub_cmd_obj->GetHelpLong(); |
| 159 | if ((long_help != NULL) |
| 160 | && (strlen (long_help) > 0)) |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 161 | output_strm.Printf ("\n%s", long_help); |
Johnny Chen | fd8ef2e | 2012-01-27 18:49:33 +0000 | [diff] [blame] | 162 | if (sub_cmd_obj->WantsRawCommandString() && !sub_cmd_obj->WantsCompletion()) |
Caroline Tice | 44c841d | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 163 | { |
Jim Ingham | 7950572 | 2012-05-02 01:12:54 +0000 | [diff] [blame] | 164 | // Emit the message about using ' -- ' between the end of the command options and the raw input |
| 165 | // conditionally, i.e., only if the command object does not want completion. |
| 166 | m_interpreter.OutputFormattedHelpText (output_strm, "", "", |
| 167 | "\nIMPORTANT NOTE: Because this command takes 'raw' input, if you use any command options" |
| 168 | " you must use ' -- ' between the end of the command options and the beginning of the raw input.", 1); |
Caroline Tice | 44c841d | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 169 | } |
Jim Ingham | 7950572 | 2012-05-02 01:12:54 +0000 | [diff] [blame] | 170 | else if (sub_cmd_obj->GetNumArgumentEntries() > 0 |
| 171 | && sub_cmd_obj->GetOptions() |
| 172 | && sub_cmd_obj->GetOptions()->NumCommandOptions() > 0) |
| 173 | { |
| 174 | // Also emit a warning about using "--" in case you are using a command that takes options and arguments. |
| 175 | m_interpreter.OutputFormattedHelpText (output_strm, "", "", |
| 176 | "\nThis command takes options and arguments, if your arguments look like option specifiers" |
| 177 | " you must use '--' to terminate the options before starting to give the arguments.", 1); |
| 178 | } |
| 179 | |
Johnny Chen | fd8ef2e | 2012-01-27 18:49:33 +0000 | [diff] [blame] | 180 | // Mark this help command with a success status. |
Johnny Chen | 8e46859 | 2010-10-08 17:21:27 +0000 | [diff] [blame] | 181 | result.SetStatus (eReturnStatusSuccessFinishNoResult); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 182 | } |
| 183 | else if (sub_cmd_obj->IsMultiwordObject()) |
| 184 | { |
Caroline Tice | fb35511 | 2010-10-01 17:46:38 +0000 | [diff] [blame] | 185 | if (sub_cmd_obj->WantsRawCommandString()) |
| 186 | { |
| 187 | std::string help_text (sub_cmd_obj->GetHelp()); |
| 188 | help_text.append (" This command takes 'raw' input (no need to quote stuff)."); |
| 189 | m_interpreter.OutputFormattedHelpText (output_strm, "", "", help_text.c_str(), 1); |
| 190 | } |
| 191 | else |
| 192 | m_interpreter.OutputFormattedHelpText (output_strm, "", "", sub_cmd_obj->GetHelp(), 1); |
Greg Clayton | 13193d5 | 2012-10-13 02:07:45 +0000 | [diff] [blame] | 193 | sub_cmd_obj->GenerateHelpText (result); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 194 | } |
| 195 | else |
| 196 | { |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 197 | const char *long_help = sub_cmd_obj->GetHelpLong(); |
| 198 | if ((long_help != NULL) |
| 199 | && (strlen (long_help) > 0)) |
Johnny Chen | 1d4e4d5 | 2011-10-28 23:30:28 +0000 | [diff] [blame] | 200 | output_strm.Printf ("%s", long_help); |
Caroline Tice | fb35511 | 2010-10-01 17:46:38 +0000 | [diff] [blame] | 201 | else if (sub_cmd_obj->WantsRawCommandString()) |
| 202 | { |
| 203 | std::string help_text (sub_cmd_obj->GetHelp()); |
| 204 | help_text.append (" This command takes 'raw' input (no need to quote stuff)."); |
| 205 | m_interpreter.OutputFormattedHelpText (output_strm, "", "", help_text.c_str(), 1); |
| 206 | } |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 207 | else |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 208 | m_interpreter.OutputFormattedHelpText (output_strm, "", "", sub_cmd_obj->GetHelp(), 1); |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 209 | output_strm.Printf ("\nSyntax: %s\n", sub_cmd_obj->GetSyntax()); |
Johnny Chen | 8e46859 | 2010-10-08 17:21:27 +0000 | [diff] [blame] | 210 | // Mark this help command with a success status. |
| 211 | result.SetStatus (eReturnStatusSuccessFinishNoResult); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 212 | } |
| 213 | } |
Caroline Tice | e6866a3 | 2010-10-28 23:17:48 +0000 | [diff] [blame] | 214 | |
| 215 | if (is_alias_command) |
| 216 | { |
| 217 | StreamString sstr; |
| 218 | m_interpreter.GetAliasHelp (alias_name.c_str(), cmd_obj->GetCommandName(), sstr); |
| 219 | result.GetOutputStream().Printf ("\n'%s' is an abbreviation for %s\n", alias_name.c_str(), sstr.GetData()); |
| 220 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 221 | } |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 222 | else if (matches.GetSize() > 0) |
| 223 | { |
| 224 | Stream &output_strm = result.GetOutputStream(); |
| 225 | output_strm.Printf("Help requested with ambiguous command name, possible completions:\n"); |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 226 | const uint32_t match_count = matches.GetSize(); |
| 227 | for (uint32_t i = 0; i < match_count; i++) |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 228 | { |
| 229 | output_strm.Printf("\t%s\n", matches.GetStringAtIndex(i)); |
| 230 | } |
| 231 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 232 | else |
| 233 | { |
Caroline Tice | fb35511 | 2010-10-01 17:46:38 +0000 | [diff] [blame] | 234 | // Maybe the user is asking for help about a command argument rather than a command. |
| 235 | const CommandArgumentType arg_type = CommandObject::LookupArgumentName (command.GetArgumentAtIndex (0)); |
| 236 | if (arg_type != eArgTypeLastArg) |
| 237 | { |
| 238 | Stream &output_strm = result.GetOutputStream (); |
| 239 | CommandObject::GetArgumentHelp (output_strm, arg_type, m_interpreter); |
| 240 | result.SetStatus (eReturnStatusSuccessFinishNoResult); |
| 241 | } |
| 242 | else |
| 243 | { |
| 244 | result.AppendErrorWithFormat |
| 245 | ("'%s' is not a known command.\nTry 'help' to see a current list of commands.\n", |
| 246 | command.GetArgumentAtIndex(0)); |
| 247 | result.SetStatus (eReturnStatusFailed); |
| 248 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 249 | } |
| 250 | } |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 251 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 252 | return result.Succeeded(); |
| 253 | } |
| 254 | |
| 255 | int |
| 256 | CommandObjectHelp::HandleCompletion |
| 257 | ( |
| 258 | Args &input, |
| 259 | int &cursor_index, |
| 260 | int &cursor_char_position, |
| 261 | int match_start_point, |
| 262 | int max_return_elements, |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 263 | bool &word_complete, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 264 | StringList &matches |
| 265 | ) |
| 266 | { |
| 267 | // Return the completions of the commands in the help system: |
| 268 | if (cursor_index == 0) |
| 269 | { |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 270 | return m_interpreter.HandleCompletionMatches (input, |
| 271 | cursor_index, |
| 272 | cursor_char_position, |
| 273 | match_start_point, |
| 274 | max_return_elements, |
| 275 | word_complete, |
| 276 | matches); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 277 | } |
| 278 | else |
| 279 | { |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 280 | CommandObject *cmd_obj = m_interpreter.GetCommandObject (input.GetArgumentAtIndex(0)); |
Jim Ingham | eeddf87 | 2011-10-26 19:32:01 +0000 | [diff] [blame] | 281 | |
| 282 | // The command that they are getting help on might be ambiguous, in which case we should complete that, |
| 283 | // otherwise complete with the command the user is getting help on... |
| 284 | |
| 285 | if (cmd_obj) |
| 286 | { |
| 287 | input.Shift(); |
| 288 | cursor_index--; |
| 289 | return cmd_obj->HandleCompletion (input, |
| 290 | cursor_index, |
| 291 | cursor_char_position, |
| 292 | match_start_point, |
| 293 | max_return_elements, |
| 294 | word_complete, |
| 295 | matches); |
| 296 | } |
| 297 | else |
| 298 | { |
| 299 | return m_interpreter.HandleCompletionMatches (input, |
| 300 | cursor_index, |
| 301 | cursor_char_position, |
| 302 | match_start_point, |
| 303 | max_return_elements, |
| 304 | word_complete, |
| 305 | matches); |
| 306 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 307 | } |
| 308 | } |