1 //===-- CommandObjectApropos.cpp ---------------------------------*- C++
4 // The LLVM Compiler Infrastructure
6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 #include "CommandObjectApropos.h"
12 #include "lldb/Interpreter/CommandInterpreter.h"
13 #include "lldb/Interpreter/CommandReturnObject.h"
14 #include "lldb/Interpreter/Options.h"
15 #include "lldb/Interpreter/Property.h"
16 #include "lldb/Utility/Args.h"
19 using namespace lldb_private;
21 //-------------------------------------------------------------------------
22 // CommandObjectApropos
23 //-------------------------------------------------------------------------
25 CommandObjectApropos::CommandObjectApropos(CommandInterpreter &interpreter)
26 : CommandObjectParsed(
27 interpreter, "apropos",
28 "List debugger commands related to a word or subject.", nullptr) {
29 CommandArgumentEntry arg;
30 CommandArgumentData search_word_arg;
32 // Define the first (and only) variant of this arg.
33 search_word_arg.arg_type = eArgTypeSearchWord;
34 search_word_arg.arg_repetition = eArgRepeatPlain;
36 // There is only one variant this argument could be; put it into the argument
38 arg.push_back(search_word_arg);
40 // Push the data for the first argument into the m_arguments vector.
41 m_arguments.push_back(arg);
44 CommandObjectApropos::~CommandObjectApropos() = default;
46 bool CommandObjectApropos::DoExecute(Args &args, CommandReturnObject &result) {
47 const size_t argc = args.GetArgumentCount();
50 auto search_word = args[0].ref;
51 if (!search_word.empty()) {
52 // The bulk of the work must be done inside the Command Interpreter,
53 // since the command dictionary is private.
54 StringList commands_found;
55 StringList commands_help;
57 m_interpreter.FindCommandsForApropos(search_word, commands_found,
58 commands_help, true, true, true);
60 if (commands_found.GetSize() == 0) {
61 result.AppendMessageWithFormat("No commands found pertaining to '%s'. "
62 "Try 'help' to see a complete list of "
63 "debugger commands.\n",
66 if (commands_found.GetSize() > 0) {
67 result.AppendMessageWithFormat(
68 "The following commands may relate to '%s':\n", args[0].c_str());
71 for (size_t i = 0; i < commands_found.GetSize(); ++i) {
72 size_t len = strlen(commands_found.GetStringAtIndex(i));
77 for (size_t i = 0; i < commands_found.GetSize(); ++i)
78 m_interpreter.OutputFormattedHelpText(
79 result.GetOutputStream(), commands_found.GetStringAtIndex(i),
80 "--", commands_help.GetStringAtIndex(i), max_len);
84 std::vector<const Property *> properties;
85 const size_t num_properties =
86 m_interpreter.GetDebugger().Apropos(search_word, properties);
88 const bool dump_qualified_name = true;
89 result.AppendMessageWithFormatv(
90 "\nThe following settings variables may relate to '{0}': \n\n",
92 for (size_t i = 0; i < num_properties; ++i)
93 properties[i]->DumpDescription(
94 m_interpreter, result.GetOutputStream(), 0, dump_qualified_name);
97 result.SetStatus(eReturnStatusSuccessFinishNoResult);
99 result.AppendError("'' is not a valid search word.\n");
100 result.SetStatus(eReturnStatusFailed);
103 result.AppendError("'apropos' must be called with exactly one argument.\n");
104 result.SetStatus(eReturnStatusFailed);
107 return result.Succeeded();