blob: 37802a85b2a9c6d33021ed10b2429133d180848d [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.
Jim Inghamd40f8a62010-07-06 22:46:59 +000057 StringList matches;
58 cmd_obj = interpreter.GetCommandObject (command.GetArgumentAtIndex (0), &matches);
Greg Clayton63094e02010-06-23 01:19:29 +000059
Chris Lattner24943d22010-06-08 16:52:24 +000060 if (cmd_obj != NULL)
61 {
62 bool all_okay = true;
63 CommandObject *sub_cmd_obj = cmd_obj;
64 // Loop down through sub_command dictionaries until we find the command object that corresponds
65 // to the help command entered.
66 for (int i = 1; i < argc && all_okay; ++i)
67 {
68 std::string sub_command = command.GetArgumentAtIndex(i);
69 if (! sub_cmd_obj->IsMultiwordObject ())
70 {
71 all_okay = false;
72 }
73 else
74 {
75 pos = ((CommandObjectMultiword *) sub_cmd_obj)->m_subcommand_dict.find (sub_command);
76 if (pos != ((CommandObjectMultiword *) sub_cmd_obj)->m_subcommand_dict.end())
Greg Clayton63094e02010-06-23 01:19:29 +000077 sub_cmd_obj = pos->second.get();
Chris Lattner24943d22010-06-08 16:52:24 +000078 else
Greg Clayton63094e02010-06-23 01:19:29 +000079 all_okay = false;
Chris Lattner24943d22010-06-08 16:52:24 +000080 }
81 }
Greg Clayton63094e02010-06-23 01:19:29 +000082
Chris Lattner24943d22010-06-08 16:52:24 +000083 if (!all_okay || (sub_cmd_obj == NULL))
84 {
85 std::string cmd_string;
86 command.GetCommandString (cmd_string);
87 result.AppendErrorWithFormat
Greg Clayton63094e02010-06-23 01:19:29 +000088 ("'%s' is not a known command.\nTry 'help' to see a current list of commands.\n",
89 cmd_string.c_str());
Chris Lattner24943d22010-06-08 16:52:24 +000090 result.SetStatus (eReturnStatusFailed);
91 }
92 else
93 {
94 Stream &output_strm = result.GetOutputStream();
95 if (sub_cmd_obj->GetOptions() != NULL)
96 {
97 output_strm.Printf ("%s\n", sub_cmd_obj->GetHelp());
98 output_strm.Printf ("\nSyntax: %s\n", sub_cmd_obj->GetSyntax());
99 sub_cmd_obj->GetOptions()->GenerateOptionUsage (output_strm, sub_cmd_obj);
100 const char *long_help = sub_cmd_obj->GetHelpLong();
101 if ((long_help != NULL)
102 && (strlen (long_help) > 0))
Greg Clayton63094e02010-06-23 01:19:29 +0000103 output_strm.Printf ("\n%s", long_help);
Chris Lattner24943d22010-06-08 16:52:24 +0000104 }
105 else if (sub_cmd_obj->IsMultiwordObject())
106 {
107 output_strm.Printf ("%s\n", sub_cmd_obj->GetHelp());
Greg Clayton63094e02010-06-23 01:19:29 +0000108 ((CommandObjectMultiword *) sub_cmd_obj)->GenerateHelpText (interpreter, result);
Chris Lattner24943d22010-06-08 16:52:24 +0000109 }
110 else
111 {
Greg Clayton63094e02010-06-23 01:19:29 +0000112 const char *long_help = sub_cmd_obj->GetHelpLong();
113 if ((long_help != NULL)
114 && (strlen (long_help) > 0))
115 output_strm.Printf ("%s", long_help);
116 else
117 output_strm.Printf ("%s\n", sub_cmd_obj->GetHelp());
118 output_strm.Printf ("\nSyntax: %s\n", sub_cmd_obj->GetSyntax());
Chris Lattner24943d22010-06-08 16:52:24 +0000119 }
120 }
121 }
Jim Inghamd40f8a62010-07-06 22:46:59 +0000122 else if (matches.GetSize() > 0)
123 {
124 Stream &output_strm = result.GetOutputStream();
125 output_strm.Printf("Help requested with ambiguous command name, possible completions:\n");
126 for (int i = 0; i < matches.GetSize(); i++)
127 {
128 output_strm.Printf("\t%s\n", matches.GetStringAtIndex(i));
129 }
130 }
Chris Lattner24943d22010-06-08 16:52:24 +0000131 else
132 {
133 result.AppendErrorWithFormat
Greg Clayton63094e02010-06-23 01:19:29 +0000134 ("'%s' is not a known command.\nTry 'help' to see a current list of commands.\n",
135 command.GetArgumentAtIndex(0));
Chris Lattner24943d22010-06-08 16:52:24 +0000136 result.SetStatus (eReturnStatusFailed);
137 }
138 }
Greg Clayton63094e02010-06-23 01:19:29 +0000139
Chris Lattner24943d22010-06-08 16:52:24 +0000140 return result.Succeeded();
141}
142
143int
144CommandObjectHelp::HandleCompletion
145(
Greg Clayton63094e02010-06-23 01:19:29 +0000146 CommandInterpreter &interpreter,
Chris Lattner24943d22010-06-08 16:52:24 +0000147 Args &input,
148 int &cursor_index,
149 int &cursor_char_position,
150 int match_start_point,
151 int max_return_elements,
Jim Ingham802f8b02010-06-30 05:02:46 +0000152 bool &word_complete,
Chris Lattner24943d22010-06-08 16:52:24 +0000153 StringList &matches
154)
155{
156 // Return the completions of the commands in the help system:
157 if (cursor_index == 0)
158 {
Jim Ingham802f8b02010-06-30 05:02:46 +0000159 return interpreter.HandleCompletionMatches(input, cursor_index, cursor_char_position, match_start_point,
160 max_return_elements, word_complete, matches);
Chris Lattner24943d22010-06-08 16:52:24 +0000161 }
162 else
163 {
Jim Inghamd40f8a62010-07-06 22:46:59 +0000164 CommandObject *cmd_obj = interpreter.GetCommandObject (input.GetArgumentAtIndex(0));
Chris Lattner24943d22010-06-08 16:52:24 +0000165 input.Shift();
166 cursor_index--;
Jim Ingham802f8b02010-06-30 05:02:46 +0000167 return cmd_obj->HandleCompletion (interpreter, input, cursor_index, cursor_char_position, match_start_point,
168 max_return_elements, word_complete, matches);
Chris Lattner24943d22010-06-08 16:52:24 +0000169 }
170}