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 | |
9 | #include "CommandObjectApropos.h" |
10 | #include "lldb/Interpreter/CommandInterpreter.h" |
11 | #include "lldb/Interpreter/CommandReturnObject.h" |
12 | #include "lldb/Interpreter/Property.h" |
13 | #include "lldb/Utility/Args.h" |
14 | |
15 | using namespace lldb; |
16 | using namespace lldb_private; |
17 | |
18 | // CommandObjectApropos |
19 | |
20 | CommandObjectApropos::CommandObjectApropos(CommandInterpreter &interpreter) |
21 | : CommandObjectParsed( |
22 | interpreter, "apropos" , |
23 | "List debugger commands related to a word or subject." , nullptr) { |
24 | AddSimpleArgumentList(arg_type: eArgTypeSearchWord); |
25 | } |
26 | |
27 | CommandObjectApropos::~CommandObjectApropos() = default; |
28 | |
29 | void CommandObjectApropos::DoExecute(Args &args, CommandReturnObject &result) { |
30 | const size_t argc = args.GetArgumentCount(); |
31 | |
32 | if (argc == 1) { |
33 | auto search_word = args[0].ref(); |
34 | if (!search_word.empty()) { |
35 | // The bulk of the work must be done inside the Command Interpreter, |
36 | // since the command dictionary is private. |
37 | StringList commands_found; |
38 | StringList commands_help; |
39 | |
40 | m_interpreter.FindCommandsForApropos( |
41 | word: search_word, commands_found, commands_help, search_builtin_commands: true, search_user_commands: true, search_alias_commands: true, search_user_mw_commands: true); |
42 | |
43 | if (commands_found.GetSize() == 0) { |
44 | result.AppendMessageWithFormat(format: "No commands found pertaining to '%s'. " |
45 | "Try 'help' to see a complete list of " |
46 | "debugger commands.\n" , |
47 | args[0].c_str()); |
48 | } else { |
49 | if (commands_found.GetSize() > 0) { |
50 | result.AppendMessageWithFormat( |
51 | format: "The following commands may relate to '%s':\n" , args[0].c_str()); |
52 | const size_t max_len = commands_found.GetMaxStringLength(); |
53 | |
54 | for (size_t i = 0; i < commands_found.GetSize(); ++i) |
55 | m_interpreter.OutputFormattedHelpText( |
56 | stream&: result.GetOutputStream(), command_word: commands_found.GetStringAtIndex(idx: i), |
57 | separator: "--" , help_text: commands_help.GetStringAtIndex(idx: i), max_word_len: max_len); |
58 | } |
59 | } |
60 | |
61 | std::vector<const Property *> properties; |
62 | const size_t num_properties = |
63 | GetDebugger().Apropos(keyword: search_word, matching_properties&: properties); |
64 | if (num_properties) { |
65 | const bool dump_qualified_name = true; |
66 | result.AppendMessageWithFormatv( |
67 | format: "\nThe following settings variables may relate to '{0}': \n\n" , |
68 | args: args[0].ref()); |
69 | for (size_t i = 0; i < num_properties; ++i) |
70 | properties[i]->DumpDescription( |
71 | interpreter&: m_interpreter, strm&: result.GetOutputStream(), output_width: 0, display_qualified_name: dump_qualified_name); |
72 | } |
73 | |
74 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
75 | } else { |
76 | result.AppendError(in_string: "'' is not a valid search word.\n" ); |
77 | } |
78 | } else { |
79 | result.AppendError(in_string: "'apropos' must be called with exactly one argument.\n" ); |
80 | } |
81 | } |
82 | |