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