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