blob: 8a037e948166e3c9b74ae1f03f17900cd68fc226 [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
28CommandObjectHelp::CommandObjectHelp () :
29 CommandObject ("help",
30 "Shows a list of all debugger commands, or give details about specific commands.",
31 "help [<cmd-name>]")
32{
33}
34
35CommandObjectHelp::~CommandObjectHelp()
36{
37}
38
39
40bool
Greg Clayton63094e02010-06-23 01:19:29 +000041CommandObjectHelp::Execute (CommandInterpreter &interpreter, Args& command, CommandReturnObject &result)
Chris Lattner24943d22010-06-08 16:52:24 +000042{
43 CommandObject::CommandMap::iterator pos;
44 CommandObject *cmd_obj;
45 const int argc = command.GetArgumentCount ();
Greg Clayton63094e02010-06-23 01:19:29 +000046
Chris Lattner24943d22010-06-08 16:52:24 +000047 // 'help' doesn't take any options or arguments, other than command names. If argc is 0, we show the user
48 // 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 +000049 if (argc == 0)
50 {
51 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Greg Clayton63094e02010-06-23 01:19:29 +000052 interpreter.GetHelp (result); // General help, for ALL commands.
Chris Lattner24943d22010-06-08 16:52:24 +000053 }
54 else
55 {
56 // Get command object for the first command argument. Only search built-in command dictionary.
Greg Clayton63094e02010-06-23 01:19:29 +000057 cmd_obj = interpreter.GetCommandObject (command.GetArgumentAtIndex (0), false, false);
Chris Lattner24943d22010-06-08 16:52:24 +000058 if (cmd_obj == NULL)
Greg Clayton63094e02010-06-23 01:19:29 +000059 {
Chris Lattner24943d22010-06-08 16:52:24 +000060 // That failed, so now search in the aliases dictionary, too.
Greg Clayton63094e02010-06-23 01:19:29 +000061 cmd_obj = interpreter.GetCommandObject (command.GetArgumentAtIndex (0), true, false);
62 }
63
Chris Lattner24943d22010-06-08 16:52:24 +000064 if (cmd_obj != NULL)
65 {
66 bool all_okay = true;
67 CommandObject *sub_cmd_obj = cmd_obj;
68 // Loop down through sub_command dictionaries until we find the command object that corresponds
69 // to the help command entered.
70 for (int i = 1; i < argc && all_okay; ++i)
71 {
72 std::string sub_command = command.GetArgumentAtIndex(i);
73 if (! sub_cmd_obj->IsMultiwordObject ())
74 {
75 all_okay = false;
76 }
77 else
78 {
79 pos = ((CommandObjectMultiword *) sub_cmd_obj)->m_subcommand_dict.find (sub_command);
80 if (pos != ((CommandObjectMultiword *) sub_cmd_obj)->m_subcommand_dict.end())
Greg Clayton63094e02010-06-23 01:19:29 +000081 sub_cmd_obj = pos->second.get();
Chris Lattner24943d22010-06-08 16:52:24 +000082 else
Greg Clayton63094e02010-06-23 01:19:29 +000083 all_okay = false;
Chris Lattner24943d22010-06-08 16:52:24 +000084 }
85 }
Greg Clayton63094e02010-06-23 01:19:29 +000086
Chris Lattner24943d22010-06-08 16:52:24 +000087 if (!all_okay || (sub_cmd_obj == NULL))
88 {
89 std::string cmd_string;
90 command.GetCommandString (cmd_string);
91 result.AppendErrorWithFormat
Greg Clayton63094e02010-06-23 01:19:29 +000092 ("'%s' is not a known command.\nTry 'help' to see a current list of commands.\n",
93 cmd_string.c_str());
Chris Lattner24943d22010-06-08 16:52:24 +000094 result.SetStatus (eReturnStatusFailed);
95 }
96 else
97 {
98 Stream &output_strm = result.GetOutputStream();
99 if (sub_cmd_obj->GetOptions() != NULL)
100 {
101 output_strm.Printf ("%s\n", sub_cmd_obj->GetHelp());
102 output_strm.Printf ("\nSyntax: %s\n", sub_cmd_obj->GetSyntax());
103 sub_cmd_obj->GetOptions()->GenerateOptionUsage (output_strm, sub_cmd_obj);
104 const char *long_help = sub_cmd_obj->GetHelpLong();
105 if ((long_help != NULL)
106 && (strlen (long_help) > 0))
Greg Clayton63094e02010-06-23 01:19:29 +0000107 output_strm.Printf ("\n%s", long_help);
Chris Lattner24943d22010-06-08 16:52:24 +0000108 }
109 else if (sub_cmd_obj->IsMultiwordObject())
110 {
111 output_strm.Printf ("%s\n", sub_cmd_obj->GetHelp());
Greg Clayton63094e02010-06-23 01:19:29 +0000112 ((CommandObjectMultiword *) sub_cmd_obj)->GenerateHelpText (interpreter, result);
Chris Lattner24943d22010-06-08 16:52:24 +0000113 }
114 else
115 {
Greg Clayton63094e02010-06-23 01:19:29 +0000116 const char *long_help = sub_cmd_obj->GetHelpLong();
117 if ((long_help != NULL)
118 && (strlen (long_help) > 0))
119 output_strm.Printf ("%s", long_help);
120 else
121 output_strm.Printf ("%s\n", sub_cmd_obj->GetHelp());
122 output_strm.Printf ("\nSyntax: %s\n", sub_cmd_obj->GetSyntax());
Chris Lattner24943d22010-06-08 16:52:24 +0000123 }
124 }
125 }
126 else
127 {
128 result.AppendErrorWithFormat
Greg Clayton63094e02010-06-23 01:19:29 +0000129 ("'%s' is not a known command.\nTry 'help' to see a current list of commands.\n",
130 command.GetArgumentAtIndex(0));
Chris Lattner24943d22010-06-08 16:52:24 +0000131 result.SetStatus (eReturnStatusFailed);
132 }
133 }
Greg Clayton63094e02010-06-23 01:19:29 +0000134
Chris Lattner24943d22010-06-08 16:52:24 +0000135 return result.Succeeded();
136}
137
138int
139CommandObjectHelp::HandleCompletion
140(
Greg Clayton63094e02010-06-23 01:19:29 +0000141 CommandInterpreter &interpreter,
Chris Lattner24943d22010-06-08 16:52:24 +0000142 Args &input,
143 int &cursor_index,
144 int &cursor_char_position,
145 int match_start_point,
146 int max_return_elements,
Chris Lattner24943d22010-06-08 16:52:24 +0000147 StringList &matches
148)
149{
150 // Return the completions of the commands in the help system:
151 if (cursor_index == 0)
152 {
Greg Clayton63094e02010-06-23 01:19:29 +0000153 return interpreter.HandleCompletionMatches(input, cursor_index, cursor_char_position, match_start_point, max_return_elements, matches);
Chris Lattner24943d22010-06-08 16:52:24 +0000154 }
155 else
156 {
Greg Clayton63094e02010-06-23 01:19:29 +0000157 CommandObject *cmd_obj = interpreter.GetCommandObject (input.GetArgumentAtIndex(0), true, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000158 input.Shift();
159 cursor_index--;
Greg Clayton63094e02010-06-23 01:19:29 +0000160 return cmd_obj->HandleCompletion (interpreter, input, cursor_index, cursor_char_position, match_start_point, max_return_elements, matches);
Chris Lattner24943d22010-06-08 16:52:24 +0000161 }
162}