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