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