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_
16 #include "lldb/Utility/Args.h"
17 #include "lldb/Utility/CompletionRequest.h"
18 #include "lldb/Utility/Status.h"
19 #include "lldb/lldb-defines.h"
20 #include "lldb/lldb-private.h"
22 #include "llvm/ADT/ArrayRef.h"
24 namespace lldb_private {
28 typedef std::vector<std::tuple<std::string, int, std::string>> OptionArgVector;
29 typedef std::shared_ptr<OptionArgVector> OptionArgVectorSP;
31 struct OptionArgElement {
32 enum { eUnrecognizedArg = -1, eBareDash = -2, eBareDoubleDash = -3 };
34 OptionArgElement(int defs_index, int pos, int arg_pos)
35 : opt_defs_index(defs_index), opt_pos(pos), opt_arg_pos(arg_pos) {}
42 typedef std::vector<OptionArgElement> OptionElementVector;
44 static inline bool isprint8(int ch) {
50 //----------------------------------------------------------------------
51 /// @class Options Options.h "lldb/Interpreter/Options.h"
52 /// A command line option parsing protocol class.
54 /// Options is designed to be subclassed to contain all needed options for a
55 /// given command. The options can be parsed by calling the Parse function.
57 /// The options are specified using the format defined for the libc options
58 /// parsing function getopt_long_only: \code
59 /// #include <getopt.h>
60 /// int getopt_long_only(int argc, char * const *argv, const char
61 /// *optstring, const struct option *longopts, int *longindex);
64 //----------------------------------------------------------------------
71 void BuildGetoptTable();
73 void BuildValidOptionSets();
75 uint32_t NumCommandOptions();
77 //------------------------------------------------------------------
78 /// Get the option definitions to use when parsing Args options.
80 /// @see Args::ParseOptions (Options&)
81 /// @see man getopt_long_only
82 //------------------------------------------------------------------
83 Option *GetLongOptions();
85 // This gets passed the short option as an integer...
86 void OptionSeen(int short_option);
88 bool VerifyOptions(CommandReturnObject &result);
90 // Verify that the options given are in the options table and can be used
91 // together, but there may be some required options that are missing (used to
92 // verify options that get folded into command aliases).
93 bool VerifyPartialOptions(CommandReturnObject &result);
95 void OutputFormattedUsageText(Stream &strm,
96 const OptionDefinition &option_def,
97 uint32_t output_max_columns);
99 void GenerateOptionUsage(Stream &strm, CommandObject *cmd,
100 uint32_t screen_width);
102 bool SupportsLongOption(const char *long_option);
104 // The following two pure virtual functions must be defined by every class
105 // that inherits from this class.
107 virtual llvm::ArrayRef<OptionDefinition> GetDefinitions() {
108 return llvm::ArrayRef<OptionDefinition>();
111 // Call this prior to parsing any options. This call will call the subclass
112 // OptionParsingStarting() and will avoid the need for all
113 // OptionParsingStarting() function instances from having to call the
114 // Option::OptionParsingStarting() like they did before. This was error prone
115 // and subclasses shouldn't have to do it.
116 void NotifyOptionParsingStarting(ExecutionContext *execution_context);
118 //------------------------------------------------------------------
119 /// Parse the provided arguments.
121 /// The parsed options are set via calls to SetOptionValue. In case of a
122 /// successful parse, the function returns a copy of the input arguments
123 /// with the parsed options removed. Otherwise, it returns an error.
125 /// param[in] platform_sp
126 /// The platform used for option validation. This is necessary
127 /// because an empty execution_context is not enough to get us
128 /// to a reasonable platform. If the platform isn't given,
129 /// we'll try to get it from the execution context. If we can't
130 /// get it from the execution context, we'll skip validation.
132 /// param[in] require_validation
133 /// When true, it will fail option parsing if validation could
134 /// not occur due to not having a platform.
135 //------------------------------------------------------------------
136 llvm::Expected<Args> Parse(const Args &args,
137 ExecutionContext *execution_context,
138 lldb::PlatformSP platform_sp,
139 bool require_validation);
141 llvm::Expected<Args> ParseAlias(const Args &args,
142 OptionArgVector *option_arg_vector,
143 std::string &input_line);
145 OptionElementVector ParseForCompletion(const Args &args,
146 uint32_t cursor_index);
148 Status NotifyOptionParsingFinished(ExecutionContext *execution_context);
150 //------------------------------------------------------------------
151 /// Set the value of an option.
153 /// @param[in] option_idx
154 /// The index into the "struct option" array that was returned
155 /// by Options::GetLongOptions().
157 /// @param[in] option_arg
158 /// The argument value for the option that the user entered, or
159 /// nullptr if there is no argument for the current option.
161 /// @param[in] execution_context
162 /// The execution context to use for evaluating the option.
163 /// May be nullptr if the option is to be evaluated outside any
164 /// particular context.
166 /// @see Args::ParseOptions (Options&)
167 /// @see man getopt_long_only
168 //------------------------------------------------------------------
169 virtual Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
170 ExecutionContext *execution_context) = 0;
172 //------------------------------------------------------------------
173 /// Handles the generic bits of figuring out whether we are in an option,
174 /// and if so completing it.
176 /// @param[in/out] request
177 /// The completion request that we need to act upon.
179 /// @param[in] interpreter
180 /// The interpreter that's doing the completing.
182 /// FIXME: This is the wrong return value, since we also need to
183 /// make a distinction between total number of matches, and the window the
184 /// user wants returned.
187 /// \btrue if we were in an option, \bfalse otherwise.
188 //------------------------------------------------------------------
189 bool HandleOptionCompletion(lldb_private::CompletionRequest &request,
190 OptionElementVector &option_map,
191 CommandInterpreter &interpreter);
193 //------------------------------------------------------------------
194 /// Handles the generic bits of figuring out whether we are in an option,
195 /// and if so completing it.
197 /// @param[in/out] request
198 /// The completion request that we need to act upon.
200 /// @param[in] interpreter
201 /// The command interpreter doing the completion.
203 /// FIXME: This is the wrong return value, since we also need to
204 /// make a distinction between total number of matches, and the window the
205 /// user wants returned.
208 /// \btrue if we were in an option, \bfalse otherwise.
209 //------------------------------------------------------------------
211 HandleOptionArgumentCompletion(lldb_private::CompletionRequest &request,
212 OptionElementVector &opt_element_vector,
213 int opt_element_index,
214 CommandInterpreter &interpreter);
217 // This is a set of options expressed as indexes into the options table for
219 typedef std::set<int> OptionSet;
220 typedef std::vector<OptionSet> OptionSetVector;
222 std::vector<Option> m_getopt_table;
223 OptionSet m_seen_options;
224 OptionSetVector m_required_options;
225 OptionSetVector m_optional_options;
227 OptionSetVector &GetRequiredOptions() {
228 BuildValidOptionSets();
229 return m_required_options;
232 OptionSetVector &GetOptionalOptions() {
233 BuildValidOptionSets();
234 return m_optional_options;
237 bool IsASubset(const OptionSet &set_a, const OptionSet &set_b);
239 size_t OptionsSetDiff(const OptionSet &set_a, const OptionSet &set_b,
242 void OptionsSetUnion(const OptionSet &set_a, const OptionSet &set_b,
243 OptionSet &union_set);
245 // Subclasses must reset their option values prior to starting a new option
246 // parse. Each subclass must override this function and revert all option
247 // settings to default values.
248 virtual void OptionParsingStarting(ExecutionContext *execution_context) = 0;
250 virtual Status OptionParsingFinished(ExecutionContext *execution_context) {
251 // If subclasses need to know when the options are done being parsed they
252 // can implement this function to do extra checking
260 OptionGroup() = default;
262 virtual ~OptionGroup() = default;
264 virtual llvm::ArrayRef<OptionDefinition> GetDefinitions() = 0;
266 virtual Status SetOptionValue(uint32_t option_idx,
267 llvm::StringRef option_value,
268 ExecutionContext *execution_context) = 0;
270 virtual void OptionParsingStarting(ExecutionContext *execution_context) = 0;
272 virtual Status OptionParsingFinished(ExecutionContext *execution_context) {
273 // If subclasses need to know when the options are done being parsed they
274 // can implement this function to do extra checking
280 class OptionGroupOptions : public Options {
283 : Options(), m_option_defs(), m_option_infos(), m_did_finalize(false) {}
285 ~OptionGroupOptions() override = default;
287 //----------------------------------------------------------------------
288 /// Append options from a OptionGroup class.
290 /// Append all options from \a group using the exact same option groups that
291 /// each option is defined with.
294 /// A group of options to take option values from and copy their
295 /// definitions into this class.
296 //----------------------------------------------------------------------
297 void Append(OptionGroup *group);
299 //----------------------------------------------------------------------
300 /// Append options from a OptionGroup class.
302 /// Append options from \a group that have a usage mask that has any bits in
303 /// "src_mask" set. After the option definition is copied into the options
304 /// definitions in this class, set the usage_mask to "dst_mask".
307 /// A group of options to take option values from and copy their
308 /// definitions into this class.
310 /// @param[in] src_mask
311 /// When copying options from \a group, you might only want some of
312 /// the options to be appended to this group. This mask allows you
313 /// to control which options from \a group get added. It also allows
314 /// you to specify the same options from \a group multiple times
315 /// for different option sets.
317 /// @param[in] dst_mask
318 /// Set the usage mask for any copied options to \a dst_mask after
319 /// copying the option definition.
320 //----------------------------------------------------------------------
321 void Append(OptionGroup *group, uint32_t src_mask, uint32_t dst_mask);
325 bool DidFinalize() { return m_did_finalize; }
327 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
328 ExecutionContext *execution_context) override;
330 void OptionParsingStarting(ExecutionContext *execution_context) override;
332 Status OptionParsingFinished(ExecutionContext *execution_context) override;
334 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
335 assert(m_did_finalize);
336 return m_option_defs;
339 const OptionGroup *GetGroupWithOption(char short_opt);
342 OptionInfo(OptionGroup *g, uint32_t i) : option_group(g), option_index(i) {}
343 OptionGroup *option_group; // The group that this option came from
344 uint32_t option_index; // The original option index from the OptionGroup
346 typedef std::vector<OptionInfo> OptionInfos;
348 std::vector<OptionDefinition> m_option_defs;
349 OptionInfos m_option_infos;
353 } // namespace lldb_private
355 #endif // liblldb_Options_h_