blob: 545f58cfe497921aebc7c9c60eb7f67f42ee6785 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- CommandObjectApropos.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 "CommandObjectApropos.h"
11
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
Jim Ingham84cdc152010-06-15 19:49:27 +000016#include "lldb/Interpreter/Args.h"
17#include "lldb/Interpreter/Options.h"
Chris Lattner24943d22010-06-08 16:52:24 +000018
19#include "lldb/Interpreter/CommandInterpreter.h"
20#include "lldb/Interpreter/CommandReturnObject.h"
Chris Lattner24943d22010-06-08 16:52:24 +000021
22using namespace lldb;
23using namespace lldb_private;
24
25//-------------------------------------------------------------------------
26// CommandObjectApropos
27//-------------------------------------------------------------------------
28
Greg Clayton238c0a12010-09-18 01:14:36 +000029CommandObjectApropos::CommandObjectApropos (CommandInterpreter &interpreter) :
30 CommandObject (interpreter,
31 "apropos",
32 "Find a list of debugger commands related to a particular word/subject.",
Caroline Tice43b014a2010-10-04 22:28:36 +000033 NULL)
Chris Lattner24943d22010-06-08 16:52:24 +000034{
Caroline Tice43b014a2010-10-04 22:28:36 +000035 CommandArgumentEntry arg;
36 CommandArgumentData search_word_arg;
37
38 // Define the first (and only) variant of this arg.
39 search_word_arg.arg_type = eArgTypeSearchWord;
40 search_word_arg.arg_repetition = eArgRepeatPlain;
41
42 // There is only one variant this argument could be; put it into the argument entry.
43 arg.push_back (search_word_arg);
44
45 // Push the data for the first argument into the m_arguments vector.
46 m_arguments.push_back (arg);
Chris Lattner24943d22010-06-08 16:52:24 +000047}
48
49CommandObjectApropos::~CommandObjectApropos()
50{
51}
52
53
54bool
Greg Clayton63094e02010-06-23 01:19:29 +000055CommandObjectApropos::Execute
56(
Greg Clayton63094e02010-06-23 01:19:29 +000057 Args& args,
58 CommandReturnObject &result
59)
Chris Lattner24943d22010-06-08 16:52:24 +000060{
Greg Clayton63094e02010-06-23 01:19:29 +000061 const int argc = args.GetArgumentCount ();
Chris Lattner24943d22010-06-08 16:52:24 +000062
63 if (argc == 1)
64 {
Greg Clayton63094e02010-06-23 01:19:29 +000065 const char *search_word = args.GetArgumentAtIndex(0);
Chris Lattner24943d22010-06-08 16:52:24 +000066 if ((search_word != NULL)
67 && (strlen (search_word) > 0))
68 {
69 // The bulk of the work must be done inside the Command Interpreter, since the command dictionary
70 // is private.
71 StringList commands_found;
72 StringList commands_help;
Greg Clayton238c0a12010-09-18 01:14:36 +000073 m_interpreter.FindCommandsForApropos (search_word, commands_found, commands_help);
Chris Lattner24943d22010-06-08 16:52:24 +000074 if (commands_found.GetSize() == 0)
75 {
76 result.AppendMessageWithFormat ("No commands found pertaining to '%s'.", search_word);
77 result.AppendMessage ("Try 'help' to see a complete list of debugger commands.");
78 }
79 else
80 {
81 result.AppendMessageWithFormat ("The following commands may relate to '%s':\n", search_word);
82 size_t max_len = 0;
83
Greg Clayton54e7afa2010-07-09 20:39:50 +000084 for (size_t i = 0; i < commands_found.GetSize(); ++i)
Chris Lattner24943d22010-06-08 16:52:24 +000085 {
Greg Clayton54e7afa2010-07-09 20:39:50 +000086 size_t len = strlen (commands_found.GetStringAtIndex (i));
Chris Lattner24943d22010-06-08 16:52:24 +000087 if (len > max_len)
88 max_len = len;
89 }
90
Greg Clayton54e7afa2010-07-09 20:39:50 +000091 for (size_t i = 0; i < commands_found.GetSize(); ++i)
Greg Clayton238c0a12010-09-18 01:14:36 +000092 m_interpreter.OutputFormattedHelpText (result.GetOutputStream(),
93 commands_found.GetStringAtIndex(i),
94 "--", commands_help.
95 GetStringAtIndex(i),
96 max_len);
97
Chris Lattner24943d22010-06-08 16:52:24 +000098 }
99 result.SetStatus (eReturnStatusSuccessFinishNoResult);
100 }
101 else
102 {
103 result.AppendError ("'' is not a valid search word.\n");
104 result.SetStatus (eReturnStatusFailed);
105 }
106 }
107 else
108 {
109 result.AppendError ("'apropos' must be called with exactly one argument.\n");
110 result.SetStatus (eReturnStatusFailed);
111 }
112
113 return result.Succeeded();
114}