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 related to a word or subject.", nullptr) {
28}
29
31
33 const size_t argc = args.GetArgumentCount();
34
35 if (argc == 1) {
36 auto search_word = args[0].ref();
37 if (!search_word.empty()) {
39
40 std::string escaped_search_word;
41 std::optional<Stream::HighlightSettings> highlight;
42 Debugger &dbg = GetDebugger();
43 if (dbg.GetUseColor()) {
44 escaped_search_word = llvm::Regex::escape(search_word);
45 highlight.emplace(escaped_search_word, dbg.GetRegexMatchAnsiPrefix(),
46 dbg.GetRegexMatchAnsiSuffix(), true);
47 }
48
49 // Find all commands matching the search word.
50 StringList commands_found;
51 StringList commands_help;
52 m_interpreter.FindCommandsForApropos(
53 search_word, commands_found, commands_help, true, true, true, true);
54
55 if (commands_found.GetSize() == 0) {
57 "No commands found pertaining to '{0}'. "
58 "Try 'help' to see a complete list of "
59 "debugger commands.",
60 args[0].c_str());
61 } else {
63 "The following commands may relate to '{0}':", args[0].c_str());
64 const size_t commands_max_len = commands_found.GetMaxStringLength();
65 for (size_t i = 0; i < commands_found.GetSize(); ++i)
66 m_interpreter.OutputFormattedHelpText(
67 result.GetOutputStream(), commands_found.GetStringAtIndex(i),
68 "--", commands_help.GetStringAtIndex(i), commands_max_len,
69 highlight);
71 }
72
73 // Find all the properties matching the search word.
74 size_t properties_max_len = 0;
75 std::vector<const Property *> properties;
76 const size_t num_properties =
77 GetDebugger().Apropos(search_word, properties);
78 for (const Property *prop : properties) {
79 StreamString qualified_name;
80 prop->DumpQualifiedName(qualified_name);
81 properties_max_len =
82 std::max(properties_max_len, qualified_name.GetString().size());
83 }
84
85 if (num_properties == 0) {
87 "No settings found pertaining to '{0}'. "
88 "Try 'settings show' to see a complete list of "
89 "debugger settings.",
90 args[0].c_str());
91
92 } else {
94 "\nThe following settings variables may relate to '{0}': \n\n",
95 search_word);
96
97 const bool dump_qualified_name = true;
98 for (size_t i = 0; i < num_properties; ++i)
99 properties[i]->DumpDescription(
100 m_interpreter, result.GetOutputStream(), properties_max_len,
101 dump_qualified_name, highlight);
102 return_status = eReturnStatusSuccessFinishResult;
103 }
104
105 result.SetStatus(return_status);
106 } else {
107 result.AppendError("'' is not a valid search word.\n");
108 }
109 } else {
110 result.AppendError("'apropos' must be called with exactly one argument.\n");
111 }
112}
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
size_t Apropos(llvm::StringRef keyword, std::vector< const Property * > &matching_properties) const
llvm::StringRef GetString() const
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