15ffd83dbSDimitry Andric //===-- CommandObjectApropos.cpp ------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "CommandObjectApropos.h"
100b57cec5SDimitry Andric #include "lldb/Interpreter/CommandInterpreter.h"
110b57cec5SDimitry Andric #include "lldb/Interpreter/CommandReturnObject.h"
120b57cec5SDimitry Andric #include "lldb/Interpreter/Property.h"
130b57cec5SDimitry Andric #include "lldb/Utility/Args.h"
140b57cec5SDimitry Andric 
150b57cec5SDimitry Andric using namespace lldb;
160b57cec5SDimitry Andric using namespace lldb_private;
170b57cec5SDimitry Andric 
180b57cec5SDimitry Andric // CommandObjectApropos
190b57cec5SDimitry Andric 
CommandObjectApropos(CommandInterpreter & interpreter)200b57cec5SDimitry Andric CommandObjectApropos::CommandObjectApropos(CommandInterpreter &interpreter)
210b57cec5SDimitry Andric     : CommandObjectParsed(
220b57cec5SDimitry Andric           interpreter, "apropos",
230b57cec5SDimitry Andric           "List debugger commands related to a word or subject.", nullptr) {
240b57cec5SDimitry Andric   CommandArgumentEntry arg;
250b57cec5SDimitry Andric   CommandArgumentData search_word_arg;
260b57cec5SDimitry Andric 
270b57cec5SDimitry Andric   // Define the first (and only) variant of this arg.
280b57cec5SDimitry Andric   search_word_arg.arg_type = eArgTypeSearchWord;
290b57cec5SDimitry Andric   search_word_arg.arg_repetition = eArgRepeatPlain;
300b57cec5SDimitry Andric 
310b57cec5SDimitry Andric   // There is only one variant this argument could be; put it into the argument
320b57cec5SDimitry Andric   // entry.
330b57cec5SDimitry Andric   arg.push_back(search_word_arg);
340b57cec5SDimitry Andric 
350b57cec5SDimitry Andric   // Push the data for the first argument into the m_arguments vector.
360b57cec5SDimitry Andric   m_arguments.push_back(arg);
370b57cec5SDimitry Andric }
380b57cec5SDimitry Andric 
390b57cec5SDimitry Andric CommandObjectApropos::~CommandObjectApropos() = default;
400b57cec5SDimitry Andric 
DoExecute(Args & args,CommandReturnObject & result)41*c9157d92SDimitry Andric void CommandObjectApropos::DoExecute(Args &args, CommandReturnObject &result) {
420b57cec5SDimitry Andric   const size_t argc = args.GetArgumentCount();
430b57cec5SDimitry Andric 
440b57cec5SDimitry Andric   if (argc == 1) {
459dba64beSDimitry Andric     auto search_word = args[0].ref();
460b57cec5SDimitry Andric     if (!search_word.empty()) {
470b57cec5SDimitry Andric       // The bulk of the work must be done inside the Command Interpreter,
480b57cec5SDimitry Andric       // since the command dictionary is private.
490b57cec5SDimitry Andric       StringList commands_found;
500b57cec5SDimitry Andric       StringList commands_help;
510b57cec5SDimitry Andric 
52349cc55cSDimitry Andric       m_interpreter.FindCommandsForApropos(
53349cc55cSDimitry Andric           search_word, commands_found, commands_help, true, true, true, true);
540b57cec5SDimitry Andric 
550b57cec5SDimitry Andric       if (commands_found.GetSize() == 0) {
560b57cec5SDimitry Andric         result.AppendMessageWithFormat("No commands found pertaining to '%s'. "
570b57cec5SDimitry Andric                                        "Try 'help' to see a complete list of "
580b57cec5SDimitry Andric                                        "debugger commands.\n",
590b57cec5SDimitry Andric                                        args[0].c_str());
600b57cec5SDimitry Andric       } else {
610b57cec5SDimitry Andric         if (commands_found.GetSize() > 0) {
620b57cec5SDimitry Andric           result.AppendMessageWithFormat(
630b57cec5SDimitry Andric               "The following commands may relate to '%s':\n", args[0].c_str());
649dba64beSDimitry Andric           const size_t max_len = commands_found.GetMaxStringLength();
650b57cec5SDimitry Andric 
660b57cec5SDimitry Andric           for (size_t i = 0; i < commands_found.GetSize(); ++i)
670b57cec5SDimitry Andric             m_interpreter.OutputFormattedHelpText(
680b57cec5SDimitry Andric                 result.GetOutputStream(), commands_found.GetStringAtIndex(i),
690b57cec5SDimitry Andric                 "--", commands_help.GetStringAtIndex(i), max_len);
700b57cec5SDimitry Andric         }
710b57cec5SDimitry Andric       }
720b57cec5SDimitry Andric 
730b57cec5SDimitry Andric       std::vector<const Property *> properties;
740b57cec5SDimitry Andric       const size_t num_properties =
750b57cec5SDimitry Andric           GetDebugger().Apropos(search_word, properties);
760b57cec5SDimitry Andric       if (num_properties) {
770b57cec5SDimitry Andric         const bool dump_qualified_name = true;
780b57cec5SDimitry Andric         result.AppendMessageWithFormatv(
790b57cec5SDimitry Andric             "\nThe following settings variables may relate to '{0}': \n\n",
809dba64beSDimitry Andric             args[0].ref());
810b57cec5SDimitry Andric         for (size_t i = 0; i < num_properties; ++i)
820b57cec5SDimitry Andric           properties[i]->DumpDescription(
830b57cec5SDimitry Andric               m_interpreter, result.GetOutputStream(), 0, dump_qualified_name);
840b57cec5SDimitry Andric       }
850b57cec5SDimitry Andric 
860b57cec5SDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishNoResult);
870b57cec5SDimitry Andric     } else {
880b57cec5SDimitry Andric       result.AppendError("'' is not a valid search word.\n");
890b57cec5SDimitry Andric     }
900b57cec5SDimitry Andric   } else {
910b57cec5SDimitry Andric     result.AppendError("'apropos' must be called with exactly one argument.\n");
920b57cec5SDimitry Andric   }
930b57cec5SDimitry Andric }
94