xref: /src/contrib/llvm-project/lldb/source/Commands/CommandObjectApropos.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1cfca06d7SDimitry Andric //===-- CommandObjectApropos.cpp ------------------------------------------===//
2f034231aSEd Maste //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f034231aSEd Maste //
7f034231aSEd Maste //===----------------------------------------------------------------------===//
8f034231aSEd Maste 
9f3fbd1c0SDimitry Andric #include "CommandObjectApropos.h"
10f034231aSEd Maste #include "lldb/Interpreter/CommandInterpreter.h"
11f034231aSEd Maste #include "lldb/Interpreter/CommandReturnObject.h"
1214f1b3e8SDimitry Andric #include "lldb/Interpreter/Property.h"
13f73363f1SDimitry Andric #include "lldb/Utility/Args.h"
14f034231aSEd Maste 
15f034231aSEd Maste using namespace lldb;
16f034231aSEd Maste using namespace lldb_private;
17f034231aSEd Maste 
18f034231aSEd Maste // CommandObjectApropos
19f034231aSEd Maste 
CommandObjectApropos(CommandInterpreter & interpreter)20f3fbd1c0SDimitry Andric CommandObjectApropos::CommandObjectApropos(CommandInterpreter &interpreter)
2114f1b3e8SDimitry Andric     : CommandObjectParsed(
2214f1b3e8SDimitry Andric           interpreter, "apropos",
2314f1b3e8SDimitry Andric           "List debugger commands related to a word or subject.", nullptr) {
24ac9a064cSDimitry Andric   AddSimpleArgumentList(eArgTypeSearchWord);
25f034231aSEd Maste }
26f034231aSEd Maste 
27f3fbd1c0SDimitry Andric CommandObjectApropos::~CommandObjectApropos() = default;
28f034231aSEd Maste 
DoExecute(Args & args,CommandReturnObject & result)29b1c73532SDimitry Andric void CommandObjectApropos::DoExecute(Args &args, CommandReturnObject &result) {
30f034231aSEd Maste   const size_t argc = args.GetArgumentCount();
31f034231aSEd Maste 
3214f1b3e8SDimitry Andric   if (argc == 1) {
33ead24645SDimitry Andric     auto search_word = args[0].ref();
3414f1b3e8SDimitry Andric     if (!search_word.empty()) {
35f73363f1SDimitry Andric       // The bulk of the work must be done inside the Command Interpreter,
36f73363f1SDimitry Andric       // since the command dictionary is private.
37f034231aSEd Maste       StringList commands_found;
38f034231aSEd Maste       StringList commands_help;
39f034231aSEd Maste 
40c0981da4SDimitry Andric       m_interpreter.FindCommandsForApropos(
41c0981da4SDimitry Andric           search_word, commands_found, commands_help, true, true, true, true);
42f034231aSEd Maste 
4314f1b3e8SDimitry Andric       if (commands_found.GetSize() == 0) {
4414f1b3e8SDimitry Andric         result.AppendMessageWithFormat("No commands found pertaining to '%s'. "
4514f1b3e8SDimitry Andric                                        "Try 'help' to see a complete list of "
4614f1b3e8SDimitry Andric                                        "debugger commands.\n",
4714f1b3e8SDimitry Andric                                        args[0].c_str());
4814f1b3e8SDimitry Andric       } else {
4914f1b3e8SDimitry Andric         if (commands_found.GetSize() > 0) {
5014f1b3e8SDimitry Andric           result.AppendMessageWithFormat(
5114f1b3e8SDimitry Andric               "The following commands may relate to '%s':\n", args[0].c_str());
52ead24645SDimitry Andric           const size_t max_len = commands_found.GetMaxStringLength();
53f034231aSEd Maste 
54f034231aSEd Maste           for (size_t i = 0; i < commands_found.GetSize(); ++i)
5514f1b3e8SDimitry Andric             m_interpreter.OutputFormattedHelpText(
5614f1b3e8SDimitry Andric                 result.GetOutputStream(), commands_found.GetStringAtIndex(i),
5714f1b3e8SDimitry Andric                 "--", commands_help.GetStringAtIndex(i), max_len);
58f034231aSEd Maste         }
59f034231aSEd Maste       }
60f034231aSEd Maste 
61f034231aSEd Maste       std::vector<const Property *> properties;
6214f1b3e8SDimitry Andric       const size_t num_properties =
635f29bb8aSDimitry Andric           GetDebugger().Apropos(search_word, properties);
6414f1b3e8SDimitry Andric       if (num_properties) {
65f034231aSEd Maste         const bool dump_qualified_name = true;
6614f1b3e8SDimitry Andric         result.AppendMessageWithFormatv(
6714f1b3e8SDimitry Andric             "\nThe following settings variables may relate to '{0}': \n\n",
68ead24645SDimitry Andric             args[0].ref());
69f034231aSEd Maste         for (size_t i = 0; i < num_properties; ++i)
7014f1b3e8SDimitry Andric           properties[i]->DumpDescription(
7114f1b3e8SDimitry Andric               m_interpreter, result.GetOutputStream(), 0, dump_qualified_name);
72f034231aSEd Maste       }
73f034231aSEd Maste 
74f034231aSEd Maste       result.SetStatus(eReturnStatusSuccessFinishNoResult);
7514f1b3e8SDimitry Andric     } else {
76f034231aSEd Maste       result.AppendError("'' is not a valid search word.\n");
77f034231aSEd Maste     }
7814f1b3e8SDimitry Andric   } else {
79f034231aSEd Maste     result.AppendError("'apropos' must be called with exactly one argument.\n");
80f034231aSEd Maste   }
81f034231aSEd Maste }
82