1 //===- Support/CommandLine.h - Flexible Command line parser -----*- C++ -*-===//
3 // This class implements a command line argument processor that is useful when
4 // creating a tool. It provides a simple, minimalistic interface that is easily
5 // extensible and supports nonlocal (library) command line options.
7 // Note that rather than trying to figure out what this code does, you should
8 // read the library documentation located in docs/CommandLine.html or looks at
9 // the many example usages in tools/*/*.cpp
11 //===----------------------------------------------------------------------===//
13 #ifndef SUPPORT_COMMANDLINE_H
14 #define SUPPORT_COMMANDLINE_H
21 #include "boost/type_traits/object_traits.hpp"
23 /// cl Namespace - This namespace contains all of the command line option
24 /// processing machinery. It is intentionally a short name to make qualified
28 //===----------------------------------------------------------------------===//
29 // ParseCommandLineOptions - Command line option processing entry point.
31 void ParseCommandLineOptions(int &argc, char **argv,
32 const char *Overview = 0);
34 //===----------------------------------------------------------------------===//
35 // ParseEnvironmentOptions - Environment variable option processing alternate
38 void ParseEnvironmentOptions(const char *progName, const char *envvar,
39 const char *Overview = 0);
41 //===----------------------------------------------------------------------===//
42 // Flags permitted to be passed to command line arguments
45 enum NumOccurrences { // Flags for the number of occurrences allowed
46 Optional = 0x01, // Zero or One occurrence
47 ZeroOrMore = 0x02, // Zero or more occurrences allowed
48 Required = 0x03, // One occurrence required
49 OneOrMore = 0x04, // One or more occurrences required
51 // ConsumeAfter - Indicates that this option is fed anything that follows the
52 // last positional argument required by the application (it is an error if
53 // there are zero positional arguments, and a ConsumeAfter option is used).
54 // Thus, for example, all arguments to LLI are processed until a filename is
55 // found. Once a filename is found, all of the succeeding arguments are
56 // passed, unprocessed, to the ConsumeAfter option.
60 OccurrencesMask = 0x07,
63 enum ValueExpected { // Is a value required for the option?
64 ValueOptional = 0x08, // The value can appear... or not
65 ValueRequired = 0x10, // The value is required to appear!
66 ValueDisallowed = 0x18, // A value may not be specified (for flags)
70 enum OptionHidden { // Control whether -help shows this option
71 NotHidden = 0x20, // Option included in --help & --help-hidden
72 Hidden = 0x40, // -help doesn't, but --help-hidden does
73 ReallyHidden = 0x60, // Neither --help nor --help-hidden show this arg
77 // Formatting flags - This controls special features that the option might have
78 // that cause it to be parsed differently...
80 // Prefix - This option allows arguments that are otherwise unrecognized to be
81 // matched by options that are a prefix of the actual value. This is useful for
82 // cases like a linker, where options are typically of the form '-lfoo' or
83 // '-L../../include' where -l or -L are the actual flags. When prefix is
84 // enabled, and used, the value for the flag comes from the suffix of the
87 // Grouping - With this option enabled, multiple letter options are allowed to
88 // bunch together with only a single hyphen for the whole group. This allows
89 // emulation of the behavior that ls uses for example: ls -la === ls -l -a
92 enum FormattingFlags {
93 NormalFormatting = 0x000, // Nothing special
94 Positional = 0x080, // Is a positional argument, no '-' required
95 Prefix = 0x100, // Can this option directly prefix its value?
96 Grouping = 0x180, // Can this option group with other options?
97 FormattingMask = 0x180,
100 enum MiscFlags { // Miscellaneous flags to adjust argument
101 CommaSeparated = 0x200, // Should this cl::list split between commas?
107 //===----------------------------------------------------------------------===//
112 friend void cl::ParseCommandLineOptions(int &, char **, const char *, int);
115 // handleOccurrences - Overriden by subclasses to handle the value passed into
116 // an argument. Should return true if there was an error processing the
117 // argument and the program should exit.
119 virtual bool handleOccurrence(const char *ArgName, const std::string &Arg) = 0;
121 virtual enum NumOccurrences getNumOccurrencesFlagDefault() const {
124 virtual enum ValueExpected getValueExpectedFlagDefault() const {
125 return ValueOptional;
127 virtual enum OptionHidden getOptionHiddenFlagDefault() const {
130 virtual enum FormattingFlags getFormattingFlagDefault() const {
131 return NormalFormatting;
134 int NumOccurrences; // The number of times specified
135 int Flags; // Flags for the argument
137 const char *ArgStr; // The argument string itself (ex: "help", "o")
138 const char *HelpStr; // The descriptive text message for --help
139 const char *ValueStr; // String describing what the value of this option is
141 inline enum NumOccurrences getNumOccurrencesFlag() const {
142 int NO = Flags & OccurrencesMask;
143 return NO ? (enum NumOccurrences)NO : getNumOccurrencesFlagDefault();
145 inline enum ValueExpected getValueExpectedFlag() const {
146 int VE = Flags & ValueMask;
147 return VE ? (enum ValueExpected)VE : getValueExpectedFlagDefault();
149 inline enum OptionHidden getOptionHiddenFlag() const {
150 int OH = Flags & HiddenMask;
151 return OH ? (enum OptionHidden)OH : getOptionHiddenFlagDefault();
153 inline enum FormattingFlags getFormattingFlag() const {
154 int OH = Flags & FormattingMask;
155 return OH ? (enum FormattingFlags)OH : getFormattingFlagDefault();
157 inline unsigned getMiscFlags() const {
158 return Flags & MiscMask;
161 // hasArgStr - Return true if the argstr != ""
162 bool hasArgStr() const { return ArgStr[0] != 0; }
164 //-------------------------------------------------------------------------===
165 // Accessor functions set by OptionModifiers
167 void setArgStr(const char *S) { ArgStr = S; }
168 void setDescription(const char *S) { HelpStr = S; }
169 void setValueStr(const char *S) { ValueStr = S; }
171 void setFlag(unsigned Flag, unsigned FlagMask) {
172 if (Flags & FlagMask) {
173 error(": Specified two settings for the same option!");
180 void setNumOccurrencesFlag(enum NumOccurrences Val) {
181 setFlag(Val, OccurrencesMask);
183 void setValueExpectedFlag(enum ValueExpected Val) { setFlag(Val, ValueMask); }
184 void setHiddenFlag(enum OptionHidden Val) { setFlag(Val, HiddenMask); }
185 void setFormattingFlag(enum FormattingFlags V) { setFlag(V, FormattingMask); }
186 void setMiscFlag(enum MiscFlags M) { setFlag(M, M); }
188 Option() : NumOccurrences(0), Flags(0),
189 ArgStr(""), HelpStr(""), ValueStr("") {}
192 // addArgument - Tell the system that this Option subclass will handle all
193 // occurrences of -ArgStr on the command line.
195 void addArgument(const char *ArgStr);
196 void removeArgument(const char *ArgStr);
198 // Return the width of the option tag for printing...
199 virtual unsigned getOptionWidth() const = 0;
201 // printOptionInfo - Print out information about this option. The
202 // to-be-maintained width is specified.
204 virtual void printOptionInfo(unsigned GlobalWidth) const = 0;
206 // addOccurrence - Wrapper around handleOccurrence that enforces Flags
208 bool addOccurrence(const char *ArgName, const std::string &Value);
210 // Prints option name followed by message. Always returns true.
211 bool error(std::string Message, const char *ArgName = 0);
214 inline int getNumOccurrences() const { return NumOccurrences; }
219 //===----------------------------------------------------------------------===//
220 // Command line option modifiers that can be used to modify the behavior of
221 // command line option parsers...
224 // desc - Modifier to set the description shown in the --help output...
227 desc(const char *Str) : Desc(Str) {}
228 void apply(Option &O) const { O.setDescription(Desc); }
231 // value_desc - Modifier to set the value description shown in the --help
235 value_desc(const char *Str) : Desc(Str) {}
236 void apply(Option &O) const { O.setValueStr(Desc); }
240 // init - Specify a default (initial) value for the command line argument, if
241 // the default constructor for the argument type does not give you what you
242 // want. This is only valid on "opt" arguments, not on "list" arguments.
247 initializer(const Ty &Val) : Init(Val) {}
250 void apply(Opt &O) const { O.setInitialValue(Init); }
254 initializer<Ty> init(const Ty &Val) {
255 return initializer<Ty>(Val);
259 // location - Allow the user to specify which external variable they want to
260 // store the results of the command line argument processing into, if they don't
261 // want to store it in the option itself.
264 struct LocationClass {
266 LocationClass(Ty &L) : Loc(L) {}
269 void apply(Opt &O) const { O.setLocation(O, Loc); }
273 LocationClass<Ty> location(Ty &L) { return LocationClass<Ty>(L); }
276 //===----------------------------------------------------------------------===//
277 // Enum valued command line option
279 #define clEnumVal(ENUMVAL, DESC) #ENUMVAL, (int)ENUMVAL, DESC
280 #define clEnumValN(ENUMVAL, FLAGNAME, DESC) FLAGNAME, (int)ENUMVAL, DESC
282 // values - For custom data types, allow specifying a group of values together
283 // as the values that go into the mapping that the option handler uses. Note
284 // that the values list must always have a 0 at the end of the list to indicate
285 // that the list has ended.
287 template<class DataType>
289 // Use a vector instead of a map, because the lists should be short,
290 // the overhead is less, and most importantly, it keeps them in the order
291 // inserted so we can print our option out nicely.
292 std::vector<std::pair<const char *, std::pair<int, const char *> > > Values;
293 void processValues(va_list Vals);
295 ValuesClass(const char *EnumName, DataType Val, const char *Desc,
297 // Insert the first value, which is required.
298 Values.push_back(std::make_pair(EnumName, std::make_pair(Val, Desc)));
300 // Process the varargs portion of the values...
301 while (const char *EnumName = va_arg(ValueArgs, const char *)) {
302 DataType EnumVal = (DataType)va_arg(ValueArgs, int);
303 const char *EnumDesc = va_arg(ValueArgs, const char *);
304 Values.push_back(std::make_pair(EnumName, // Add value to value map
305 std::make_pair(EnumVal, EnumDesc)));
310 void apply(Opt &O) const {
311 for (unsigned i = 0, e = Values.size(); i != e; ++i)
312 O.getParser().addLiteralOption(Values[i].first, Values[i].second.first,
313 Values[i].second.second);
317 template<class DataType>
318 ValuesClass<DataType> values(const char *Arg, DataType Val, const char *Desc,
321 va_start(ValueArgs, Desc);
322 ValuesClass<DataType> Vals(Arg, Val, Desc, ValueArgs);
328 //===----------------------------------------------------------------------===//
329 // parser class - Parameterizable parser for different data types. By default,
330 // known data types (string, int, bool) have specialized parsers, that do what
331 // you would expect. The default parser, used for data types that are not
332 // built-in, uses a mapping table to map specific options to values, which is
333 // used, among other things, to handle enum types.
335 //--------------------------------------------------
336 // generic_parser_base - This class holds all the non-generic code that we do
337 // not need replicated for every instance of the generic parser. This also
338 // allows us to put stuff into CommandLine.cpp
340 struct generic_parser_base {
341 virtual ~generic_parser_base() {} // Base class should have virtual-dtor
343 // getNumOptions - Virtual function implemented by generic subclass to
344 // indicate how many entries are in Values.
346 virtual unsigned getNumOptions() const = 0;
348 // getOption - Return option name N.
349 virtual const char *getOption(unsigned N) const = 0;
351 // getDescription - Return description N
352 virtual const char *getDescription(unsigned N) const = 0;
354 // Return the width of the option tag for printing...
355 virtual unsigned getOptionWidth(const Option &O) const;
357 // printOptionInfo - Print out information about this option. The
358 // to-be-maintained width is specified.
360 virtual void printOptionInfo(const Option &O, unsigned GlobalWidth) const;
362 void initialize(Option &O) {
363 // All of the modifiers for the option have been processed by now, so the
364 // argstr field should be stable, copy it down now.
366 hasArgStr = O.hasArgStr();
368 // If there has been no argstr specified, that means that we need to add an
369 // argument for every possible option. This ensures that our options are
373 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
374 O.addArgument(getOption(i));
377 enum ValueExpected getValueExpectedFlagDefault() const {
378 // If there is an ArgStr specified, then we are of the form:
380 // -opt=O2 or -opt O2 or -optO2
382 // In which case, the value is required. Otherwise if an arg str has not
383 // been specified, we are of the form:
385 // -O2 or O2 or -la (where -l and -a are separate options)
387 // If this is the case, we cannot allow a value.
390 return ValueRequired;
392 return ValueDisallowed;
395 // findOption - Return the option number corresponding to the specified
396 // argument string. If the option is not found, getNumOptions() is returned.
398 unsigned findOption(const char *Name);
404 // Default parser implementation - This implementation depends on having a
405 // mapping of recognized options to values of some sort. In addition to this,
406 // each entry in the mapping also tracks a help message that is printed with the
407 // command line option for --help. Because this is a simple mapping parser, the
408 // data type can be any unsupported type.
410 template <class DataType>
411 class parser : public generic_parser_base {
413 std::vector<std::pair<const char *,
414 std::pair<DataType, const char *> > > Values;
416 typedef DataType parser_data_type;
418 // Implement virtual functions needed by generic_parser_base
419 unsigned getNumOptions() const { return Values.size(); }
420 const char *getOption(unsigned N) const { return Values[N].first; }
421 const char *getDescription(unsigned N) const {
422 return Values[N].second.second;
425 // parse - Return true on error.
426 bool parse(Option &O, const char *ArgName, const std::string &Arg,
434 for (unsigned i = 0, e = Values.size(); i != e; ++i)
435 if (ArgVal == Values[i].first) {
436 V = Values[i].second.first;
440 return O.error(": Cannot find option named '" + ArgVal + "'!");
443 // addLiteralOption - Add an entry to the mapping table...
445 void addLiteralOption(const char *Name, const DT &V, const char *HelpStr) {
446 assert(findOption(Name) == Values.size() && "Option already exists!");
447 Values.push_back(std::make_pair(Name, std::make_pair((DataType)V,HelpStr)));
450 // removeLiteralOption - Remove the specified option.
452 void removeLiteralOption(const char *Name) {
453 unsigned N = findOption(Name);
454 assert(N != Values.size() && "Option not found!");
455 Values.erase(Values.begin()+N);
459 //--------------------------------------------------
460 // basic_parser - Super class of parsers to provide boilerplate code
462 struct basic_parser_impl { // non-template implementation of basic_parser<t>
463 virtual ~basic_parser_impl() {}
465 enum ValueExpected getValueExpectedFlagDefault() const {
466 return ValueRequired;
469 void initialize(Option &O) {}
471 // Return the width of the option tag for printing...
472 unsigned getOptionWidth(const Option &O) const;
474 // printOptionInfo - Print out information about this option. The
475 // to-be-maintained width is specified.
477 void printOptionInfo(const Option &O, unsigned GlobalWidth) const;
480 // getValueName - Overload in subclass to provide a better default value.
481 virtual const char *getValueName() const { return "value"; }
484 // basic_parser - The real basic parser is just a template wrapper that provides
485 // a typedef for the provided data type.
487 template<class DataType>
488 struct basic_parser : public basic_parser_impl {
489 typedef DataType parser_data_type;
493 //--------------------------------------------------
497 struct parser<bool> : public basic_parser<bool> {
499 // parse - Return true on error.
500 bool parse(Option &O, const char *ArgName, const std::string &Arg, bool &Val);
502 enum ValueExpected getValueExpectedFlagDefault() const {
503 return ValueOptional;
506 // getValueName - Do not print =<value> at all
507 virtual const char *getValueName() const { return 0; }
511 //--------------------------------------------------
515 struct parser<int> : public basic_parser<int> {
517 // parse - Return true on error.
518 bool parse(Option &O, const char *ArgName, const std::string &Arg, int &Val);
520 // getValueName - Overload in subclass to provide a better default value.
521 virtual const char *getValueName() const { return "int"; }
525 //--------------------------------------------------
529 struct parser<unsigned> : public basic_parser<unsigned> {
531 // parse - Return true on error.
532 bool parse(Option &O, const char *ArgName, const std::string &Arg,
535 // getValueName - Overload in subclass to provide a better default value.
536 virtual const char *getValueName() const { return "uint"; }
540 //--------------------------------------------------
544 struct parser<double> : public basic_parser<double> {
545 // parse - Return true on error.
546 bool parse(Option &O, const char *AN, const std::string &Arg, double &Val);
548 // getValueName - Overload in subclass to provide a better default value.
549 virtual const char *getValueName() const { return "number"; }
553 //--------------------------------------------------
557 struct parser<float> : public basic_parser<float> {
558 // parse - Return true on error.
559 bool parse(Option &O, const char *AN, const std::string &Arg, float &Val);
561 // getValueName - Overload in subclass to provide a better default value.
562 virtual const char *getValueName() const { return "number"; }
566 //--------------------------------------------------
567 // parser<std::string>
570 struct parser<std::string> : public basic_parser<std::string> {
571 // parse - Return true on error.
572 bool parse(Option &O, const char *ArgName, const std::string &Arg,
573 std::string &Value) {
578 // getValueName - Overload in subclass to provide a better default value.
579 virtual const char *getValueName() const { return "string"; }
584 //===----------------------------------------------------------------------===//
585 // applicator class - This class is used because we must use partial
586 // specialization to handle literal string arguments specially (const char* does
587 // not correctly respond to the apply method). Because the syntax to use this
588 // is a pain, we have the 'apply' method below to handle the nastiness...
590 template<class Mod> struct applicator {
592 static void opt(const Mod &M, Opt &O) { M.apply(O); }
595 // Handle const char* as a special case...
596 template<unsigned n> struct applicator<char[n]> {
598 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
600 template<unsigned n> struct applicator<const char[n]> {
602 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
604 template<> struct applicator<const char*> {
606 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
609 template<> struct applicator<NumOccurrences> {
610 static void opt(NumOccurrences NO, Option &O) { O.setNumOccurrencesFlag(NO); }
612 template<> struct applicator<ValueExpected> {
613 static void opt(ValueExpected VE, Option &O) { O.setValueExpectedFlag(VE); }
615 template<> struct applicator<OptionHidden> {
616 static void opt(OptionHidden OH, Option &O) { O.setHiddenFlag(OH); }
618 template<> struct applicator<FormattingFlags> {
619 static void opt(FormattingFlags FF, Option &O) { O.setFormattingFlag(FF); }
621 template<> struct applicator<MiscFlags> {
622 static void opt(MiscFlags MF, Option &O) { O.setMiscFlag(MF); }
625 // apply method - Apply a modifier to an option in a type safe way.
626 template<class Mod, class Opt>
627 void apply(const Mod &M, Opt *O) {
628 applicator<Mod>::opt(M, *O);
632 //===----------------------------------------------------------------------===//
635 // Default storage class definition: external storage. This implementation
636 // assumes the user will specify a variable to store the data into with the
637 // cl::location(x) modifier.
639 template<class DataType, bool ExternalStorage, bool isClass>
641 DataType *Location; // Where to store the object...
644 assert(Location != 0 && "cl::location(...) not specified for a command "
645 "line option with external storage, "
646 "or cl::init specified before cl::location()!!");
649 opt_storage() : Location(0) {}
651 bool setLocation(Option &O, DataType &L) {
653 return O.error(": cl::location(x) specified more than once!");
659 void setValue(const T &V) {
664 DataType &getValue() { check(); return *Location; }
665 const DataType &getValue() const { check(); return *Location; }
669 // Define how to hold a class type object, such as a string. Since we can
670 // inherit from a class, we do so. This makes us exactly compatible with the
671 // object in all cases that it is used.
673 template<class DataType>
674 struct opt_storage<DataType,false,true> : public DataType {
677 void setValue(const T &V) { DataType::operator=(V); }
679 DataType &getValue() { return *this; }
680 const DataType &getValue() const { return *this; }
683 // Define a partial specialization to handle things we cannot inherit from. In
684 // this case, we store an instance through containment, and overload operators
685 // to get at the value.
687 template<class DataType>
688 struct opt_storage<DataType, false, false> {
691 // Make sure we initialize the value with the default constructor for the
693 opt_storage() : Value(DataType()) {}
696 void setValue(const T &V) { Value = V; }
697 DataType &getValue() { return Value; }
698 DataType getValue() const { return Value; }
702 //===----------------------------------------------------------------------===//
703 // opt - A scalar command line option.
705 template <class DataType, bool ExternalStorage = false,
706 class ParserClass = parser<DataType> >
707 class opt : public Option,
708 public opt_storage<DataType, ExternalStorage,
709 ::boost::is_class<DataType>::value> {
712 virtual bool handleOccurrence(const char *ArgName, const std::string &Arg) {
713 typename ParserClass::parser_data_type Val;
714 if (Parser.parse(*this, ArgName, Arg, Val))
715 return true; // Parse error!
720 virtual enum ValueExpected getValueExpectedFlagDefault() const {
721 return Parser.getValueExpectedFlagDefault();
724 // Forward printing stuff to the parser...
725 virtual unsigned getOptionWidth() const {return Parser.getOptionWidth(*this);}
726 virtual void printOptionInfo(unsigned GlobalWidth) const {
727 Parser.printOptionInfo(*this, GlobalWidth);
732 Parser.initialize(*this);
735 // setInitialValue - Used by the cl::init modifier...
736 void setInitialValue(const DataType &V) { this->setValue(V); }
738 ParserClass &getParser() { return Parser; }
740 operator DataType() const { return this->getValue(); }
743 DataType &operator=(const T &Val) {
745 return this->getValue();
756 template<class M0t, class M1t>
757 opt(const M0t &M0, const M1t &M1) {
758 apply(M0, this); apply(M1, this);
763 template<class M0t, class M1t, class M2t>
764 opt(const M0t &M0, const M1t &M1, const M2t &M2) {
765 apply(M0, this); apply(M1, this); apply(M2, this);
769 template<class M0t, class M1t, class M2t, class M3t>
770 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3) {
771 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
775 template<class M0t, class M1t, class M2t, class M3t, class M4t>
776 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
778 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
783 template<class M0t, class M1t, class M2t, class M3t,
784 class M4t, class M5t>
785 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
786 const M4t &M4, const M5t &M5) {
787 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
788 apply(M4, this); apply(M5, this);
792 template<class M0t, class M1t, class M2t, class M3t,
793 class M4t, class M5t, class M6t>
794 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
795 const M4t &M4, const M5t &M5, const M6t &M6) {
796 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
797 apply(M4, this); apply(M5, this); apply(M6, this);
801 template<class M0t, class M1t, class M2t, class M3t,
802 class M4t, class M5t, class M6t, class M7t>
803 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
804 const M4t &M4, const M5t &M5, const M6t &M6, const M7t &M7) {
805 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
806 apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
811 //===----------------------------------------------------------------------===//
812 // list_storage class
814 // Default storage class definition: external storage. This implementation
815 // assumes the user will specify a variable to store the data into with the
816 // cl::location(x) modifier.
818 template<class DataType, class StorageClass>
820 StorageClass *Location; // Where to store the object...
823 list_storage() : Location(0) {}
825 bool setLocation(Option &O, StorageClass &L) {
827 return O.error(": cl::location(x) specified more than once!");
833 void addValue(const T &V) {
834 assert(Location != 0 && "cl::location(...) not specified for a command "
835 "line option with external storage!");
836 Location->push_back(V);
841 // Define how to hold a class type object, such as a string. Since we can
842 // inherit from a class, we do so. This makes us exactly compatible with the
843 // object in all cases that it is used.
845 template<class DataType>
846 struct list_storage<DataType, bool> : public std::vector<DataType> {
849 void addValue(const T &V) { push_back(V); }
853 //===----------------------------------------------------------------------===//
854 // list - A list of command line options.
856 template <class DataType, class Storage = bool,
857 class ParserClass = parser<DataType> >
858 class list : public Option, public list_storage<DataType, Storage> {
861 virtual enum NumOccurrences getNumOccurrencesFlagDefault() const {
864 virtual enum ValueExpected getValueExpectedFlagDefault() const {
865 return Parser.getValueExpectedFlagDefault();
868 virtual bool handleOccurrence(const char *ArgName, const std::string &Arg) {
869 typename ParserClass::parser_data_type Val;
870 if (Parser.parse(*this, ArgName, Arg, Val))
871 return true; // Parse Error!
876 // Forward printing stuff to the parser...
877 virtual unsigned getOptionWidth() const {return Parser.getOptionWidth(*this);}
878 virtual void printOptionInfo(unsigned GlobalWidth) const {
879 Parser.printOptionInfo(*this, GlobalWidth);
884 Parser.initialize(*this);
887 ParserClass &getParser() { return Parser; }
891 list(const M0t &M0) {
896 template<class M0t, class M1t>
897 list(const M0t &M0, const M1t &M1) {
898 apply(M0, this); apply(M1, this);
902 template<class M0t, class M1t, class M2t>
903 list(const M0t &M0, const M1t &M1, const M2t &M2) {
904 apply(M0, this); apply(M1, this); apply(M2, this);
908 template<class M0t, class M1t, class M2t, class M3t>
909 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3) {
910 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
914 template<class M0t, class M1t, class M2t, class M3t, class M4t>
915 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
917 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
922 template<class M0t, class M1t, class M2t, class M3t,
923 class M4t, class M5t>
924 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
925 const M4t &M4, const M5t &M5) {
926 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
927 apply(M4, this); apply(M5, this);
931 template<class M0t, class M1t, class M2t, class M3t,
932 class M4t, class M5t, class M6t>
933 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
934 const M4t &M4, const M5t &M5, const M6t &M6) {
935 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
936 apply(M4, this); apply(M5, this); apply(M6, this);
940 template<class M0t, class M1t, class M2t, class M3t,
941 class M4t, class M5t, class M6t, class M7t>
942 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
943 const M4t &M4, const M5t &M5, const M6t &M6, const M7t &M7) {
944 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
945 apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
952 //===----------------------------------------------------------------------===//
953 // Aliased command line option (alias this name to a preexisting name)
956 class alias : public Option {
958 virtual bool handleOccurrence(const char *ArgName, const std::string &Arg) {
959 return AliasFor->handleOccurrence(AliasFor->ArgStr, Arg);
961 // Aliases default to be hidden...
962 virtual enum OptionHidden getOptionHiddenFlagDefault() const {return Hidden;}
964 // Handle printing stuff...
965 virtual unsigned getOptionWidth() const;
966 virtual void printOptionInfo(unsigned GlobalWidth) const;
970 error(": cl::alias must have argument name specified!");
972 error(": cl::alias must have an cl::aliasopt(option) specified!");
976 void setAliasFor(Option &O) {
978 error(": cl::alias must only have one cl::aliasopt(...) specified!");
984 alias(const M0t &M0) : AliasFor(0) {
989 template<class M0t, class M1t>
990 alias(const M0t &M0, const M1t &M1) : AliasFor(0) {
991 apply(M0, this); apply(M1, this);
995 template<class M0t, class M1t, class M2t>
996 alias(const M0t &M0, const M1t &M1, const M2t &M2) : AliasFor(0) {
997 apply(M0, this); apply(M1, this); apply(M2, this);
1001 template<class M0t, class M1t, class M2t, class M3t>
1002 alias(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3)
1004 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1009 // aliasfor - Modifier to set the option an alias aliases.
1012 aliasopt(Option &O) : Opt(O) {}
1013 void apply(alias &A) const { A.setAliasFor(Opt); }
1016 } // End namespace cl