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_
20 // Other libraries and framework includes
22 #include "lldb/lldb-private.h"
23 #include "lldb/lldb-defines.h"
24 #include "lldb/Interpreter/Args.h"
26 namespace lldb_private {
37 //----------------------------------------------------------------------
38 /// @class Options Options.h "lldb/Interpreter/Options.h"
39 /// @brief A command line option parsing protocol class.
41 /// Options is designed to be subclassed to contain all needed
42 /// options for a given command. The options can be parsed by calling:
44 /// Error Args::ParseOptions (Options &);
47 /// The options are specified using the format defined for the libc
48 /// options parsing function getopt_long_only:
50 /// #include <getopt.h>
51 /// int getopt_long_only(int argc, char * const *argv, const char *optstring, const struct option *longopts, int *longindex);
56 /// #include <getopt.h>
59 /// class CommandOptions : public Options
62 /// virtual struct option *
63 /// GetLongOptions() {
68 /// SetOptionValue (uint32_t option_idx, int option_val, const char *option_arg)
71 /// switch (option_val)
73 /// case 'g': debug = true; break;
74 /// case 'v': verbose = true; break;
75 /// case 'l': log_file = option_arg; break;
76 /// case 'f': log_flags = strtoull(option_arg, NULL, 0); break;
78 /// error.SetErrorStringWithFormat("unrecognized short option %c", option_val);
85 /// CommandOptions (CommandInterpreter &interpreter) : debug (true), verbose (false), log_file (), log_flags (0)
90 /// std::string log_file;
91 /// uint32_t log_flags;
93 /// static struct option g_options[];
97 /// struct option CommandOptions::g_options[] =
99 /// { "debug", no_argument, NULL, 'g' },
100 /// { "log-file", required_argument, NULL, 'l' },
101 /// { "log-flags", required_argument, NULL, 'f' },
102 /// { "verbose", no_argument, NULL, 'v' },
103 /// { NULL, 0, NULL, 0 }
106 /// int main (int argc, const char **argv, const char **envp)
108 /// CommandOptions options;
109 /// Args main_command;
110 /// main_command.SetArguments(argc, argv, false);
111 /// main_command.ParseOptions(options);
113 /// if (options.verbose)
115 /// std::cout << "verbose is on" << std::endl;
119 //----------------------------------------------------------------------
124 Options (CommandInterpreter &interpreter);
133 BuildValidOptionSets ();
136 NumCommandOptions ();
138 //------------------------------------------------------------------
139 /// Get the option definitions to use when parsing Args options.
141 /// @see Args::ParseOptions (Options&)
142 /// @see man getopt_long_only
143 //------------------------------------------------------------------
147 // This gets passed the short option as an integer...
149 OptionSeen (int short_option);
152 VerifyOptions (CommandReturnObject &result);
154 // Verify that the options given are in the options table and can
155 // be used together, but there may be some required options that are
156 // missing (used to verify options that get folded into command aliases).
159 VerifyPartialOptions (CommandReturnObject &result);
162 OutputFormattedUsageText (Stream &strm,
164 uint32_t output_max_columns);
167 GenerateOptionUsage (Stream &strm,
171 SupportsLongOption (const char *long_option);
173 // The following two pure virtual functions must be defined by every
174 // class that inherits from this class.
176 virtual const OptionDefinition*
177 GetDefinitions () { return NULL; }
179 // Call this prior to parsing any options. This call will call the
180 // subclass OptionParsingStarting() and will avoid the need for all
181 // OptionParsingStarting() function instances from having to call the
182 // Option::OptionParsingStarting() like they did before. This was error
183 // prone and subclasses shouldn't have to do it.
185 NotifyOptionParsingStarting ();
188 NotifyOptionParsingFinished ();
190 //------------------------------------------------------------------
191 /// Set the value of an option.
193 /// @param[in] option_idx
194 /// The index into the "struct option" array that was returned
195 /// by Options::GetLongOptions().
197 /// @param[in] option_arg
198 /// The argument value for the option that the user entered, or
199 /// NULL if there is no argument for the current option.
202 /// @see Args::ParseOptions (Options&)
203 /// @see man getopt_long_only
204 //------------------------------------------------------------------
206 SetOptionValue (uint32_t option_idx, const char *option_arg) = 0;
208 //------------------------------------------------------------------
209 /// Handles the generic bits of figuring out whether we are in an
210 /// option, and if so completing it.
213 /// The command line parsed into words
215 /// @param[in] cursor_index
216 /// The index in \ainput of the word in which the cursor lies.
218 /// @param[in] char_pos
219 /// The character position of the cursor in its argument word.
221 /// @param[in] match_start_point
222 /// @param[in] match_return_elements
223 /// See CommandObject::HandleCompletions for a description of
226 /// @param[in] interpreter
227 /// The interpreter that's doing the completing.
229 /// @param[out] word_complete
230 /// \btrue if this is a complete option value (a space will be
231 /// inserted after the completion.) \b false otherwise.
233 /// @param[out] matches
234 /// The array of matches returned.
236 /// FIXME: This is the wrong return value, since we also need to
237 /// make a distinction between total number of matches, and the
238 /// window the user wants returned.
241 /// \btrue if we were in an option, \bfalse otherwise.
242 //------------------------------------------------------------------
244 HandleOptionCompletion (Args &input,
245 OptionElementVector &option_map,
248 int match_start_point,
249 int max_return_elements,
251 lldb_private::StringList &matches);
253 //------------------------------------------------------------------
254 /// Handles the generic bits of figuring out whether we are in an
255 /// option, and if so completing it.
257 /// @param[in] interpreter
258 /// The command interpreter doing the completion.
261 /// The command line parsed into words
263 /// @param[in] cursor_index
264 /// The index in \ainput of the word in which the cursor lies.
266 /// @param[in] char_pos
267 /// The character position of the cursor in its argument word.
269 /// @param[in] opt_element_vector
270 /// The results of the options parse of \a input.
272 /// @param[in] opt_element_index
273 /// The position in \a opt_element_vector of the word in \a
274 /// input containing the cursor.
276 /// @param[in] match_start_point
277 /// @param[in] match_return_elements
278 /// See CommandObject::HandleCompletions for a description of
281 /// @param[out] word_complete
282 /// \btrue if this is a complete option value (a space will
283 /// be inserted after the completion.) \bfalse otherwise.
285 /// @param[out] matches
286 /// The array of matches returned.
288 /// FIXME: This is the wrong return value, since we also need to
289 /// make a distinction between total number of matches, and the
290 /// window the user wants returned.
293 /// \btrue if we were in an option, \bfalse otherwise.
294 //------------------------------------------------------------------
296 HandleOptionArgumentCompletion (Args &input,
299 OptionElementVector &opt_element_vector,
300 int opt_element_index,
301 int match_start_point,
302 int max_return_elements,
304 StringList &matches);
307 // This is a set of options expressed as indexes into the options table for this Option.
308 typedef std::set<int> OptionSet;
309 typedef std::vector<OptionSet> OptionSetVector;
311 CommandInterpreter &m_interpreter;
312 std::vector<struct option> m_getopt_table;
313 OptionSet m_seen_options;
314 OptionSetVector m_required_options;
315 OptionSetVector m_optional_options;
317 OptionSetVector &GetRequiredOptions ()
319 BuildValidOptionSets();
320 return m_required_options;
323 OptionSetVector &GetOptionalOptions ()
325 BuildValidOptionSets();
326 return m_optional_options;
330 IsASubset (const OptionSet& set_a, const OptionSet& set_b);
333 OptionsSetDiff (const OptionSet &set_a, const OptionSet &set_b, OptionSet &diffs);
336 OptionsSetUnion (const OptionSet &set_a, const OptionSet &set_b, OptionSet &union_set);
338 // Subclasses must reset their option values prior to starting a new
339 // option parse. Each subclass must override this function and revert
340 // all option settings to default values.
342 OptionParsingStarting () = 0;
345 OptionParsingFinished ()
347 // If subclasses need to know when the options are done being parsed
348 // they can implement this function to do extra checking
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
394 OptionGroupOptions (CommandInterpreter &interpreter) :
395 Options (interpreter),
398 m_did_finalize (false)
403 ~OptionGroupOptions ()
408 //----------------------------------------------------------------------
409 /// Append options from a OptionGroup class.
411 /// Append all options from \a group using the exact same option groups
412 /// that each option is defined with.
415 /// A group of options to take option values from and copy their
416 /// definitions into this class.
417 //----------------------------------------------------------------------
419 Append (OptionGroup* group);
421 //----------------------------------------------------------------------
422 /// Append options from a OptionGroup class.
424 /// Append options from \a group that have a usage mask that has any bits
425 /// in "src_mask" set. After the option definition is copied into the
426 /// options definitions in this class, set the usage_mask to "dst_mask".
429 /// A group of options to take option values from and copy their
430 /// definitions into this class.
432 /// @param[in] src_mask
433 /// When copying options from \a group, you might only want some of
434 /// the options to be appended to this group. This mask allows you
435 /// to control which options from \a group get added. It also allows
436 /// you to specify the same options from \a group multiple times
437 /// for different option sets.
439 /// @param[in] dst_mask
440 /// Set the usage mask for any copied options to \a dst_mask after
441 /// copying the option definition.
442 //----------------------------------------------------------------------
444 Append (OptionGroup* group,
452 SetOptionValue (uint32_t option_idx,
453 const char *option_arg);
456 OptionParsingStarting ();
459 OptionParsingFinished ();
461 const OptionDefinition*
464 assert (m_did_finalize);
465 return &m_option_defs[0];
469 OptionInfo (OptionGroup* g, uint32_t i) :
474 OptionGroup* option_group; // The group that this option came from
475 uint32_t option_index; // The original option index from the OptionGroup
477 typedef std::vector<OptionInfo> OptionInfos;
479 std::vector<OptionDefinition> m_option_defs;
480 OptionInfos m_option_infos;
485 } // namespace lldb_private
487 #endif // liblldb_Options_h_