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
28 #include "boost/type_traits/object_traits.hpp"
30 /// cl Namespace - This namespace contains all of the command line option
31 /// processing machinery. It is intentionally a short name to make qualified
35 //===----------------------------------------------------------------------===//
36 // ParseCommandLineOptions - Command line option processing entry point.
38 void ParseCommandLineOptions(int &argc, char **argv,
39 const char *Overview = 0);
41 //===----------------------------------------------------------------------===//
42 // ParseEnvironmentOptions - Environment variable option processing alternate
45 void ParseEnvironmentOptions(const char *progName, const char *envvar,
46 const char *Overview = 0);
48 //===----------------------------------------------------------------------===//
49 // Flags permitted to be passed to command line arguments
52 enum NumOccurrences { // Flags for the number of occurrences allowed
53 Optional = 0x01, // Zero or One occurrence
54 ZeroOrMore = 0x02, // Zero or more occurrences allowed
55 Required = 0x03, // One occurrence required
56 OneOrMore = 0x04, // One or more occurrences required
58 // ConsumeAfter - Indicates that this option is fed anything that follows the
59 // last positional argument required by the application (it is an error if
60 // there are zero positional arguments, and a ConsumeAfter option is used).
61 // Thus, for example, all arguments to LLI are processed until a filename is
62 // found. Once a filename is found, all of the succeeding arguments are
63 // passed, unprocessed, to the ConsumeAfter option.
67 OccurrencesMask = 0x07,
70 enum ValueExpected { // Is a value required for the option?
71 ValueOptional = 0x08, // The value can appear... or not
72 ValueRequired = 0x10, // The value is required to appear!
73 ValueDisallowed = 0x18, // A value may not be specified (for flags)
77 enum OptionHidden { // Control whether -help shows this option
78 NotHidden = 0x20, // Option included in --help & --help-hidden
79 Hidden = 0x40, // -help doesn't, but --help-hidden does
80 ReallyHidden = 0x60, // Neither --help nor --help-hidden show this arg
84 // Formatting flags - This controls special features that the option might have
85 // that cause it to be parsed differently...
87 // Prefix - This option allows arguments that are otherwise unrecognized to be
88 // matched by options that are a prefix of the actual value. This is useful for
89 // cases like a linker, where options are typically of the form '-lfoo' or
90 // '-L../../include' where -l or -L are the actual flags. When prefix is
91 // enabled, and used, the value for the flag comes from the suffix of the
94 // Grouping - With this option enabled, multiple letter options are allowed to
95 // bunch together with only a single hyphen for the whole group. This allows
96 // emulation of the behavior that ls uses for example: ls -la === ls -l -a
99 enum FormattingFlags {
100 NormalFormatting = 0x000, // Nothing special
101 Positional = 0x080, // Is a positional argument, no '-' required
102 Prefix = 0x100, // Can this option directly prefix its value?
103 Grouping = 0x180, // Can this option group with other options?
104 FormattingMask = 0x180,
107 enum MiscFlags { // Miscellaneous flags to adjust argument
108 CommaSeparated = 0x200, // Should this cl::list split between commas?
114 //===----------------------------------------------------------------------===//
119 friend void cl::ParseCommandLineOptions(int &, char **, const char *, int);
122 // handleOccurrences - Overriden by subclasses to handle the value passed into
123 // an argument. Should return true if there was an error processing the
124 // argument and the program should exit.
126 virtual bool handleOccurrence(const char *ArgName, const std::string &Arg) = 0;
128 virtual enum NumOccurrences getNumOccurrencesFlagDefault() const {
131 virtual enum ValueExpected getValueExpectedFlagDefault() const {
132 return ValueOptional;
134 virtual enum OptionHidden getOptionHiddenFlagDefault() const {
137 virtual enum FormattingFlags getFormattingFlagDefault() const {
138 return NormalFormatting;
141 int NumOccurrences; // The number of times specified
142 int Flags; // Flags for the argument
144 const char *ArgStr; // The argument string itself (ex: "help", "o")
145 const char *HelpStr; // The descriptive text message for --help
146 const char *ValueStr; // String describing what the value of this option is
148 inline enum NumOccurrences getNumOccurrencesFlag() const {
149 int NO = Flags & OccurrencesMask;
150 return NO ? (enum NumOccurrences)NO : getNumOccurrencesFlagDefault();
152 inline enum ValueExpected getValueExpectedFlag() const {
153 int VE = Flags & ValueMask;
154 return VE ? (enum ValueExpected)VE : getValueExpectedFlagDefault();
156 inline enum OptionHidden getOptionHiddenFlag() const {
157 int OH = Flags & HiddenMask;
158 return OH ? (enum OptionHidden)OH : getOptionHiddenFlagDefault();
160 inline enum FormattingFlags getFormattingFlag() const {
161 int OH = Flags & FormattingMask;
162 return OH ? (enum FormattingFlags)OH : getFormattingFlagDefault();
164 inline unsigned getMiscFlags() const {
165 return Flags & MiscMask;
168 // hasArgStr - Return true if the argstr != ""
169 bool hasArgStr() const { return ArgStr[0] != 0; }
171 //-------------------------------------------------------------------------===
172 // Accessor functions set by OptionModifiers
174 void setArgStr(const char *S) { ArgStr = S; }
175 void setDescription(const char *S) { HelpStr = S; }
176 void setValueStr(const char *S) { ValueStr = S; }
178 void setFlag(unsigned Flag, unsigned FlagMask) {
179 if (Flags & FlagMask) {
180 error(": Specified two settings for the same option!");
187 void setNumOccurrencesFlag(enum NumOccurrences Val) {
188 setFlag(Val, OccurrencesMask);
190 void setValueExpectedFlag(enum ValueExpected Val) { setFlag(Val, ValueMask); }
191 void setHiddenFlag(enum OptionHidden Val) { setFlag(Val, HiddenMask); }
192 void setFormattingFlag(enum FormattingFlags V) { setFlag(V, FormattingMask); }
193 void setMiscFlag(enum MiscFlags M) { setFlag(M, M); }
195 Option() : NumOccurrences(0), Flags(0),
196 ArgStr(""), HelpStr(""), ValueStr("") {}
199 // addArgument - Tell the system that this Option subclass will handle all
200 // occurrences of -ArgStr on the command line.
202 void addArgument(const char *ArgStr);
203 void removeArgument(const char *ArgStr);
205 // Return the width of the option tag for printing...
206 virtual unsigned getOptionWidth() const = 0;
208 // printOptionInfo - Print out information about this option. The
209 // to-be-maintained width is specified.
211 virtual void printOptionInfo(unsigned GlobalWidth) const = 0;
213 // addOccurrence - Wrapper around handleOccurrence that enforces Flags
215 bool addOccurrence(const char *ArgName, const std::string &Value);
217 // Prints option name followed by message. Always returns true.
218 bool error(std::string Message, const char *ArgName = 0);
221 inline int getNumOccurrences() const { return NumOccurrences; }
226 //===----------------------------------------------------------------------===//
227 // Command line option modifiers that can be used to modify the behavior of
228 // command line option parsers...
231 // desc - Modifier to set the description shown in the --help output...
234 desc(const char *Str) : Desc(Str) {}
235 void apply(Option &O) const { O.setDescription(Desc); }
238 // value_desc - Modifier to set the value description shown in the --help
242 value_desc(const char *Str) : Desc(Str) {}
243 void apply(Option &O) const { O.setValueStr(Desc); }
247 // init - Specify a default (initial) value for the command line argument, if
248 // the default constructor for the argument type does not give you what you
249 // want. This is only valid on "opt" arguments, not on "list" arguments.
254 initializer(const Ty &Val) : Init(Val) {}
257 void apply(Opt &O) const { O.setInitialValue(Init); }
261 initializer<Ty> init(const Ty &Val) {
262 return initializer<Ty>(Val);
266 // location - Allow the user to specify which external variable they want to
267 // store the results of the command line argument processing into, if they don't
268 // want to store it in the option itself.
271 struct LocationClass {
273 LocationClass(Ty &L) : Loc(L) {}
276 void apply(Opt &O) const { O.setLocation(O, Loc); }
280 LocationClass<Ty> location(Ty &L) { return LocationClass<Ty>(L); }
283 //===----------------------------------------------------------------------===//
284 // Enum valued command line option
286 #define clEnumVal(ENUMVAL, DESC) #ENUMVAL, (int)ENUMVAL, DESC
287 #define clEnumValN(ENUMVAL, FLAGNAME, DESC) FLAGNAME, (int)ENUMVAL, DESC
289 // values - For custom data types, allow specifying a group of values together
290 // as the values that go into the mapping that the option handler uses. Note
291 // that the values list must always have a 0 at the end of the list to indicate
292 // that the list has ended.
294 template<class DataType>
296 // Use a vector instead of a map, because the lists should be short,
297 // the overhead is less, and most importantly, it keeps them in the order
298 // inserted so we can print our option out nicely.
299 std::vector<std::pair<const char *, std::pair<int, const char *> > > Values;
300 void processValues(va_list Vals);
302 ValuesClass(const char *EnumName, DataType Val, const char *Desc,
304 // Insert the first value, which is required.
305 Values.push_back(std::make_pair(EnumName, std::make_pair(Val, Desc)));
307 // Process the varargs portion of the values...
308 while (const char *EnumName = va_arg(ValueArgs, const char *)) {
309 DataType EnumVal = (DataType)va_arg(ValueArgs, int);
310 const char *EnumDesc = va_arg(ValueArgs, const char *);
311 Values.push_back(std::make_pair(EnumName, // Add value to value map
312 std::make_pair(EnumVal, EnumDesc)));
317 void apply(Opt &O) const {
318 for (unsigned i = 0, e = Values.size(); i != e; ++i)
319 O.getParser().addLiteralOption(Values[i].first, Values[i].second.first,
320 Values[i].second.second);
324 template<class DataType>
325 ValuesClass<DataType> values(const char *Arg, DataType Val, const char *Desc,
328 va_start(ValueArgs, Desc);
329 ValuesClass<DataType> Vals(Arg, Val, Desc, ValueArgs);
335 //===----------------------------------------------------------------------===//
336 // parser class - Parameterizable parser for different data types. By default,
337 // known data types (string, int, bool) have specialized parsers, that do what
338 // you would expect. The default parser, used for data types that are not
339 // built-in, uses a mapping table to map specific options to values, which is
340 // used, among other things, to handle enum types.
342 //--------------------------------------------------
343 // generic_parser_base - This class holds all the non-generic code that we do
344 // not need replicated for every instance of the generic parser. This also
345 // allows us to put stuff into CommandLine.cpp
347 struct generic_parser_base {
348 virtual ~generic_parser_base() {} // Base class should have virtual-dtor
350 // getNumOptions - Virtual function implemented by generic subclass to
351 // indicate how many entries are in Values.
353 virtual unsigned getNumOptions() const = 0;
355 // getOption - Return option name N.
356 virtual const char *getOption(unsigned N) const = 0;
358 // getDescription - Return description N
359 virtual const char *getDescription(unsigned N) const = 0;
361 // Return the width of the option tag for printing...
362 virtual unsigned getOptionWidth(const Option &O) const;
364 // printOptionInfo - Print out information about this option. The
365 // to-be-maintained width is specified.
367 virtual void printOptionInfo(const Option &O, unsigned GlobalWidth) const;
369 void initialize(Option &O) {
370 // All of the modifiers for the option have been processed by now, so the
371 // argstr field should be stable, copy it down now.
373 hasArgStr = O.hasArgStr();
375 // If there has been no argstr specified, that means that we need to add an
376 // argument for every possible option. This ensures that our options are
380 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
381 O.addArgument(getOption(i));
384 enum ValueExpected getValueExpectedFlagDefault() const {
385 // If there is an ArgStr specified, then we are of the form:
387 // -opt=O2 or -opt O2 or -optO2
389 // In which case, the value is required. Otherwise if an arg str has not
390 // been specified, we are of the form:
392 // -O2 or O2 or -la (where -l and -a are separate options)
394 // If this is the case, we cannot allow a value.
397 return ValueRequired;
399 return ValueDisallowed;
402 // findOption - Return the option number corresponding to the specified
403 // argument string. If the option is not found, getNumOptions() is returned.
405 unsigned findOption(const char *Name);
411 // Default parser implementation - This implementation depends on having a
412 // mapping of recognized options to values of some sort. In addition to this,
413 // each entry in the mapping also tracks a help message that is printed with the
414 // command line option for --help. Because this is a simple mapping parser, the
415 // data type can be any unsupported type.
417 template <class DataType>
418 class parser : public generic_parser_base {
420 std::vector<std::pair<const char *,
421 std::pair<DataType, const char *> > > Values;
423 typedef DataType parser_data_type;
425 // Implement virtual functions needed by generic_parser_base
426 unsigned getNumOptions() const { return Values.size(); }
427 const char *getOption(unsigned N) const { return Values[N].first; }
428 const char *getDescription(unsigned N) const {
429 return Values[N].second.second;
432 // parse - Return true on error.
433 bool parse(Option &O, const char *ArgName, const std::string &Arg,
441 for (unsigned i = 0, e = Values.size(); i != e; ++i)
442 if (ArgVal == Values[i].first) {
443 V = Values[i].second.first;
447 return O.error(": Cannot find option named '" + ArgVal + "'!");
450 // addLiteralOption - Add an entry to the mapping table...
452 void addLiteralOption(const char *Name, const DT &V, const char *HelpStr) {
453 assert(findOption(Name) == Values.size() && "Option already exists!");
454 Values.push_back(std::make_pair(Name, std::make_pair((DataType)V,HelpStr)));
457 // removeLiteralOption - Remove the specified option.
459 void removeLiteralOption(const char *Name) {
460 unsigned N = findOption(Name);
461 assert(N != Values.size() && "Option not found!");
462 Values.erase(Values.begin()+N);
466 //--------------------------------------------------
467 // basic_parser - Super class of parsers to provide boilerplate code
469 struct basic_parser_impl { // non-template implementation of basic_parser<t>
470 virtual ~basic_parser_impl() {}
472 enum ValueExpected getValueExpectedFlagDefault() const {
473 return ValueRequired;
476 void initialize(Option &O) {}
478 // Return the width of the option tag for printing...
479 unsigned getOptionWidth(const Option &O) const;
481 // printOptionInfo - Print out information about this option. The
482 // to-be-maintained width is specified.
484 void printOptionInfo(const Option &O, unsigned GlobalWidth) const;
487 // getValueName - Overload in subclass to provide a better default value.
488 virtual const char *getValueName() const { return "value"; }
491 // basic_parser - The real basic parser is just a template wrapper that provides
492 // a typedef for the provided data type.
494 template<class DataType>
495 struct basic_parser : public basic_parser_impl {
496 typedef DataType parser_data_type;
500 //--------------------------------------------------
504 struct parser<bool> : public basic_parser<bool> {
506 // parse - Return true on error.
507 bool parse(Option &O, const char *ArgName, const std::string &Arg, bool &Val);
509 enum ValueExpected getValueExpectedFlagDefault() const {
510 return ValueOptional;
513 // getValueName - Do not print =<value> at all
514 virtual const char *getValueName() const { return 0; }
518 //--------------------------------------------------
522 struct parser<int> : public basic_parser<int> {
524 // parse - Return true on error.
525 bool parse(Option &O, const char *ArgName, const std::string &Arg, int &Val);
527 // getValueName - Overload in subclass to provide a better default value.
528 virtual const char *getValueName() const { return "int"; }
532 //--------------------------------------------------
536 struct parser<unsigned> : public basic_parser<unsigned> {
538 // parse - Return true on error.
539 bool parse(Option &O, const char *ArgName, const std::string &Arg,
542 // getValueName - Overload in subclass to provide a better default value.
543 virtual const char *getValueName() const { return "uint"; }
547 //--------------------------------------------------
551 struct parser<double> : public basic_parser<double> {
552 // parse - Return true on error.
553 bool parse(Option &O, const char *AN, const std::string &Arg, double &Val);
555 // getValueName - Overload in subclass to provide a better default value.
556 virtual const char *getValueName() const { return "number"; }
560 //--------------------------------------------------
564 struct parser<float> : public basic_parser<float> {
565 // parse - Return true on error.
566 bool parse(Option &O, const char *AN, const std::string &Arg, float &Val);
568 // getValueName - Overload in subclass to provide a better default value.
569 virtual const char *getValueName() const { return "number"; }
573 //--------------------------------------------------
574 // parser<std::string>
577 struct parser<std::string> : public basic_parser<std::string> {
578 // parse - Return true on error.
579 bool parse(Option &O, const char *ArgName, const std::string &Arg,
580 std::string &Value) {
585 // getValueName - Overload in subclass to provide a better default value.
586 virtual const char *getValueName() const { return "string"; }
591 //===----------------------------------------------------------------------===//
592 // applicator class - This class is used because we must use partial
593 // specialization to handle literal string arguments specially (const char* does
594 // not correctly respond to the apply method). Because the syntax to use this
595 // is a pain, we have the 'apply' method below to handle the nastiness...
597 template<class Mod> struct applicator {
599 static void opt(const Mod &M, Opt &O) { M.apply(O); }
602 // Handle const char* as a special case...
603 template<unsigned n> struct applicator<char[n]> {
605 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
607 template<unsigned n> struct applicator<const char[n]> {
609 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
611 template<> struct applicator<const char*> {
613 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
616 template<> struct applicator<NumOccurrences> {
617 static void opt(NumOccurrences NO, Option &O) { O.setNumOccurrencesFlag(NO); }
619 template<> struct applicator<ValueExpected> {
620 static void opt(ValueExpected VE, Option &O) { O.setValueExpectedFlag(VE); }
622 template<> struct applicator<OptionHidden> {
623 static void opt(OptionHidden OH, Option &O) { O.setHiddenFlag(OH); }
625 template<> struct applicator<FormattingFlags> {
626 static void opt(FormattingFlags FF, Option &O) { O.setFormattingFlag(FF); }
628 template<> struct applicator<MiscFlags> {
629 static void opt(MiscFlags MF, Option &O) { O.setMiscFlag(MF); }
632 // apply method - Apply a modifier to an option in a type safe way.
633 template<class Mod, class Opt>
634 void apply(const Mod &M, Opt *O) {
635 applicator<Mod>::opt(M, *O);
639 //===----------------------------------------------------------------------===//
642 // Default storage class definition: external storage. This implementation
643 // assumes the user will specify a variable to store the data into with the
644 // cl::location(x) modifier.
646 template<class DataType, bool ExternalStorage, bool isClass>
648 DataType *Location; // Where to store the object...
651 assert(Location != 0 && "cl::location(...) not specified for a command "
652 "line option with external storage, "
653 "or cl::init specified before cl::location()!!");
656 opt_storage() : Location(0) {}
658 bool setLocation(Option &O, DataType &L) {
660 return O.error(": cl::location(x) specified more than once!");
666 void setValue(const T &V) {
671 DataType &getValue() { check(); return *Location; }
672 const DataType &getValue() const { check(); return *Location; }
676 // Define how to hold a class type object, such as a string. Since we can
677 // inherit from a class, we do so. This makes us exactly compatible with the
678 // object in all cases that it is used.
680 template<class DataType>
681 struct opt_storage<DataType,false,true> : public DataType {
684 void setValue(const T &V) { DataType::operator=(V); }
686 DataType &getValue() { return *this; }
687 const DataType &getValue() const { return *this; }
690 // Define a partial specialization to handle things we cannot inherit from. In
691 // this case, we store an instance through containment, and overload operators
692 // to get at the value.
694 template<class DataType>
695 struct opt_storage<DataType, false, false> {
698 // Make sure we initialize the value with the default constructor for the
700 opt_storage() : Value(DataType()) {}
703 void setValue(const T &V) { Value = V; }
704 DataType &getValue() { return Value; }
705 DataType getValue() const { return Value; }
709 //===----------------------------------------------------------------------===//
710 // opt - A scalar command line option.
712 template <class DataType, bool ExternalStorage = false,
713 class ParserClass = parser<DataType> >
714 class opt : public Option,
715 public opt_storage<DataType, ExternalStorage,
716 ::boost::is_class<DataType>::value> {
719 virtual bool handleOccurrence(const char *ArgName, const std::string &Arg) {
720 typename ParserClass::parser_data_type Val;
721 if (Parser.parse(*this, ArgName, Arg, Val))
722 return true; // Parse error!
727 virtual enum ValueExpected getValueExpectedFlagDefault() const {
728 return Parser.getValueExpectedFlagDefault();
731 // Forward printing stuff to the parser...
732 virtual unsigned getOptionWidth() const {return Parser.getOptionWidth(*this);}
733 virtual void printOptionInfo(unsigned GlobalWidth) const {
734 Parser.printOptionInfo(*this, GlobalWidth);
739 Parser.initialize(*this);
742 // setInitialValue - Used by the cl::init modifier...
743 void setInitialValue(const DataType &V) { this->setValue(V); }
745 ParserClass &getParser() { return Parser; }
747 operator DataType() const { return this->getValue(); }
750 DataType &operator=(const T &Val) {
752 return this->getValue();
763 template<class M0t, class M1t>
764 opt(const M0t &M0, const M1t &M1) {
765 apply(M0, this); apply(M1, this);
770 template<class M0t, class M1t, class M2t>
771 opt(const M0t &M0, const M1t &M1, const M2t &M2) {
772 apply(M0, this); apply(M1, this); apply(M2, this);
776 template<class M0t, class M1t, class M2t, class M3t>
777 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);
782 template<class M0t, class M1t, class M2t, class M3t, class M4t>
783 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);
790 template<class M0t, class M1t, class M2t, class M3t,
791 class M4t, class M5t>
792 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
793 const M4t &M4, const M5t &M5) {
794 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
795 apply(M4, this); apply(M5, this);
799 template<class M0t, class M1t, class M2t, class M3t,
800 class M4t, class M5t, class M6t>
801 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
802 const M4t &M4, const M5t &M5, const M6t &M6) {
803 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
804 apply(M4, this); apply(M5, this); apply(M6, this);
808 template<class M0t, class M1t, class M2t, class M3t,
809 class M4t, class M5t, class M6t, class M7t>
810 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
811 const M4t &M4, const M5t &M5, const M6t &M6, const M7t &M7) {
812 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
813 apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
818 //===----------------------------------------------------------------------===//
819 // list_storage class
821 // Default storage class definition: external storage. This implementation
822 // assumes the user will specify a variable to store the data into with the
823 // cl::location(x) modifier.
825 template<class DataType, class StorageClass>
827 StorageClass *Location; // Where to store the object...
830 list_storage() : Location(0) {}
832 bool setLocation(Option &O, StorageClass &L) {
834 return O.error(": cl::location(x) specified more than once!");
840 void addValue(const T &V) {
841 assert(Location != 0 && "cl::location(...) not specified for a command "
842 "line option with external storage!");
843 Location->push_back(V);
848 // Define how to hold a class type object, such as a string. Since we can
849 // inherit from a class, we do so. This makes us exactly compatible with the
850 // object in all cases that it is used.
852 template<class DataType>
853 struct list_storage<DataType, bool> : public std::vector<DataType> {
856 void addValue(const T &V) { push_back(V); }
860 //===----------------------------------------------------------------------===//
861 // list - A list of command line options.
863 template <class DataType, class Storage = bool,
864 class ParserClass = parser<DataType> >
865 class list : public Option, public list_storage<DataType, Storage> {
868 virtual enum NumOccurrences getNumOccurrencesFlagDefault() const {
871 virtual enum ValueExpected getValueExpectedFlagDefault() const {
872 return Parser.getValueExpectedFlagDefault();
875 virtual bool handleOccurrence(const char *ArgName, const std::string &Arg) {
876 typename ParserClass::parser_data_type Val;
877 if (Parser.parse(*this, ArgName, Arg, Val))
878 return true; // Parse Error!
883 // Forward printing stuff to the parser...
884 virtual unsigned getOptionWidth() const {return Parser.getOptionWidth(*this);}
885 virtual void printOptionInfo(unsigned GlobalWidth) const {
886 Parser.printOptionInfo(*this, GlobalWidth);
891 Parser.initialize(*this);
894 ParserClass &getParser() { return Parser; }
898 list(const M0t &M0) {
903 template<class M0t, class M1t>
904 list(const M0t &M0, const M1t &M1) {
905 apply(M0, this); apply(M1, this);
909 template<class M0t, class M1t, class M2t>
910 list(const M0t &M0, const M1t &M1, const M2t &M2) {
911 apply(M0, this); apply(M1, this); apply(M2, this);
915 template<class M0t, class M1t, class M2t, class M3t>
916 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);
921 template<class M0t, class M1t, class M2t, class M3t, class M4t>
922 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);
929 template<class M0t, class M1t, class M2t, class M3t,
930 class M4t, class M5t>
931 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
932 const M4t &M4, const M5t &M5) {
933 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
934 apply(M4, this); apply(M5, this);
938 template<class M0t, class M1t, class M2t, class M3t,
939 class M4t, class M5t, class M6t>
940 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
941 const M4t &M4, const M5t &M5, const M6t &M6) {
942 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
943 apply(M4, this); apply(M5, this); apply(M6, this);
947 template<class M0t, class M1t, class M2t, class M3t,
948 class M4t, class M5t, class M6t, class M7t>
949 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
950 const M4t &M4, const M5t &M5, const M6t &M6, const M7t &M7) {
951 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
952 apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
959 //===----------------------------------------------------------------------===//
960 // Aliased command line option (alias this name to a preexisting name)
963 class alias : public Option {
965 virtual bool handleOccurrence(const char *ArgName, const std::string &Arg) {
966 return AliasFor->handleOccurrence(AliasFor->ArgStr, Arg);
968 // Aliases default to be hidden...
969 virtual enum OptionHidden getOptionHiddenFlagDefault() const {return Hidden;}
971 // Handle printing stuff...
972 virtual unsigned getOptionWidth() const;
973 virtual void printOptionInfo(unsigned GlobalWidth) const;
977 error(": cl::alias must have argument name specified!");
979 error(": cl::alias must have an cl::aliasopt(option) specified!");
983 void setAliasFor(Option &O) {
985 error(": cl::alias must only have one cl::aliasopt(...) specified!");
991 alias(const M0t &M0) : AliasFor(0) {
996 template<class M0t, class M1t>
997 alias(const M0t &M0, const M1t &M1) : AliasFor(0) {
998 apply(M0, this); apply(M1, this);
1002 template<class M0t, class M1t, class M2t>
1003 alias(const M0t &M0, const M1t &M1, const M2t &M2) : AliasFor(0) {
1004 apply(M0, this); apply(M1, this); apply(M2, this);
1008 template<class M0t, class M1t, class M2t, class M3t>
1009 alias(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3)
1011 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1016 // aliasfor - Modifier to set the option an alias aliases.
1019 aliasopt(Option &O) : Opt(O) {}
1020 void apply(alias &A) const { A.setAliasFor(Opt); }
1023 } // End namespace cl