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" 18 #include "lldb/Interpreter/Options.h" 19 #include "lldb/Interpreter/CommandReturnObject.h" 20 21 using namespace lldb; 22 using namespace lldb_private; 23 24 //------------------------------------------------------------------------- 25 // CommandObjectHelp 26 //------------------------------------------------------------------------- 27 28 CommandObjectHelp::CommandObjectHelp (CommandInterpreter &interpreter) : 29 CommandObject (interpreter, 30 "help", 31 "Show a list of all debugger commands, or give details about specific commands.", 32 "help [<cmd-name>]"), m_options (interpreter) 33 { 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); 46 } 47 48 CommandObjectHelp::~CommandObjectHelp() 49 { 50 } 51 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 60 bool 61 CommandObjectHelp::Execute (Args& command, CommandReturnObject &result) 62 { 63 CommandObject::CommandMap::iterator pos; 64 CommandObject *cmd_obj; 65 const int argc = command.GetArgumentCount (); 66 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. 69 if (argc == 0) 70 { 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 77 result.SetStatus (eReturnStatusSuccessFinishNoResult); 78 m_interpreter.GetHelp (result, cmd_types); // General help 79 } 80 else 81 { 82 // Get command object for the first command argument. Only search built-in command dictionary. 83 StringList matches; 84 cmd_obj = m_interpreter.GetCommandObject (command.GetArgumentAtIndex (0), &matches); 85 bool is_alias_command = m_interpreter.AliasExists (command.GetArgumentAtIndex (0)); 86 std::string alias_name = command.GetArgumentAtIndex(0); 87 88 if (cmd_obj != NULL) 89 { 90 StringList matches; 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); 98 matches.Clear(); 99 if (! sub_cmd_obj->IsMultiwordObject ()) 100 { 101 all_okay = false; 102 } 103 else 104 { 105 CommandObject *found_cmd; 106 found_cmd = ((CommandObjectMultiword *) sub_cmd_obj)->GetSubcommandObject(sub_command.c_str(), 107 &matches); 108 if (found_cmd == NULL) 109 all_okay = false; 110 else if (matches.GetSize() > 1) 111 all_okay = false; 112 else 113 sub_cmd_obj = found_cmd; 114 } 115 } 116 117 if (!all_okay || (sub_cmd_obj == NULL)) 118 { 119 std::string cmd_string; 120 command.GetCommandString (cmd_string); 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 140 result.SetStatus (eReturnStatusFailed); 141 } 142 else 143 { 144 Stream &output_strm = result.GetOutputStream(); 145 if (sub_cmd_obj->GetOptions() != NULL) 146 { 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); 155 output_strm.Printf ("\nSyntax: %s\n", sub_cmd_obj->GetSyntax()); 156 sub_cmd_obj->GetOptions()->GenerateOptionUsage (output_strm, sub_cmd_obj); 157 const char *long_help = sub_cmd_obj->GetHelpLong(); 158 if ((long_help != NULL) 159 && (strlen (long_help) > 0)) 160 output_strm.Printf ("\n%s", long_help); 161 // Emit the message about using ' -- ' between the end of the command options and the raw input 162 // conditionally, i.e., only if the command object does not want completion. 163 if (sub_cmd_obj->WantsRawCommandString() && !sub_cmd_obj->WantsCompletion()) 164 { 165 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); 166 } 167 // Mark this help command with a success status. 168 result.SetStatus (eReturnStatusSuccessFinishNoResult); 169 } 170 else if (sub_cmd_obj->IsMultiwordObject()) 171 { 172 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 } 178 else 179 m_interpreter.OutputFormattedHelpText (output_strm, "", "", sub_cmd_obj->GetHelp(), 1); 180 ((CommandObjectMultiword *) sub_cmd_obj)->GenerateHelpText (result); 181 } 182 else 183 { 184 const char *long_help = sub_cmd_obj->GetHelpLong(); 185 if ((long_help != NULL) 186 && (strlen (long_help) > 0)) 187 output_strm.Printf ("%s", long_help); 188 else if (sub_cmd_obj->WantsRawCommandString()) 189 { 190 std::string help_text (sub_cmd_obj->GetHelp()); 191 help_text.append (" This command takes 'raw' input (no need to quote stuff)."); 192 m_interpreter.OutputFormattedHelpText (output_strm, "", "", help_text.c_str(), 1); 193 } 194 else 195 m_interpreter.OutputFormattedHelpText (output_strm, "", "", sub_cmd_obj->GetHelp(), 1); 196 output_strm.Printf ("\nSyntax: %s\n", sub_cmd_obj->GetSyntax()); 197 // Mark this help command with a success status. 198 result.SetStatus (eReturnStatusSuccessFinishNoResult); 199 } 200 } 201 202 if (is_alias_command) 203 { 204 StreamString sstr; 205 m_interpreter.GetAliasHelp (alias_name.c_str(), cmd_obj->GetCommandName(), sstr); 206 result.GetOutputStream().Printf ("\n'%s' is an abbreviation for %s\n", alias_name.c_str(), sstr.GetData()); 207 } 208 } 209 else if (matches.GetSize() > 0) 210 { 211 Stream &output_strm = result.GetOutputStream(); 212 output_strm.Printf("Help requested with ambiguous command name, possible completions:\n"); 213 const uint32_t match_count = matches.GetSize(); 214 for (uint32_t i = 0; i < match_count; i++) 215 { 216 output_strm.Printf("\t%s\n", matches.GetStringAtIndex(i)); 217 } 218 } 219 else 220 { 221 // Maybe the user is asking for help about a command argument rather than a command. 222 const CommandArgumentType arg_type = CommandObject::LookupArgumentName (command.GetArgumentAtIndex (0)); 223 if (arg_type != eArgTypeLastArg) 224 { 225 Stream &output_strm = result.GetOutputStream (); 226 CommandObject::GetArgumentHelp (output_strm, arg_type, m_interpreter); 227 result.SetStatus (eReturnStatusSuccessFinishNoResult); 228 } 229 else 230 { 231 result.AppendErrorWithFormat 232 ("'%s' is not a known command.\nTry 'help' to see a current list of commands.\n", 233 command.GetArgumentAtIndex(0)); 234 result.SetStatus (eReturnStatusFailed); 235 } 236 } 237 } 238 239 return result.Succeeded(); 240 } 241 242 int 243 CommandObjectHelp::HandleCompletion 244 ( 245 Args &input, 246 int &cursor_index, 247 int &cursor_char_position, 248 int match_start_point, 249 int max_return_elements, 250 bool &word_complete, 251 StringList &matches 252 ) 253 { 254 // Return the completions of the commands in the help system: 255 if (cursor_index == 0) 256 { 257 return m_interpreter.HandleCompletionMatches (input, 258 cursor_index, 259 cursor_char_position, 260 match_start_point, 261 max_return_elements, 262 word_complete, 263 matches); 264 } 265 else 266 { 267 CommandObject *cmd_obj = m_interpreter.GetCommandObject (input.GetArgumentAtIndex(0)); 268 269 // The command that they are getting help on might be ambiguous, in which case we should complete that, 270 // otherwise complete with the command the user is getting help on... 271 272 if (cmd_obj) 273 { 274 input.Shift(); 275 cursor_index--; 276 return cmd_obj->HandleCompletion (input, 277 cursor_index, 278 cursor_char_position, 279 match_start_point, 280 max_return_elements, 281 word_complete, 282 matches); 283 } 284 else 285 { 286 return m_interpreter.HandleCompletionMatches (input, 287 cursor_index, 288 cursor_char_position, 289 match_start_point, 290 max_return_elements, 291 word_complete, 292 matches); 293 } 294 } 295 } 296