1 //===-- Options.h -----------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_Options_h_
11 #define liblldb_Options_h_
18 // Other libraries and framework includes
20 #include "lldb/Utility/Args.h"
21 #include "lldb/Utility/CompletionRequest.h"
22 #include "lldb/Utility/Status.h"
23 #include "lldb/lldb-defines.h"
24 #include "lldb/lldb-private.h"
26 #include "llvm/ADT/ArrayRef.h"
28 namespace lldb_private {
32 typedef std::vector<std::tuple<std::string, int, std::string>> OptionArgVector;
33 typedef std::shared_ptr<OptionArgVector> OptionArgVectorSP;
35 struct OptionArgElement {
36 enum { eUnrecognizedArg = -1, eBareDash = -2, eBareDoubleDash = -3 };
38 OptionArgElement(int defs_index, int pos, int arg_pos)
39 : opt_defs_index(defs_index), opt_pos(pos), opt_arg_pos(arg_pos) {}
46 typedef std::vector<OptionArgElement> OptionElementVector;
48 static inline bool isprint8(int ch) {
54 //----------------------------------------------------------------------
55 /// @class Options Options.h "lldb/Interpreter/Options.h"
56 /// A command line option parsing protocol class.
58 /// Options is designed to be subclassed to contain all needed options for a
59 /// given command. The options can be parsed by calling the Parse function.
61 /// The options are specified using the format defined for the libc options
62 /// parsing function getopt_long_only: \code
63 /// #include <getopt.h>
64 /// int getopt_long_only(int argc, char * const *argv, const char
65 /// *optstring, const struct option *longopts, int *longindex);
68 //----------------------------------------------------------------------
75 void BuildGetoptTable();
77 void BuildValidOptionSets();
79 uint32_t NumCommandOptions();
81 //------------------------------------------------------------------
82 /// Get the option definitions to use when parsing Args options.
84 /// @see Args::ParseOptions (Options&)
85 /// @see man getopt_long_only
86 //------------------------------------------------------------------
87 Option *GetLongOptions();
89 // This gets passed the short option as an integer...
90 void OptionSeen(int short_option);
92 bool VerifyOptions(CommandReturnObject &result);
94 // Verify that the options given are in the options table and can be used
95 // together, but there may be some required options that are missing (used to
96 // verify options that get folded into command aliases).
97 bool VerifyPartialOptions(CommandReturnObject &result);
99 void OutputFormattedUsageText(Stream &strm,
100 const OptionDefinition &option_def,
101 uint32_t output_max_columns);
103 void GenerateOptionUsage(Stream &strm, CommandObject *cmd,
104 uint32_t screen_width);
106 bool SupportsLongOption(const char *long_option);
108 // The following two pure virtual functions must be defined by every class
109 // that inherits from this class.
111 virtual llvm::ArrayRef<OptionDefinition> GetDefinitions() {
112 return llvm::ArrayRef<OptionDefinition>();
115 // Call this prior to parsing any options. This call will call the subclass
116 // OptionParsingStarting() and will avoid the need for all
117 // OptionParsingStarting() function instances from having to call the
118 // Option::OptionParsingStarting() like they did before. This was error prone
119 // and subclasses shouldn't have to do it.
120 void NotifyOptionParsingStarting(ExecutionContext *execution_context);
122 //------------------------------------------------------------------
123 /// Parse the provided arguments.
125 /// The parsed options are set via calls to SetOptionValue. In case of a
126 /// successful parse, the function returns a copy of the input arguments
127 /// with the parsed options removed. Otherwise, it returns an error.
129 /// param[in] platform_sp
130 /// The platform used for option validation. This is necessary
131 /// because an empty execution_context is not enough to get us
132 /// to a reasonable platform. If the platform isn't given,
133 /// we'll try to get it from the execution context. If we can't
134 /// get it from the execution context, we'll skip validation.
136 /// param[in] require_validation
137 /// When true, it will fail option parsing if validation could
138 /// not occur due to not having a platform.
139 //------------------------------------------------------------------
140 llvm::Expected<Args> Parse(const Args &args,
141 ExecutionContext *execution_context,
142 lldb::PlatformSP platform_sp,
143 bool require_validation);
145 llvm::Expected<Args> ParseAlias(const Args &args,
146 OptionArgVector *option_arg_vector,
147 std::string &input_line);
149 OptionElementVector ParseForCompletion(const Args &args,
150 uint32_t cursor_index);
152 Status NotifyOptionParsingFinished(ExecutionContext *execution_context);
154 //------------------------------------------------------------------
155 /// Set the value of an option.
157 /// @param[in] option_idx
158 /// The index into the "struct option" array that was returned
159 /// by Options::GetLongOptions().
161 /// @param[in] option_arg
162 /// The argument value for the option that the user entered, or
163 /// nullptr if there is no argument for the current option.
165 /// @param[in] execution_context
166 /// The execution context to use for evaluating the option.
167 /// May be nullptr if the option is to be evaluated outside any
168 /// particular context.
170 /// @see Args::ParseOptions (Options&)
171 /// @see man getopt_long_only
172 //------------------------------------------------------------------
173 virtual Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
174 ExecutionContext *execution_context) = 0;
176 //------------------------------------------------------------------
177 /// Handles the generic bits of figuring out whether we are in an option,
178 /// and if so completing it.
180 /// @param[in/out] request
181 /// The completion request that we need to act upon.
183 /// @param[in] interpreter
184 /// The interpreter that's doing the completing.
186 /// FIXME: This is the wrong return value, since we also need to
187 /// make a distinction between total number of matches, and the window the
188 /// user wants returned.
191 /// \btrue if we were in an option, \bfalse otherwise.
192 //------------------------------------------------------------------
193 bool HandleOptionCompletion(lldb_private::CompletionRequest &request,
194 OptionElementVector &option_map,
195 CommandInterpreter &interpreter);
197 //------------------------------------------------------------------
198 /// Handles the generic bits of figuring out whether we are in an option,
199 /// and if so completing it.
201 /// @param[in/out] request
202 /// The completion request that we need to act upon.
204 /// @param[in] interpreter
205 /// The command interpreter doing the completion.
207 /// FIXME: This is the wrong return value, since we also need to
208 /// make a distinction between total number of matches, and the window the
209 /// user wants returned.
212 /// \btrue if we were in an option, \bfalse otherwise.
213 //------------------------------------------------------------------
215 HandleOptionArgumentCompletion(lldb_private::CompletionRequest &request,
216 OptionElementVector &opt_element_vector,
217 int opt_element_index,
218 CommandInterpreter &interpreter);
221 // This is a set of options expressed as indexes into the options table for
223 typedef std::set<int> OptionSet;
224 typedef std::vector<OptionSet> OptionSetVector;
226 std::vector<Option> m_getopt_table;
227 OptionSet m_seen_options;
228 OptionSetVector m_required_options;
229 OptionSetVector m_optional_options;
231 OptionSetVector &GetRequiredOptions() {
232 BuildValidOptionSets();
233 return m_required_options;
236 OptionSetVector &GetOptionalOptions() {
237 BuildValidOptionSets();
238 return m_optional_options;
241 bool IsASubset(const OptionSet &set_a, const OptionSet &set_b);
243 size_t OptionsSetDiff(const OptionSet &set_a, const OptionSet &set_b,
246 void OptionsSetUnion(const OptionSet &set_a, const OptionSet &set_b,
247 OptionSet &union_set);
249 // Subclasses must reset their option values prior to starting a new option
250 // parse. Each subclass must override this function and revert all option
251 // settings to default values.
252 virtual void OptionParsingStarting(ExecutionContext *execution_context) = 0;
254 virtual Status OptionParsingFinished(ExecutionContext *execution_context) {
255 // If subclasses need to know when the options are done being parsed they
256 // can implement this function to do extra checking
264 OptionGroup() = default;
266 virtual ~OptionGroup() = default;
268 virtual llvm::ArrayRef<OptionDefinition> GetDefinitions() = 0;
270 virtual Status SetOptionValue(uint32_t option_idx,
271 llvm::StringRef option_value,
272 ExecutionContext *execution_context) = 0;
274 virtual void OptionParsingStarting(ExecutionContext *execution_context) = 0;
276 virtual Status OptionParsingFinished(ExecutionContext *execution_context) {
277 // If subclasses need to know when the options are done being parsed they
278 // can implement this function to do extra checking
284 class OptionGroupOptions : public Options {
287 : Options(), m_option_defs(), m_option_infos(), m_did_finalize(false) {}
289 ~OptionGroupOptions() override = default;
291 //----------------------------------------------------------------------
292 /// Append options from a OptionGroup class.
294 /// Append all options from \a group using the exact same option groups that
295 /// each option is defined with.
298 /// A group of options to take option values from and copy their
299 /// definitions into this class.
300 //----------------------------------------------------------------------
301 void Append(OptionGroup *group);
303 //----------------------------------------------------------------------
304 /// Append options from a OptionGroup class.
306 /// Append options from \a group that have a usage mask that has any bits in
307 /// "src_mask" set. After the option definition is copied into the options
308 /// definitions in this class, set the usage_mask to "dst_mask".
311 /// A group of options to take option values from and copy their
312 /// definitions into this class.
314 /// @param[in] src_mask
315 /// When copying options from \a group, you might only want some of
316 /// the options to be appended to this group. This mask allows you
317 /// to control which options from \a group get added. It also allows
318 /// you to specify the same options from \a group multiple times
319 /// for different option sets.
321 /// @param[in] dst_mask
322 /// Set the usage mask for any copied options to \a dst_mask after
323 /// copying the option definition.
324 //----------------------------------------------------------------------
325 void Append(OptionGroup *group, uint32_t src_mask, uint32_t dst_mask);
329 bool DidFinalize() { return m_did_finalize; }
331 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
332 ExecutionContext *execution_context) override;
334 void OptionParsingStarting(ExecutionContext *execution_context) override;
336 Status OptionParsingFinished(ExecutionContext *execution_context) override;
338 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
339 assert(m_did_finalize);
340 return m_option_defs;
343 const OptionGroup *GetGroupWithOption(char short_opt);
346 OptionInfo(OptionGroup *g, uint32_t i) : option_group(g), option_index(i) {}
347 OptionGroup *option_group; // The group that this option came from
348 uint32_t option_index; // The original option index from the OptionGroup
350 typedef std::vector<OptionInfo> OptionInfos;
352 std::vector<OptionDefinition> m_option_defs;
353 OptionInfos m_option_infos;
357 } // namespace lldb_private
359 #endif // liblldb_Options_h_