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 *);
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
295 #define clEnumValEnd ((void*)0)
297 // values - For custom data types, allow specifying a group of values together
298 // as the values that go into the mapping that the option handler uses. Note
299 // that the values list must always have a 0 at the end of the list to indicate
300 // that the list has ended.
302 template<class DataType>
304 // Use a vector instead of a map, because the lists should be short,
305 // the overhead is less, and most importantly, it keeps them in the order
306 // inserted so we can print our option out nicely.
307 std::vector<std::pair<const char *, std::pair<int, const char *> > > Values;
308 void processValues(va_list Vals);
310 ValuesClass(const char *EnumName, DataType Val, const char *Desc,
312 // Insert the first value, which is required.
313 Values.push_back(std::make_pair(EnumName, std::make_pair(Val, Desc)));
315 // Process the varargs portion of the values...
316 while (const char *EnumName = va_arg(ValueArgs, const char *)) {
317 DataType EnumVal = static_cast<DataType>(va_arg(ValueArgs, int));
318 const char *EnumDesc = va_arg(ValueArgs, const char *);
319 Values.push_back(std::make_pair(EnumName, // Add value to value map
320 std::make_pair(EnumVal, EnumDesc)));
325 void apply(Opt &O) const {
326 for (unsigned i = 0, e = Values.size(); i != e; ++i)
327 O.getParser().addLiteralOption(Values[i].first, Values[i].second.first,
328 Values[i].second.second);
332 template<class DataType>
333 ValuesClass<DataType> values(const char *Arg, DataType Val, const char *Desc,
336 va_start(ValueArgs, Desc);
337 ValuesClass<DataType> Vals(Arg, Val, Desc, ValueArgs);
343 //===----------------------------------------------------------------------===//
344 // parser class - Parameterizable parser for different data types. By default,
345 // known data types (string, int, bool) have specialized parsers, that do what
346 // you would expect. The default parser, used for data types that are not
347 // built-in, uses a mapping table to map specific options to values, which is
348 // used, among other things, to handle enum types.
350 //--------------------------------------------------
351 // generic_parser_base - This class holds all the non-generic code that we do
352 // not need replicated for every instance of the generic parser. This also
353 // allows us to put stuff into CommandLine.cpp
355 struct generic_parser_base {
356 virtual ~generic_parser_base() {} // Base class should have virtual-dtor
358 // getNumOptions - Virtual function implemented by generic subclass to
359 // indicate how many entries are in Values.
361 virtual unsigned getNumOptions() const = 0;
363 // getOption - Return option name N.
364 virtual const char *getOption(unsigned N) const = 0;
366 // getDescription - Return description N
367 virtual const char *getDescription(unsigned N) const = 0;
369 // Return the width of the option tag for printing...
370 virtual unsigned getOptionWidth(const Option &O) const;
372 // printOptionInfo - Print out information about this option. The
373 // to-be-maintained width is specified.
375 virtual void printOptionInfo(const Option &O, unsigned GlobalWidth) const;
377 void initialize(Option &O) {
378 // All of the modifiers for the option have been processed by now, so the
379 // argstr field should be stable, copy it down now.
381 hasArgStr = O.hasArgStr();
383 // If there has been no argstr specified, that means that we need to add an
384 // argument for every possible option. This ensures that our options are
388 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
389 O.addArgument(getOption(i));
392 enum ValueExpected getValueExpectedFlagDefault() const {
393 // If there is an ArgStr specified, then we are of the form:
395 // -opt=O2 or -opt O2 or -optO2
397 // In which case, the value is required. Otherwise if an arg str has not
398 // been specified, we are of the form:
400 // -O2 or O2 or -la (where -l and -a are separate options)
402 // If this is the case, we cannot allow a value.
405 return ValueRequired;
407 return ValueDisallowed;
410 // findOption - Return the option number corresponding to the specified
411 // argument string. If the option is not found, getNumOptions() is returned.
413 unsigned findOption(const char *Name);
419 // Default parser implementation - This implementation depends on having a
420 // mapping of recognized options to values of some sort. In addition to this,
421 // each entry in the mapping also tracks a help message that is printed with the
422 // command line option for --help. Because this is a simple mapping parser, the
423 // data type can be any unsupported type.
425 template <class DataType>
426 class parser : public generic_parser_base {
428 std::vector<std::pair<const char *,
429 std::pair<DataType, const char *> > > Values;
431 typedef DataType parser_data_type;
433 // Implement virtual functions needed by generic_parser_base
434 unsigned getNumOptions() const { return Values.size(); }
435 const char *getOption(unsigned N) const { return Values[N].first; }
436 const char *getDescription(unsigned N) const {
437 return Values[N].second.second;
440 // parse - Return true on error.
441 bool parse(Option &O, const char *ArgName, const std::string &Arg,
449 for (unsigned i = 0, e = Values.size(); i != e; ++i)
450 if (ArgVal == Values[i].first) {
451 V = Values[i].second.first;
455 return O.error(": Cannot find option named '" + ArgVal + "'!");
458 // addLiteralOption - Add an entry to the mapping table...
460 void addLiteralOption(const char *Name, const DT &V, const char *HelpStr) {
461 assert(findOption(Name) == Values.size() && "Option already exists!");
462 Values.push_back(std::make_pair(Name,
463 std::make_pair(static_cast<DataType>(V),HelpStr)));
466 // removeLiteralOption - Remove the specified option.
468 void removeLiteralOption(const char *Name) {
469 unsigned N = findOption(Name);
470 assert(N != Values.size() && "Option not found!");
471 Values.erase(Values.begin()+N);
475 //--------------------------------------------------
476 // basic_parser - Super class of parsers to provide boilerplate code
478 struct basic_parser_impl { // non-template implementation of basic_parser<t>
479 virtual ~basic_parser_impl() {}
481 enum ValueExpected getValueExpectedFlagDefault() const {
482 return ValueRequired;
485 void initialize(Option &O) {}
487 // Return the width of the option tag for printing...
488 unsigned getOptionWidth(const Option &O) const;
490 // printOptionInfo - Print out information about this option. The
491 // to-be-maintained width is specified.
493 void printOptionInfo(const Option &O, unsigned GlobalWidth) const;
496 // getValueName - Overload in subclass to provide a better default value.
497 virtual const char *getValueName() const { return "value"; }
500 // basic_parser - The real basic parser is just a template wrapper that provides
501 // a typedef for the provided data type.
503 template<class DataType>
504 struct basic_parser : public basic_parser_impl {
505 typedef DataType parser_data_type;
509 //--------------------------------------------------
513 struct parser<bool> : public basic_parser<bool> {
515 // parse - Return true on error.
516 bool parse(Option &O, const char *ArgName, const std::string &Arg, bool &Val);
518 enum ValueExpected getValueExpectedFlagDefault() const {
519 return ValueOptional;
522 // getValueName - Do not print =<value> at all
523 virtual const char *getValueName() const { return 0; }
527 //--------------------------------------------------
531 struct parser<int> : public basic_parser<int> {
533 // parse - Return true on error.
534 bool parse(Option &O, const char *ArgName, const std::string &Arg, int &Val);
536 // getValueName - Overload in subclass to provide a better default value.
537 virtual const char *getValueName() const { return "int"; }
541 //--------------------------------------------------
545 struct parser<unsigned> : public basic_parser<unsigned> {
547 // parse - Return true on error.
548 bool parse(Option &O, const char *ArgName, const std::string &Arg,
551 // getValueName - Overload in subclass to provide a better default value.
552 virtual const char *getValueName() const { return "uint"; }
556 //--------------------------------------------------
560 struct parser<double> : public basic_parser<double> {
561 // parse - Return true on error.
562 bool parse(Option &O, const char *AN, const std::string &Arg, double &Val);
564 // getValueName - Overload in subclass to provide a better default value.
565 virtual const char *getValueName() const { return "number"; }
569 //--------------------------------------------------
573 struct parser<float> : public basic_parser<float> {
574 // parse - Return true on error.
575 bool parse(Option &O, const char *AN, const std::string &Arg, float &Val);
577 // getValueName - Overload in subclass to provide a better default value.
578 virtual const char *getValueName() const { return "number"; }
582 //--------------------------------------------------
583 // parser<std::string>
586 struct parser<std::string> : public basic_parser<std::string> {
587 // parse - Return true on error.
588 bool parse(Option &O, const char *ArgName, const std::string &Arg,
589 std::string &Value) {
594 // getValueName - Overload in subclass to provide a better default value.
595 virtual const char *getValueName() const { return "string"; }
600 //===----------------------------------------------------------------------===//
601 // applicator class - This class is used because we must use partial
602 // specialization to handle literal string arguments specially (const char* does
603 // not correctly respond to the apply method). Because the syntax to use this
604 // is a pain, we have the 'apply' method below to handle the nastiness...
606 template<class Mod> struct applicator {
608 static void opt(const Mod &M, Opt &O) { M.apply(O); }
611 // Handle const char* as a special case...
612 template<unsigned n> struct applicator<char[n]> {
614 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
616 template<unsigned n> struct applicator<const char[n]> {
618 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
620 template<> struct applicator<const char*> {
622 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
625 template<> struct applicator<NumOccurrences> {
626 static void opt(NumOccurrences NO, Option &O) { O.setNumOccurrencesFlag(NO); }
628 template<> struct applicator<ValueExpected> {
629 static void opt(ValueExpected VE, Option &O) { O.setValueExpectedFlag(VE); }
631 template<> struct applicator<OptionHidden> {
632 static void opt(OptionHidden OH, Option &O) { O.setHiddenFlag(OH); }
634 template<> struct applicator<FormattingFlags> {
635 static void opt(FormattingFlags FF, Option &O) { O.setFormattingFlag(FF); }
637 template<> struct applicator<MiscFlags> {
638 static void opt(MiscFlags MF, Option &O) { O.setMiscFlag(MF); }
641 // apply method - Apply a modifier to an option in a type safe way.
642 template<class Mod, class Opt>
643 void apply(const Mod &M, Opt *O) {
644 applicator<Mod>::opt(M, *O);
648 //===----------------------------------------------------------------------===//
651 // Default storage class definition: external storage. This implementation
652 // assumes the user will specify a variable to store the data into with the
653 // cl::location(x) modifier.
655 template<class DataType, bool ExternalStorage, bool isClass>
657 DataType *Location; // Where to store the object...
660 assert(Location != 0 && "cl::location(...) not specified for a command "
661 "line option with external storage, "
662 "or cl::init specified before cl::location()!!");
665 opt_storage() : Location(0) {}
667 bool setLocation(Option &O, DataType &L) {
669 return O.error(": cl::location(x) specified more than once!");
675 void setValue(const T &V) {
680 DataType &getValue() { check(); return *Location; }
681 const DataType &getValue() const { check(); return *Location; }
685 // Define how to hold a class type object, such as a string. Since we can
686 // inherit from a class, we do so. This makes us exactly compatible with the
687 // object in all cases that it is used.
689 template<class DataType>
690 struct opt_storage<DataType,false,true> : public DataType {
693 void setValue(const T &V) { DataType::operator=(V); }
695 DataType &getValue() { return *this; }
696 const DataType &getValue() const { return *this; }
699 // Define a partial specialization to handle things we cannot inherit from. In
700 // this case, we store an instance through containment, and overload operators
701 // to get at the value.
703 template<class DataType>
704 struct opt_storage<DataType, false, false> {
707 // Make sure we initialize the value with the default constructor for the
709 opt_storage() : Value(DataType()) {}
712 void setValue(const T &V) { Value = V; }
713 DataType &getValue() { return Value; }
714 DataType getValue() const { return Value; }
716 // If the datatype is a pointer, support -> on it.
717 DataType operator->() const { return Value; }
721 //===----------------------------------------------------------------------===//
722 // opt - A scalar command line option.
724 template <class DataType, bool ExternalStorage = false,
725 class ParserClass = parser<DataType> >
726 class opt : public Option,
727 public opt_storage<DataType, ExternalStorage,
728 is_class<DataType>::value> {
731 virtual bool handleOccurrence(const char *ArgName, const std::string &Arg) {
732 typename ParserClass::parser_data_type Val;
733 if (Parser.parse(*this, ArgName, Arg, Val))
734 return true; // Parse error!
739 virtual enum ValueExpected getValueExpectedFlagDefault() const {
740 return Parser.getValueExpectedFlagDefault();
743 // Forward printing stuff to the parser...
744 virtual unsigned getOptionWidth() const {return Parser.getOptionWidth(*this);}
745 virtual void printOptionInfo(unsigned GlobalWidth) const {
746 Parser.printOptionInfo(*this, GlobalWidth);
751 Parser.initialize(*this);
754 // setInitialValue - Used by the cl::init modifier...
755 void setInitialValue(const DataType &V) { this->setValue(V); }
757 ParserClass &getParser() { return Parser; }
759 operator DataType() const { return this->getValue(); }
762 DataType &operator=(const T &Val) {
764 return this->getValue();
775 template<class M0t, class M1t>
776 opt(const M0t &M0, const M1t &M1) {
777 apply(M0, this); apply(M1, this);
782 template<class M0t, class M1t, class M2t>
783 opt(const M0t &M0, const M1t &M1, const M2t &M2) {
784 apply(M0, this); apply(M1, this); apply(M2, this);
788 template<class M0t, class M1t, class M2t, class M3t>
789 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3) {
790 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
794 template<class M0t, class M1t, class M2t, class M3t, class M4t>
795 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
797 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
802 template<class M0t, class M1t, class M2t, class M3t,
803 class M4t, class M5t>
804 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
805 const M4t &M4, const M5t &M5) {
806 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
807 apply(M4, this); apply(M5, this);
811 template<class M0t, class M1t, class M2t, class M3t,
812 class M4t, class M5t, class M6t>
813 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
814 const M4t &M4, const M5t &M5, const M6t &M6) {
815 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
816 apply(M4, this); apply(M5, this); apply(M6, this);
820 template<class M0t, class M1t, class M2t, class M3t,
821 class M4t, class M5t, class M6t, class M7t>
822 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
823 const M4t &M4, const M5t &M5, const M6t &M6, const M7t &M7) {
824 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
825 apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
830 //===----------------------------------------------------------------------===//
831 // list_storage class
833 // Default storage class definition: external storage. This implementation
834 // assumes the user will specify a variable to store the data into with the
835 // cl::location(x) modifier.
837 template<class DataType, class StorageClass>
839 StorageClass *Location; // Where to store the object...
842 list_storage() : Location(0) {}
844 bool setLocation(Option &O, StorageClass &L) {
846 return O.error(": cl::location(x) specified more than once!");
852 void addValue(const T &V) {
853 assert(Location != 0 && "cl::location(...) not specified for a command "
854 "line option with external storage!");
855 Location->push_back(V);
860 // Define how to hold a class type object, such as a string. Since we can
861 // inherit from a class, we do so. This makes us exactly compatible with the
862 // object in all cases that it is used.
864 template<class DataType>
865 struct list_storage<DataType, bool> : public std::vector<DataType> {
868 void addValue(const T &V) { push_back(V); }
872 //===----------------------------------------------------------------------===//
873 // list - A list of command line options.
875 template <class DataType, class Storage = bool,
876 class ParserClass = parser<DataType> >
877 class list : public Option, public list_storage<DataType, Storage> {
880 virtual enum NumOccurrences getNumOccurrencesFlagDefault() const {
883 virtual enum ValueExpected getValueExpectedFlagDefault() const {
884 return Parser.getValueExpectedFlagDefault();
887 virtual bool handleOccurrence(const char *ArgName, const std::string &Arg) {
888 typename ParserClass::parser_data_type Val;
889 if (Parser.parse(*this, ArgName, Arg, Val))
890 return true; // Parse Error!
895 // Forward printing stuff to the parser...
896 virtual unsigned getOptionWidth() const {return Parser.getOptionWidth(*this);}
897 virtual void printOptionInfo(unsigned GlobalWidth) const {
898 Parser.printOptionInfo(*this, GlobalWidth);
903 Parser.initialize(*this);
906 ParserClass &getParser() { return Parser; }
910 list(const M0t &M0) {
915 template<class M0t, class M1t>
916 list(const M0t &M0, const M1t &M1) {
917 apply(M0, this); apply(M1, this);
921 template<class M0t, class M1t, class M2t>
922 list(const M0t &M0, const M1t &M1, const M2t &M2) {
923 apply(M0, this); apply(M1, this); apply(M2, this);
927 template<class M0t, class M1t, class M2t, class M3t>
928 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3) {
929 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
933 template<class M0t, class M1t, class M2t, class M3t, class M4t>
934 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
936 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
941 template<class M0t, class M1t, class M2t, class M3t,
942 class M4t, class M5t>
943 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
944 const M4t &M4, const M5t &M5) {
945 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
946 apply(M4, this); apply(M5, this);
950 template<class M0t, class M1t, class M2t, class M3t,
951 class M4t, class M5t, class M6t>
952 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
953 const M4t &M4, const M5t &M5, const M6t &M6) {
954 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
955 apply(M4, this); apply(M5, this); apply(M6, this);
959 template<class M0t, class M1t, class M2t, class M3t,
960 class M4t, class M5t, class M6t, class M7t>
961 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
962 const M4t &M4, const M5t &M5, const M6t &M6, const M7t &M7) {
963 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
964 apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
971 //===----------------------------------------------------------------------===//
972 // Aliased command line option (alias this name to a preexisting name)
975 class alias : public Option {
977 virtual bool handleOccurrence(const char *ArgName, const std::string &Arg) {
978 return AliasFor->handleOccurrence(AliasFor->ArgStr, Arg);
980 // Aliases default to be hidden...
981 virtual enum OptionHidden getOptionHiddenFlagDefault() const {return Hidden;}
983 // Handle printing stuff...
984 virtual unsigned getOptionWidth() const;
985 virtual void printOptionInfo(unsigned GlobalWidth) const;
989 error(": cl::alias must have argument name specified!");
991 error(": cl::alias must have an cl::aliasopt(option) specified!");
995 void setAliasFor(Option &O) {
997 error(": cl::alias must only have one cl::aliasopt(...) specified!");
1003 alias(const M0t &M0) : AliasFor(0) {
1008 template<class M0t, class M1t>
1009 alias(const M0t &M0, const M1t &M1) : AliasFor(0) {
1010 apply(M0, this); apply(M1, this);
1014 template<class M0t, class M1t, class M2t>
1015 alias(const M0t &M0, const M1t &M1, const M2t &M2) : AliasFor(0) {
1016 apply(M0, this); apply(M1, this); apply(M2, this);
1020 template<class M0t, class M1t, class M2t, class M3t>
1021 alias(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3)
1023 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1028 // aliasfor - Modifier to set the option an alias aliases.
1031 aliasopt(Option &O) : Opt(O) {}
1032 void apply(alias &A) const { A.setAliasFor(Opt); }
1035 } // End namespace cl
1037 } // End namespace llvm