blob: 047575278faa1ec511109006a06dde401b0f7eaa [file] [log] [blame]
Kate Stoneb9c1b512016-09-06 20:57:50 +00001//===-- CommandObjectApropos.cpp ---------------------------------*- C++
2//-*-===//
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003//
4// The LLVM Compiler Infrastructure
5//
6// This file is distributed under the University of Illinois Open Source
7// License. See LICENSE.TXT for details.
8//
9//===----------------------------------------------------------------------===//
10
Chris Lattner30fdc8d2010-06-08 16:52:24 +000011// C Includes
12// C++ Includes
13// Other libraries and framework includes
14// Project includes
Eugene Zelenkoc8ecc2a2016-02-19 19:33:46 +000015#include "CommandObjectApropos.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000016#include "lldb/Interpreter/CommandInterpreter.h"
17#include "lldb/Interpreter/CommandReturnObject.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000018#include "lldb/Interpreter/Options.h"
19#include "lldb/Interpreter/Property.h"
Pavel Labath145d95c2018-04-17 18:53:35 +000020#include "lldb/Utility/Args.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021
22using namespace lldb;
23using namespace lldb_private;
24
25//-------------------------------------------------------------------------
26// CommandObjectApropos
27//-------------------------------------------------------------------------
28
Kate Stone7428a182016-07-14 22:03:10 +000029CommandObjectApropos::CommandObjectApropos(CommandInterpreter &interpreter)
Kate Stoneb9c1b512016-09-06 20:57:50 +000030 : CommandObjectParsed(
31 interpreter, "apropos",
32 "List debugger commands related to a word or subject.", nullptr) {
33 CommandArgumentEntry arg;
34 CommandArgumentData search_word_arg;
Caroline Tice405fe672010-10-04 22:28:36 +000035
Kate Stoneb9c1b512016-09-06 20:57:50 +000036 // Define the first (and only) variant of this arg.
37 search_word_arg.arg_type = eArgTypeSearchWord;
38 search_word_arg.arg_repetition = eArgRepeatPlain;
Caroline Tice405fe672010-10-04 22:28:36 +000039
Kate Stoneb9c1b512016-09-06 20:57:50 +000040 // There is only one variant this argument could be; put it into the argument
41 // entry.
42 arg.push_back(search_word_arg);
Caroline Tice405fe672010-10-04 22:28:36 +000043
Kate Stoneb9c1b512016-09-06 20:57:50 +000044 // Push the data for the first argument into the m_arguments vector.
45 m_arguments.push_back(arg);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000046}
47
Eugene Zelenkoc8ecc2a2016-02-19 19:33:46 +000048CommandObjectApropos::~CommandObjectApropos() = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000049
Kate Stoneb9c1b512016-09-06 20:57:50 +000050bool CommandObjectApropos::DoExecute(Args &args, CommandReturnObject &result) {
51 const size_t argc = args.GetArgumentCount();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000052
Kate Stoneb9c1b512016-09-06 20:57:50 +000053 if (argc == 1) {
Zachary Turner867e7d12016-12-09 01:20:58 +000054 auto search_word = args[0].ref;
55 if (!search_word.empty()) {
Adrian Prantl05097242018-04-30 16:49:04 +000056 // The bulk of the work must be done inside the Command Interpreter,
57 // since the command dictionary is private.
Kate Stoneb9c1b512016-09-06 20:57:50 +000058 StringList commands_found;
59 StringList commands_help;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000060
Kate Stoneb9c1b512016-09-06 20:57:50 +000061 m_interpreter.FindCommandsForApropos(search_word, commands_found,
62 commands_help, true, true, true);
Jim Inghamaf3753e2013-05-17 01:30:37 +000063
Kate Stoneb9c1b512016-09-06 20:57:50 +000064 if (commands_found.GetSize() == 0) {
65 result.AppendMessageWithFormat("No commands found pertaining to '%s'. "
66 "Try 'help' to see a complete list of "
67 "debugger commands.\n",
Zachary Turner867e7d12016-12-09 01:20:58 +000068 args[0].c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +000069 } else {
70 if (commands_found.GetSize() > 0) {
71 result.AppendMessageWithFormat(
Zachary Turner867e7d12016-12-09 01:20:58 +000072 "The following commands may relate to '%s':\n", args[0].c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +000073 size_t max_len = 0;
Greg Clayton67cc0632012-08-22 17:17:09 +000074
Kate Stoneb9c1b512016-09-06 20:57:50 +000075 for (size_t i = 0; i < commands_found.GetSize(); ++i) {
76 size_t len = strlen(commands_found.GetStringAtIndex(i));
77 if (len > max_len)
78 max_len = len;
79 }
80
81 for (size_t i = 0; i < commands_found.GetSize(); ++i)
82 m_interpreter.OutputFormattedHelpText(
83 result.GetOutputStream(), commands_found.GetStringAtIndex(i),
84 "--", commands_help.GetStringAtIndex(i), max_len);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000085 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000086 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000087
Kate Stoneb9c1b512016-09-06 20:57:50 +000088 std::vector<const Property *> properties;
89 const size_t num_properties =
90 m_interpreter.GetDebugger().Apropos(search_word, properties);
91 if (num_properties) {
92 const bool dump_qualified_name = true;
Zachary Turner827d5d72016-12-16 04:27:00 +000093 result.AppendMessageWithFormatv(
94 "\nThe following settings variables may relate to '{0}': \n\n",
95 args[0].ref);
Kate Stoneb9c1b512016-09-06 20:57:50 +000096 for (size_t i = 0; i < num_properties; ++i)
97 properties[i]->DumpDescription(
98 m_interpreter, result.GetOutputStream(), 0, dump_qualified_name);
99 }
100
101 result.SetStatus(eReturnStatusSuccessFinishNoResult);
102 } else {
103 result.AppendError("'' is not a valid search word.\n");
104 result.SetStatus(eReturnStatusFailed);
105 }
106 } else {
107 result.AppendError("'apropos' must be called with exactly one argument.\n");
108 result.SetStatus(eReturnStatusFailed);
109 }
110
111 return result.Succeeded();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000112}