1dda28197Spatrick //===-- CommandObjectApropos.cpp ------------------------------------------===//
2061da546Spatrick //
3061da546Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4061da546Spatrick // See https://llvm.org/LICENSE.txt for license information.
5061da546Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6061da546Spatrick //
7061da546Spatrick //===----------------------------------------------------------------------===//
8061da546Spatrick
9061da546Spatrick #include "CommandObjectApropos.h"
10061da546Spatrick #include "lldb/Interpreter/CommandInterpreter.h"
11061da546Spatrick #include "lldb/Interpreter/CommandReturnObject.h"
12061da546Spatrick #include "lldb/Interpreter/Property.h"
13061da546Spatrick #include "lldb/Utility/Args.h"
14061da546Spatrick
15061da546Spatrick using namespace lldb;
16061da546Spatrick using namespace lldb_private;
17061da546Spatrick
18061da546Spatrick // CommandObjectApropos
19061da546Spatrick
CommandObjectApropos(CommandInterpreter & interpreter)20061da546Spatrick CommandObjectApropos::CommandObjectApropos(CommandInterpreter &interpreter)
21061da546Spatrick : CommandObjectParsed(
22061da546Spatrick interpreter, "apropos",
23061da546Spatrick "List debugger commands related to a word or subject.", nullptr) {
24061da546Spatrick CommandArgumentEntry arg;
25061da546Spatrick CommandArgumentData search_word_arg;
26061da546Spatrick
27061da546Spatrick // Define the first (and only) variant of this arg.
28061da546Spatrick search_word_arg.arg_type = eArgTypeSearchWord;
29061da546Spatrick search_word_arg.arg_repetition = eArgRepeatPlain;
30061da546Spatrick
31061da546Spatrick // There is only one variant this argument could be; put it into the argument
32061da546Spatrick // entry.
33061da546Spatrick arg.push_back(search_word_arg);
34061da546Spatrick
35061da546Spatrick // Push the data for the first argument into the m_arguments vector.
36061da546Spatrick m_arguments.push_back(arg);
37061da546Spatrick }
38061da546Spatrick
39061da546Spatrick CommandObjectApropos::~CommandObjectApropos() = default;
40061da546Spatrick
DoExecute(Args & args,CommandReturnObject & result)41061da546Spatrick bool CommandObjectApropos::DoExecute(Args &args, CommandReturnObject &result) {
42061da546Spatrick const size_t argc = args.GetArgumentCount();
43061da546Spatrick
44061da546Spatrick if (argc == 1) {
45061da546Spatrick auto search_word = args[0].ref();
46061da546Spatrick if (!search_word.empty()) {
47061da546Spatrick // The bulk of the work must be done inside the Command Interpreter,
48061da546Spatrick // since the command dictionary is private.
49061da546Spatrick StringList commands_found;
50061da546Spatrick StringList commands_help;
51061da546Spatrick
52*f6aab3d8Srobert m_interpreter.FindCommandsForApropos(
53*f6aab3d8Srobert search_word, commands_found, commands_help, true, true, true, true);
54061da546Spatrick
55061da546Spatrick if (commands_found.GetSize() == 0) {
56061da546Spatrick result.AppendMessageWithFormat("No commands found pertaining to '%s'. "
57061da546Spatrick "Try 'help' to see a complete list of "
58061da546Spatrick "debugger commands.\n",
59061da546Spatrick args[0].c_str());
60061da546Spatrick } else {
61061da546Spatrick if (commands_found.GetSize() > 0) {
62061da546Spatrick result.AppendMessageWithFormat(
63061da546Spatrick "The following commands may relate to '%s':\n", args[0].c_str());
64061da546Spatrick const size_t max_len = commands_found.GetMaxStringLength();
65061da546Spatrick
66061da546Spatrick for (size_t i = 0; i < commands_found.GetSize(); ++i)
67061da546Spatrick m_interpreter.OutputFormattedHelpText(
68061da546Spatrick result.GetOutputStream(), commands_found.GetStringAtIndex(i),
69061da546Spatrick "--", commands_help.GetStringAtIndex(i), max_len);
70061da546Spatrick }
71061da546Spatrick }
72061da546Spatrick
73061da546Spatrick std::vector<const Property *> properties;
74061da546Spatrick const size_t num_properties =
75061da546Spatrick GetDebugger().Apropos(search_word, properties);
76061da546Spatrick if (num_properties) {
77061da546Spatrick const bool dump_qualified_name = true;
78061da546Spatrick result.AppendMessageWithFormatv(
79061da546Spatrick "\nThe following settings variables may relate to '{0}': \n\n",
80061da546Spatrick args[0].ref());
81061da546Spatrick for (size_t i = 0; i < num_properties; ++i)
82061da546Spatrick properties[i]->DumpDescription(
83061da546Spatrick m_interpreter, result.GetOutputStream(), 0, dump_qualified_name);
84061da546Spatrick }
85061da546Spatrick
86061da546Spatrick result.SetStatus(eReturnStatusSuccessFinishNoResult);
87061da546Spatrick } else {
88061da546Spatrick result.AppendError("'' is not a valid search word.\n");
89061da546Spatrick }
90061da546Spatrick } else {
91061da546Spatrick result.AppendError("'apropos' must be called with exactly one argument.\n");
92061da546Spatrick }
93061da546Spatrick
94061da546Spatrick return result.Succeeded();
95061da546Spatrick }
96