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/lldb-private.h"
21 #include "lldb/lldb-defines.h"
22 #include "lldb/Interpreter/Args.h"
24 namespace lldb_private {
34 //----------------------------------------------------------------------
35 /// @class Options Options.h "lldb/Interpreter/Options.h"
36 /// @brief A command line option parsing protocol class.
38 /// Options is designed to be subclassed to contain all needed
39 /// options for a given command. The options can be parsed by calling:
41 /// Error Args::ParseOptions (Options &);
44 /// The options are specified using the format defined for the libc
45 /// options parsing function getopt_long_only:
47 /// #include <getopt.h>
48 /// int getopt_long_only(int argc, char * const *argv, const char *optstring, const struct option *longopts, int *longindex);
53 /// #include <getopt.h>
56 /// class CommandOptions : public Options
59 /// virtual struct option *
60 /// GetLongOptions() {
65 /// SetOptionValue (uint32_t option_idx, int option_val, const char *option_arg)
68 /// switch (option_val)
70 /// case 'g': debug = true; break;
71 /// case 'v': verbose = true; break;
72 /// case 'l': log_file = option_arg; break;
73 /// case 'f': log_flags = strtoull(option_arg, nullptr, 0); break;
75 /// error.SetErrorStringWithFormat("unrecognized short option %c", option_val);
82 /// CommandOptions (CommandInterpreter &interpreter) : debug (true), verbose (false), log_file (), log_flags (0)
87 /// std::string log_file;
88 /// uint32_t log_flags;
90 /// static struct option g_options[];
94 /// struct option CommandOptions::g_options[] =
96 /// { "debug", no_argument, nullptr, 'g' },
97 /// { "log-file", required_argument, nullptr, 'l' },
98 /// { "log-flags", required_argument, nullptr, 'f' },
99 /// { "verbose", no_argument, nullptr, 'v' },
100 /// { nullptr, 0, nullptr, 0 }
103 /// int main (int argc, const char **argv, const char **envp)
105 /// CommandOptions options;
106 /// Args main_command;
107 /// main_command.SetArguments(argc, argv, false);
108 /// main_command.ParseOptions(options);
110 /// if (options.verbose)
112 /// std::cout << "verbose is on" << std::endl;
116 //----------------------------------------------------------------------
120 Options (CommandInterpreter &interpreter);
129 BuildValidOptionSets ();
132 NumCommandOptions ();
134 //------------------------------------------------------------------
135 /// Get the option definitions to use when parsing Args options.
137 /// @see Args::ParseOptions (Options&)
138 /// @see man getopt_long_only
139 //------------------------------------------------------------------
143 // This gets passed the short option as an integer...
145 OptionSeen (int short_option);
148 VerifyOptions (CommandReturnObject &result);
150 // Verify that the options given are in the options table and can
151 // be used together, but there may be some required options that are
152 // missing (used to verify options that get folded into command aliases).
154 VerifyPartialOptions (CommandReturnObject &result);
157 OutputFormattedUsageText (Stream &strm,
158 const OptionDefinition &option_def,
159 uint32_t output_max_columns);
162 GenerateOptionUsage (Stream &strm,
166 SupportsLongOption (const char *long_option);
168 // The following two pure virtual functions must be defined by every
169 // class that inherits from this class.
171 virtual const OptionDefinition*
177 // Call this prior to parsing any options. This call will call the
178 // subclass OptionParsingStarting() and will avoid the need for all
179 // OptionParsingStarting() function instances from having to call the
180 // Option::OptionParsingStarting() like they did before. This was error
181 // prone and subclasses shouldn't have to do it.
183 NotifyOptionParsingStarting ();
186 NotifyOptionParsingFinished ();
188 //------------------------------------------------------------------
189 /// Set the value of an option.
191 /// @param[in] option_idx
192 /// The index into the "struct option" array that was returned
193 /// by Options::GetLongOptions().
195 /// @param[in] option_arg
196 /// The argument value for the option that the user entered, or
197 /// nullptr if there is no argument for the current option.
200 /// @see Args::ParseOptions (Options&)
201 /// @see man getopt_long_only
202 //------------------------------------------------------------------
204 SetOptionValue (uint32_t option_idx, const char *option_arg) = 0;
206 //------------------------------------------------------------------
207 /// Handles the generic bits of figuring out whether we are in an
208 /// option, and if so completing it.
211 /// The command line parsed into words
213 /// @param[in] cursor_index
214 /// The index in \ainput of the word in which the cursor lies.
216 /// @param[in] char_pos
217 /// The character position of the cursor in its argument word.
219 /// @param[in] match_start_point
220 /// @param[in] match_return_elements
221 /// See CommandObject::HandleCompletions for a description of
224 /// @param[in] interpreter
225 /// The interpreter that's doing the completing.
227 /// @param[out] word_complete
228 /// \btrue if this is a complete option value (a space will be
229 /// inserted after the completion.) \b false otherwise.
231 /// @param[out] matches
232 /// The array of matches returned.
234 /// FIXME: This is the wrong return value, since we also need to
235 /// make a distinction between total number of matches, and the
236 /// window the user wants returned.
239 /// \btrue if we were in an option, \bfalse otherwise.
240 //------------------------------------------------------------------
242 HandleOptionCompletion (Args &input,
243 OptionElementVector &option_map,
246 int match_start_point,
247 int max_return_elements,
249 lldb_private::StringList &matches);
251 //------------------------------------------------------------------
252 /// Handles the generic bits of figuring out whether we are in an
253 /// option, and if so completing it.
255 /// @param[in] interpreter
256 /// The command interpreter doing the completion.
259 /// The command line parsed into words
261 /// @param[in] cursor_index
262 /// The index in \ainput of the word in which the cursor lies.
264 /// @param[in] char_pos
265 /// The character position of the cursor in its argument word.
267 /// @param[in] opt_element_vector
268 /// The results of the options parse of \a input.
270 /// @param[in] opt_element_index
271 /// The position in \a opt_element_vector of the word in \a
272 /// input containing the cursor.
274 /// @param[in] match_start_point
275 /// @param[in] match_return_elements
276 /// See CommandObject::HandleCompletions for a description of
279 /// @param[out] word_complete
280 /// \btrue if this is a complete option value (a space will
281 /// be inserted after the completion.) \bfalse otherwise.
283 /// @param[out] matches
284 /// The array of matches returned.
286 /// FIXME: This is the wrong return value, since we also need to
287 /// make a distinction between total number of matches, and the
288 /// window the user wants returned.
291 /// \btrue if we were in an option, \bfalse otherwise.
292 //------------------------------------------------------------------
294 HandleOptionArgumentCompletion (Args &input,
297 OptionElementVector &opt_element_vector,
298 int opt_element_index,
299 int match_start_point,
300 int max_return_elements,
302 StringList &matches);
307 return m_interpreter;
311 // This is a set of options expressed as indexes into the options table for this Option.
312 typedef std::set<int> OptionSet;
313 typedef std::vector<OptionSet> OptionSetVector;
315 CommandInterpreter &m_interpreter;
316 std::vector<Option> m_getopt_table;
317 OptionSet m_seen_options;
318 OptionSetVector m_required_options;
319 OptionSetVector m_optional_options;
321 OptionSetVector &GetRequiredOptions ()
323 BuildValidOptionSets();
324 return m_required_options;
327 OptionSetVector &GetOptionalOptions ()
329 BuildValidOptionSets();
330 return m_optional_options;
334 IsASubset (const OptionSet& set_a, const OptionSet& set_b);
337 OptionsSetDiff (const OptionSet &set_a, const OptionSet &set_b, OptionSet &diffs);
340 OptionsSetUnion (const OptionSet &set_a, const OptionSet &set_b, OptionSet &union_set);
342 // Subclasses must reset their option values prior to starting a new
343 // option parse. Each subclass must override this function and revert
344 // all option settings to default values.
346 OptionParsingStarting () = 0;
349 OptionParsingFinished ()
351 // If subclasses need to know when the options are done being parsed
352 // they can implement this function to do extra checking
361 OptionGroup() = default;
364 ~OptionGroup() = default;
367 GetNumDefinitions () = 0;
369 virtual const OptionDefinition*
370 GetDefinitions () = 0;
373 SetOptionValue (CommandInterpreter &interpreter,
375 const char *option_value) = 0;
378 OptionParsingStarting (CommandInterpreter &interpreter) = 0;
381 OptionParsingFinished (CommandInterpreter &interpreter)
383 // If subclasses need to know when the options are done being parsed
384 // they can implement this function to do extra checking
390 class OptionGroupOptions : public Options
393 OptionGroupOptions (CommandInterpreter &interpreter) :
394 Options (interpreter),
397 m_did_finalize (false)
401 ~OptionGroupOptions() override = default;
403 //----------------------------------------------------------------------
404 /// Append options from a OptionGroup class.
406 /// Append all options from \a group using the exact same option groups
407 /// that each option is defined with.
410 /// A group of options to take option values from and copy their
411 /// definitions into this class.
412 //----------------------------------------------------------------------
414 Append (OptionGroup* group);
416 //----------------------------------------------------------------------
417 /// Append options from a OptionGroup class.
419 /// Append options from \a group that have a usage mask that has any bits
420 /// in "src_mask" set. After the option definition is copied into the
421 /// options definitions in this class, set the usage_mask to "dst_mask".
424 /// A group of options to take option values from and copy their
425 /// definitions into this class.
427 /// @param[in] src_mask
428 /// When copying options from \a group, you might only want some of
429 /// the options to be appended to this group. This mask allows you
430 /// to control which options from \a group get added. It also allows
431 /// you to specify the same options from \a group multiple times
432 /// for different option sets.
434 /// @param[in] dst_mask
435 /// Set the usage mask for any copied options to \a dst_mask after
436 /// copying the option definition.
437 //----------------------------------------------------------------------
439 Append (OptionGroup* group,
449 return m_did_finalize;
453 SetOptionValue(uint32_t option_idx,
454 const char *option_arg) override;
457 OptionParsingStarting() override;
460 OptionParsingFinished() override;
462 const OptionDefinition*
463 GetDefinitions() override
465 assert (m_did_finalize);
466 return &m_option_defs[0];
470 GetGroupWithOption (char short_opt);
474 OptionInfo (OptionGroup* g, uint32_t i) :
479 OptionGroup* option_group; // The group that this option came from
480 uint32_t option_index; // The original option index from the OptionGroup
482 typedef std::vector<OptionInfo> OptionInfos;
484 std::vector<OptionDefinition> m_option_defs;
485 OptionInfos m_option_infos;
489 } // namespace lldb_private
491 #endif // liblldb_Options_h_