blob: ff66639863854415a6d9b4fa991664cc7e122d56 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- 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 Ingham84cdc152010-06-15 19:49:27 +000018#include "lldb/Interpreter/Options.h"
Chris Lattner24943d22010-06-08 16:52:24 +000019#include "lldb/Interpreter/CommandReturnObject.h"
20
21using namespace lldb;
22using namespace lldb_private;
23
24//-------------------------------------------------------------------------
25// CommandObjectHelp
26//-------------------------------------------------------------------------
27
Greg Clayton238c0a12010-09-18 01:14:36 +000028CommandObjectHelp::CommandObjectHelp (CommandInterpreter &interpreter) :
29 CommandObject (interpreter,
30 "help",
Caroline Ticec1ad82e2010-09-07 22:38:08 +000031 "Show a list of all debugger commands, or give details about specific commands.",
Greg Clayton54e7afa2010-07-09 20:39:50 +000032 "help [<cmd-name>]")
Chris Lattner24943d22010-06-08 16:52:24 +000033{
Caroline Tice43b014a2010-10-04 22:28:36 +000034 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 Lattner24943d22010-06-08 16:52:24 +000046}
47
48CommandObjectHelp::~CommandObjectHelp()
49{
50}
51
52
53bool
Greg Clayton238c0a12010-09-18 01:14:36 +000054CommandObjectHelp::Execute (Args& command, CommandReturnObject &result)
Chris Lattner24943d22010-06-08 16:52:24 +000055{
56 CommandObject::CommandMap::iterator pos;
57 CommandObject *cmd_obj;
58 const int argc = command.GetArgumentCount ();
Greg Clayton63094e02010-06-23 01:19:29 +000059
Chris Lattner24943d22010-06-08 16:52:24 +000060 // 'help' doesn't take any options or arguments, other than command names. If argc is 0, we show the user
61 // all commands and aliases. Otherwise every argument must be the name of a command or a sub-command.
Chris Lattner24943d22010-06-08 16:52:24 +000062 if (argc == 0)
63 {
64 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Greg Clayton238c0a12010-09-18 01:14:36 +000065 m_interpreter.GetHelp (result); // General help, for ALL commands.
Chris Lattner24943d22010-06-08 16:52:24 +000066 }
67 else
68 {
69 // Get command object for the first command argument. Only search built-in command dictionary.
Jim Inghamd40f8a62010-07-06 22:46:59 +000070 StringList matches;
Greg Clayton238c0a12010-09-18 01:14:36 +000071 cmd_obj = m_interpreter.GetCommandObject (command.GetArgumentAtIndex (0), &matches);
Greg Clayton63094e02010-06-23 01:19:29 +000072
Chris Lattner24943d22010-06-08 16:52:24 +000073 if (cmd_obj != NULL)
74 {
75 bool all_okay = true;
76 CommandObject *sub_cmd_obj = cmd_obj;
77 // Loop down through sub_command dictionaries until we find the command object that corresponds
78 // to the help command entered.
79 for (int i = 1; i < argc && all_okay; ++i)
80 {
81 std::string sub_command = command.GetArgumentAtIndex(i);
82 if (! sub_cmd_obj->IsMultiwordObject ())
83 {
84 all_okay = false;
85 }
86 else
87 {
88 pos = ((CommandObjectMultiword *) sub_cmd_obj)->m_subcommand_dict.find (sub_command);
89 if (pos != ((CommandObjectMultiword *) sub_cmd_obj)->m_subcommand_dict.end())
Greg Clayton63094e02010-06-23 01:19:29 +000090 sub_cmd_obj = pos->second.get();
Chris Lattner24943d22010-06-08 16:52:24 +000091 else
Greg Clayton63094e02010-06-23 01:19:29 +000092 all_okay = false;
Chris Lattner24943d22010-06-08 16:52:24 +000093 }
94 }
Greg Clayton63094e02010-06-23 01:19:29 +000095
Chris Lattner24943d22010-06-08 16:52:24 +000096 if (!all_okay || (sub_cmd_obj == NULL))
97 {
98 std::string cmd_string;
99 command.GetCommandString (cmd_string);
100 result.AppendErrorWithFormat
Greg Clayton63094e02010-06-23 01:19:29 +0000101 ("'%s' is not a known command.\nTry 'help' to see a current list of commands.\n",
102 cmd_string.c_str());
Chris Lattner24943d22010-06-08 16:52:24 +0000103 result.SetStatus (eReturnStatusFailed);
104 }
105 else
106 {
107 Stream &output_strm = result.GetOutputStream();
108 if (sub_cmd_obj->GetOptions() != NULL)
109 {
Caroline Ticefb355112010-10-01 17:46:38 +0000110 if (sub_cmd_obj->WantsRawCommandString())
111 {
112 std::string help_text (sub_cmd_obj->GetHelp());
113 help_text.append (" This command takes 'raw' input (no need to quote stuff).");
114 m_interpreter.OutputFormattedHelpText (output_strm, "", "", help_text.c_str(), 1);
115 }
116 else
117 m_interpreter.OutputFormattedHelpText (output_strm, "", "", sub_cmd_obj->GetHelp(), 1);
Chris Lattner24943d22010-06-08 16:52:24 +0000118 output_strm.Printf ("\nSyntax: %s\n", sub_cmd_obj->GetSyntax());
Greg Clayton238c0a12010-09-18 01:14:36 +0000119 sub_cmd_obj->GetOptions()->GenerateOptionUsage (m_interpreter, output_strm, sub_cmd_obj);
Chris Lattner24943d22010-06-08 16:52:24 +0000120 const char *long_help = sub_cmd_obj->GetHelpLong();
121 if ((long_help != NULL)
122 && (strlen (long_help) > 0))
Greg Clayton63094e02010-06-23 01:19:29 +0000123 output_strm.Printf ("\n%s", long_help);
Chris Lattner24943d22010-06-08 16:52:24 +0000124 }
125 else if (sub_cmd_obj->IsMultiwordObject())
126 {
Caroline Ticefb355112010-10-01 17:46:38 +0000127 if (sub_cmd_obj->WantsRawCommandString())
128 {
129 std::string help_text (sub_cmd_obj->GetHelp());
130 help_text.append (" This command takes 'raw' input (no need to quote stuff).");
131 m_interpreter.OutputFormattedHelpText (output_strm, "", "", help_text.c_str(), 1);
132 }
133 else
134 m_interpreter.OutputFormattedHelpText (output_strm, "", "", sub_cmd_obj->GetHelp(), 1);
Greg Clayton238c0a12010-09-18 01:14:36 +0000135 ((CommandObjectMultiword *) sub_cmd_obj)->GenerateHelpText (result);
Chris Lattner24943d22010-06-08 16:52:24 +0000136 }
137 else
138 {
Greg Clayton63094e02010-06-23 01:19:29 +0000139 const char *long_help = sub_cmd_obj->GetHelpLong();
140 if ((long_help != NULL)
141 && (strlen (long_help) > 0))
Caroline Ticeabb507a2010-09-08 21:06:11 +0000142 output_strm.Printf ("\n%s", long_help);
Caroline Ticefb355112010-10-01 17:46:38 +0000143 else if (sub_cmd_obj->WantsRawCommandString())
144 {
145 std::string help_text (sub_cmd_obj->GetHelp());
146 help_text.append (" This command takes 'raw' input (no need to quote stuff).");
147 m_interpreter.OutputFormattedHelpText (output_strm, "", "", help_text.c_str(), 1);
148 }
Greg Clayton63094e02010-06-23 01:19:29 +0000149 else
Greg Clayton238c0a12010-09-18 01:14:36 +0000150 m_interpreter.OutputFormattedHelpText (output_strm, "", "", sub_cmd_obj->GetHelp(), 1);
Greg Clayton63094e02010-06-23 01:19:29 +0000151 output_strm.Printf ("\nSyntax: %s\n", sub_cmd_obj->GetSyntax());
Chris Lattner24943d22010-06-08 16:52:24 +0000152 }
153 }
154 }
Jim Inghamd40f8a62010-07-06 22:46:59 +0000155 else if (matches.GetSize() > 0)
156 {
157 Stream &output_strm = result.GetOutputStream();
158 output_strm.Printf("Help requested with ambiguous command name, possible completions:\n");
Greg Clayton54e7afa2010-07-09 20:39:50 +0000159 const uint32_t match_count = matches.GetSize();
160 for (uint32_t i = 0; i < match_count; i++)
Jim Inghamd40f8a62010-07-06 22:46:59 +0000161 {
162 output_strm.Printf("\t%s\n", matches.GetStringAtIndex(i));
163 }
164 }
Chris Lattner24943d22010-06-08 16:52:24 +0000165 else
166 {
Caroline Ticefb355112010-10-01 17:46:38 +0000167 // Maybe the user is asking for help about a command argument rather than a command.
168 const CommandArgumentType arg_type = CommandObject::LookupArgumentName (command.GetArgumentAtIndex (0));
169 if (arg_type != eArgTypeLastArg)
170 {
171 Stream &output_strm = result.GetOutputStream ();
172 CommandObject::GetArgumentHelp (output_strm, arg_type, m_interpreter);
173 result.SetStatus (eReturnStatusSuccessFinishNoResult);
174 }
175 else
176 {
177 result.AppendErrorWithFormat
178 ("'%s' is not a known command.\nTry 'help' to see a current list of commands.\n",
179 command.GetArgumentAtIndex(0));
180 result.SetStatus (eReturnStatusFailed);
181 }
Chris Lattner24943d22010-06-08 16:52:24 +0000182 }
183 }
Greg Clayton63094e02010-06-23 01:19:29 +0000184
Chris Lattner24943d22010-06-08 16:52:24 +0000185 return result.Succeeded();
186}
187
188int
189CommandObjectHelp::HandleCompletion
190(
191 Args &input,
192 int &cursor_index,
193 int &cursor_char_position,
194 int match_start_point,
195 int max_return_elements,
Jim Ingham802f8b02010-06-30 05:02:46 +0000196 bool &word_complete,
Chris Lattner24943d22010-06-08 16:52:24 +0000197 StringList &matches
198)
199{
200 // Return the completions of the commands in the help system:
201 if (cursor_index == 0)
202 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000203 return m_interpreter.HandleCompletionMatches (input,
204 cursor_index,
205 cursor_char_position,
206 match_start_point,
207 max_return_elements,
208 word_complete,
209 matches);
Chris Lattner24943d22010-06-08 16:52:24 +0000210 }
211 else
212 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000213 CommandObject *cmd_obj = m_interpreter.GetCommandObject (input.GetArgumentAtIndex(0));
Chris Lattner24943d22010-06-08 16:52:24 +0000214 input.Shift();
215 cursor_index--;
Greg Clayton238c0a12010-09-18 01:14:36 +0000216 return cmd_obj->HandleCompletion (input,
217 cursor_index,
218 cursor_char_position,
219 match_start_point,
220 max_return_elements,
221 word_complete,
222 matches);
Chris Lattner24943d22010-06-08 16:52:24 +0000223 }
224}