blob: 4f8b752a2ed76b8e2b6d6de06d7ebc00aa9b3d0c [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{
34}
35
36CommandObjectHelp::~CommandObjectHelp()
37{
38}
39
40
41bool
Greg Clayton238c0a12010-09-18 01:14:36 +000042CommandObjectHelp::Execute (Args& command, CommandReturnObject &result)
Chris Lattner24943d22010-06-08 16:52:24 +000043{
44 CommandObject::CommandMap::iterator pos;
45 CommandObject *cmd_obj;
46 const int argc = command.GetArgumentCount ();
Greg Clayton63094e02010-06-23 01:19:29 +000047
Chris Lattner24943d22010-06-08 16:52:24 +000048 // '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.
Chris Lattner24943d22010-06-08 16:52:24 +000050 if (argc == 0)
51 {
52 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Greg Clayton238c0a12010-09-18 01:14:36 +000053 m_interpreter.GetHelp (result); // General help, for ALL commands.
Chris Lattner24943d22010-06-08 16:52:24 +000054 }
55 else
56 {
57 // Get command object for the first command argument. Only search built-in command dictionary.
Jim Inghamd40f8a62010-07-06 22:46:59 +000058 StringList matches;
Greg Clayton238c0a12010-09-18 01:14:36 +000059 cmd_obj = m_interpreter.GetCommandObject (command.GetArgumentAtIndex (0), &matches);
Greg Clayton63094e02010-06-23 01:19:29 +000060
Chris Lattner24943d22010-06-08 16:52:24 +000061 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())
Greg Clayton63094e02010-06-23 01:19:29 +000078 sub_cmd_obj = pos->second.get();
Chris Lattner24943d22010-06-08 16:52:24 +000079 else
Greg Clayton63094e02010-06-23 01:19:29 +000080 all_okay = false;
Chris Lattner24943d22010-06-08 16:52:24 +000081 }
82 }
Greg Clayton63094e02010-06-23 01:19:29 +000083
Chris Lattner24943d22010-06-08 16:52:24 +000084 if (!all_okay || (sub_cmd_obj == NULL))
85 {
86 std::string cmd_string;
87 command.GetCommandString (cmd_string);
88 result.AppendErrorWithFormat
Greg Clayton63094e02010-06-23 01:19:29 +000089 ("'%s' is not a known command.\nTry 'help' to see a current list of commands.\n",
90 cmd_string.c_str());
Chris Lattner24943d22010-06-08 16:52:24 +000091 result.SetStatus (eReturnStatusFailed);
92 }
93 else
94 {
95 Stream &output_strm = result.GetOutputStream();
96 if (sub_cmd_obj->GetOptions() != NULL)
97 {
Caroline Ticefb355112010-10-01 17:46:38 +000098 if (sub_cmd_obj->WantsRawCommandString())
99 {
100 std::string help_text (sub_cmd_obj->GetHelp());
101 help_text.append (" This command takes 'raw' input (no need to quote stuff).");
102 m_interpreter.OutputFormattedHelpText (output_strm, "", "", help_text.c_str(), 1);
103 }
104 else
105 m_interpreter.OutputFormattedHelpText (output_strm, "", "", sub_cmd_obj->GetHelp(), 1);
Chris Lattner24943d22010-06-08 16:52:24 +0000106 output_strm.Printf ("\nSyntax: %s\n", sub_cmd_obj->GetSyntax());
Greg Clayton238c0a12010-09-18 01:14:36 +0000107 sub_cmd_obj->GetOptions()->GenerateOptionUsage (m_interpreter, output_strm, sub_cmd_obj);
Chris Lattner24943d22010-06-08 16:52:24 +0000108 const char *long_help = sub_cmd_obj->GetHelpLong();
109 if ((long_help != NULL)
110 && (strlen (long_help) > 0))
Greg Clayton63094e02010-06-23 01:19:29 +0000111 output_strm.Printf ("\n%s", long_help);
Chris Lattner24943d22010-06-08 16:52:24 +0000112 }
113 else if (sub_cmd_obj->IsMultiwordObject())
114 {
Caroline Ticefb355112010-10-01 17:46:38 +0000115 if (sub_cmd_obj->WantsRawCommandString())
116 {
117 std::string help_text (sub_cmd_obj->GetHelp());
118 help_text.append (" This command takes 'raw' input (no need to quote stuff).");
119 m_interpreter.OutputFormattedHelpText (output_strm, "", "", help_text.c_str(), 1);
120 }
121 else
122 m_interpreter.OutputFormattedHelpText (output_strm, "", "", sub_cmd_obj->GetHelp(), 1);
Greg Clayton238c0a12010-09-18 01:14:36 +0000123 ((CommandObjectMultiword *) sub_cmd_obj)->GenerateHelpText (result);
Chris Lattner24943d22010-06-08 16:52:24 +0000124 }
125 else
126 {
Greg Clayton63094e02010-06-23 01:19:29 +0000127 const char *long_help = sub_cmd_obj->GetHelpLong();
128 if ((long_help != NULL)
129 && (strlen (long_help) > 0))
Caroline Ticeabb507a2010-09-08 21:06:11 +0000130 output_strm.Printf ("\n%s", long_help);
Caroline Ticefb355112010-10-01 17:46:38 +0000131 else if (sub_cmd_obj->WantsRawCommandString())
132 {
133 std::string help_text (sub_cmd_obj->GetHelp());
134 help_text.append (" This command takes 'raw' input (no need to quote stuff).");
135 m_interpreter.OutputFormattedHelpText (output_strm, "", "", help_text.c_str(), 1);
136 }
Greg Clayton63094e02010-06-23 01:19:29 +0000137 else
Greg Clayton238c0a12010-09-18 01:14:36 +0000138 m_interpreter.OutputFormattedHelpText (output_strm, "", "", sub_cmd_obj->GetHelp(), 1);
Greg Clayton63094e02010-06-23 01:19:29 +0000139 output_strm.Printf ("\nSyntax: %s\n", sub_cmd_obj->GetSyntax());
Chris Lattner24943d22010-06-08 16:52:24 +0000140 }
141 }
142 }
Jim Inghamd40f8a62010-07-06 22:46:59 +0000143 else if (matches.GetSize() > 0)
144 {
145 Stream &output_strm = result.GetOutputStream();
146 output_strm.Printf("Help requested with ambiguous command name, possible completions:\n");
Greg Clayton54e7afa2010-07-09 20:39:50 +0000147 const uint32_t match_count = matches.GetSize();
148 for (uint32_t i = 0; i < match_count; i++)
Jim Inghamd40f8a62010-07-06 22:46:59 +0000149 {
150 output_strm.Printf("\t%s\n", matches.GetStringAtIndex(i));
151 }
152 }
Chris Lattner24943d22010-06-08 16:52:24 +0000153 else
154 {
Caroline Ticefb355112010-10-01 17:46:38 +0000155 // Maybe the user is asking for help about a command argument rather than a command.
156 const CommandArgumentType arg_type = CommandObject::LookupArgumentName (command.GetArgumentAtIndex (0));
157 if (arg_type != eArgTypeLastArg)
158 {
159 Stream &output_strm = result.GetOutputStream ();
160 CommandObject::GetArgumentHelp (output_strm, arg_type, m_interpreter);
161 result.SetStatus (eReturnStatusSuccessFinishNoResult);
162 }
163 else
164 {
165 result.AppendErrorWithFormat
166 ("'%s' is not a known command.\nTry 'help' to see a current list of commands.\n",
167 command.GetArgumentAtIndex(0));
168 result.SetStatus (eReturnStatusFailed);
169 }
Chris Lattner24943d22010-06-08 16:52:24 +0000170 }
171 }
Greg Clayton63094e02010-06-23 01:19:29 +0000172
Chris Lattner24943d22010-06-08 16:52:24 +0000173 return result.Succeeded();
174}
175
176int
177CommandObjectHelp::HandleCompletion
178(
179 Args &input,
180 int &cursor_index,
181 int &cursor_char_position,
182 int match_start_point,
183 int max_return_elements,
Jim Ingham802f8b02010-06-30 05:02:46 +0000184 bool &word_complete,
Chris Lattner24943d22010-06-08 16:52:24 +0000185 StringList &matches
186)
187{
188 // Return the completions of the commands in the help system:
189 if (cursor_index == 0)
190 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000191 return m_interpreter.HandleCompletionMatches (input,
192 cursor_index,
193 cursor_char_position,
194 match_start_point,
195 max_return_elements,
196 word_complete,
197 matches);
Chris Lattner24943d22010-06-08 16:52:24 +0000198 }
199 else
200 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000201 CommandObject *cmd_obj = m_interpreter.GetCommandObject (input.GetArgumentAtIndex(0));
Chris Lattner24943d22010-06-08 16:52:24 +0000202 input.Shift();
203 cursor_index--;
Greg Clayton238c0a12010-09-18 01:14:36 +0000204 return cmd_obj->HandleCompletion (input,
205 cursor_index,
206 cursor_char_position,
207 match_start_point,
208 max_return_elements,
209 word_complete,
210 matches);
Chris Lattner24943d22010-06-08 16:52:24 +0000211 }
212}