xref: /llvm-project/lldb/source/Commands/CommandObjectApropos.cpp (revision 2946cd701067404b99c39fb29dc9c74bd7193eb3)
1 //===-- CommandObjectApropos.cpp ---------------------------------*- C++
2 //-*-===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "CommandObjectApropos.h"
11 #include "lldb/Interpreter/CommandInterpreter.h"
12 #include "lldb/Interpreter/CommandReturnObject.h"
13 #include "lldb/Interpreter/Options.h"
14 #include "lldb/Interpreter/Property.h"
15 #include "lldb/Utility/Args.h"
16 
17 using namespace lldb;
18 using namespace lldb_private;
19 
20 //-------------------------------------------------------------------------
21 // CommandObjectApropos
22 //-------------------------------------------------------------------------
23 
24 CommandObjectApropos::CommandObjectApropos(CommandInterpreter &interpreter)
25     : CommandObjectParsed(
26           interpreter, "apropos",
27           "List debugger commands related to a word or subject.", nullptr) {
28   CommandArgumentEntry arg;
29   CommandArgumentData search_word_arg;
30 
31   // Define the first (and only) variant of this arg.
32   search_word_arg.arg_type = eArgTypeSearchWord;
33   search_word_arg.arg_repetition = eArgRepeatPlain;
34 
35   // There is only one variant this argument could be; put it into the argument
36   // entry.
37   arg.push_back(search_word_arg);
38 
39   // Push the data for the first argument into the m_arguments vector.
40   m_arguments.push_back(arg);
41 }
42 
43 CommandObjectApropos::~CommandObjectApropos() = default;
44 
45 bool CommandObjectApropos::DoExecute(Args &args, CommandReturnObject &result) {
46   const size_t argc = args.GetArgumentCount();
47 
48   if (argc == 1) {
49     auto search_word = args[0].ref;
50     if (!search_word.empty()) {
51       // The bulk of the work must be done inside the Command Interpreter,
52       // since the command dictionary is private.
53       StringList commands_found;
54       StringList commands_help;
55 
56       m_interpreter.FindCommandsForApropos(search_word, commands_found,
57                                            commands_help, true, true, true);
58 
59       if (commands_found.GetSize() == 0) {
60         result.AppendMessageWithFormat("No commands found pertaining to '%s'. "
61                                        "Try 'help' to see a complete list of "
62                                        "debugger commands.\n",
63                                        args[0].c_str());
64       } else {
65         if (commands_found.GetSize() > 0) {
66           result.AppendMessageWithFormat(
67               "The following commands may relate to '%s':\n", args[0].c_str());
68           size_t max_len = 0;
69 
70           for (size_t i = 0; i < commands_found.GetSize(); ++i) {
71             size_t len = strlen(commands_found.GetStringAtIndex(i));
72             if (len > max_len)
73               max_len = len;
74           }
75 
76           for (size_t i = 0; i < commands_found.GetSize(); ++i)
77             m_interpreter.OutputFormattedHelpText(
78                 result.GetOutputStream(), commands_found.GetStringAtIndex(i),
79                 "--", commands_help.GetStringAtIndex(i), max_len);
80         }
81       }
82 
83       std::vector<const Property *> properties;
84       const size_t num_properties =
85           m_interpreter.GetDebugger().Apropos(search_word, properties);
86       if (num_properties) {
87         const bool dump_qualified_name = true;
88         result.AppendMessageWithFormatv(
89             "\nThe following settings variables may relate to '{0}': \n\n",
90             args[0].ref);
91         for (size_t i = 0; i < num_properties; ++i)
92           properties[i]->DumpDescription(
93               m_interpreter, result.GetOutputStream(), 0, dump_qualified_name);
94       }
95 
96       result.SetStatus(eReturnStatusSuccessFinishNoResult);
97     } else {
98       result.AppendError("'' is not a valid search word.\n");
99       result.SetStatus(eReturnStatusFailed);
100     }
101   } else {
102     result.AppendError("'apropos' must be called with exactly one argument.\n");
103     result.SetStatus(eReturnStatusFailed);
104   }
105 
106   return result.Succeeded();
107 }
108