//===-- Options.h -----------------------------------------------*- C++ -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// #ifndef LLDB_INTERPRETER_OPTIONS_H #define LLDB_INTERPRETER_OPTIONS_H #include #include #include "lldb/Utility/Args.h" #include "lldb/Utility/CompletionRequest.h" #include "lldb/Utility/OptionDefinition.h" #include "lldb/Utility/Status.h" #include "lldb/lldb-defines.h" #include "lldb/lldb-private.h" #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/StringRef.h" namespace lldb_private { struct Option; typedef std::vector> OptionArgVector; typedef std::shared_ptr OptionArgVectorSP; struct OptionArgElement { enum { eUnrecognizedArg = -1, eBareDash = -2, eBareDoubleDash = -3 }; OptionArgElement(int defs_index, int pos, int arg_pos) : opt_defs_index(defs_index), opt_pos(pos), opt_arg_pos(arg_pos) {} int opt_defs_index; int opt_pos; int opt_arg_pos; }; typedef std::vector OptionElementVector; /// \class Options Options.h "lldb/Interpreter/Options.h" /// A command line option parsing protocol class. /// /// Options is designed to be subclassed to contain all needed options for a /// given command. The options can be parsed by calling the Parse function. /// /// The options are specified using the format defined for the libc options /// parsing function getopt_long_only: \code /// #include /// int getopt_long_only(int argc, char * const *argv, const char /// *optstring, const struct option *longopts, int *longindex); /// \endcode /// class Options { public: Options(); virtual ~Options(); void BuildGetoptTable(); void BuildValidOptionSets(); uint32_t NumCommandOptions(); /// Get the option definitions to use when parsing Args options. /// /// \see Args::ParseOptions (Options&) /// \see man getopt_long_only Option *GetLongOptions(); // This gets passed the short option as an integer... void OptionSeen(int short_option); bool VerifyOptions(CommandReturnObject &result); // Verify that the options given are in the options table and can be used // together, but there may be some required options that are missing (used to // verify options that get folded into command aliases). bool VerifyPartialOptions(CommandReturnObject &result); void OutputFormattedUsageText(Stream &strm, const OptionDefinition &option_def, uint32_t output_max_columns); void GenerateOptionUsage(Stream &strm, CommandObject &cmd, uint32_t screen_width); bool SupportsLongOption(const char *long_option); // The following two pure virtual functions must be defined by every class // that inherits from this class. virtual llvm::ArrayRef GetDefinitions() { return llvm::ArrayRef(); } // Call this prior to parsing any options. This call will call the subclass // OptionParsingStarting() and will avoid the need for all // OptionParsingStarting() function instances from having to call the // Option::OptionParsingStarting() like they did before. This was error prone // and subclasses shouldn't have to do it. void NotifyOptionParsingStarting(ExecutionContext *execution_context); /// Parse the provided arguments. /// /// The parsed options are set via calls to SetOptionValue. In case of a /// successful parse, the function returns a copy of the input arguments /// with the parsed options removed. Otherwise, it returns an error. /// /// param[in] platform_sp /// The platform used for option validation. This is necessary /// because an empty execution_context is not enough to get us /// to a reasonable platform. If the platform isn't given, /// we'll try to get it from the execution context. If we can't /// get it from the execution context, we'll skip validation. /// /// param[in] require_validation /// When true, it will fail option parsing if validation could /// not occur due to not having a platform. llvm::Expected Parse(const Args &args, ExecutionContext *execution_context, lldb::PlatformSP platform_sp, bool require_validation); llvm::Expected ParseAlias(const Args &args, OptionArgVector *option_arg_vector, std::string &input_line); OptionElementVector ParseForCompletion(const Args &args, uint32_t cursor_index); Status NotifyOptionParsingFinished(ExecutionContext *execution_context); /// Set the value of an option. /// /// \param[in] option_idx /// The index into the "struct option" array that was returned /// by Options::GetLongOptions(). /// /// \param[in] option_arg /// The argument value for the option that the user entered, or /// nullptr if there is no argument for the current option. /// /// \param[in] execution_context /// The execution context to use for evaluating the option. /// May be nullptr if the option is to be evaluated outside any /// particular context. /// /// \see Args::ParseOptions (Options&) /// \see man getopt_long_only virtual Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, ExecutionContext *execution_context) = 0; /// Handles the generic bits of figuring out whether we are in an option, /// and if so completing it. /// /// \param[in,out] request /// The completion request that we need to act upon. /// /// \param[in] interpreter /// The interpreter that's doing the completing. /// /// FIXME: This is the wrong return value, since we also need to /// make a distinction between total number of matches, and the window the /// user wants returned. /// /// \return /// \b true if we were in an option, \b false otherwise. bool HandleOptionCompletion(lldb_private::CompletionRequest &request, OptionElementVector &option_map, CommandInterpreter &interpreter); /// Handles the generic bits of figuring out whether we are in an option, /// and if so completing it. /// /// \param[in,out] request /// The completion request that we need to act upon. /// /// \param[in] interpreter /// The command interpreter doing the completion. virtual void HandleOptionArgumentCompletion(lldb_private::CompletionRequest &request, OptionElementVector &opt_element_vector, int opt_element_index, CommandInterpreter &interpreter); protected: // This is a set of options expressed as indexes into the options table for // this Option. typedef std::set OptionSet; typedef std::vector OptionSetVector; std::vector