blob: eba052df392a660b0b5cff1a51ea214545fe725f [file] [log] [blame]
Kate Stoneb9c1b512016-09-06 20:57:50 +00001//===-- CommandObjectApropos.cpp ---------------------------------*- C++
2//-*-===//
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003//
Chandler Carruth2946cd72019-01-19 08:50:56 +00004// 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
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007//
8//===----------------------------------------------------------------------===//
9
Eugene Zelenkoc8ecc2a2016-02-19 19:33:46 +000010#include "CommandObjectApropos.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000011#include "lldb/Interpreter/CommandInterpreter.h"
12#include "lldb/Interpreter/CommandReturnObject.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000013#include "lldb/Interpreter/Options.h"
14#include "lldb/Interpreter/Property.h"
Pavel Labath145d95c2018-04-17 18:53:35 +000015#include "lldb/Utility/Args.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000016
17using namespace lldb;
18using namespace lldb_private;
19
20//-------------------------------------------------------------------------
21// CommandObjectApropos
22//-------------------------------------------------------------------------
23
Kate Stone7428a182016-07-14 22:03:10 +000024CommandObjectApropos::CommandObjectApropos(CommandInterpreter &interpreter)
Kate Stoneb9c1b512016-09-06 20:57:50 +000025 : CommandObjectParsed(
26 interpreter, "apropos",
27 "List debugger commands related to a word or subject.", nullptr) {
28 CommandArgumentEntry arg;
29 CommandArgumentData search_word_arg;
Caroline Tice405fe672010-10-04 22:28:36 +000030
Kate Stoneb9c1b512016-09-06 20:57:50 +000031 // Define the first (and only) variant of this arg.
32 search_word_arg.arg_type = eArgTypeSearchWord;
33 search_word_arg.arg_repetition = eArgRepeatPlain;
Caroline Tice405fe672010-10-04 22:28:36 +000034
Kate Stoneb9c1b512016-09-06 20:57:50 +000035 // There is only one variant this argument could be; put it into the argument
36 // entry.
37 arg.push_back(search_word_arg);
Caroline Tice405fe672010-10-04 22:28:36 +000038
Kate Stoneb9c1b512016-09-06 20:57:50 +000039 // Push the data for the first argument into the m_arguments vector.
40 m_arguments.push_back(arg);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000041}
42
Eugene Zelenkoc8ecc2a2016-02-19 19:33:46 +000043CommandObjectApropos::~CommandObjectApropos() = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000044
Kate Stoneb9c1b512016-09-06 20:57:50 +000045bool CommandObjectApropos::DoExecute(Args &args, CommandReturnObject &result) {
46 const size_t argc = args.GetArgumentCount();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000047
Kate Stoneb9c1b512016-09-06 20:57:50 +000048 if (argc == 1) {
Zachary Turner867e7d12016-12-09 01:20:58 +000049 auto search_word = args[0].ref;
50 if (!search_word.empty()) {
Adrian Prantl05097242018-04-30 16:49:04 +000051 // The bulk of the work must be done inside the Command Interpreter,
52 // since the command dictionary is private.
Kate Stoneb9c1b512016-09-06 20:57:50 +000053 StringList commands_found;
54 StringList commands_help;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000055
Kate Stoneb9c1b512016-09-06 20:57:50 +000056 m_interpreter.FindCommandsForApropos(search_word, commands_found,
57 commands_help, true, true, true);
Jim Inghamaf3753e2013-05-17 01:30:37 +000058
Kate Stoneb9c1b512016-09-06 20:57:50 +000059 if (commands_found.GetSize() == 0) {
60 result.AppendMessageWithFormat("No commands found pertaining to '%s'. "
61 "Try 'help' to see a complete list of "
62 "debugger commands.\n",
Zachary Turner867e7d12016-12-09 01:20:58 +000063 args[0].c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +000064 } else {
65 if (commands_found.GetSize() > 0) {
66 result.AppendMessageWithFormat(
Zachary Turner867e7d12016-12-09 01:20:58 +000067 "The following commands may relate to '%s':\n", args[0].c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +000068 size_t max_len = 0;
Greg Clayton67cc0632012-08-22 17:17:09 +000069
Kate Stoneb9c1b512016-09-06 20:57:50 +000070 for (size_t i = 0; i < commands_found.GetSize(); ++i) {
71 size_t len = strlen(commands_found.GetStringAtIndex(i));
72 if (len > max_len)
73 max_len = len;
74 }
75
76 for (size_t i = 0; i < commands_found.GetSize(); ++i)
77 m_interpreter.OutputFormattedHelpText(
78 result.GetOutputStream(), commands_found.GetStringAtIndex(i),
79 "--", commands_help.GetStringAtIndex(i), max_len);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000080 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000081 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000082
Kate Stoneb9c1b512016-09-06 20:57:50 +000083 std::vector<const Property *> properties;
84 const size_t num_properties =
85 m_interpreter.GetDebugger().Apropos(search_word, properties);
86 if (num_properties) {
87 const bool dump_qualified_name = true;
Zachary Turner827d5d72016-12-16 04:27:00 +000088 result.AppendMessageWithFormatv(
89 "\nThe following settings variables may relate to '{0}': \n\n",
90 args[0].ref);
Kate Stoneb9c1b512016-09-06 20:57:50 +000091 for (size_t i = 0; i < num_properties; ++i)
92 properties[i]->DumpDescription(
93 m_interpreter, result.GetOutputStream(), 0, dump_qualified_name);
94 }
95
96 result.SetStatus(eReturnStatusSuccessFinishNoResult);
97 } else {
98 result.AppendError("'' is not a valid search word.\n");
99 result.SetStatus(eReturnStatusFailed);
100 }
101 } else {
102 result.AppendError("'apropos' must be called with exactly one argument.\n");
103 result.SetStatus(eReturnStatusFailed);
104 }
105
106 return result.Succeeded();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000107}