180814287SRaphael Isemann //===-- CommandObjectApropos.cpp ------------------------------------------===//
230fdc8d8SChris Lattner //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
630fdc8d8SChris Lattner //
730fdc8d8SChris Lattner //===----------------------------------------------------------------------===//
830fdc8d8SChris Lattner 
9c8ecc2a9SEugene Zelenko #include "CommandObjectApropos.h"
1030fdc8d8SChris Lattner #include "lldb/Interpreter/CommandInterpreter.h"
1130fdc8d8SChris Lattner #include "lldb/Interpreter/CommandReturnObject.h"
12b9c1b51eSKate Stone #include "lldb/Interpreter/Property.h"
13145d95c9SPavel Labath #include "lldb/Utility/Args.h"
1430fdc8d8SChris Lattner 
1530fdc8d8SChris Lattner using namespace lldb;
1630fdc8d8SChris Lattner using namespace lldb_private;
1730fdc8d8SChris Lattner 
1830fdc8d8SChris Lattner // CommandObjectApropos
1930fdc8d8SChris Lattner 
CommandObjectApropos(CommandInterpreter & interpreter)207428a18cSKate Stone CommandObjectApropos::CommandObjectApropos(CommandInterpreter &interpreter)
21b9c1b51eSKate Stone     : CommandObjectParsed(
22b9c1b51eSKate Stone           interpreter, "apropos",
23b9c1b51eSKate Stone           "List debugger commands related to a word or subject.", nullptr) {
24405fe67fSCaroline Tice   CommandArgumentEntry arg;
25405fe67fSCaroline Tice   CommandArgumentData search_word_arg;
26405fe67fSCaroline Tice 
27405fe67fSCaroline Tice   // Define the first (and only) variant of this arg.
28405fe67fSCaroline Tice   search_word_arg.arg_type = eArgTypeSearchWord;
29405fe67fSCaroline Tice   search_word_arg.arg_repetition = eArgRepeatPlain;
30405fe67fSCaroline Tice 
31b9c1b51eSKate Stone   // There is only one variant this argument could be; put it into the argument
32b9c1b51eSKate Stone   // entry.
33405fe67fSCaroline Tice   arg.push_back(search_word_arg);
34405fe67fSCaroline Tice 
35405fe67fSCaroline Tice   // Push the data for the first argument into the m_arguments vector.
36405fe67fSCaroline Tice   m_arguments.push_back(arg);
3730fdc8d8SChris Lattner }
3830fdc8d8SChris Lattner 
39c8ecc2a9SEugene Zelenko CommandObjectApropos::~CommandObjectApropos() = default;
4030fdc8d8SChris Lattner 
DoExecute(Args & args,CommandReturnObject & result)41b9c1b51eSKate Stone bool CommandObjectApropos::DoExecute(Args &args, CommandReturnObject &result) {
42c7bece56SGreg Clayton   const size_t argc = args.GetArgumentCount();
4330fdc8d8SChris Lattner 
44b9c1b51eSKate Stone   if (argc == 1) {
450d9a201eSRaphael Isemann     auto search_word = args[0].ref();
46867e7d17SZachary Turner     if (!search_word.empty()) {
4705097246SAdrian Prantl       // The bulk of the work must be done inside the Command Interpreter,
4805097246SAdrian Prantl       // since the command dictionary is private.
4930fdc8d8SChris Lattner       StringList commands_found;
5030fdc8d8SChris Lattner       StringList commands_help;
51af3753ebSJim Ingham 
52*c5011aedSJim Ingham       m_interpreter.FindCommandsForApropos(
53*c5011aedSJim Ingham           search_word, commands_found, commands_help, true, true, true, true);
54af3753ebSJim Ingham 
55b9c1b51eSKate Stone       if (commands_found.GetSize() == 0) {
56b9c1b51eSKate Stone         result.AppendMessageWithFormat("No commands found pertaining to '%s'. "
57b9c1b51eSKate Stone                                        "Try 'help' to see a complete list of "
58b9c1b51eSKate Stone                                        "debugger commands.\n",
59867e7d17SZachary Turner                                        args[0].c_str());
60b9c1b51eSKate Stone       } else {
61b9c1b51eSKate Stone         if (commands_found.GetSize() > 0) {
62b9c1b51eSKate Stone           result.AppendMessageWithFormat(
63867e7d17SZachary Turner               "The following commands may relate to '%s':\n", args[0].c_str());
6481094aadSRaphael Isemann           const size_t max_len = commands_found.GetMaxStringLength();
6530fdc8d8SChris Lattner 
66c982c768SGreg Clayton           for (size_t i = 0; i < commands_found.GetSize(); ++i)
67b9c1b51eSKate Stone             m_interpreter.OutputFormattedHelpText(
68b9c1b51eSKate Stone                 result.GetOutputStream(), commands_found.GetStringAtIndex(i),
69b9c1b51eSKate Stone                 "--", commands_help.GetStringAtIndex(i), max_len);
70af3753ebSJim Ingham         }
7130fdc8d8SChris Lattner       }
7273fd2728SCaroline Tice 
7367cc0636SGreg Clayton       std::vector<const Property *> properties;
74b9c1b51eSKate Stone       const size_t num_properties =
7557179860SJonas Devlieghere           GetDebugger().Apropos(search_word, properties);
76b9c1b51eSKate Stone       if (num_properties) {
7767cc0636SGreg Clayton         const bool dump_qualified_name = true;
78827d5d74SZachary Turner         result.AppendMessageWithFormatv(
79827d5d74SZachary Turner             "\nThe following settings variables may relate to '{0}': \n\n",
800d9a201eSRaphael Isemann             args[0].ref());
8167cc0636SGreg Clayton         for (size_t i = 0; i < num_properties; ++i)
82b9c1b51eSKate Stone           properties[i]->DumpDescription(
83b9c1b51eSKate Stone               m_interpreter, result.GetOutputStream(), 0, dump_qualified_name);
8473fd2728SCaroline Tice       }
8573fd2728SCaroline Tice 
8630fdc8d8SChris Lattner       result.SetStatus(eReturnStatusSuccessFinishNoResult);
87b9c1b51eSKate Stone     } else {
8830fdc8d8SChris Lattner       result.AppendError("'' is not a valid search word.\n");
8930fdc8d8SChris Lattner     }
90b9c1b51eSKate Stone   } else {
9130fdc8d8SChris Lattner     result.AppendError("'apropos' must be called with exactly one argument.\n");
9230fdc8d8SChris Lattner   }
9330fdc8d8SChris Lattner 
9430fdc8d8SChris Lattner   return result.Succeeded();
9530fdc8d8SChris Lattner }
96