LLDB mainline
CommandObjectApropos.cpp
Go to the documentation of this file.
1//===-- CommandObjectApropos.cpp ------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10#include "lldb/Core/Debugger.h"
14#include "lldb/Utility/Args.h"
16#include "llvm/Support/Regex.h"
17
18using namespace lldb;
19using namespace lldb_private;
20
21// CommandObjectApropos
22
25 interpreter, "apropos",
26 "List debugger commands and settings related to a word or subject.",
27 nullptr) {
29}
30
32
34 const size_t argc = args.GetArgumentCount();
35
36 if (argc == 1) {
37 auto search_word = args[0].ref();
38 if (!search_word.empty()) {
40
41 std::string escaped_search_word;
42 std::optional<Stream::HighlightSettings> highlight;
43 Debugger &dbg = GetDebugger();
44 if (dbg.GetUseColor()) {
45 escaped_search_word = llvm::Regex::escape(search_word);
46 highlight.emplace(escaped_search_word, dbg.GetRegexMatchAnsiPrefix(),
47 dbg.GetRegexMatchAnsiSuffix(), true);
48 }
49
50 // Find all commands matching the search word.
51 StringList commands_found;
52 StringList commands_help;
53 m_interpreter.FindCommandsForApropos(
54 search_word, commands_found, commands_help, true, true, true, true);
55
56 if (commands_found.GetSize() == 0) {
58 "No commands found pertaining to '{0}'. "
59 "Try 'help' to see a complete list of "
60 "debugger commands.",
61 args[0].c_str());
62 } else {
64 "The following commands may relate to '{0}':", args[0].c_str());
65 const size_t commands_max_len = commands_found.GetMaxStringLength();
66 for (size_t i = 0; i < commands_found.GetSize(); ++i)
67 m_interpreter.OutputFormattedHelpText(
68 result.GetOutputStream(), commands_found.GetStringAtIndex(i),
69 "--", commands_help.GetStringAtIndex(i), commands_max_len,
70 highlight);
72 }
73
74 // Find all the properties matching the search word.
75 size_t properties_max_len = 0;
76 std::vector<const Property *> properties;
77 std::vector<const Property *> property_paths;
78 GetDebugger().Apropos(search_word, properties, property_paths);
79 for (const Property *prop : properties) {
80 StreamString qualified_name;
81 prop->DumpQualifiedName(qualified_name);
82 properties_max_len =
83 std::max(properties_max_len, qualified_name.GetString().size());
84 }
85
86 if (properties.empty() && property_paths.empty()) {
88 "No settings found pertaining to '{0}'. "
89 "Try 'settings show' to see a complete list of "
90 "debugger settings.",
91 args[0].c_str());
92
93 } else {
95
96 if (!property_paths.empty()) {
98 "\nThe following settings paths may relate to '{0}': \n\n",
99 search_word);
100
101 auto &out_strm = result.GetOutputStream();
102 out_strm.IndentMore();
103 for (auto path : property_paths) {
104 StreamString qual_name_strm;
105 if (path->DumpQualifiedName(qual_name_strm, highlight)) {
106 result.GetOutputStream().Indent();
107 result.GetOutputStream() << qual_name_strm.GetString() << '\n';
108 }
109 }
110 out_strm.IndentLess();
111
112 result.AppendMessageWithFormatv("\n(use 'settings list <path>' to "
113 "show settings with a given path)");
114 }
115
116 if (!properties.empty()) {
118 "\nThe following settings variables may relate to '{0}': \n\n",
119 search_word);
120
121 const bool dump_qualified_name = true;
122 for (auto property : properties)
123 property->DumpDescription(m_interpreter, result.GetOutputStream(),
124 properties_max_len, dump_qualified_name,
125 highlight);
126 }
127 }
128 result.SetStatus(return_status);
129 } else {
130 result.AppendError("'' is not a valid search word.\n");
131 }
132 } else {
133 result.AppendError("'apropos' must be called with exactly one argument.\n");
134 }
135}
A command line argument class.
Definition Args.h:33
size_t GetArgumentCount() const
Gets the number of arguments left in this command object.
Definition Args.h:120
void DoExecute(Args &command, CommandReturnObject &result) override
CommandObjectApropos(CommandInterpreter &interpreter)
CommandObjectParsed(CommandInterpreter &interpreter, const char *name, const char *help=nullptr, const char *syntax=nullptr, uint32_t flags=0)
void AddSimpleArgumentList(lldb::CommandArgumentType arg_type, ArgumentRepetitionType repetition_type=eArgRepeatPlain)
CommandInterpreter & m_interpreter
void AppendError(llvm::StringRef in_string)
void SetStatus(lldb::ReturnStatus status)
void void AppendMessageWithFormatv(const char *format, Args &&...args)
A class to manage flag bits.
Definition Debugger.h:100
bool GetUseColor() const
Definition Debugger.cpp:525
llvm::StringRef GetRegexMatchAnsiSuffix() const
Definition Debugger.cpp:630
llvm::StringRef GetRegexMatchAnsiPrefix() const
Definition Debugger.cpp:624
void Apropos(llvm::StringRef keyword, std::vector< const Property * > &matching_properties, std::vector< const Property * > &matching_property_paths) const
llvm::StringRef GetString() const
size_t Indent(llvm::StringRef s="")
Indent the current line in the stream.
Definition Stream.cpp:157
void IndentMore(unsigned amount=2)
Increment the current indentation level.
Definition Stream.cpp:201
size_t GetMaxStringLength() const
const char * GetStringAtIndex(size_t idx) const
A class that represents a running process on the host machine.
ReturnStatus
Command Return Status Types.
@ eReturnStatusSuccessFinishResult
@ eReturnStatusSuccessFinishNoResult