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?
111 PositionalEatsArgs = 0x400, // Should this positional cl::list eat -args?
117 //===----------------------------------------------------------------------===//
122 friend void cl::ParseCommandLineOptions(int &, char **, const char *, int);
125 // handleOccurrences - Overriden by subclasses to handle the value passed into
126 // an argument. Should return true if there was an error processing the
127 // argument and the program should exit.
129 virtual bool handleOccurrence(const char *ArgName, const std::string &Arg) = 0;
131 virtual enum NumOccurrences getNumOccurrencesFlagDefault() const {
134 virtual enum ValueExpected getValueExpectedFlagDefault() const {
135 return ValueOptional;
137 virtual enum OptionHidden getOptionHiddenFlagDefault() const {
140 virtual enum FormattingFlags getFormattingFlagDefault() const {
141 return NormalFormatting;
144 int NumOccurrences; // The number of times specified
145 int Flags; // Flags for the argument
147 const char *ArgStr; // The argument string itself (ex: "help", "o")
148 const char *HelpStr; // The descriptive text message for --help
149 const char *ValueStr; // String describing what the value of this option is
151 inline enum NumOccurrences getNumOccurrencesFlag() const {
152 int NO = Flags & OccurrencesMask;
153 return NO ? static_cast<enum NumOccurrences>(NO)
154 : getNumOccurrencesFlagDefault();
156 inline enum ValueExpected getValueExpectedFlag() const {
157 int VE = Flags & ValueMask;
158 return VE ? static_cast<enum ValueExpected>(VE)
159 : getValueExpectedFlagDefault();
161 inline enum OptionHidden getOptionHiddenFlag() const {
162 int OH = Flags & HiddenMask;
163 return OH ? static_cast<enum OptionHidden>(OH)
164 : getOptionHiddenFlagDefault();
166 inline enum FormattingFlags getFormattingFlag() const {
167 int OH = Flags & FormattingMask;
168 return OH ? static_cast<enum FormattingFlags>(OH)
169 : getFormattingFlagDefault();
171 inline unsigned getMiscFlags() const {
172 return Flags & MiscMask;
175 // hasArgStr - Return true if the argstr != ""
176 bool hasArgStr() const { return ArgStr[0] != 0; }
178 //-------------------------------------------------------------------------===
179 // Accessor functions set by OptionModifiers
181 void setArgStr(const char *S) { ArgStr = S; }
182 void setDescription(const char *S) { HelpStr = S; }
183 void setValueStr(const char *S) { ValueStr = S; }
185 void setFlag(unsigned Flag, unsigned FlagMask) {
186 if (Flags & FlagMask) {
187 error(": Specified two settings for the same option!");
194 void setNumOccurrencesFlag(enum NumOccurrences Val) {
195 setFlag(Val, OccurrencesMask);
197 void setValueExpectedFlag(enum ValueExpected Val) { setFlag(Val, ValueMask); }
198 void setHiddenFlag(enum OptionHidden Val) { setFlag(Val, HiddenMask); }
199 void setFormattingFlag(enum FormattingFlags V) { setFlag(V, FormattingMask); }
200 void setMiscFlag(enum MiscFlags M) { setFlag(M, M); }
202 Option() : NumOccurrences(0), Flags(0),
203 ArgStr(""), HelpStr(""), ValueStr("") {}
206 // addArgument - Tell the system that this Option subclass will handle all
207 // occurrences of -ArgStr on the command line.
209 void addArgument(const char *ArgStr);
210 void removeArgument(const char *ArgStr);
212 // Return the width of the option tag for printing...
213 virtual unsigned getOptionWidth() const = 0;
215 // printOptionInfo - Print out information about this option. The
216 // to-be-maintained width is specified.
218 virtual void printOptionInfo(unsigned GlobalWidth) const = 0;
220 // addOccurrence - Wrapper around handleOccurrence that enforces Flags
222 bool addOccurrence(const char *ArgName, const std::string &Value);
224 // Prints option name followed by message. Always returns true.
225 bool error(std::string Message, const char *ArgName = 0);
228 inline int getNumOccurrences() const { return NumOccurrences; }
233 //===----------------------------------------------------------------------===//
234 // Command line option modifiers that can be used to modify the behavior of
235 // command line option parsers...
238 // desc - Modifier to set the description shown in the --help output...
241 desc(const char *Str) : Desc(Str) {}
242 void apply(Option &O) const { O.setDescription(Desc); }
245 // value_desc - Modifier to set the value description shown in the --help
249 value_desc(const char *Str) : Desc(Str) {}
250 void apply(Option &O) const { O.setValueStr(Desc); }
254 // init - Specify a default (initial) value for the command line argument, if
255 // the default constructor for the argument type does not give you what you
256 // want. This is only valid on "opt" arguments, not on "list" arguments.
261 initializer(const Ty &Val) : Init(Val) {}
264 void apply(Opt &O) const { O.setInitialValue(Init); }
268 initializer<Ty> init(const Ty &Val) {
269 return initializer<Ty>(Val);
273 // location - Allow the user to specify which external variable they want to
274 // store the results of the command line argument processing into, if they don't
275 // want to store it in the option itself.
278 struct LocationClass {
280 LocationClass(Ty &L) : Loc(L) {}
283 void apply(Opt &O) const { O.setLocation(O, Loc); }
287 LocationClass<Ty> location(Ty &L) { return LocationClass<Ty>(L); }
290 //===----------------------------------------------------------------------===//
291 // Enum valued command line option
293 #define clEnumVal(ENUMVAL, DESC) #ENUMVAL, (int)ENUMVAL, DESC
294 #define clEnumValN(ENUMVAL, FLAGNAME, DESC) FLAGNAME, (int)ENUMVAL, DESC
296 // values - For custom data types, allow specifying a group of values together
297 // as the values that go into the mapping that the option handler uses. Note
298 // that the values list must always have a 0 at the end of the list to indicate
299 // that the list has ended.
301 template<class DataType>
303 // Use a vector instead of a map, because the lists should be short,
304 // the overhead is less, and most importantly, it keeps them in the order
305 // inserted so we can print our option out nicely.
306 std::vector<std::pair<const char *, std::pair<int, const char *> > > Values;
307 void processValues(va_list Vals);
309 ValuesClass(const char *EnumName, DataType Val, const char *Desc,
311 // Insert the first value, which is required.
312 Values.push_back(std::make_pair(EnumName, std::make_pair(Val, Desc)));
314 // Process the varargs portion of the values...
315 while (const char *EnumName = va_arg(ValueArgs, const char *)) {
316 DataType EnumVal = static_cast<DataType>(va_arg(ValueArgs, int));
317 const char *EnumDesc = va_arg(ValueArgs, const char *);
318 Values.push_back(std::make_pair(EnumName, // Add value to value map
319 std::make_pair(EnumVal, EnumDesc)));
324 void apply(Opt &O) const {
325 for (unsigned i = 0, e = Values.size(); i != e; ++i)
326 O.getParser().addLiteralOption(Values[i].first, Values[i].second.first,
327 Values[i].second.second);
331 template<class DataType>
332 ValuesClass<DataType> values(const char *Arg, DataType Val, const char *Desc,
335 va_start(ValueArgs, Desc);
336 ValuesClass<DataType> Vals(Arg, Val, Desc, ValueArgs);
342 //===----------------------------------------------------------------------===//
343 // parser class - Parameterizable parser for different data types. By default,
344 // known data types (string, int, bool) have specialized parsers, that do what
345 // you would expect. The default parser, used for data types that are not
346 // built-in, uses a mapping table to map specific options to values, which is
347 // used, among other things, to handle enum types.
349 //--------------------------------------------------
350 // generic_parser_base - This class holds all the non-generic code that we do
351 // not need replicated for every instance of the generic parser. This also
352 // allows us to put stuff into CommandLine.cpp
354 struct generic_parser_base {
355 virtual ~generic_parser_base() {} // Base class should have virtual-dtor
357 // getNumOptions - Virtual function implemented by generic subclass to
358 // indicate how many entries are in Values.
360 virtual unsigned getNumOptions() const = 0;
362 // getOption - Return option name N.
363 virtual const char *getOption(unsigned N) const = 0;
365 // getDescription - Return description N
366 virtual const char *getDescription(unsigned N) const = 0;
368 // Return the width of the option tag for printing...
369 virtual unsigned getOptionWidth(const Option &O) const;
371 // printOptionInfo - Print out information about this option. The
372 // to-be-maintained width is specified.
374 virtual void printOptionInfo(const Option &O, unsigned GlobalWidth) const;
376 void initialize(Option &O) {
377 // All of the modifiers for the option have been processed by now, so the
378 // argstr field should be stable, copy it down now.
380 hasArgStr = O.hasArgStr();
382 // If there has been no argstr specified, that means that we need to add an
383 // argument for every possible option. This ensures that our options are
387 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
388 O.addArgument(getOption(i));
391 enum ValueExpected getValueExpectedFlagDefault() const {
392 // If there is an ArgStr specified, then we are of the form:
394 // -opt=O2 or -opt O2 or -optO2
396 // In which case, the value is required. Otherwise if an arg str has not
397 // been specified, we are of the form:
399 // -O2 or O2 or -la (where -l and -a are separate options)
401 // If this is the case, we cannot allow a value.
404 return ValueRequired;
406 return ValueDisallowed;
409 // findOption - Return the option number corresponding to the specified
410 // argument string. If the option is not found, getNumOptions() is returned.
412 unsigned findOption(const char *Name);
418 // Default parser implementation - This implementation depends on having a
419 // mapping of recognized options to values of some sort. In addition to this,
420 // each entry in the mapping also tracks a help message that is printed with the
421 // command line option for --help. Because this is a simple mapping parser, the
422 // data type can be any unsupported type.
424 template <class DataType>
425 class parser : public generic_parser_base {
427 std::vector<std::pair<const char *,
428 std::pair<DataType, const char *> > > Values;
430 typedef DataType parser_data_type;
432 // Implement virtual functions needed by generic_parser_base
433 unsigned getNumOptions() const { return Values.size(); }
434 const char *getOption(unsigned N) const { return Values[N].first; }
435 const char *getDescription(unsigned N) const {
436 return Values[N].second.second;
439 // parse - Return true on error.
440 bool parse(Option &O, const char *ArgName, const std::string &Arg,
448 for (unsigned i = 0, e = Values.size(); i != e; ++i)
449 if (ArgVal == Values[i].first) {
450 V = Values[i].second.first;
454 return O.error(": Cannot find option named '" + ArgVal + "'!");
457 // addLiteralOption - Add an entry to the mapping table...
459 void addLiteralOption(const char *Name, const DT &V, const char *HelpStr) {
460 assert(findOption(Name) == Values.size() && "Option already exists!");
461 Values.push_back(std::make_pair(Name,
462 std::make_pair(static_cast<DataType>(V),HelpStr)));
465 // removeLiteralOption - Remove the specified option.
467 void removeLiteralOption(const char *Name) {
468 unsigned N = findOption(Name);
469 assert(N != Values.size() && "Option not found!");
470 Values.erase(Values.begin()+N);
474 //--------------------------------------------------
475 // basic_parser - Super class of parsers to provide boilerplate code
477 struct basic_parser_impl { // non-template implementation of basic_parser<t>
478 virtual ~basic_parser_impl() {}
480 enum ValueExpected getValueExpectedFlagDefault() const {
481 return ValueRequired;
484 void initialize(Option &O) {}
486 // Return the width of the option tag for printing...
487 unsigned getOptionWidth(const Option &O) const;
489 // printOptionInfo - Print out information about this option. The
490 // to-be-maintained width is specified.
492 void printOptionInfo(const Option &O, unsigned GlobalWidth) const;
495 // getValueName - Overload in subclass to provide a better default value.
496 virtual const char *getValueName() const { return "value"; }
499 // basic_parser - The real basic parser is just a template wrapper that provides
500 // a typedef for the provided data type.
502 template<class DataType>
503 struct basic_parser : public basic_parser_impl {
504 typedef DataType parser_data_type;
508 //--------------------------------------------------
512 struct parser<bool> : public basic_parser<bool> {
514 // parse - Return true on error.
515 bool parse(Option &O, const char *ArgName, const std::string &Arg, bool &Val);
517 enum ValueExpected getValueExpectedFlagDefault() const {
518 return ValueOptional;
521 // getValueName - Do not print =<value> at all
522 virtual const char *getValueName() const { return 0; }
526 //--------------------------------------------------
530 struct parser<int> : public basic_parser<int> {
532 // parse - Return true on error.
533 bool parse(Option &O, const char *ArgName, const std::string &Arg, int &Val);
535 // getValueName - Overload in subclass to provide a better default value.
536 virtual const char *getValueName() const { return "int"; }
540 //--------------------------------------------------
544 struct parser<unsigned> : public basic_parser<unsigned> {
546 // parse - Return true on error.
547 bool parse(Option &O, const char *ArgName, const std::string &Arg,
550 // getValueName - Overload in subclass to provide a better default value.
551 virtual const char *getValueName() const { return "uint"; }
555 //--------------------------------------------------
559 struct parser<double> : public basic_parser<double> {
560 // parse - Return true on error.
561 bool parse(Option &O, const char *AN, const std::string &Arg, double &Val);
563 // getValueName - Overload in subclass to provide a better default value.
564 virtual const char *getValueName() const { return "number"; }
568 //--------------------------------------------------
572 struct parser<float> : public basic_parser<float> {
573 // parse - Return true on error.
574 bool parse(Option &O, const char *AN, const std::string &Arg, float &Val);
576 // getValueName - Overload in subclass to provide a better default value.
577 virtual const char *getValueName() const { return "number"; }
581 //--------------------------------------------------
582 // parser<std::string>
585 struct parser<std::string> : public basic_parser<std::string> {
586 // parse - Return true on error.
587 bool parse(Option &O, const char *ArgName, const std::string &Arg,
588 std::string &Value) {
593 // getValueName - Overload in subclass to provide a better default value.
594 virtual const char *getValueName() const { return "string"; }
599 //===----------------------------------------------------------------------===//
600 // applicator class - This class is used because we must use partial
601 // specialization to handle literal string arguments specially (const char* does
602 // not correctly respond to the apply method). Because the syntax to use this
603 // is a pain, we have the 'apply' method below to handle the nastiness...
605 template<class Mod> struct applicator {
607 static void opt(const Mod &M, Opt &O) { M.apply(O); }
610 // Handle const char* as a special case...
611 template<unsigned n> struct applicator<char[n]> {
613 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
615 template<unsigned n> struct applicator<const char[n]> {
617 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
619 template<> struct applicator<const char*> {
621 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
624 template<> struct applicator<NumOccurrences> {
625 static void opt(NumOccurrences NO, Option &O) { O.setNumOccurrencesFlag(NO); }
627 template<> struct applicator<ValueExpected> {
628 static void opt(ValueExpected VE, Option &O) { O.setValueExpectedFlag(VE); }
630 template<> struct applicator<OptionHidden> {
631 static void opt(OptionHidden OH, Option &O) { O.setHiddenFlag(OH); }
633 template<> struct applicator<FormattingFlags> {
634 static void opt(FormattingFlags FF, Option &O) { O.setFormattingFlag(FF); }
636 template<> struct applicator<MiscFlags> {
637 static void opt(MiscFlags MF, Option &O) { O.setMiscFlag(MF); }
640 // apply method - Apply a modifier to an option in a type safe way.
641 template<class Mod, class Opt>
642 void apply(const Mod &M, Opt *O) {
643 applicator<Mod>::opt(M, *O);
647 //===----------------------------------------------------------------------===//
650 // Default storage class definition: external storage. This implementation
651 // assumes the user will specify a variable to store the data into with the
652 // cl::location(x) modifier.
654 template<class DataType, bool ExternalStorage, bool isClass>
656 DataType *Location; // Where to store the object...
659 assert(Location != 0 && "cl::location(...) not specified for a command "
660 "line option with external storage, "
661 "or cl::init specified before cl::location()!!");
664 opt_storage() : Location(0) {}
666 bool setLocation(Option &O, DataType &L) {
668 return O.error(": cl::location(x) specified more than once!");
674 void setValue(const T &V) {
679 DataType &getValue() { check(); return *Location; }
680 const DataType &getValue() const { check(); return *Location; }
684 // Define how to hold a class type object, such as a string. Since we can
685 // inherit from a class, we do so. This makes us exactly compatible with the
686 // object in all cases that it is used.
688 template<class DataType>
689 struct opt_storage<DataType,false,true> : public DataType {
692 void setValue(const T &V) { DataType::operator=(V); }
694 DataType &getValue() { return *this; }
695 const DataType &getValue() const { return *this; }
698 // Define a partial specialization to handle things we cannot inherit from. In
699 // this case, we store an instance through containment, and overload operators
700 // to get at the value.
702 template<class DataType>
703 struct opt_storage<DataType, false, false> {
706 // Make sure we initialize the value with the default constructor for the
708 opt_storage() : Value(DataType()) {}
711 void setValue(const T &V) { Value = V; }
712 DataType &getValue() { return Value; }
713 DataType getValue() const { return Value; }
717 //===----------------------------------------------------------------------===//
718 // opt - A scalar command line option.
720 template <class DataType, bool ExternalStorage = false,
721 class ParserClass = parser<DataType> >
722 class opt : public Option,
723 public opt_storage<DataType, ExternalStorage,
724 is_class<DataType>::value> {
727 virtual bool handleOccurrence(const char *ArgName, const std::string &Arg) {
728 typename ParserClass::parser_data_type Val;
729 if (Parser.parse(*this, ArgName, Arg, Val))
730 return true; // Parse error!
735 virtual enum ValueExpected getValueExpectedFlagDefault() const {
736 return Parser.getValueExpectedFlagDefault();
739 // Forward printing stuff to the parser...
740 virtual unsigned getOptionWidth() const {return Parser.getOptionWidth(*this);}
741 virtual void printOptionInfo(unsigned GlobalWidth) const {
742 Parser.printOptionInfo(*this, GlobalWidth);
747 Parser.initialize(*this);
750 // setInitialValue - Used by the cl::init modifier...
751 void setInitialValue(const DataType &V) { this->setValue(V); }
753 ParserClass &getParser() { return Parser; }
755 operator DataType() const { return this->getValue(); }
758 DataType &operator=(const T &Val) {
760 return this->getValue();
771 template<class M0t, class M1t>
772 opt(const M0t &M0, const M1t &M1) {
773 apply(M0, this); apply(M1, this);
778 template<class M0t, class M1t, class M2t>
779 opt(const M0t &M0, const M1t &M1, const M2t &M2) {
780 apply(M0, this); apply(M1, this); apply(M2, this);
784 template<class M0t, class M1t, class M2t, class M3t>
785 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3) {
786 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
790 template<class M0t, class M1t, class M2t, class M3t, class M4t>
791 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
793 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
798 template<class M0t, class M1t, class M2t, class M3t,
799 class M4t, class M5t>
800 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
801 const M4t &M4, const M5t &M5) {
802 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
803 apply(M4, this); apply(M5, this);
807 template<class M0t, class M1t, class M2t, class M3t,
808 class M4t, class M5t, class M6t>
809 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
810 const M4t &M4, const M5t &M5, const M6t &M6) {
811 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
812 apply(M4, this); apply(M5, this); apply(M6, this);
816 template<class M0t, class M1t, class M2t, class M3t,
817 class M4t, class M5t, class M6t, class M7t>
818 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
819 const M4t &M4, const M5t &M5, const M6t &M6, const M7t &M7) {
820 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
821 apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
826 //===----------------------------------------------------------------------===//
827 // list_storage class
829 // Default storage class definition: external storage. This implementation
830 // assumes the user will specify a variable to store the data into with the
831 // cl::location(x) modifier.
833 template<class DataType, class StorageClass>
835 StorageClass *Location; // Where to store the object...
838 list_storage() : Location(0) {}
840 bool setLocation(Option &O, StorageClass &L) {
842 return O.error(": cl::location(x) specified more than once!");
848 void addValue(const T &V) {
849 assert(Location != 0 && "cl::location(...) not specified for a command "
850 "line option with external storage!");
851 Location->push_back(V);
856 // Define how to hold a class type object, such as a string. Since we can
857 // inherit from a class, we do so. This makes us exactly compatible with the
858 // object in all cases that it is used.
860 template<class DataType>
861 struct list_storage<DataType, bool> : public std::vector<DataType> {
864 void addValue(const T &V) { push_back(V); }
868 //===----------------------------------------------------------------------===//
869 // list - A list of command line options.
871 template <class DataType, class Storage = bool,
872 class ParserClass = parser<DataType> >
873 class list : public Option, public list_storage<DataType, Storage> {
876 virtual enum NumOccurrences getNumOccurrencesFlagDefault() const {
879 virtual enum ValueExpected getValueExpectedFlagDefault() const {
880 return Parser.getValueExpectedFlagDefault();
883 virtual bool handleOccurrence(const char *ArgName, const std::string &Arg) {
884 typename ParserClass::parser_data_type Val;
885 if (Parser.parse(*this, ArgName, Arg, Val))
886 return true; // Parse Error!
891 // Forward printing stuff to the parser...
892 virtual unsigned getOptionWidth() const {return Parser.getOptionWidth(*this);}
893 virtual void printOptionInfo(unsigned GlobalWidth) const {
894 Parser.printOptionInfo(*this, GlobalWidth);
899 Parser.initialize(*this);
902 ParserClass &getParser() { return Parser; }
906 list(const M0t &M0) {
911 template<class M0t, class M1t>
912 list(const M0t &M0, const M1t &M1) {
913 apply(M0, this); apply(M1, this);
917 template<class M0t, class M1t, class M2t>
918 list(const M0t &M0, const M1t &M1, const M2t &M2) {
919 apply(M0, this); apply(M1, this); apply(M2, this);
923 template<class M0t, class M1t, class M2t, class M3t>
924 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3) {
925 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
929 template<class M0t, class M1t, class M2t, class M3t, class M4t>
930 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
932 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
937 template<class M0t, class M1t, class M2t, class M3t,
938 class M4t, class M5t>
939 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
940 const M4t &M4, const M5t &M5) {
941 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
942 apply(M4, this); apply(M5, this);
946 template<class M0t, class M1t, class M2t, class M3t,
947 class M4t, class M5t, class M6t>
948 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
949 const M4t &M4, const M5t &M5, const M6t &M6) {
950 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
951 apply(M4, this); apply(M5, this); apply(M6, this);
955 template<class M0t, class M1t, class M2t, class M3t,
956 class M4t, class M5t, class M6t, class M7t>
957 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
958 const M4t &M4, const M5t &M5, const M6t &M6, const M7t &M7) {
959 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
960 apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
967 //===----------------------------------------------------------------------===//
968 // Aliased command line option (alias this name to a preexisting name)
971 class alias : public Option {
973 virtual bool handleOccurrence(const char *ArgName, const std::string &Arg) {
974 return AliasFor->handleOccurrence(AliasFor->ArgStr, Arg);
976 // Aliases default to be hidden...
977 virtual enum OptionHidden getOptionHiddenFlagDefault() const {return Hidden;}
979 // Handle printing stuff...
980 virtual unsigned getOptionWidth() const;
981 virtual void printOptionInfo(unsigned GlobalWidth) const;
985 error(": cl::alias must have argument name specified!");
987 error(": cl::alias must have an cl::aliasopt(option) specified!");
991 void setAliasFor(Option &O) {
993 error(": cl::alias must only have one cl::aliasopt(...) specified!");
999 alias(const M0t &M0) : AliasFor(0) {
1004 template<class M0t, class M1t>
1005 alias(const M0t &M0, const M1t &M1) : AliasFor(0) {
1006 apply(M0, this); apply(M1, this);
1010 template<class M0t, class M1t, class M2t>
1011 alias(const M0t &M0, const M1t &M1, const M2t &M2) : AliasFor(0) {
1012 apply(M0, this); apply(M1, this); apply(M2, this);
1016 template<class M0t, class M1t, class M2t, class M3t>
1017 alias(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3)
1019 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1024 // aliasfor - Modifier to set the option an alias aliases.
1027 aliasopt(Option &O) : Opt(O) {}
1028 void apply(alias &A) const { A.setAliasFor(Opt); }
1031 } // End namespace cl
1033 } // End namespace llvm