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"
31 /// cl Namespace - This namespace contains all of the command line option
32 /// processing machinery. It is intentionally a short name to make qualified
36 //===----------------------------------------------------------------------===//
37 // ParseCommandLineOptions - Command line option processing entry point.
39 void ParseCommandLineOptions(int &argc, char **argv,
40 const char *Overview = 0);
42 //===----------------------------------------------------------------------===//
43 // ParseEnvironmentOptions - Environment variable option processing alternate
46 void ParseEnvironmentOptions(const char *progName, const char *envvar,
47 const char *Overview = 0);
49 //===----------------------------------------------------------------------===//
50 // Flags permitted to be passed to command line arguments
53 enum NumOccurrences { // Flags for the number of occurrences allowed
54 Optional = 0x01, // Zero or One occurrence
55 ZeroOrMore = 0x02, // Zero or more occurrences allowed
56 Required = 0x03, // One occurrence required
57 OneOrMore = 0x04, // One or more occurrences required
59 // ConsumeAfter - Indicates that this option is fed anything that follows the
60 // last positional argument required by the application (it is an error if
61 // there are zero positional arguments, and a ConsumeAfter option is used).
62 // Thus, for example, all arguments to LLI are processed until a filename is
63 // found. Once a filename is found, all of the succeeding arguments are
64 // passed, unprocessed, to the ConsumeAfter option.
68 OccurrencesMask = 0x07,
71 enum ValueExpected { // Is a value required for the option?
72 ValueOptional = 0x08, // The value can appear... or not
73 ValueRequired = 0x10, // The value is required to appear!
74 ValueDisallowed = 0x18, // A value may not be specified (for flags)
78 enum OptionHidden { // Control whether -help shows this option
79 NotHidden = 0x20, // Option included in --help & --help-hidden
80 Hidden = 0x40, // -help doesn't, but --help-hidden does
81 ReallyHidden = 0x60, // Neither --help nor --help-hidden show this arg
85 // Formatting flags - This controls special features that the option might have
86 // that cause it to be parsed differently...
88 // Prefix - This option allows arguments that are otherwise unrecognized to be
89 // matched by options that are a prefix of the actual value. This is useful for
90 // cases like a linker, where options are typically of the form '-lfoo' or
91 // '-L../../include' where -l or -L are the actual flags. When prefix is
92 // enabled, and used, the value for the flag comes from the suffix of the
95 // Grouping - With this option enabled, multiple letter options are allowed to
96 // bunch together with only a single hyphen for the whole group. This allows
97 // emulation of the behavior that ls uses for example: ls -la === ls -l -a
100 enum FormattingFlags {
101 NormalFormatting = 0x000, // Nothing special
102 Positional = 0x080, // Is a positional argument, no '-' required
103 Prefix = 0x100, // Can this option directly prefix its value?
104 Grouping = 0x180, // Can this option group with other options?
105 FormattingMask = 0x180,
108 enum MiscFlags { // Miscellaneous flags to adjust argument
109 CommaSeparated = 0x200, // Should this cl::list split between commas?
115 //===----------------------------------------------------------------------===//
120 friend void cl::ParseCommandLineOptions(int &, char **, const char *, int);
123 // handleOccurrences - Overriden by subclasses to handle the value passed into
124 // an argument. Should return true if there was an error processing the
125 // argument and the program should exit.
127 virtual bool handleOccurrence(const char *ArgName, const std::string &Arg) = 0;
129 virtual enum NumOccurrences getNumOccurrencesFlagDefault() const {
132 virtual enum ValueExpected getValueExpectedFlagDefault() const {
133 return ValueOptional;
135 virtual enum OptionHidden getOptionHiddenFlagDefault() const {
138 virtual enum FormattingFlags getFormattingFlagDefault() const {
139 return NormalFormatting;
142 int NumOccurrences; // The number of times specified
143 int Flags; // Flags for the argument
145 const char *ArgStr; // The argument string itself (ex: "help", "o")
146 const char *HelpStr; // The descriptive text message for --help
147 const char *ValueStr; // String describing what the value of this option is
149 inline enum NumOccurrences getNumOccurrencesFlag() const {
150 int NO = Flags & OccurrencesMask;
151 return NO ? static_cast<enum NumOccurrences>(NO)
152 : getNumOccurrencesFlagDefault();
154 inline enum ValueExpected getValueExpectedFlag() const {
155 int VE = Flags & ValueMask;
156 return VE ? static_cast<enum ValueExpected>(VE)
157 : getValueExpectedFlagDefault();
159 inline enum OptionHidden getOptionHiddenFlag() const {
160 int OH = Flags & HiddenMask;
161 return OH ? static_cast<enum OptionHidden>(OH)
162 : getOptionHiddenFlagDefault();
164 inline enum FormattingFlags getFormattingFlag() const {
165 int OH = Flags & FormattingMask;
166 return OH ? static_cast<enum FormattingFlags>(OH)
167 : getFormattingFlagDefault();
169 inline unsigned getMiscFlags() const {
170 return Flags & MiscMask;
173 // hasArgStr - Return true if the argstr != ""
174 bool hasArgStr() const { return ArgStr[0] != 0; }
176 //-------------------------------------------------------------------------===
177 // Accessor functions set by OptionModifiers
179 void setArgStr(const char *S) { ArgStr = S; }
180 void setDescription(const char *S) { HelpStr = S; }
181 void setValueStr(const char *S) { ValueStr = S; }
183 void setFlag(unsigned Flag, unsigned FlagMask) {
184 if (Flags & FlagMask) {
185 error(": Specified two settings for the same option!");
192 void setNumOccurrencesFlag(enum NumOccurrences Val) {
193 setFlag(Val, OccurrencesMask);
195 void setValueExpectedFlag(enum ValueExpected Val) { setFlag(Val, ValueMask); }
196 void setHiddenFlag(enum OptionHidden Val) { setFlag(Val, HiddenMask); }
197 void setFormattingFlag(enum FormattingFlags V) { setFlag(V, FormattingMask); }
198 void setMiscFlag(enum MiscFlags M) { setFlag(M, M); }
200 Option() : NumOccurrences(0), Flags(0),
201 ArgStr(""), HelpStr(""), ValueStr("") {}
204 // addArgument - Tell the system that this Option subclass will handle all
205 // occurrences of -ArgStr on the command line.
207 void addArgument(const char *ArgStr);
208 void removeArgument(const char *ArgStr);
210 // Return the width of the option tag for printing...
211 virtual unsigned getOptionWidth() const = 0;
213 // printOptionInfo - Print out information about this option. The
214 // to-be-maintained width is specified.
216 virtual void printOptionInfo(unsigned GlobalWidth) const = 0;
218 // addOccurrence - Wrapper around handleOccurrence that enforces Flags
220 bool addOccurrence(const char *ArgName, const std::string &Value);
222 // Prints option name followed by message. Always returns true.
223 bool error(std::string Message, const char *ArgName = 0);
226 inline int getNumOccurrences() const { return NumOccurrences; }
231 //===----------------------------------------------------------------------===//
232 // Command line option modifiers that can be used to modify the behavior of
233 // command line option parsers...
236 // desc - Modifier to set the description shown in the --help output...
239 desc(const char *Str) : Desc(Str) {}
240 void apply(Option &O) const { O.setDescription(Desc); }
243 // value_desc - Modifier to set the value description shown in the --help
247 value_desc(const char *Str) : Desc(Str) {}
248 void apply(Option &O) const { O.setValueStr(Desc); }
252 // init - Specify a default (initial) value for the command line argument, if
253 // the default constructor for the argument type does not give you what you
254 // want. This is only valid on "opt" arguments, not on "list" arguments.
259 initializer(const Ty &Val) : Init(Val) {}
262 void apply(Opt &O) const { O.setInitialValue(Init); }
266 initializer<Ty> init(const Ty &Val) {
267 return initializer<Ty>(Val);
271 // location - Allow the user to specify which external variable they want to
272 // store the results of the command line argument processing into, if they don't
273 // want to store it in the option itself.
276 struct LocationClass {
278 LocationClass(Ty &L) : Loc(L) {}
281 void apply(Opt &O) const { O.setLocation(O, Loc); }
285 LocationClass<Ty> location(Ty &L) { return LocationClass<Ty>(L); }
288 //===----------------------------------------------------------------------===//
289 // Enum valued command line option
291 #define clEnumVal(ENUMVAL, DESC) #ENUMVAL, (int)ENUMVAL, DESC
292 #define clEnumValN(ENUMVAL, FLAGNAME, DESC) FLAGNAME, (int)ENUMVAL, DESC
294 // values - For custom data types, allow specifying a group of values together
295 // as the values that go into the mapping that the option handler uses. Note
296 // that the values list must always have a 0 at the end of the list to indicate
297 // that the list has ended.
299 template<class DataType>
301 // Use a vector instead of a map, because the lists should be short,
302 // the overhead is less, and most importantly, it keeps them in the order
303 // inserted so we can print our option out nicely.
304 std::vector<std::pair<const char *, std::pair<int, const char *> > > Values;
305 void processValues(va_list Vals);
307 ValuesClass(const char *EnumName, DataType Val, const char *Desc,
309 // Insert the first value, which is required.
310 Values.push_back(std::make_pair(EnumName, std::make_pair(Val, Desc)));
312 // Process the varargs portion of the values...
313 while (const char *EnumName = va_arg(ValueArgs, const char *)) {
314 DataType EnumVal = static_cast<DataType>(va_arg(ValueArgs, int));
315 const char *EnumDesc = va_arg(ValueArgs, const char *);
316 Values.push_back(std::make_pair(EnumName, // Add value to value map
317 std::make_pair(EnumVal, EnumDesc)));
322 void apply(Opt &O) const {
323 for (unsigned i = 0, e = Values.size(); i != e; ++i)
324 O.getParser().addLiteralOption(Values[i].first, Values[i].second.first,
325 Values[i].second.second);
329 template<class DataType>
330 ValuesClass<DataType> values(const char *Arg, DataType Val, const char *Desc,
333 va_start(ValueArgs, Desc);
334 ValuesClass<DataType> Vals(Arg, Val, Desc, ValueArgs);
340 //===----------------------------------------------------------------------===//
341 // parser class - Parameterizable parser for different data types. By default,
342 // known data types (string, int, bool) have specialized parsers, that do what
343 // you would expect. The default parser, used for data types that are not
344 // built-in, uses a mapping table to map specific options to values, which is
345 // used, among other things, to handle enum types.
347 //--------------------------------------------------
348 // generic_parser_base - This class holds all the non-generic code that we do
349 // not need replicated for every instance of the generic parser. This also
350 // allows us to put stuff into CommandLine.cpp
352 struct generic_parser_base {
353 virtual ~generic_parser_base() {} // Base class should have virtual-dtor
355 // getNumOptions - Virtual function implemented by generic subclass to
356 // indicate how many entries are in Values.
358 virtual unsigned getNumOptions() const = 0;
360 // getOption - Return option name N.
361 virtual const char *getOption(unsigned N) const = 0;
363 // getDescription - Return description N
364 virtual const char *getDescription(unsigned N) const = 0;
366 // Return the width of the option tag for printing...
367 virtual unsigned getOptionWidth(const Option &O) const;
369 // printOptionInfo - Print out information about this option. The
370 // to-be-maintained width is specified.
372 virtual void printOptionInfo(const Option &O, unsigned GlobalWidth) const;
374 void initialize(Option &O) {
375 // All of the modifiers for the option have been processed by now, so the
376 // argstr field should be stable, copy it down now.
378 hasArgStr = O.hasArgStr();
380 // If there has been no argstr specified, that means that we need to add an
381 // argument for every possible option. This ensures that our options are
385 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
386 O.addArgument(getOption(i));
389 enum ValueExpected getValueExpectedFlagDefault() const {
390 // If there is an ArgStr specified, then we are of the form:
392 // -opt=O2 or -opt O2 or -optO2
394 // In which case, the value is required. Otherwise if an arg str has not
395 // been specified, we are of the form:
397 // -O2 or O2 or -la (where -l and -a are separate options)
399 // If this is the case, we cannot allow a value.
402 return ValueRequired;
404 return ValueDisallowed;
407 // findOption - Return the option number corresponding to the specified
408 // argument string. If the option is not found, getNumOptions() is returned.
410 unsigned findOption(const char *Name);
416 // Default parser implementation - This implementation depends on having a
417 // mapping of recognized options to values of some sort. In addition to this,
418 // each entry in the mapping also tracks a help message that is printed with the
419 // command line option for --help. Because this is a simple mapping parser, the
420 // data type can be any unsupported type.
422 template <class DataType>
423 class parser : public generic_parser_base {
425 std::vector<std::pair<const char *,
426 std::pair<DataType, const char *> > > Values;
428 typedef DataType parser_data_type;
430 // Implement virtual functions needed by generic_parser_base
431 unsigned getNumOptions() const { return Values.size(); }
432 const char *getOption(unsigned N) const { return Values[N].first; }
433 const char *getDescription(unsigned N) const {
434 return Values[N].second.second;
437 // parse - Return true on error.
438 bool parse(Option &O, const char *ArgName, const std::string &Arg,
446 for (unsigned i = 0, e = Values.size(); i != e; ++i)
447 if (ArgVal == Values[i].first) {
448 V = Values[i].second.first;
452 return O.error(": Cannot find option named '" + ArgVal + "'!");
455 // addLiteralOption - Add an entry to the mapping table...
457 void addLiteralOption(const char *Name, const DT &V, const char *HelpStr) {
458 assert(findOption(Name) == Values.size() && "Option already exists!");
459 Values.push_back(std::make_pair(Name,
460 std::make_pair(static_cast<DataType>(V),HelpStr)));
463 // removeLiteralOption - Remove the specified option.
465 void removeLiteralOption(const char *Name) {
466 unsigned N = findOption(Name);
467 assert(N != Values.size() && "Option not found!");
468 Values.erase(Values.begin()+N);
472 //--------------------------------------------------
473 // basic_parser - Super class of parsers to provide boilerplate code
475 struct basic_parser_impl { // non-template implementation of basic_parser<t>
476 virtual ~basic_parser_impl() {}
478 enum ValueExpected getValueExpectedFlagDefault() const {
479 return ValueRequired;
482 void initialize(Option &O) {}
484 // Return the width of the option tag for printing...
485 unsigned getOptionWidth(const Option &O) const;
487 // printOptionInfo - Print out information about this option. The
488 // to-be-maintained width is specified.
490 void printOptionInfo(const Option &O, unsigned GlobalWidth) const;
493 // getValueName - Overload in subclass to provide a better default value.
494 virtual const char *getValueName() const { return "value"; }
497 // basic_parser - The real basic parser is just a template wrapper that provides
498 // a typedef for the provided data type.
500 template<class DataType>
501 struct basic_parser : public basic_parser_impl {
502 typedef DataType parser_data_type;
506 //--------------------------------------------------
510 struct parser<bool> : public basic_parser<bool> {
512 // parse - Return true on error.
513 bool parse(Option &O, const char *ArgName, const std::string &Arg, bool &Val);
515 enum ValueExpected getValueExpectedFlagDefault() const {
516 return ValueOptional;
519 // getValueName - Do not print =<value> at all
520 virtual const char *getValueName() const { return 0; }
524 //--------------------------------------------------
528 struct parser<int> : public basic_parser<int> {
530 // parse - Return true on error.
531 bool parse(Option &O, const char *ArgName, const std::string &Arg, int &Val);
533 // getValueName - Overload in subclass to provide a better default value.
534 virtual const char *getValueName() const { return "int"; }
538 //--------------------------------------------------
542 struct parser<unsigned> : public basic_parser<unsigned> {
544 // parse - Return true on error.
545 bool parse(Option &O, const char *ArgName, const std::string &Arg,
548 // getValueName - Overload in subclass to provide a better default value.
549 virtual const char *getValueName() const { return "uint"; }
553 //--------------------------------------------------
557 struct parser<double> : public basic_parser<double> {
558 // parse - Return true on error.
559 bool parse(Option &O, const char *AN, const std::string &Arg, double &Val);
561 // getValueName - Overload in subclass to provide a better default value.
562 virtual const char *getValueName() const { return "number"; }
566 //--------------------------------------------------
570 struct parser<float> : public basic_parser<float> {
571 // parse - Return true on error.
572 bool parse(Option &O, const char *AN, const std::string &Arg, float &Val);
574 // getValueName - Overload in subclass to provide a better default value.
575 virtual const char *getValueName() const { return "number"; }
579 //--------------------------------------------------
580 // parser<std::string>
583 struct parser<std::string> : public basic_parser<std::string> {
584 // parse - Return true on error.
585 bool parse(Option &O, const char *ArgName, const std::string &Arg,
586 std::string &Value) {
591 // getValueName - Overload in subclass to provide a better default value.
592 virtual const char *getValueName() const { return "string"; }
597 //===----------------------------------------------------------------------===//
598 // applicator class - This class is used because we must use partial
599 // specialization to handle literal string arguments specially (const char* does
600 // not correctly respond to the apply method). Because the syntax to use this
601 // is a pain, we have the 'apply' method below to handle the nastiness...
603 template<class Mod> struct applicator {
605 static void opt(const Mod &M, Opt &O) { M.apply(O); }
608 // Handle const char* as a special case...
609 template<unsigned n> struct applicator<char[n]> {
611 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
613 template<unsigned n> struct applicator<const char[n]> {
615 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
617 template<> struct applicator<const char*> {
619 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
622 template<> struct applicator<NumOccurrences> {
623 static void opt(NumOccurrences NO, Option &O) { O.setNumOccurrencesFlag(NO); }
625 template<> struct applicator<ValueExpected> {
626 static void opt(ValueExpected VE, Option &O) { O.setValueExpectedFlag(VE); }
628 template<> struct applicator<OptionHidden> {
629 static void opt(OptionHidden OH, Option &O) { O.setHiddenFlag(OH); }
631 template<> struct applicator<FormattingFlags> {
632 static void opt(FormattingFlags FF, Option &O) { O.setFormattingFlag(FF); }
634 template<> struct applicator<MiscFlags> {
635 static void opt(MiscFlags MF, Option &O) { O.setMiscFlag(MF); }
638 // apply method - Apply a modifier to an option in a type safe way.
639 template<class Mod, class Opt>
640 void apply(const Mod &M, Opt *O) {
641 applicator<Mod>::opt(M, *O);
645 //===----------------------------------------------------------------------===//
648 // Default storage class definition: external storage. This implementation
649 // assumes the user will specify a variable to store the data into with the
650 // cl::location(x) modifier.
652 template<class DataType, bool ExternalStorage, bool isClass>
654 DataType *Location; // Where to store the object...
657 assert(Location != 0 && "cl::location(...) not specified for a command "
658 "line option with external storage, "
659 "or cl::init specified before cl::location()!!");
662 opt_storage() : Location(0) {}
664 bool setLocation(Option &O, DataType &L) {
666 return O.error(": cl::location(x) specified more than once!");
672 void setValue(const T &V) {
677 DataType &getValue() { check(); return *Location; }
678 const DataType &getValue() const { check(); return *Location; }
682 // Define how to hold a class type object, such as a string. Since we can
683 // inherit from a class, we do so. This makes us exactly compatible with the
684 // object in all cases that it is used.
686 template<class DataType>
687 struct opt_storage<DataType,false,true> : public DataType {
690 void setValue(const T &V) { DataType::operator=(V); }
692 DataType &getValue() { return *this; }
693 const DataType &getValue() const { return *this; }
696 // Define a partial specialization to handle things we cannot inherit from. In
697 // this case, we store an instance through containment, and overload operators
698 // to get at the value.
700 template<class DataType>
701 struct opt_storage<DataType, false, false> {
704 // Make sure we initialize the value with the default constructor for the
706 opt_storage() : Value(DataType()) {}
709 void setValue(const T &V) { Value = V; }
710 DataType &getValue() { return Value; }
711 DataType getValue() const { return Value; }
715 //===----------------------------------------------------------------------===//
716 // opt - A scalar command line option.
718 template <class DataType, bool ExternalStorage = false,
719 class ParserClass = parser<DataType> >
720 class opt : public Option,
721 public opt_storage<DataType, ExternalStorage,
722 ::boost::is_class<DataType>::value> {
725 virtual bool handleOccurrence(const char *ArgName, const std::string &Arg) {
726 typename ParserClass::parser_data_type Val;
727 if (Parser.parse(*this, ArgName, Arg, Val))
728 return true; // Parse error!
733 virtual enum ValueExpected getValueExpectedFlagDefault() const {
734 return Parser.getValueExpectedFlagDefault();
737 // Forward printing stuff to the parser...
738 virtual unsigned getOptionWidth() const {return Parser.getOptionWidth(*this);}
739 virtual void printOptionInfo(unsigned GlobalWidth) const {
740 Parser.printOptionInfo(*this, GlobalWidth);
745 Parser.initialize(*this);
748 // setInitialValue - Used by the cl::init modifier...
749 void setInitialValue(const DataType &V) { this->setValue(V); }
751 ParserClass &getParser() { return Parser; }
753 operator DataType() const { return this->getValue(); }
756 DataType &operator=(const T &Val) {
758 return this->getValue();
769 template<class M0t, class M1t>
770 opt(const M0t &M0, const M1t &M1) {
771 apply(M0, this); apply(M1, this);
776 template<class M0t, class M1t, class M2t>
777 opt(const M0t &M0, const M1t &M1, const M2t &M2) {
778 apply(M0, this); apply(M1, this); apply(M2, this);
782 template<class M0t, class M1t, class M2t, class M3t>
783 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3) {
784 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
788 template<class M0t, class M1t, class M2t, class M3t, class M4t>
789 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
791 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
796 template<class M0t, class M1t, class M2t, class M3t,
797 class M4t, class M5t>
798 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
799 const M4t &M4, const M5t &M5) {
800 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
801 apply(M4, this); apply(M5, this);
805 template<class M0t, class M1t, class M2t, class M3t,
806 class M4t, class M5t, class M6t>
807 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
808 const M4t &M4, const M5t &M5, const M6t &M6) {
809 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
810 apply(M4, this); apply(M5, this); apply(M6, this);
814 template<class M0t, class M1t, class M2t, class M3t,
815 class M4t, class M5t, class M6t, class M7t>
816 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
817 const M4t &M4, const M5t &M5, const M6t &M6, const M7t &M7) {
818 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
819 apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
824 //===----------------------------------------------------------------------===//
825 // list_storage class
827 // Default storage class definition: external storage. This implementation
828 // assumes the user will specify a variable to store the data into with the
829 // cl::location(x) modifier.
831 template<class DataType, class StorageClass>
833 StorageClass *Location; // Where to store the object...
836 list_storage() : Location(0) {}
838 bool setLocation(Option &O, StorageClass &L) {
840 return O.error(": cl::location(x) specified more than once!");
846 void addValue(const T &V) {
847 assert(Location != 0 && "cl::location(...) not specified for a command "
848 "line option with external storage!");
849 Location->push_back(V);
854 // Define how to hold a class type object, such as a string. Since we can
855 // inherit from a class, we do so. This makes us exactly compatible with the
856 // object in all cases that it is used.
858 template<class DataType>
859 struct list_storage<DataType, bool> : public std::vector<DataType> {
862 void addValue(const T &V) { push_back(V); }
866 //===----------------------------------------------------------------------===//
867 // list - A list of command line options.
869 template <class DataType, class Storage = bool,
870 class ParserClass = parser<DataType> >
871 class list : public Option, public list_storage<DataType, Storage> {
874 virtual enum NumOccurrences getNumOccurrencesFlagDefault() const {
877 virtual enum ValueExpected getValueExpectedFlagDefault() const {
878 return Parser.getValueExpectedFlagDefault();
881 virtual bool handleOccurrence(const char *ArgName, const std::string &Arg) {
882 typename ParserClass::parser_data_type Val;
883 if (Parser.parse(*this, ArgName, Arg, Val))
884 return true; // Parse Error!
889 // Forward printing stuff to the parser...
890 virtual unsigned getOptionWidth() const {return Parser.getOptionWidth(*this);}
891 virtual void printOptionInfo(unsigned GlobalWidth) const {
892 Parser.printOptionInfo(*this, GlobalWidth);
897 Parser.initialize(*this);
900 ParserClass &getParser() { return Parser; }
904 list(const M0t &M0) {
909 template<class M0t, class M1t>
910 list(const M0t &M0, const M1t &M1) {
911 apply(M0, this); apply(M1, this);
915 template<class M0t, class M1t, class M2t>
916 list(const M0t &M0, const M1t &M1, const M2t &M2) {
917 apply(M0, this); apply(M1, this); apply(M2, this);
921 template<class M0t, class M1t, class M2t, class M3t>
922 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3) {
923 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
927 template<class M0t, class M1t, class M2t, class M3t, class M4t>
928 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
930 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
935 template<class M0t, class M1t, class M2t, class M3t,
936 class M4t, class M5t>
937 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
938 const M4t &M4, const M5t &M5) {
939 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
940 apply(M4, this); apply(M5, this);
944 template<class M0t, class M1t, class M2t, class M3t,
945 class M4t, class M5t, class M6t>
946 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
947 const M4t &M4, const M5t &M5, const M6t &M6) {
948 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
949 apply(M4, this); apply(M5, this); apply(M6, this);
953 template<class M0t, class M1t, class M2t, class M3t,
954 class M4t, class M5t, class M6t, class M7t>
955 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
956 const M4t &M4, const M5t &M5, const M6t &M6, const M7t &M7) {
957 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
958 apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
965 //===----------------------------------------------------------------------===//
966 // Aliased command line option (alias this name to a preexisting name)
969 class alias : public Option {
971 virtual bool handleOccurrence(const char *ArgName, const std::string &Arg) {
972 return AliasFor->handleOccurrence(AliasFor->ArgStr, Arg);
974 // Aliases default to be hidden...
975 virtual enum OptionHidden getOptionHiddenFlagDefault() const {return Hidden;}
977 // Handle printing stuff...
978 virtual unsigned getOptionWidth() const;
979 virtual void printOptionInfo(unsigned GlobalWidth) const;
983 error(": cl::alias must have argument name specified!");
985 error(": cl::alias must have an cl::aliasopt(option) specified!");
989 void setAliasFor(Option &O) {
991 error(": cl::alias must only have one cl::aliasopt(...) specified!");
997 alias(const M0t &M0) : AliasFor(0) {
1002 template<class M0t, class M1t>
1003 alias(const M0t &M0, const M1t &M1) : AliasFor(0) {
1004 apply(M0, this); apply(M1, this);
1008 template<class M0t, class M1t, class M2t>
1009 alias(const M0t &M0, const M1t &M1, const M2t &M2) : AliasFor(0) {
1010 apply(M0, this); apply(M1, this); apply(M2, this);
1014 template<class M0t, class M1t, class M2t, class M3t>
1015 alias(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3)
1017 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1022 // aliasfor - Modifier to set the option an alias aliases.
1025 aliasopt(Option &O) : Opt(O) {}
1026 void apply(alias &A) const { A.setAliasFor(Opt); }
1029 } // End namespace cl
1031 } // End namespace llvm