1 //===- Support/CommandLine.h - Flexible Command line parser -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This class implements a command line argument processor that is useful when
11 // creating a tool. It provides a simple, minimalistic interface that is easily
12 // extensible and supports nonlocal (library) command line options.
14 // Note that rather than trying to figure out what this code does, you should
15 // read the library documentation located in docs/CommandLine.html or looks at
16 // the many example usages in tools/*/*.cpp
18 //===----------------------------------------------------------------------===//
20 #ifndef SUPPORT_COMMANDLINE_H
21 #define SUPPORT_COMMANDLINE_H
23 #include "Support/type_traits.h"
32 /// cl Namespace - This namespace contains all of the command line option
33 /// processing machinery. It is intentionally a short name to make qualified
37 //===----------------------------------------------------------------------===//
38 // ParseCommandLineOptions - Command line option processing entry point.
40 void ParseCommandLineOptions(int &argc, char **argv,
41 const char *Overview = 0);
43 //===----------------------------------------------------------------------===//
44 // ParseEnvironmentOptions - Environment variable option processing alternate
47 void ParseEnvironmentOptions(const char *progName, const char *envvar,
48 const char *Overview = 0);
50 //===----------------------------------------------------------------------===//
51 // Flags permitted to be passed to command line arguments
54 enum NumOccurrences { // Flags for the number of occurrences allowed
55 Optional = 0x01, // Zero or One occurrence
56 ZeroOrMore = 0x02, // Zero or more occurrences allowed
57 Required = 0x03, // One occurrence required
58 OneOrMore = 0x04, // One or more occurrences required
60 // ConsumeAfter - Indicates that this option is fed anything that follows the
61 // last positional argument required by the application (it is an error if
62 // there are zero positional arguments, and a ConsumeAfter option is used).
63 // Thus, for example, all arguments to LLI are processed until a filename is
64 // found. Once a filename is found, all of the succeeding arguments are
65 // passed, unprocessed, to the ConsumeAfter option.
69 OccurrencesMask = 0x07,
72 enum ValueExpected { // Is a value required for the option?
73 ValueOptional = 0x08, // The value can appear... or not
74 ValueRequired = 0x10, // The value is required to appear!
75 ValueDisallowed = 0x18, // A value may not be specified (for flags)
79 enum OptionHidden { // Control whether -help shows this option
80 NotHidden = 0x20, // Option included in --help & --help-hidden
81 Hidden = 0x40, // -help doesn't, but --help-hidden does
82 ReallyHidden = 0x60, // Neither --help nor --help-hidden show this arg
86 // Formatting flags - This controls special features that the option might have
87 // that cause it to be parsed differently...
89 // Prefix - This option allows arguments that are otherwise unrecognized to be
90 // matched by options that are a prefix of the actual value. This is useful for
91 // cases like a linker, where options are typically of the form '-lfoo' or
92 // '-L../../include' where -l or -L are the actual flags. When prefix is
93 // enabled, and used, the value for the flag comes from the suffix of the
96 // Grouping - With this option enabled, multiple letter options are allowed to
97 // bunch together with only a single hyphen for the whole group. This allows
98 // emulation of the behavior that ls uses for example: ls -la === ls -l -a
101 enum FormattingFlags {
102 NormalFormatting = 0x000, // Nothing special
103 Positional = 0x080, // Is a positional argument, no '-' required
104 Prefix = 0x100, // Can this option directly prefix its value?
105 Grouping = 0x180, // Can this option group with other options?
106 FormattingMask = 0x180,
109 enum MiscFlags { // Miscellaneous flags to adjust argument
110 CommaSeparated = 0x200, // Should this cl::list split between commas?
116 //===----------------------------------------------------------------------===//
121 friend void cl::ParseCommandLineOptions(int &, char **, const char *, int);
124 // handleOccurrences - Overriden by subclasses to handle the value passed into
125 // an argument. Should return true if there was an error processing the
126 // argument and the program should exit.
128 virtual bool handleOccurrence(const char *ArgName, const std::string &Arg) = 0;
130 virtual enum NumOccurrences getNumOccurrencesFlagDefault() const {
133 virtual enum ValueExpected getValueExpectedFlagDefault() const {
134 return ValueOptional;
136 virtual enum OptionHidden getOptionHiddenFlagDefault() const {
139 virtual enum FormattingFlags getFormattingFlagDefault() const {
140 return NormalFormatting;
143 int NumOccurrences; // The number of times specified
144 int Flags; // Flags for the argument
146 const char *ArgStr; // The argument string itself (ex: "help", "o")
147 const char *HelpStr; // The descriptive text message for --help
148 const char *ValueStr; // String describing what the value of this option is
150 inline enum NumOccurrences getNumOccurrencesFlag() const {
151 int NO = Flags & OccurrencesMask;
152 return NO ? static_cast<enum NumOccurrences>(NO)
153 : getNumOccurrencesFlagDefault();
155 inline enum ValueExpected getValueExpectedFlag() const {
156 int VE = Flags & ValueMask;
157 return VE ? static_cast<enum ValueExpected>(VE)
158 : getValueExpectedFlagDefault();
160 inline enum OptionHidden getOptionHiddenFlag() const {
161 int OH = Flags & HiddenMask;
162 return OH ? static_cast<enum OptionHidden>(OH)
163 : getOptionHiddenFlagDefault();
165 inline enum FormattingFlags getFormattingFlag() const {
166 int OH = Flags & FormattingMask;
167 return OH ? static_cast<enum FormattingFlags>(OH)
168 : getFormattingFlagDefault();
170 inline unsigned getMiscFlags() const {
171 return Flags & MiscMask;
174 // hasArgStr - Return true if the argstr != ""
175 bool hasArgStr() const { return ArgStr[0] != 0; }
177 //-------------------------------------------------------------------------===
178 // Accessor functions set by OptionModifiers
180 void setArgStr(const char *S) { ArgStr = S; }
181 void setDescription(const char *S) { HelpStr = S; }
182 void setValueStr(const char *S) { ValueStr = S; }
184 void setFlag(unsigned Flag, unsigned FlagMask) {
185 if (Flags & FlagMask) {
186 error(": Specified two settings for the same option!");
193 void setNumOccurrencesFlag(enum NumOccurrences Val) {
194 setFlag(Val, OccurrencesMask);
196 void setValueExpectedFlag(enum ValueExpected Val) { setFlag(Val, ValueMask); }
197 void setHiddenFlag(enum OptionHidden Val) { setFlag(Val, HiddenMask); }
198 void setFormattingFlag(enum FormattingFlags V) { setFlag(V, FormattingMask); }
199 void setMiscFlag(enum MiscFlags M) { setFlag(M, M); }
201 Option() : NumOccurrences(0), Flags(0),
202 ArgStr(""), HelpStr(""), ValueStr("") {}
205 // addArgument - Tell the system that this Option subclass will handle all
206 // occurrences of -ArgStr on the command line.
208 void addArgument(const char *ArgStr);
209 void removeArgument(const char *ArgStr);
211 // Return the width of the option tag for printing...
212 virtual unsigned getOptionWidth() const = 0;
214 // printOptionInfo - Print out information about this option. The
215 // to-be-maintained width is specified.
217 virtual void printOptionInfo(unsigned GlobalWidth) const = 0;
219 // addOccurrence - Wrapper around handleOccurrence that enforces Flags
221 bool addOccurrence(const char *ArgName, const std::string &Value);
223 // Prints option name followed by message. Always returns true.
224 bool error(std::string Message, const char *ArgName = 0);
227 inline int getNumOccurrences() const { return NumOccurrences; }
232 //===----------------------------------------------------------------------===//
233 // Command line option modifiers that can be used to modify the behavior of
234 // command line option parsers...
237 // desc - Modifier to set the description shown in the --help output...
240 desc(const char *Str) : Desc(Str) {}
241 void apply(Option &O) const { O.setDescription(Desc); }
244 // value_desc - Modifier to set the value description shown in the --help
248 value_desc(const char *Str) : Desc(Str) {}
249 void apply(Option &O) const { O.setValueStr(Desc); }
253 // init - Specify a default (initial) value for the command line argument, if
254 // the default constructor for the argument type does not give you what you
255 // want. This is only valid on "opt" arguments, not on "list" arguments.
260 initializer(const Ty &Val) : Init(Val) {}
263 void apply(Opt &O) const { O.setInitialValue(Init); }
267 initializer<Ty> init(const Ty &Val) {
268 return initializer<Ty>(Val);
272 // location - Allow the user to specify which external variable they want to
273 // store the results of the command line argument processing into, if they don't
274 // want to store it in the option itself.
277 struct LocationClass {
279 LocationClass(Ty &L) : Loc(L) {}
282 void apply(Opt &O) const { O.setLocation(O, Loc); }
286 LocationClass<Ty> location(Ty &L) { return LocationClass<Ty>(L); }
289 //===----------------------------------------------------------------------===//
290 // Enum valued command line option
292 #define clEnumVal(ENUMVAL, DESC) #ENUMVAL, (int)ENUMVAL, DESC
293 #define clEnumValN(ENUMVAL, FLAGNAME, DESC) FLAGNAME, (int)ENUMVAL, DESC
295 // values - For custom data types, allow specifying a group of values together
296 // as the values that go into the mapping that the option handler uses. Note
297 // that the values list must always have a 0 at the end of the list to indicate
298 // that the list has ended.
300 template<class DataType>
302 // Use a vector instead of a map, because the lists should be short,
303 // the overhead is less, and most importantly, it keeps them in the order
304 // inserted so we can print our option out nicely.
305 std::vector<std::pair<const char *, std::pair<int, const char *> > > Values;
306 void processValues(va_list Vals);
308 ValuesClass(const char *EnumName, DataType Val, const char *Desc,
310 // Insert the first value, which is required.
311 Values.push_back(std::make_pair(EnumName, std::make_pair(Val, Desc)));
313 // Process the varargs portion of the values...
314 while (const char *EnumName = va_arg(ValueArgs, const char *)) {
315 DataType EnumVal = static_cast<DataType>(va_arg(ValueArgs, int));
316 const char *EnumDesc = va_arg(ValueArgs, const char *);
317 Values.push_back(std::make_pair(EnumName, // Add value to value map
318 std::make_pair(EnumVal, EnumDesc)));
323 void apply(Opt &O) const {
324 for (unsigned i = 0, e = Values.size(); i != e; ++i)
325 O.getParser().addLiteralOption(Values[i].first, Values[i].second.first,
326 Values[i].second.second);
330 template<class DataType>
331 ValuesClass<DataType> values(const char *Arg, DataType Val, const char *Desc,
334 va_start(ValueArgs, Desc);
335 ValuesClass<DataType> Vals(Arg, Val, Desc, ValueArgs);
341 //===----------------------------------------------------------------------===//
342 // parser class - Parameterizable parser for different data types. By default,
343 // known data types (string, int, bool) have specialized parsers, that do what
344 // you would expect. The default parser, used for data types that are not
345 // built-in, uses a mapping table to map specific options to values, which is
346 // used, among other things, to handle enum types.
348 //--------------------------------------------------
349 // generic_parser_base - This class holds all the non-generic code that we do
350 // not need replicated for every instance of the generic parser. This also
351 // allows us to put stuff into CommandLine.cpp
353 struct generic_parser_base {
354 virtual ~generic_parser_base() {} // Base class should have virtual-dtor
356 // getNumOptions - Virtual function implemented by generic subclass to
357 // indicate how many entries are in Values.
359 virtual unsigned getNumOptions() const = 0;
361 // getOption - Return option name N.
362 virtual const char *getOption(unsigned N) const = 0;
364 // getDescription - Return description N
365 virtual const char *getDescription(unsigned N) const = 0;
367 // Return the width of the option tag for printing...
368 virtual unsigned getOptionWidth(const Option &O) const;
370 // printOptionInfo - Print out information about this option. The
371 // to-be-maintained width is specified.
373 virtual void printOptionInfo(const Option &O, unsigned GlobalWidth) const;
375 void initialize(Option &O) {
376 // All of the modifiers for the option have been processed by now, so the
377 // argstr field should be stable, copy it down now.
379 hasArgStr = O.hasArgStr();
381 // If there has been no argstr specified, that means that we need to add an
382 // argument for every possible option. This ensures that our options are
386 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
387 O.addArgument(getOption(i));
390 enum ValueExpected getValueExpectedFlagDefault() const {
391 // If there is an ArgStr specified, then we are of the form:
393 // -opt=O2 or -opt O2 or -optO2
395 // In which case, the value is required. Otherwise if an arg str has not
396 // been specified, we are of the form:
398 // -O2 or O2 or -la (where -l and -a are separate options)
400 // If this is the case, we cannot allow a value.
403 return ValueRequired;
405 return ValueDisallowed;
408 // findOption - Return the option number corresponding to the specified
409 // argument string. If the option is not found, getNumOptions() is returned.
411 unsigned findOption(const char *Name);
417 // Default parser implementation - This implementation depends on having a
418 // mapping of recognized options to values of some sort. In addition to this,
419 // each entry in the mapping also tracks a help message that is printed with the
420 // command line option for --help. Because this is a simple mapping parser, the
421 // data type can be any unsupported type.
423 template <class DataType>
424 class parser : public generic_parser_base {
426 std::vector<std::pair<const char *,
427 std::pair<DataType, const char *> > > Values;
429 typedef DataType parser_data_type;
431 // Implement virtual functions needed by generic_parser_base
432 unsigned getNumOptions() const { return Values.size(); }
433 const char *getOption(unsigned N) const { return Values[N].first; }
434 const char *getDescription(unsigned N) const {
435 return Values[N].second.second;
438 // parse - Return true on error.
439 bool parse(Option &O, const char *ArgName, const std::string &Arg,
447 for (unsigned i = 0, e = Values.size(); i != e; ++i)
448 if (ArgVal == Values[i].first) {
449 V = Values[i].second.first;
453 return O.error(": Cannot find option named '" + ArgVal + "'!");
456 // addLiteralOption - Add an entry to the mapping table...
458 void addLiteralOption(const char *Name, const DT &V, const char *HelpStr) {
459 assert(findOption(Name) == Values.size() && "Option already exists!");
460 Values.push_back(std::make_pair(Name,
461 std::make_pair(static_cast<DataType>(V),HelpStr)));
464 // removeLiteralOption - Remove the specified option.
466 void removeLiteralOption(const char *Name) {
467 unsigned N = findOption(Name);
468 assert(N != Values.size() && "Option not found!");
469 Values.erase(Values.begin()+N);
473 //--------------------------------------------------
474 // basic_parser - Super class of parsers to provide boilerplate code
476 struct basic_parser_impl { // non-template implementation of basic_parser<t>
477 virtual ~basic_parser_impl() {}
479 enum ValueExpected getValueExpectedFlagDefault() const {
480 return ValueRequired;
483 void initialize(Option &O) {}
485 // Return the width of the option tag for printing...
486 unsigned getOptionWidth(const Option &O) const;
488 // printOptionInfo - Print out information about this option. The
489 // to-be-maintained width is specified.
491 void printOptionInfo(const Option &O, unsigned GlobalWidth) const;
494 // getValueName - Overload in subclass to provide a better default value.
495 virtual const char *getValueName() const { return "value"; }
498 // basic_parser - The real basic parser is just a template wrapper that provides
499 // a typedef for the provided data type.
501 template<class DataType>
502 struct basic_parser : public basic_parser_impl {
503 typedef DataType parser_data_type;
507 //--------------------------------------------------
511 struct parser<bool> : public basic_parser<bool> {
513 // parse - Return true on error.
514 bool parse(Option &O, const char *ArgName, const std::string &Arg, bool &Val);
516 enum ValueExpected getValueExpectedFlagDefault() const {
517 return ValueOptional;
520 // getValueName - Do not print =<value> at all
521 virtual const char *getValueName() const { return 0; }
525 //--------------------------------------------------
529 struct parser<int> : public basic_parser<int> {
531 // parse - Return true on error.
532 bool parse(Option &O, const char *ArgName, const std::string &Arg, int &Val);
534 // getValueName - Overload in subclass to provide a better default value.
535 virtual const char *getValueName() const { return "int"; }
539 //--------------------------------------------------
543 struct parser<unsigned> : public basic_parser<unsigned> {
545 // parse - Return true on error.
546 bool parse(Option &O, const char *ArgName, const std::string &Arg,
549 // getValueName - Overload in subclass to provide a better default value.
550 virtual const char *getValueName() const { return "uint"; }
554 //--------------------------------------------------
558 struct parser<double> : public basic_parser<double> {
559 // parse - Return true on error.
560 bool parse(Option &O, const char *AN, const std::string &Arg, double &Val);
562 // getValueName - Overload in subclass to provide a better default value.
563 virtual const char *getValueName() const { return "number"; }
567 //--------------------------------------------------
571 struct parser<float> : public basic_parser<float> {
572 // parse - Return true on error.
573 bool parse(Option &O, const char *AN, const std::string &Arg, float &Val);
575 // getValueName - Overload in subclass to provide a better default value.
576 virtual const char *getValueName() const { return "number"; }
580 //--------------------------------------------------
581 // parser<std::string>
584 struct parser<std::string> : public basic_parser<std::string> {
585 // parse - Return true on error.
586 bool parse(Option &O, const char *ArgName, const std::string &Arg,
587 std::string &Value) {
592 // getValueName - Overload in subclass to provide a better default value.
593 virtual const char *getValueName() const { return "string"; }
598 //===----------------------------------------------------------------------===//
599 // applicator class - This class is used because we must use partial
600 // specialization to handle literal string arguments specially (const char* does
601 // not correctly respond to the apply method). Because the syntax to use this
602 // is a pain, we have the 'apply' method below to handle the nastiness...
604 template<class Mod> struct applicator {
606 static void opt(const Mod &M, Opt &O) { M.apply(O); }
609 // Handle const char* as a special case...
610 template<unsigned n> struct applicator<char[n]> {
612 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
614 template<unsigned n> struct applicator<const char[n]> {
616 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
618 template<> struct applicator<const char*> {
620 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
623 template<> struct applicator<NumOccurrences> {
624 static void opt(NumOccurrences NO, Option &O) { O.setNumOccurrencesFlag(NO); }
626 template<> struct applicator<ValueExpected> {
627 static void opt(ValueExpected VE, Option &O) { O.setValueExpectedFlag(VE); }
629 template<> struct applicator<OptionHidden> {
630 static void opt(OptionHidden OH, Option &O) { O.setHiddenFlag(OH); }
632 template<> struct applicator<FormattingFlags> {
633 static void opt(FormattingFlags FF, Option &O) { O.setFormattingFlag(FF); }
635 template<> struct applicator<MiscFlags> {
636 static void opt(MiscFlags MF, Option &O) { O.setMiscFlag(MF); }
639 // apply method - Apply a modifier to an option in a type safe way.
640 template<class Mod, class Opt>
641 void apply(const Mod &M, Opt *O) {
642 applicator<Mod>::opt(M, *O);
646 //===----------------------------------------------------------------------===//
649 // Default storage class definition: external storage. This implementation
650 // assumes the user will specify a variable to store the data into with the
651 // cl::location(x) modifier.
653 template<class DataType, bool ExternalStorage, bool isClass>
655 DataType *Location; // Where to store the object...
658 assert(Location != 0 && "cl::location(...) not specified for a command "
659 "line option with external storage, "
660 "or cl::init specified before cl::location()!!");
663 opt_storage() : Location(0) {}
665 bool setLocation(Option &O, DataType &L) {
667 return O.error(": cl::location(x) specified more than once!");
673 void setValue(const T &V) {
678 DataType &getValue() { check(); return *Location; }
679 const DataType &getValue() const { check(); return *Location; }
683 // Define how to hold a class type object, such as a string. Since we can
684 // inherit from a class, we do so. This makes us exactly compatible with the
685 // object in all cases that it is used.
687 template<class DataType>
688 struct opt_storage<DataType,false,true> : public DataType {
691 void setValue(const T &V) { DataType::operator=(V); }
693 DataType &getValue() { return *this; }
694 const DataType &getValue() const { return *this; }
697 // Define a partial specialization to handle things we cannot inherit from. In
698 // this case, we store an instance through containment, and overload operators
699 // to get at the value.
701 template<class DataType>
702 struct opt_storage<DataType, false, false> {
705 // Make sure we initialize the value with the default constructor for the
707 opt_storage() : Value(DataType()) {}
710 void setValue(const T &V) { Value = V; }
711 DataType &getValue() { return Value; }
712 DataType getValue() const { return Value; }
716 //===----------------------------------------------------------------------===//
717 // opt - A scalar command line option.
719 template <class DataType, bool ExternalStorage = false,
720 class ParserClass = parser<DataType> >
721 class opt : public Option,
722 public opt_storage<DataType, ExternalStorage,
723 is_class<DataType>::value> {
726 virtual bool handleOccurrence(const char *ArgName, const std::string &Arg) {
727 typename ParserClass::parser_data_type Val;
728 if (Parser.parse(*this, ArgName, Arg, Val))
729 return true; // Parse error!
734 virtual enum ValueExpected getValueExpectedFlagDefault() const {
735 return Parser.getValueExpectedFlagDefault();
738 // Forward printing stuff to the parser...
739 virtual unsigned getOptionWidth() const {return Parser.getOptionWidth(*this);}
740 virtual void printOptionInfo(unsigned GlobalWidth) const {
741 Parser.printOptionInfo(*this, GlobalWidth);
746 Parser.initialize(*this);
749 // setInitialValue - Used by the cl::init modifier...
750 void setInitialValue(const DataType &V) { this->setValue(V); }
752 ParserClass &getParser() { return Parser; }
754 operator DataType() const { return this->getValue(); }
757 DataType &operator=(const T &Val) {
759 return this->getValue();
770 template<class M0t, class M1t>
771 opt(const M0t &M0, const M1t &M1) {
772 apply(M0, this); apply(M1, this);
777 template<class M0t, class M1t, class M2t>
778 opt(const M0t &M0, const M1t &M1, const M2t &M2) {
779 apply(M0, this); apply(M1, this); apply(M2, this);
783 template<class M0t, class M1t, class M2t, class M3t>
784 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3) {
785 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
789 template<class M0t, class M1t, class M2t, class M3t, class M4t>
790 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
792 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
797 template<class M0t, class M1t, class M2t, class M3t,
798 class M4t, class M5t>
799 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
800 const M4t &M4, const M5t &M5) {
801 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
802 apply(M4, this); apply(M5, this);
806 template<class M0t, class M1t, class M2t, class M3t,
807 class M4t, class M5t, class M6t>
808 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
809 const M4t &M4, const M5t &M5, const M6t &M6) {
810 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
811 apply(M4, this); apply(M5, this); apply(M6, this);
815 template<class M0t, class M1t, class M2t, class M3t,
816 class M4t, class M5t, class M6t, class M7t>
817 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
818 const M4t &M4, const M5t &M5, const M6t &M6, const M7t &M7) {
819 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
820 apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
825 //===----------------------------------------------------------------------===//
826 // list_storage class
828 // Default storage class definition: external storage. This implementation
829 // assumes the user will specify a variable to store the data into with the
830 // cl::location(x) modifier.
832 template<class DataType, class StorageClass>
834 StorageClass *Location; // Where to store the object...
837 list_storage() : Location(0) {}
839 bool setLocation(Option &O, StorageClass &L) {
841 return O.error(": cl::location(x) specified more than once!");
847 void addValue(const T &V) {
848 assert(Location != 0 && "cl::location(...) not specified for a command "
849 "line option with external storage!");
850 Location->push_back(V);
855 // Define how to hold a class type object, such as a string. Since we can
856 // inherit from a class, we do so. This makes us exactly compatible with the
857 // object in all cases that it is used.
859 template<class DataType>
860 struct list_storage<DataType, bool> : public std::vector<DataType> {
863 void addValue(const T &V) { push_back(V); }
867 //===----------------------------------------------------------------------===//
868 // list - A list of command line options.
870 template <class DataType, class Storage = bool,
871 class ParserClass = parser<DataType> >
872 class list : public Option, public list_storage<DataType, Storage> {
875 virtual enum NumOccurrences getNumOccurrencesFlagDefault() const {
878 virtual enum ValueExpected getValueExpectedFlagDefault() const {
879 return Parser.getValueExpectedFlagDefault();
882 virtual bool handleOccurrence(const char *ArgName, const std::string &Arg) {
883 typename ParserClass::parser_data_type Val;
884 if (Parser.parse(*this, ArgName, Arg, Val))
885 return true; // Parse Error!
890 // Forward printing stuff to the parser...
891 virtual unsigned getOptionWidth() const {return Parser.getOptionWidth(*this);}
892 virtual void printOptionInfo(unsigned GlobalWidth) const {
893 Parser.printOptionInfo(*this, GlobalWidth);
898 Parser.initialize(*this);
901 ParserClass &getParser() { return Parser; }
905 list(const M0t &M0) {
910 template<class M0t, class M1t>
911 list(const M0t &M0, const M1t &M1) {
912 apply(M0, this); apply(M1, this);
916 template<class M0t, class M1t, class M2t>
917 list(const M0t &M0, const M1t &M1, const M2t &M2) {
918 apply(M0, this); apply(M1, this); apply(M2, this);
922 template<class M0t, class M1t, class M2t, class M3t>
923 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3) {
924 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
928 template<class M0t, class M1t, class M2t, class M3t, class M4t>
929 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
931 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
936 template<class M0t, class M1t, class M2t, class M3t,
937 class M4t, class M5t>
938 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
939 const M4t &M4, const M5t &M5) {
940 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
941 apply(M4, this); apply(M5, this);
945 template<class M0t, class M1t, class M2t, class M3t,
946 class M4t, class M5t, class M6t>
947 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
948 const M4t &M4, const M5t &M5, const M6t &M6) {
949 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
950 apply(M4, this); apply(M5, this); apply(M6, this);
954 template<class M0t, class M1t, class M2t, class M3t,
955 class M4t, class M5t, class M6t, class M7t>
956 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
957 const M4t &M4, const M5t &M5, const M6t &M6, const M7t &M7) {
958 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
959 apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
966 //===----------------------------------------------------------------------===//
967 // Aliased command line option (alias this name to a preexisting name)
970 class alias : public Option {
972 virtual bool handleOccurrence(const char *ArgName, const std::string &Arg) {
973 return AliasFor->handleOccurrence(AliasFor->ArgStr, Arg);
975 // Aliases default to be hidden...
976 virtual enum OptionHidden getOptionHiddenFlagDefault() const {return Hidden;}
978 // Handle printing stuff...
979 virtual unsigned getOptionWidth() const;
980 virtual void printOptionInfo(unsigned GlobalWidth) const;
984 error(": cl::alias must have argument name specified!");
986 error(": cl::alias must have an cl::aliasopt(option) specified!");
990 void setAliasFor(Option &O) {
992 error(": cl::alias must only have one cl::aliasopt(...) specified!");
998 alias(const M0t &M0) : AliasFor(0) {
1003 template<class M0t, class M1t>
1004 alias(const M0t &M0, const M1t &M1) : AliasFor(0) {
1005 apply(M0, this); apply(M1, this);
1009 template<class M0t, class M1t, class M2t>
1010 alias(const M0t &M0, const M1t &M1, const M2t &M2) : AliasFor(0) {
1011 apply(M0, this); apply(M1, this); apply(M2, this);
1015 template<class M0t, class M1t, class M2t, class M3t>
1016 alias(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3)
1018 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1023 // aliasfor - Modifier to set the option an alias aliases.
1026 aliasopt(Option &O) : Opt(O) {}
1027 void apply(alias &A) const { A.setAliasFor(Opt); }
1030 } // End namespace cl
1032 } // End namespace llvm