blob: 2d4b9f28482b8c1e1d79ecd8b16d3528ee873dca [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 {
Greg Clayton238c0a12010-09-18 01:14:36 +000098 m_interpreter.OutputFormattedHelpText (output_strm, "", "", sub_cmd_obj->GetHelp(), 1);
Chris Lattner24943d22010-06-08 16:52:24 +000099 output_strm.Printf ("\nSyntax: %s\n", sub_cmd_obj->GetSyntax());
Greg Clayton238c0a12010-09-18 01:14:36 +0000100 sub_cmd_obj->GetOptions()->GenerateOptionUsage (m_interpreter, output_strm, sub_cmd_obj);
Chris Lattner24943d22010-06-08 16:52:24 +0000101 const char *long_help = sub_cmd_obj->GetHelpLong();
102 if ((long_help != NULL)
103 && (strlen (long_help) > 0))
Greg Clayton63094e02010-06-23 01:19:29 +0000104 output_strm.Printf ("\n%s", long_help);
Chris Lattner24943d22010-06-08 16:52:24 +0000105 }
106 else if (sub_cmd_obj->IsMultiwordObject())
107 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000108 m_interpreter.OutputFormattedHelpText (output_strm, "", "", sub_cmd_obj->GetHelp(), 1);
109 ((CommandObjectMultiword *) sub_cmd_obj)->GenerateHelpText (result);
Chris Lattner24943d22010-06-08 16:52:24 +0000110 }
111 else
112 {
Greg Clayton63094e02010-06-23 01:19:29 +0000113 const char *long_help = sub_cmd_obj->GetHelpLong();
114 if ((long_help != NULL)
115 && (strlen (long_help) > 0))
Caroline Ticeabb507a2010-09-08 21:06:11 +0000116 output_strm.Printf ("\n%s", long_help);
Greg Clayton63094e02010-06-23 01:19:29 +0000117 else
Greg Clayton238c0a12010-09-18 01:14:36 +0000118 m_interpreter.OutputFormattedHelpText (output_strm, "", "", sub_cmd_obj->GetHelp(), 1);
Greg Clayton63094e02010-06-23 01:19:29 +0000119 output_strm.Printf ("\nSyntax: %s\n", sub_cmd_obj->GetSyntax());
Chris Lattner24943d22010-06-08 16:52:24 +0000120 }
121 }
122 }
Jim Inghamd40f8a62010-07-06 22:46:59 +0000123 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");
Greg Clayton54e7afa2010-07-09 20:39:50 +0000127 const uint32_t match_count = matches.GetSize();
128 for (uint32_t i = 0; i < match_count; i++)
Jim Inghamd40f8a62010-07-06 22:46:59 +0000129 {
130 output_strm.Printf("\t%s\n", matches.GetStringAtIndex(i));
131 }
132 }
Chris Lattner24943d22010-06-08 16:52:24 +0000133 else
134 {
135 result.AppendErrorWithFormat
Greg Clayton63094e02010-06-23 01:19:29 +0000136 ("'%s' is not a known command.\nTry 'help' to see a current list of commands.\n",
137 command.GetArgumentAtIndex(0));
Chris Lattner24943d22010-06-08 16:52:24 +0000138 result.SetStatus (eReturnStatusFailed);
139 }
140 }
Greg Clayton63094e02010-06-23 01:19:29 +0000141
Chris Lattner24943d22010-06-08 16:52:24 +0000142 return result.Succeeded();
143}
144
145int
146CommandObjectHelp::HandleCompletion
147(
148 Args &input,
149 int &cursor_index,
150 int &cursor_char_position,
151 int match_start_point,
152 int max_return_elements,
Jim Ingham802f8b02010-06-30 05:02:46 +0000153 bool &word_complete,
Chris Lattner24943d22010-06-08 16:52:24 +0000154 StringList &matches
155)
156{
157 // Return the completions of the commands in the help system:
158 if (cursor_index == 0)
159 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000160 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);
Chris Lattner24943d22010-06-08 16:52:24 +0000167 }
168 else
169 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000170 CommandObject *cmd_obj = m_interpreter.GetCommandObject (input.GetArgumentAtIndex(0));
Chris Lattner24943d22010-06-08 16:52:24 +0000171 input.Shift();
172 cursor_index--;
Greg Clayton238c0a12010-09-18 01:14:36 +0000173 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);
Chris Lattner24943d22010-06-08 16:52:24 +0000180 }
181}