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>]") 33 { 34 } 35 36 CommandObjectHelp::~CommandObjectHelp() 37 { 38 } 39 40 41 bool 42 CommandObjectHelp::Execute (Args& command, CommandReturnObject &result) 43 { 44 CommandObject::CommandMap::iterator pos; 45 CommandObject *cmd_obj; 46 const int argc = command.GetArgumentCount (); 47 48 // 'help' doesn't take any options or arguments, other than command names. If argc is 0, we show the user 49 // all commands and aliases. Otherwise every argument must be the name of a command or a sub-command. 50 if (argc == 0) 51 { 52 result.SetStatus (eReturnStatusSuccessFinishNoResult); 53 m_interpreter.GetHelp (result); // General help, for ALL commands. 54 } 55 else 56 { 57 // Get command object for the first command argument. Only search built-in command dictionary. 58 StringList matches; 59 cmd_obj = m_interpreter.GetCommandObject (command.GetArgumentAtIndex (0), &matches); 60 61 if (cmd_obj != NULL) 62 { 63 bool all_okay = true; 64 CommandObject *sub_cmd_obj = cmd_obj; 65 // Loop down through sub_command dictionaries until we find the command object that corresponds 66 // to the help command entered. 67 for (int i = 1; i < argc && all_okay; ++i) 68 { 69 std::string sub_command = command.GetArgumentAtIndex(i); 70 if (! sub_cmd_obj->IsMultiwordObject ()) 71 { 72 all_okay = false; 73 } 74 else 75 { 76 pos = ((CommandObjectMultiword *) sub_cmd_obj)->m_subcommand_dict.find (sub_command); 77 if (pos != ((CommandObjectMultiword *) sub_cmd_obj)->m_subcommand_dict.end()) 78 sub_cmd_obj = pos->second.get(); 79 else 80 all_okay = false; 81 } 82 } 83 84 if (!all_okay || (sub_cmd_obj == NULL)) 85 { 86 std::string cmd_string; 87 command.GetCommandString (cmd_string); 88 result.AppendErrorWithFormat 89 ("'%s' is not a known command.\nTry 'help' to see a current list of commands.\n", 90 cmd_string.c_str()); 91 result.SetStatus (eReturnStatusFailed); 92 } 93 else 94 { 95 Stream &output_strm = result.GetOutputStream(); 96 if (sub_cmd_obj->GetOptions() != NULL) 97 { 98 m_interpreter.OutputFormattedHelpText (output_strm, "", "", sub_cmd_obj->GetHelp(), 1); 99 output_strm.Printf ("\nSyntax: %s\n", sub_cmd_obj->GetSyntax()); 100 sub_cmd_obj->GetOptions()->GenerateOptionUsage (m_interpreter, output_strm, sub_cmd_obj); 101 const char *long_help = sub_cmd_obj->GetHelpLong(); 102 if ((long_help != NULL) 103 && (strlen (long_help) > 0)) 104 output_strm.Printf ("\n%s", long_help); 105 } 106 else if (sub_cmd_obj->IsMultiwordObject()) 107 { 108 m_interpreter.OutputFormattedHelpText (output_strm, "", "", sub_cmd_obj->GetHelp(), 1); 109 ((CommandObjectMultiword *) sub_cmd_obj)->GenerateHelpText (result); 110 } 111 else 112 { 113 const char *long_help = sub_cmd_obj->GetHelpLong(); 114 if ((long_help != NULL) 115 && (strlen (long_help) > 0)) 116 output_strm.Printf ("\n%s", long_help); 117 else 118 m_interpreter.OutputFormattedHelpText (output_strm, "", "", sub_cmd_obj->GetHelp(), 1); 119 output_strm.Printf ("\nSyntax: %s\n", sub_cmd_obj->GetSyntax()); 120 } 121 } 122 } 123 else if (matches.GetSize() > 0) 124 { 125 Stream &output_strm = result.GetOutputStream(); 126 output_strm.Printf("Help requested with ambiguous command name, possible completions:\n"); 127 const uint32_t match_count = matches.GetSize(); 128 for (uint32_t i = 0; i < match_count; i++) 129 { 130 output_strm.Printf("\t%s\n", matches.GetStringAtIndex(i)); 131 } 132 } 133 else 134 { 135 result.AppendErrorWithFormat 136 ("'%s' is not a known command.\nTry 'help' to see a current list of commands.\n", 137 command.GetArgumentAtIndex(0)); 138 result.SetStatus (eReturnStatusFailed); 139 } 140 } 141 142 return result.Succeeded(); 143 } 144 145 int 146 CommandObjectHelp::HandleCompletion 147 ( 148 Args &input, 149 int &cursor_index, 150 int &cursor_char_position, 151 int match_start_point, 152 int max_return_elements, 153 bool &word_complete, 154 StringList &matches 155 ) 156 { 157 // Return the completions of the commands in the help system: 158 if (cursor_index == 0) 159 { 160 return m_interpreter.HandleCompletionMatches (input, 161 cursor_index, 162 cursor_char_position, 163 match_start_point, 164 max_return_elements, 165 word_complete, 166 matches); 167 } 168 else 169 { 170 CommandObject *cmd_obj = m_interpreter.GetCommandObject (input.GetArgumentAtIndex(0)); 171 input.Shift(); 172 cursor_index--; 173 return cmd_obj->HandleCompletion (input, 174 cursor_index, 175 cursor_char_position, 176 match_start_point, 177 max_return_elements, 178 word_complete, 179 matches); 180 } 181 } 182