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 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) { 24*0fca6ea1SDimitry Andric AddSimpleArgumentList(eArgTypeSearchWord); 250b57cec5SDimitry Andric } 260b57cec5SDimitry Andric 270b57cec5SDimitry Andric CommandObjectApropos::~CommandObjectApropos() = default; 280b57cec5SDimitry Andric 295f757f3fSDimitry Andric void CommandObjectApropos::DoExecute(Args &args, CommandReturnObject &result) { 300b57cec5SDimitry Andric const size_t argc = args.GetArgumentCount(); 310b57cec5SDimitry Andric 320b57cec5SDimitry Andric if (argc == 1) { 339dba64beSDimitry Andric auto search_word = args[0].ref(); 340b57cec5SDimitry Andric if (!search_word.empty()) { 350b57cec5SDimitry Andric // The bulk of the work must be done inside the Command Interpreter, 360b57cec5SDimitry Andric // since the command dictionary is private. 370b57cec5SDimitry Andric StringList commands_found; 380b57cec5SDimitry Andric StringList commands_help; 390b57cec5SDimitry Andric 40349cc55cSDimitry Andric m_interpreter.FindCommandsForApropos( 41349cc55cSDimitry Andric search_word, commands_found, commands_help, true, true, true, true); 420b57cec5SDimitry Andric 430b57cec5SDimitry Andric if (commands_found.GetSize() == 0) { 440b57cec5SDimitry Andric result.AppendMessageWithFormat("No commands found pertaining to '%s'. " 450b57cec5SDimitry Andric "Try 'help' to see a complete list of " 460b57cec5SDimitry Andric "debugger commands.\n", 470b57cec5SDimitry Andric args[0].c_str()); 480b57cec5SDimitry Andric } else { 490b57cec5SDimitry Andric if (commands_found.GetSize() > 0) { 500b57cec5SDimitry Andric result.AppendMessageWithFormat( 510b57cec5SDimitry Andric "The following commands may relate to '%s':\n", args[0].c_str()); 529dba64beSDimitry Andric const size_t max_len = commands_found.GetMaxStringLength(); 530b57cec5SDimitry Andric 540b57cec5SDimitry Andric for (size_t i = 0; i < commands_found.GetSize(); ++i) 550b57cec5SDimitry Andric m_interpreter.OutputFormattedHelpText( 560b57cec5SDimitry Andric result.GetOutputStream(), commands_found.GetStringAtIndex(i), 570b57cec5SDimitry Andric "--", commands_help.GetStringAtIndex(i), max_len); 580b57cec5SDimitry Andric } 590b57cec5SDimitry Andric } 600b57cec5SDimitry Andric 610b57cec5SDimitry Andric std::vector<const Property *> properties; 620b57cec5SDimitry Andric const size_t num_properties = 630b57cec5SDimitry Andric GetDebugger().Apropos(search_word, properties); 640b57cec5SDimitry Andric if (num_properties) { 650b57cec5SDimitry Andric const bool dump_qualified_name = true; 660b57cec5SDimitry Andric result.AppendMessageWithFormatv( 670b57cec5SDimitry Andric "\nThe following settings variables may relate to '{0}': \n\n", 689dba64beSDimitry Andric args[0].ref()); 690b57cec5SDimitry Andric for (size_t i = 0; i < num_properties; ++i) 700b57cec5SDimitry Andric properties[i]->DumpDescription( 710b57cec5SDimitry Andric m_interpreter, result.GetOutputStream(), 0, dump_qualified_name); 720b57cec5SDimitry Andric } 730b57cec5SDimitry Andric 740b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessFinishNoResult); 750b57cec5SDimitry Andric } else { 760b57cec5SDimitry Andric result.AppendError("'' is not a valid search word.\n"); 770b57cec5SDimitry Andric } 780b57cec5SDimitry Andric } else { 790b57cec5SDimitry Andric result.AppendError("'apropos' must be called with exactly one argument.\n"); 800b57cec5SDimitry Andric } 810b57cec5SDimitry Andric } 82