blob: 59621af5f24d139bcec815fbd416d65fe9edd88d [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
29CommandObjectApropos::CommandObjectApropos () :
30 CommandObject ("apropos",
31 "Finds a list of debugger commands related to a particular word/subject.",
32 "apropos <search-word>")
33{
34}
35
36CommandObjectApropos::~CommandObjectApropos()
37{
38}
39
40
41bool
Greg Clayton63094e02010-06-23 01:19:29 +000042CommandObjectApropos::Execute
43(
44 CommandInterpreter &interpreter,
45 Args& args,
46 CommandReturnObject &result
47)
Chris Lattner24943d22010-06-08 16:52:24 +000048{
Greg Clayton63094e02010-06-23 01:19:29 +000049 const int argc = args.GetArgumentCount ();
Chris Lattner24943d22010-06-08 16:52:24 +000050
51 if (argc == 1)
52 {
Greg Clayton63094e02010-06-23 01:19:29 +000053 const char *search_word = args.GetArgumentAtIndex(0);
Chris Lattner24943d22010-06-08 16:52:24 +000054 if ((search_word != NULL)
55 && (strlen (search_word) > 0))
56 {
57 // The bulk of the work must be done inside the Command Interpreter, since the command dictionary
58 // is private.
59 StringList commands_found;
60 StringList commands_help;
Greg Clayton63094e02010-06-23 01:19:29 +000061 interpreter.FindCommandsForApropos (search_word, commands_found, commands_help);
Chris Lattner24943d22010-06-08 16:52:24 +000062 if (commands_found.GetSize() == 0)
63 {
64 result.AppendMessageWithFormat ("No commands found pertaining to '%s'.", search_word);
65 result.AppendMessage ("Try 'help' to see a complete list of debugger commands.");
66 }
67 else
68 {
69 result.AppendMessageWithFormat ("The following commands may relate to '%s':\n", search_word);
70 size_t max_len = 0;
71
Greg Clayton54e7afa2010-07-09 20:39:50 +000072 for (size_t i = 0; i < commands_found.GetSize(); ++i)
Chris Lattner24943d22010-06-08 16:52:24 +000073 {
Greg Clayton54e7afa2010-07-09 20:39:50 +000074 size_t len = strlen (commands_found.GetStringAtIndex (i));
Chris Lattner24943d22010-06-08 16:52:24 +000075 if (len > max_len)
76 max_len = len;
77 }
78
Greg Clayton54e7afa2010-07-09 20:39:50 +000079 for (size_t i = 0; i < commands_found.GetSize(); ++i)
Greg Clayton63094e02010-06-23 01:19:29 +000080 interpreter.OutputFormattedHelpText (result.GetOutputStream(),
81 commands_found.GetStringAtIndex(i),
82 "--", commands_help.
83 GetStringAtIndex(i),
84 max_len);
Chris Lattner24943d22010-06-08 16:52:24 +000085
86 }
87 result.SetStatus (eReturnStatusSuccessFinishNoResult);
88 }
89 else
90 {
91 result.AppendError ("'' is not a valid search word.\n");
92 result.SetStatus (eReturnStatusFailed);
93 }
94 }
95 else
96 {
97 result.AppendError ("'apropos' must be called with exactly one argument.\n");
98 result.SetStatus (eReturnStatusFailed);
99 }
100
101 return result.Succeeded();
102}