728bb1467835f71e9a886e6f92e4dc27b161e035
[oota-llvm.git] / include / llvm / Support / CommandLine.h
1 //===- Support/CommandLine.h - Flexible Command line parser ------*- C++ -*--=//
2 //
3 // This class implements a command line argument processor that is useful when
4 // creating a tool.  It provides a simple, minimalistic interface that is easily
5 // extensible and supports nonlocal (library) command line options.
6 //
7 // Note that rather than trying to figure out what this code does, you should
8 // read the library documentation located in docs/CommandLine.html or looks at
9 // the many example usages in tools/*/*.cpp
10 //
11 //===----------------------------------------------------------------------===//
12
13 #ifndef SUPPORT_COMMANDLINE_H
14 #define SUPPORT_COMMANDLINE_H
15
16 #include <string>
17 #include <vector>
18 #include <utility>
19 #include <cstdarg>
20 #include "boost/type_traits/object_traits.hpp"
21
22 #include <assert.h>
23
24 /// cl Namespace - This namespace contains all of the command line option
25 /// processing machinery.  It is intentionally a short name to make qualified
26 /// usage concise.
27 namespace cl {
28
29 //===----------------------------------------------------------------------===//
30 // ParseCommandLineOptions - Command line option processing entry point.
31 //
32 void cl::ParseCommandLineOptions(int &argc, char **argv,
33                                  const char *Overview = 0);
34
35 //===----------------------------------------------------------------------===//
36 // Flags permitted to be passed to command line arguments
37 //
38
39 enum NumOccurances {           // Flags for the number of occurances allowed...
40   Optional        = 0x01,      // Zero or One occurance
41   ZeroOrMore      = 0x02,      // Zero or more occurances allowed
42   Required        = 0x03,      // One occurance required
43   OneOrMore       = 0x04,      // One or more occurances required
44
45   // ConsumeAfter - Indicates that this option is fed anything that follows the
46   // last positional argument required by the application (it is an error if
47   // there are zero positional arguments, and a ConsumeAfter option is used).
48   // Thus, for example, all arguments to LLI are processed until a filename is
49   // found.  Once a filename is found, all of the succeeding arguments are
50   // passed, unprocessed, to the ConsumeAfter option.
51   //
52   ConsumeAfter    = 0x05,
53
54   OccurancesMask  = 0x07,
55 };
56
57 enum ValueExpected {           // Is a value required for the option?
58   ValueOptional   = 0x08,      // The value can appear... or not
59   ValueRequired   = 0x10,      // The value is required to appear!
60   ValueDisallowed = 0x18,      // A value may not be specified (for flags)
61   ValueMask       = 0x18,
62 };
63
64 enum OptionHidden {            // Control whether -help shows this option
65   NotHidden       = 0x20,      // Option included in --help & --help-hidden
66   Hidden          = 0x40,      // -help doesn't, but --help-hidden does
67   ReallyHidden    = 0x60,      // Neither --help nor --help-hidden show this arg
68   HiddenMask      = 0x60,
69 };
70
71 // Formatting flags - This controls special features that the option might have
72 // that cause it to be parsed differently...
73 //
74 // Prefix - This option allows arguments that are otherwise unrecognized to be
75 // matched by options that are a prefix of the actual value.  This is useful for
76 // cases like a linker, where options are typically of the form '-lfoo' or
77 // '-L../../include' where -l or -L are the actual flags.  When prefix is
78 // enabled, and used, the value for the flag comes from the suffix of the
79 // argument.
80 //
81 // Grouping - With this option enabled, multiple letter options are allowed to
82 // bunch together with only a single hyphen for the whole group.  This allows
83 // emulation of the behavior that ls uses for example: ls -la === ls -l -a
84 //
85
86 enum FormattingFlags {
87   NormalFormatting = 0x000,     // Nothing special
88   Positional       = 0x080,     // Is a positional argument, no '-' required
89   Prefix           = 0x100,     // Can this option directly prefix its value?
90   Grouping         = 0x180,     // Can this option group with other options?
91   FormattingMask   = 0x180,
92 };
93
94 enum MiscFlags {                // Miscellaneous flags to adjust argument
95   CommaSeparated   = 0x200,     // Should this cl::list split between commas?
96   MiscMask         = 0x200,
97 };
98
99
100
101 //===----------------------------------------------------------------------===//
102 // Option Base class
103 //
104 class alias;
105 class Option {
106   friend void cl::ParseCommandLineOptions(int &, char **, const char *, int);
107   friend class alias;
108
109   // handleOccurances - Overriden by subclasses to handle the value passed into
110   // an argument.  Should return true if there was an error processing the
111   // argument and the program should exit.
112   //
113   virtual bool handleOccurance(const char *ArgName, const std::string &Arg) = 0;
114
115   virtual enum NumOccurances getNumOccurancesFlagDefault() const { 
116     return Optional;
117   }
118   virtual enum ValueExpected getValueExpectedFlagDefault() const {
119     return ValueOptional; 
120   }
121   virtual enum OptionHidden getOptionHiddenFlagDefault() const {
122     return NotHidden;
123   }
124   virtual enum FormattingFlags getFormattingFlagDefault() const {
125     return NormalFormatting;
126   }
127
128   int NumOccurances;    // The number of times specified
129   int Flags;            // Flags for the argument
130 public:
131   const char *ArgStr;   // The argument string itself (ex: "help", "o")
132   const char *HelpStr;  // The descriptive text message for --help
133   const char *ValueStr; // String describing what the value of this option is
134
135   inline enum NumOccurances getNumOccurancesFlag() const {
136     int NO = Flags & OccurancesMask;
137     return NO ? (enum NumOccurances)NO : getNumOccurancesFlagDefault();
138   }
139   inline enum ValueExpected getValueExpectedFlag() const {
140     int VE = Flags & ValueMask;
141     return VE ? (enum ValueExpected)VE : getValueExpectedFlagDefault();
142   }
143   inline enum OptionHidden getOptionHiddenFlag() const {
144     int OH = Flags & HiddenMask;
145     return OH ? (enum OptionHidden)OH : getOptionHiddenFlagDefault();
146   }
147   inline enum FormattingFlags getFormattingFlag() const {
148     int OH = Flags & FormattingMask;
149     return OH ? (enum FormattingFlags)OH : getFormattingFlagDefault();
150   }
151   inline unsigned getMiscFlags() const {
152     return Flags & MiscMask;
153   }
154
155   // hasArgStr - Return true if the argstr != ""
156   bool hasArgStr() const { return ArgStr[0] != 0; }
157
158   //-------------------------------------------------------------------------===
159   // Accessor functions set by OptionModifiers
160   //
161   void setArgStr(const char *S) { ArgStr = S; }
162   void setDescription(const char *S) { HelpStr = S; }
163   void setValueStr(const char *S) { ValueStr = S; }
164
165   void setFlag(unsigned Flag, unsigned FlagMask) {
166     if (Flags & FlagMask) {
167       error(": Specified two settings for the same option!");
168       exit(1);
169     }
170
171     Flags |= Flag;
172   }
173
174   void setNumOccurancesFlag(enum NumOccurances Val) {
175     setFlag(Val, OccurancesMask);
176   }
177   void setValueExpectedFlag(enum ValueExpected Val) { setFlag(Val, ValueMask); }
178   void setHiddenFlag(enum OptionHidden Val) { setFlag(Val, HiddenMask); }
179   void setFormattingFlag(enum FormattingFlags V) { setFlag(V, FormattingMask); }
180   void setMiscFlag(enum MiscFlags M) { setFlag(M, M); }
181 protected:
182   Option() : NumOccurances(0), Flags(0),
183              ArgStr(""), HelpStr(""), ValueStr("") {}
184
185 public:
186   // addArgument - Tell the system that this Option subclass will handle all
187   // occurances of -ArgStr on the command line.
188   //
189   void addArgument(const char *ArgStr);
190   void removeArgument(const char *ArgStr);
191
192   // Return the width of the option tag for printing...
193   virtual unsigned getOptionWidth() const = 0;
194
195   // printOptionInfo - Print out information about this option.  The 
196   // to-be-maintained width is specified.
197   //
198   virtual void printOptionInfo(unsigned GlobalWidth) const = 0;
199
200   // addOccurance - Wrapper around handleOccurance that enforces Flags
201   //
202   bool addOccurance(const char *ArgName, const std::string &Value);
203
204   // Prints option name followed by message.  Always returns true.
205   bool error(std::string Message, const char *ArgName = 0);
206
207 public:
208   inline int getNumOccurances() const { return NumOccurances; }
209   virtual ~Option() {}
210 };
211
212
213 //===----------------------------------------------------------------------===//
214 // Command line option modifiers that can be used to modify the behavior of
215 // command line option parsers...
216 //
217
218 // desc - Modifier to set the description shown in the --help output...
219 struct desc {
220   const char *Desc;
221   desc(const char *Str) : Desc(Str) {}
222   void apply(Option &O) const { O.setDescription(Desc); }
223 };
224
225 // value_desc - Modifier to set the value description shown in the --help
226 // output...
227 struct value_desc {
228   const char *Desc;
229   value_desc(const char *Str) : Desc(Str) {}
230   void apply(Option &O) const { O.setValueStr(Desc); }
231 };
232
233
234 // init - Specify a default (initial) value for the command line argument, if
235 // the default constructor for the argument type does not give you what you
236 // want.  This is only valid on "opt" arguments, not on "list" arguments.
237 //
238 template<class Ty>
239 struct initializer {
240   const Ty &Init;
241   initializer(const Ty &Val) : Init(Val) {}
242
243   template<class Opt>
244   void apply(Opt &O) const { O.setInitialValue(Init); }
245 };
246
247 template<class Ty>
248 initializer<Ty> init(const Ty &Val) {
249   return initializer<Ty>(Val);
250 }
251
252
253 // location - Allow the user to specify which external variable they want to
254 // store the results of the command line argument processing into, if they don't
255 // want to store it in the option itself.
256 //
257 template<class Ty>
258 struct LocationClass {
259   Ty &Loc;
260   LocationClass(Ty &L) : Loc(L) {}
261
262   template<class Opt>
263   void apply(Opt &O) const { O.setLocation(O, Loc); }
264 };
265
266 template<class Ty>
267 LocationClass<Ty> location(Ty &L) { return LocationClass<Ty>(L); }
268
269
270 //===----------------------------------------------------------------------===//
271 // Enum valued command line option
272 //
273 #define clEnumVal(ENUMVAL, DESC) #ENUMVAL, (int)ENUMVAL, DESC
274 #define clEnumValN(ENUMVAL, FLAGNAME, DESC) FLAGNAME, (int)ENUMVAL, DESC
275
276 // values - For custom data types, allow specifying a group of values together
277 // as the values that go into the mapping that the option handler uses.  Note
278 // that the values list must always have a 0 at the end of the list to indicate
279 // that the list has ended.
280 //
281 template<class DataType>
282 class ValuesClass {
283   // Use a vector instead of a map, because the lists should be short,
284   // the overhead is less, and most importantly, it keeps them in the order
285   // inserted so we can print our option out nicely.
286   std::vector<std::pair<const char *, std::pair<int, const char *> > > Values;
287   void processValues(va_list Vals);
288 public:
289   ValuesClass(const char *EnumName, DataType Val, const char *Desc, 
290               va_list ValueArgs) {
291     // Insert the first value, which is required.
292     Values.push_back(std::make_pair(EnumName, std::make_pair(Val, Desc)));
293
294     // Process the varargs portion of the values...
295     while (const char *EnumName = va_arg(ValueArgs, const char *)) {
296       DataType EnumVal = (DataType)va_arg(ValueArgs, int);
297       const char *EnumDesc = va_arg(ValueArgs, const char *);
298       Values.push_back(std::make_pair(EnumName,      // Add value to value map
299                                       std::make_pair(EnumVal, EnumDesc)));
300     }
301   }
302
303   template<class Opt>
304   void apply(Opt &O) const {
305     for (unsigned i = 0, e = Values.size(); i != e; ++i)
306       O.getParser().addLiteralOption(Values[i].first, Values[i].second.first,
307                                      Values[i].second.second);
308   }
309 };
310
311 template<class DataType>
312 ValuesClass<DataType> values(const char *Arg, DataType Val, const char *Desc,
313                              ...) {
314     va_list ValueArgs;
315     va_start(ValueArgs, Desc);
316     ValuesClass<DataType> Vals(Arg, Val, Desc, ValueArgs);
317     va_end(ValueArgs);
318     return Vals;
319 }
320
321
322 //===----------------------------------------------------------------------===//
323 // parser class - Parameterizable parser for different data types.  By default,
324 // known data types (string, int, bool) have specialized parsers, that do what
325 // you would expect.  The default parser, used for data types that are not
326 // built-in, uses a mapping table to map specific options to values, which is
327 // used, among other things, to handle enum types.
328
329 //--------------------------------------------------
330 // generic_parser_base - This class holds all the non-generic code that we do
331 // not need replicated for every instance of the generic parser.  This also
332 // allows us to put stuff into CommandLine.cpp
333 //
334 struct generic_parser_base {
335   virtual ~generic_parser_base() {}  // Base class should have virtual-dtor
336
337   // getNumOptions - Virtual function implemented by generic subclass to
338   // indicate how many entries are in Values.
339   //
340   virtual unsigned getNumOptions() const = 0;
341
342   // getOption - Return option name N.
343   virtual const char *getOption(unsigned N) const = 0;
344   
345   // getDescription - Return description N
346   virtual const char *getDescription(unsigned N) const = 0;
347
348   // Return the width of the option tag for printing...
349   virtual unsigned getOptionWidth(const Option &O) const;
350
351   // printOptionInfo - Print out information about this option.  The 
352   // to-be-maintained width is specified.
353   //
354   virtual void printOptionInfo(const Option &O, unsigned GlobalWidth) const;
355
356   void initialize(Option &O) {
357     // All of the modifiers for the option have been processed by now, so the
358     // argstr field should be stable, copy it down now.
359     //
360     hasArgStr = O.hasArgStr();
361
362     // If there has been no argstr specified, that means that we need to add an
363     // argument for every possible option.  This ensures that our options are
364     // vectored to us.
365     //
366     if (!hasArgStr)
367       for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
368         O.addArgument(getOption(i));
369   }
370
371   enum ValueExpected getValueExpectedFlagDefault() const {
372     // If there is an ArgStr specified, then we are of the form:
373     //
374     //    -opt=O2   or   -opt O2  or  -optO2
375     //
376     // In which case, the value is required.  Otherwise if an arg str has not
377     // been specified, we are of the form:
378     //
379     //    -O2 or O2 or -la (where -l and -a are seperate options)
380     //
381     // If this is the case, we cannot allow a value.
382     //
383     if (hasArgStr)
384       return ValueRequired;
385     else
386       return ValueDisallowed;
387   }
388
389   // findOption - Return the option number corresponding to the specified
390   // argument string.  If the option is not found, getNumOptions() is returned.
391   //
392   unsigned findOption(const char *Name);
393
394 protected:
395   bool hasArgStr;
396 };
397
398 // Default parser implementation - This implementation depends on having a
399 // mapping of recognized options to values of some sort.  In addition to this,
400 // each entry in the mapping also tracks a help message that is printed with the
401 // command line option for --help.  Because this is a simple mapping parser, the
402 // data type can be any unsupported type.
403 //
404 template <class DataType>
405 class parser : public generic_parser_base {
406 protected:
407   std::vector<std::pair<const char *,
408                         std::pair<DataType, const char *> > > Values;
409 public:
410   typedef DataType parser_data_type;
411
412   // Implement virtual functions needed by generic_parser_base
413   unsigned getNumOptions() const { return Values.size(); }
414   const char *getOption(unsigned N) const { return Values[N].first; }
415   const char *getDescription(unsigned N) const {
416     return Values[N].second.second;
417   }
418
419   // parse - Return true on error.
420   bool parse(Option &O, const char *ArgName, const std::string &Arg,
421              DataType &V) {
422     std::string ArgVal;
423     if (hasArgStr)
424       ArgVal = Arg;
425     else
426       ArgVal = ArgName;
427
428     for (unsigned i = 0, e = Values.size(); i != e; ++i)
429       if (ArgVal == Values[i].first) {
430         V = Values[i].second.first;
431         return false;
432       }
433
434     return O.error(": Cannot find option named '" + ArgVal + "'!");
435   }
436
437   // addLiteralOption - Add an entry to the mapping table...
438   template <class DT>
439   void addLiteralOption(const char *Name, const DT &V, const char *HelpStr) {
440     assert(findOption(Name) == Values.size() && "Option already exists!");
441     Values.push_back(std::make_pair(Name, std::make_pair((DataType)V,HelpStr)));
442   }
443
444   // removeLiteralOption - Remove the specified option.
445   //
446   void removeLiteralOption(const char *Name) {
447     unsigned N = findOption(Name);
448     assert(N != Values.size() && "Option not found!");
449     Values.erase(Values.begin()+N);
450   }
451 };
452
453 //--------------------------------------------------
454 // basic_parser - Super class of parsers to provide boilerplate code
455 //
456 struct basic_parser_impl {  // non-template implementation of basic_parser<t>
457   virtual ~basic_parser_impl() {}
458
459   enum ValueExpected getValueExpectedFlagDefault() const {
460     return ValueRequired;
461   }
462   
463   void initialize(Option &O) {}
464   
465   // Return the width of the option tag for printing...
466   unsigned getOptionWidth(const Option &O) const;
467   
468   // printOptionInfo - Print out information about this option.  The
469   // to-be-maintained width is specified.
470   //
471   void printOptionInfo(const Option &O, unsigned GlobalWidth) const;
472
473
474   // getValueName - Overload in subclass to provide a better default value.
475   virtual const char *getValueName() const { return "value"; }
476 };
477
478 // basic_parser - The real basic parser is just a template wrapper that provides
479 // a typedef for the provided data type.
480 //
481 template<class DataType>
482 struct basic_parser : public basic_parser_impl {
483   typedef DataType parser_data_type;
484 };
485
486
487 //--------------------------------------------------
488 // parser<bool>
489 //
490 template<>
491 struct parser<bool> : public basic_parser<bool> {
492
493   // parse - Return true on error.
494   bool parse(Option &O, const char *ArgName, const std::string &Arg, bool &Val);
495
496   enum ValueExpected getValueExpectedFlagDefault() const {
497     return ValueOptional; 
498   }
499
500   // getValueName - Do not print =<value> at all
501   virtual const char *getValueName() const { return 0; }
502 };
503
504
505 //--------------------------------------------------
506 // parser<int>
507 //
508 template<>
509 struct parser<int> : public basic_parser<int> {
510   
511   // parse - Return true on error.
512   bool parse(Option &O, const char *ArgName, const std::string &Arg, int &Val);
513
514   // getValueName - Overload in subclass to provide a better default value.
515   virtual const char *getValueName() const { return "int"; }
516 };
517
518
519 //--------------------------------------------------
520 // parser<double>
521 //
522 template<>
523 struct parser<double> : public basic_parser<double> {
524   // parse - Return true on error.
525   bool parse(Option &O, const char *AN, const std::string &Arg, double &Val);
526
527   // getValueName - Overload in subclass to provide a better default value.
528   virtual const char *getValueName() const { return "number"; }
529 };
530
531
532 //--------------------------------------------------
533 // parser<float>
534 //
535 template<>
536 struct parser<float> : public basic_parser<float> {
537   // parse - Return true on error.
538   bool parse(Option &O, const char *AN, const std::string &Arg, float &Val);
539
540   // getValueName - Overload in subclass to provide a better default value.
541   virtual const char *getValueName() const { return "number"; }
542 };
543
544
545 //--------------------------------------------------
546 // parser<std::string>
547 //
548 template<>
549 struct parser<std::string> : public basic_parser<std::string> {
550   // parse - Return true on error.
551   bool parse(Option &O, const char *ArgName, const std::string &Arg,
552              std::string &Value) {
553     Value = Arg;
554     return false;
555   }
556
557   // getValueName - Overload in subclass to provide a better default value.
558   virtual const char *getValueName() const { return "string"; }
559 };
560
561
562
563 //===----------------------------------------------------------------------===//
564 // applicator class - This class is used because we must use partial
565 // specialization to handle literal string arguments specially (const char* does
566 // not correctly respond to the apply method).  Because the syntax to use this
567 // is a pain, we have the 'apply' method below to handle the nastiness...
568 //
569 template<class Mod> struct applicator {
570   template<class Opt>
571   static void opt(const Mod &M, Opt &O) { M.apply(O); }
572 };
573
574 // Handle const char* as a special case...
575 template<unsigned n> struct applicator<char[n]> {
576   template<class Opt>
577   static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
578 };
579 template<unsigned n> struct applicator<const char[n]> {
580   template<class Opt>
581   static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
582 };
583 template<> struct applicator<const char*> {
584   template<class Opt>
585   static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
586 };
587
588 template<> struct applicator<NumOccurances> {
589   static void opt(NumOccurances NO, Option &O) { O.setNumOccurancesFlag(NO); }
590 };
591 template<> struct applicator<ValueExpected> {
592   static void opt(ValueExpected VE, Option &O) { O.setValueExpectedFlag(VE); }
593 };
594 template<> struct applicator<OptionHidden> {
595   static void opt(OptionHidden OH, Option &O) { O.setHiddenFlag(OH); }
596 };
597 template<> struct applicator<FormattingFlags> {
598   static void opt(FormattingFlags FF, Option &O) { O.setFormattingFlag(FF); }
599 };
600 template<> struct applicator<MiscFlags> {
601   static void opt(MiscFlags MF, Option &O) { O.setMiscFlag(MF); }
602 };
603
604 // apply method - Apply a modifier to an option in a type safe way.
605 template<class Mod, class Opt>
606 void apply(const Mod &M, Opt *O) {
607   applicator<Mod>::opt(M, *O);
608 }
609
610
611 //===----------------------------------------------------------------------===//
612 // opt_storage class
613
614 // Default storage class definition: external storage.  This implementation
615 // assumes the user will specify a variable to store the data into with the
616 // cl::location(x) modifier.
617 //
618 template<class DataType, bool ExternalStorage, bool isClass>
619 class opt_storage {
620   DataType *Location;   // Where to store the object...
621
622   void check() {
623     assert(Location != 0 && "cl::location(...) not specified for a command "
624            "line option with external storage!");
625   }
626 public:
627   opt_storage() : Location(0) {}
628
629   bool setLocation(Option &O, DataType &L) {
630     if (Location)
631       return O.error(": cl::location(x) specified more than once!");
632     Location = &L;
633     return false;
634   }
635
636   template<class T>
637   void setValue(const T &V) {
638     check();
639     *Location = V;
640   }
641
642   DataType &getValue() { check(); return *Location; }
643   const DataType &getValue() const { check(); return *Location; }
644 };
645
646
647 // Define how to hold a class type object, such as a string.  Since we can
648 // inherit from a class, we do so.  This makes us exactly compatible with the
649 // object in all cases that it is used.
650 //
651 template<class DataType>
652 struct opt_storage<DataType,false,true> : public DataType {
653
654   template<class T>
655   void setValue(const T &V) { DataType::operator=(V); }
656
657   DataType &getValue() { return *this; }
658   const DataType &getValue() const { return *this; }
659 };
660
661 // Define a partial specialization to handle things we cannot inherit from.  In
662 // this case, we store an instance through containment, and overload operators
663 // to get at the value.
664 //
665 template<class DataType>
666 struct opt_storage<DataType, false, false> {
667   DataType Value;
668
669   // Make sure we initialize the value with the default constructor for the
670   // type.
671   opt_storage() : Value(DataType()) {}
672
673   template<class T>
674   void setValue(const T &V) { Value = V; }
675   DataType &getValue() { return Value; }
676   DataType getValue() const { return Value; }
677 };
678
679
680 //===----------------------------------------------------------------------===//
681 // opt - A scalar command line option.
682 //
683 template <class DataType, bool ExternalStorage = false,
684           class ParserClass = parser<DataType> >
685 class opt : public Option, 
686             public opt_storage<DataType, ExternalStorage,
687                                ::boost::is_class<DataType>::value> {
688   ParserClass Parser;
689
690   virtual bool handleOccurance(const char *ArgName, const std::string &Arg) {
691     typename ParserClass::parser_data_type Val;
692     if (Parser.parse(*this, ArgName, Arg, Val))
693       return true;                            // Parse error!
694     setValue(Val);
695     return false;
696   }
697
698   virtual enum ValueExpected getValueExpectedFlagDefault() const {
699     return Parser.getValueExpectedFlagDefault();
700   }
701
702   // Forward printing stuff to the parser...
703   virtual unsigned getOptionWidth() const {return Parser.getOptionWidth(*this);}
704   virtual void printOptionInfo(unsigned GlobalWidth) const {
705     Parser.printOptionInfo(*this, GlobalWidth);
706   }
707
708   void done() {
709     addArgument(ArgStr);
710     Parser.initialize(*this);
711   }
712 public:
713   // setInitialValue - Used by the cl::init modifier...
714   void setInitialValue(const DataType &V) { setValue(V); }
715
716   ParserClass &getParser() { return Parser; }
717
718   operator DataType() const { return getValue(); }
719
720   template<class T>
721   DataType &operator=(const T &Val) { setValue(Val); return getValue(); }
722
723   // One option...
724   template<class M0t>
725   opt(const M0t &M0) {
726     apply(M0, this);
727     done();
728   }
729
730   // Two options...
731   template<class M0t, class M1t>
732   opt(const M0t &M0, const M1t &M1) {
733     apply(M0, this); apply(M1, this);
734     done();
735   }
736
737   // Three options...
738   template<class M0t, class M1t, class M2t>
739   opt(const M0t &M0, const M1t &M1, const M2t &M2) {
740     apply(M0, this); apply(M1, this); apply(M2, this);
741     done();
742   }
743   // Four options...
744   template<class M0t, class M1t, class M2t, class M3t>
745   opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3) {
746     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
747     done();
748   }
749   // Five options...
750   template<class M0t, class M1t, class M2t, class M3t, class M4t>
751   opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
752       const M4t &M4) {
753     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
754     apply(M4, this);
755     done();
756   }
757   // Six options...
758   template<class M0t, class M1t, class M2t, class M3t,
759            class M4t, class M5t>
760   opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
761       const M4t &M4, const M5t &M5) {
762     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
763     apply(M4, this); apply(M5, this);
764     done();
765   }
766   // Seven options...
767   template<class M0t, class M1t, class M2t, class M3t,
768            class M4t, class M5t, class M6t>
769   opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
770       const M4t &M4, const M5t &M5, const M6t &M6) {
771     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
772     apply(M4, this); apply(M5, this); apply(M6, this);
773     done();
774   }
775   // Eight options...
776   template<class M0t, class M1t, class M2t, class M3t,
777            class M4t, class M5t, class M6t, class M7t>
778   opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
779       const M4t &M4, const M5t &M5, const M6t &M6, const M7t &M7) {
780     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
781     apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
782     done();
783   }
784 };
785
786 //===----------------------------------------------------------------------===//
787 // list_storage class
788
789 // Default storage class definition: external storage.  This implementation
790 // assumes the user will specify a variable to store the data into with the
791 // cl::location(x) modifier.
792 //
793 template<class DataType, class StorageClass>
794 class list_storage {
795   StorageClass *Location;   // Where to store the object...
796
797 public:
798   list_storage() : Location(0) {}
799
800   bool setLocation(Option &O, StorageClass &L) {
801     if (Location)
802       return O.error(": cl::location(x) specified more than once!");
803     Location = &L;
804     return false;
805   }
806
807   template<class T>
808   void addValue(const T &V) {
809     assert(Location != 0 && "cl::location(...) not specified for a command "
810            "line option with external storage!");
811     Location->push_back(V);
812   }
813 };
814
815
816 // Define how to hold a class type object, such as a string.  Since we can
817 // inherit from a class, we do so.  This makes us exactly compatible with the
818 // object in all cases that it is used.
819 //
820 template<class DataType>
821 struct list_storage<DataType, bool> : public std::vector<DataType> {
822
823   template<class T>
824   void addValue(const T &V) { push_back(V); }
825 };
826
827
828 //===----------------------------------------------------------------------===//
829 // list - A list of command line options.
830 //
831 template <class DataType, class Storage = bool,
832           class ParserClass = parser<DataType> >
833 class list : public Option, public list_storage<DataType, Storage> {
834   ParserClass Parser;
835
836   virtual enum NumOccurances getNumOccurancesFlagDefault() const { 
837     return ZeroOrMore;
838   }
839   virtual enum ValueExpected getValueExpectedFlagDefault() const {
840     return Parser.getValueExpectedFlagDefault();
841   }
842
843   virtual bool handleOccurance(const char *ArgName, const std::string &Arg) {
844     typename ParserClass::parser_data_type Val;
845     if (Parser.parse(*this, ArgName, Arg, Val))
846       return true;  // Parse Error!
847     addValue(Val);
848     return false;
849   }
850
851   // Forward printing stuff to the parser...
852   virtual unsigned getOptionWidth() const {return Parser.getOptionWidth(*this);}
853   virtual void printOptionInfo(unsigned GlobalWidth) const {
854     Parser.printOptionInfo(*this, GlobalWidth);
855   }
856
857   void done() {
858     addArgument(ArgStr);
859     Parser.initialize(*this);
860   }
861 public:
862   ParserClass &getParser() { return Parser; }
863
864   // One option...
865   template<class M0t>
866   list(const M0t &M0) {
867     apply(M0, this);
868     done();
869   }
870   // Two options...
871   template<class M0t, class M1t>
872   list(const M0t &M0, const M1t &M1) {
873     apply(M0, this); apply(M1, this);
874     done();
875   }
876   // Three options...
877   template<class M0t, class M1t, class M2t>
878   list(const M0t &M0, const M1t &M1, const M2t &M2) {
879     apply(M0, this); apply(M1, this); apply(M2, this);
880     done();
881   }
882   // Four options...
883   template<class M0t, class M1t, class M2t, class M3t>
884   list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3) {
885     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
886     done();
887   }
888   // Five options...
889   template<class M0t, class M1t, class M2t, class M3t, class M4t>
890   list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
891        const M4t &M4) {
892     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
893     apply(M4, this);
894     done();
895   }
896   // Six options...
897   template<class M0t, class M1t, class M2t, class M3t,
898            class M4t, class M5t>
899   list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
900        const M4t &M4, const M5t &M5) {
901     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
902     apply(M4, this); apply(M5, this);
903     done();
904   }
905   // Seven options...
906   template<class M0t, class M1t, class M2t, class M3t,
907            class M4t, class M5t, class M6t>
908   list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
909       const M4t &M4, const M5t &M5, const M6t &M6) {
910     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
911     apply(M4, this); apply(M5, this); apply(M6, this);
912     done();
913   }
914   // Eight options...
915   template<class M0t, class M1t, class M2t, class M3t,
916            class M4t, class M5t, class M6t, class M7t>
917   list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
918       const M4t &M4, const M5t &M5, const M6t &M6, const M7t &M7) {
919     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
920     apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
921     done();
922   }
923 };
924
925
926
927 //===----------------------------------------------------------------------===//
928 // Aliased command line option (alias this name to a preexisting name)
929 //
930
931 class alias : public Option {
932   Option *AliasFor;
933   virtual bool handleOccurance(const char *ArgName, const std::string &Arg) {
934     return AliasFor->handleOccurance(AliasFor->ArgStr, Arg);
935   }
936   // Aliases default to be hidden...
937   virtual enum OptionHidden getOptionHiddenFlagDefault() const {return Hidden;}
938
939   // Handle printing stuff...
940   virtual unsigned getOptionWidth() const;
941   virtual void printOptionInfo(unsigned GlobalWidth) const;
942
943   void done() {
944     if (!hasArgStr())
945       error(": cl::alias must have argument name specified!");
946     if (AliasFor == 0)
947       error(": cl::alias must have an cl::aliasopt(option) specified!");
948     addArgument(ArgStr);
949   }
950 public:
951   void setAliasFor(Option &O) {
952     if (AliasFor)
953       error(": cl::alias must only have one cl::aliasopt(...) specified!");
954     AliasFor = &O;
955   }
956
957   // One option...
958   template<class M0t>
959   alias(const M0t &M0) : AliasFor(0) {
960     apply(M0, this);
961     done();
962   }
963   // Two options...
964   template<class M0t, class M1t>
965   alias(const M0t &M0, const M1t &M1) : AliasFor(0) {
966     apply(M0, this); apply(M1, this);
967     done();
968   }
969   // Three options...
970   template<class M0t, class M1t, class M2t>
971   alias(const M0t &M0, const M1t &M1, const M2t &M2) : AliasFor(0) {
972     apply(M0, this); apply(M1, this); apply(M2, this);
973     done();
974   }
975   // Four options...
976   template<class M0t, class M1t, class M2t, class M3t>
977   alias(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3)
978     : AliasFor(0) {
979     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
980     done();
981   }
982 };
983
984 // aliasfor - Modifier to set the option an alias aliases.
985 struct aliasopt {
986   Option &Opt;
987   aliasopt(Option &O) : Opt(O) {}
988   void apply(alias &A) const { A.setAliasFor(Opt); }
989 };
990
991 } // End namespace cl
992
993 #endif