1 //===-- CommandObjectHelp.cpp -----------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/lldb-python.h"
12 #include "CommandObjectHelp.h"
16 // Other libraries and framework includes
18 #include "lldb/Interpreter/CommandObjectMultiword.h"
19 #include "lldb/Interpreter/CommandInterpreter.h"
20 #include "lldb/Interpreter/Options.h"
21 #include "lldb/Interpreter/CommandReturnObject.h"
24 using namespace lldb_private;
26 //-------------------------------------------------------------------------
28 //-------------------------------------------------------------------------
30 CommandObjectHelp::CommandObjectHelp (CommandInterpreter &interpreter) :
31 CommandObjectParsed (interpreter,
33 "Show a list of all debugger commands, or give details about specific commands.",
34 "help [<cmd-name>]"), m_options (interpreter)
36 CommandArgumentEntry arg;
37 CommandArgumentData command_arg;
39 // Define the first (and only) variant of this arg.
40 command_arg.arg_type = eArgTypeCommandName;
41 command_arg.arg_repetition = eArgRepeatStar;
43 // There is only one variant this argument could be; put it into the argument entry.
44 arg.push_back (command_arg);
46 // Push the data for the first argument into the m_arguments vector.
47 m_arguments.push_back (arg);
50 CommandObjectHelp::~CommandObjectHelp()
55 CommandObjectHelp::CommandOptions::g_option_table[] =
57 { LLDB_OPT_SET_ALL, false, "show-aliases", 'a', OptionParser::eNoArgument, NULL, 0, eArgTypeNone, "Show aliases in the command list."},
58 { LLDB_OPT_SET_ALL, false, "hide-user-commands", 'u', OptionParser::eNoArgument, NULL, 0, eArgTypeNone, "Hide user-defined commands from the list."},
59 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
63 CommandObjectHelp::DoExecute (Args& command, CommandReturnObject &result)
65 CommandObject::CommandMap::iterator pos;
66 CommandObject *cmd_obj;
67 const size_t argc = command.GetArgumentCount ();
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.
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;
79 result.SetStatus (eReturnStatusSuccessFinishNoResult);
80 m_interpreter.GetHelp (result, cmd_types); // General help
84 // Get command object for the first command argument. Only search built-in command dictionary.
86 cmd_obj = m_interpreter.GetCommandObject (command.GetArgumentAtIndex (0), &matches);
87 bool is_alias_command = m_interpreter.AliasExists (command.GetArgumentAtIndex (0));
88 std::string alias_name = command.GetArgumentAtIndex(0);
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 (size_t i = 1; i < argc && all_okay; ++i)
99 std::string sub_command = command.GetArgumentAtIndex(i);
101 if (! sub_cmd_obj->IsMultiwordObject ())
107 CommandObject *found_cmd;
108 found_cmd = sub_cmd_obj->GetSubcommandObject(sub_command.c_str(), &matches);
109 if (found_cmd == NULL)
111 else if (matches.GetSize() > 1)
114 sub_cmd_obj = found_cmd;
118 if (!all_okay || (sub_cmd_obj == NULL))
120 std::string cmd_string;
121 command.GetCommandString (cmd_string);
122 if (matches.GetSize() >= 2)
125 s.Printf ("ambiguous command %s", cmd_string.c_str());
126 size_t num_matches = matches.GetSize();
127 for (size_t match_idx = 0; match_idx < num_matches; match_idx++)
129 s.Printf ("\n\t%s", matches.GetStringAtIndex(match_idx));
132 result.AppendError(s.GetData());
133 result.SetStatus (eReturnStatusFailed);
136 else if (!sub_cmd_obj)
138 result.AppendErrorWithFormat("'%s' is not a known command.\n"
139 "Try 'help' to see a current list of commands.\n",
141 result.SetStatus (eReturnStatusFailed);
146 result.GetOutputStream().Printf("'%s' is not a known command.\n"
147 "Try 'help' to see a current list of commands.\n"
148 "The closest match is '%s'. Help on it follows.\n\n",
150 sub_cmd_obj->GetCommandName());
154 sub_cmd_obj->GenerateHelpText(result);
156 if (is_alias_command)
159 m_interpreter.GetAliasHelp (alias_name.c_str(), cmd_obj->GetCommandName(), sstr);
160 result.GetOutputStream().Printf ("\n'%s' is an abbreviation for %s\n", alias_name.c_str(), sstr.GetData());
163 else if (matches.GetSize() > 0)
165 Stream &output_strm = result.GetOutputStream();
166 output_strm.Printf("Help requested with ambiguous command name, possible completions:\n");
167 const size_t match_count = matches.GetSize();
168 for (size_t i = 0; i < match_count; i++)
170 output_strm.Printf("\t%s\n", matches.GetStringAtIndex(i));
175 // Maybe the user is asking for help about a command argument rather than a command.
176 const CommandArgumentType arg_type = CommandObject::LookupArgumentName (command.GetArgumentAtIndex (0));
177 if (arg_type != eArgTypeLastArg)
179 Stream &output_strm = result.GetOutputStream ();
180 CommandObject::GetArgumentHelp (output_strm, arg_type, m_interpreter);
181 result.SetStatus (eReturnStatusSuccessFinishNoResult);
185 result.AppendErrorWithFormat
186 ("'%s' is not a known command.\nTry 'help' to see a current list of commands.\n",
187 command.GetArgumentAtIndex(0));
188 result.SetStatus (eReturnStatusFailed);
193 return result.Succeeded();
197 CommandObjectHelp::HandleCompletion
201 int &cursor_char_position,
202 int match_start_point,
203 int max_return_elements,
208 // Return the completions of the commands in the help system:
209 if (cursor_index == 0)
211 return m_interpreter.HandleCompletionMatches (input,
213 cursor_char_position,
221 CommandObject *cmd_obj = m_interpreter.GetCommandObject (input.GetArgumentAtIndex(0));
223 // The command that they are getting help on might be ambiguous, in which case we should complete that,
224 // otherwise complete with the command the user is getting help on...
230 return cmd_obj->HandleCompletion (input,
232 cursor_char_position,
240 return m_interpreter.HandleCompletionMatches (input,
242 cursor_char_position,