blob: eb1aa18a0b6c1036555defd145a900aa95a266e5 [file] [log] [blame]
Chris Lattnercee8f9a2001-11-27 00:03:19 +00001//===- 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//
Chris Lattner331de232002-07-22 02:07:59 +00007// 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
Chris Lattnercee8f9a2001-11-27 00:03:19 +000010//
11//===----------------------------------------------------------------------===//
12
Brian Gaekea9f6e4a2003-06-17 00:35:55 +000013#ifndef SUPPORT_COMMANDLINE_H
14#define SUPPORT_COMMANDLINE_H
Chris Lattnercee8f9a2001-11-27 00:03:19 +000015
16#include <string>
17#include <vector>
18#include <utility>
Chris Lattnerb3b729b2003-05-22 20:25:57 +000019#include <cstdarg>
Chris Lattner331de232002-07-22 02:07:59 +000020#include "boost/type_traits/object_traits.hpp"
John Criswellbe583b92003-06-11 14:01:36 +000021
Chris Lattnerbf0ac3f2003-06-03 15:30:37 +000022/// 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.
25namespace cl {
Chris Lattnercee8f9a2001-11-27 00:03:19 +000026
27//===----------------------------------------------------------------------===//
Chris Lattner331de232002-07-22 02:07:59 +000028// ParseCommandLineOptions - Command line option processing entry point.
Chris Lattnercee8f9a2001-11-27 00:03:19 +000029//
30void cl::ParseCommandLineOptions(int &argc, char **argv,
Chris Lattner331de232002-07-22 02:07:59 +000031 const char *Overview = 0);
Chris Lattnercee8f9a2001-11-27 00:03:19 +000032
33//===----------------------------------------------------------------------===//
Chris Lattner331de232002-07-22 02:07:59 +000034// Flags permitted to be passed to command line arguments
35//
Chris Lattnercee8f9a2001-11-27 00:03:19 +000036
37enum NumOccurances { // Flags for the number of occurances allowed...
38 Optional = 0x01, // Zero or One occurance
39 ZeroOrMore = 0x02, // Zero or more occurances allowed
40 Required = 0x03, // One occurance required
41 OneOrMore = 0x04, // One or more occurances required
42
Chris Lattner331de232002-07-22 02:07:59 +000043 // 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.
Chris Lattnercee8f9a2001-11-27 00:03:19 +000049 //
50 ConsumeAfter = 0x05,
51
52 OccurancesMask = 0x07,
53};
54
55enum ValueExpected { // Is a value required for the option?
Chris Lattnerb3b729b2003-05-22 20:25:57 +000056 ValueOptional = 0x08, // The value can appear... or not
Chris Lattnercee8f9a2001-11-27 00:03:19 +000057 ValueRequired = 0x10, // The value is required to appear!
58 ValueDisallowed = 0x18, // A value may not be specified (for flags)
59 ValueMask = 0x18,
60};
61
62enum 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
Chris Lattner331de232002-07-22 02:07:59 +000069// 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
84enum 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
Chris Lattnerb3b729b2003-05-22 20:25:57 +000092enum MiscFlags { // Miscellaneous flags to adjust argument
93 CommaSeparated = 0x200, // Should this cl::list split between commas?
94 MiscMask = 0x200,
95};
96
97
Chris Lattnercee8f9a2001-11-27 00:03:19 +000098
99//===----------------------------------------------------------------------===//
100// Option Base class
101//
Chris Lattner331de232002-07-22 02:07:59 +0000102class alias;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000103class Option {
104 friend void cl::ParseCommandLineOptions(int &, char **, const char *, int);
Chris Lattner331de232002-07-22 02:07:59 +0000105 friend class alias;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000106
107 // handleOccurances - 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 //
Chris Lattner697954c2002-01-20 22:54:45 +0000111 virtual bool handleOccurance(const char *ArgName, const std::string &Arg) = 0;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000112
113 virtual enum NumOccurances getNumOccurancesFlagDefault() 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 }
Chris Lattner331de232002-07-22 02:07:59 +0000122 virtual enum FormattingFlags getFormattingFlagDefault() const {
123 return NormalFormatting;
124 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000125
Chris Lattner331de232002-07-22 02:07:59 +0000126 int NumOccurances; // The number of times specified
127 int Flags; // Flags for the argument
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000128public:
Chris Lattner331de232002-07-22 02:07:59 +0000129 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
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000132
133 inline enum NumOccurances getNumOccurancesFlag() const {
134 int NO = Flags & OccurancesMask;
135 return NO ? (enum NumOccurances)NO : getNumOccurancesFlagDefault();
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 }
Chris Lattner331de232002-07-22 02:07:59 +0000145 inline enum FormattingFlags getFormattingFlag() const {
146 int OH = Flags & FormattingMask;
147 return OH ? (enum FormattingFlags)OH : getFormattingFlagDefault();
148 }
Chris Lattnerb3b729b2003-05-22 20:25:57 +0000149 inline unsigned getMiscFlags() const {
150 return Flags & MiscMask;
151 }
Chris Lattner331de232002-07-22 02:07:59 +0000152
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 setNumOccurancesFlag(enum NumOccurances Val) {
173 setFlag(Val, OccurancesMask);
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); }
Chris Lattnerb3b729b2003-05-22 20:25:57 +0000178 void setMiscFlag(enum MiscFlags M) { setFlag(M, M); }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000179protected:
Chris Lattner331de232002-07-22 02:07:59 +0000180 Option() : NumOccurances(0), Flags(0),
181 ArgStr(""), HelpStr(""), ValueStr("") {}
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000182
183public:
Chris Lattner331de232002-07-22 02:07:59 +0000184 // addArgument - Tell the system that this Option subclass will handle all
185 // occurances of -ArgStr on the command line.
186 //
187 void addArgument(const char *ArgStr);
Chris Lattnerae1257a2002-07-23 00:44:37 +0000188 void removeArgument(const char *ArgStr);
Chris Lattner331de232002-07-22 02:07:59 +0000189
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000190 // Return the width of the option tag for printing...
Chris Lattner331de232002-07-22 02:07:59 +0000191 virtual unsigned getOptionWidth() const = 0;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000192
193 // printOptionInfo - Print out information about this option. The
194 // to-be-maintained width is specified.
195 //
Chris Lattner331de232002-07-22 02:07:59 +0000196 virtual void printOptionInfo(unsigned GlobalWidth) const = 0;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000197
198 // addOccurance - Wrapper around handleOccurance that enforces Flags
199 //
Chris Lattner697954c2002-01-20 22:54:45 +0000200 bool addOccurance(const char *ArgName, const std::string &Value);
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000201
202 // Prints option name followed by message. Always returns true.
Chris Lattner697954c2002-01-20 22:54:45 +0000203 bool error(std::string Message, const char *ArgName = 0);
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000204
205public:
206 inline int getNumOccurances() const { return NumOccurances; }
207 virtual ~Option() {}
208};
209
210
211//===----------------------------------------------------------------------===//
Chris Lattner331de232002-07-22 02:07:59 +0000212// Command line option modifiers that can be used to modify the behavior of
213// command line option parsers...
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000214//
Chris Lattner331de232002-07-22 02:07:59 +0000215
216// desc - Modifier to set the description shown in the --help output...
217struct desc {
218 const char *Desc;
219 desc(const char *Str) : Desc(Str) {}
220 void apply(Option &O) const { O.setDescription(Desc); }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000221};
222
Chris Lattner331de232002-07-22 02:07:59 +0000223// value_desc - Modifier to set the value description shown in the --help
224// output...
225struct value_desc {
226 const char *Desc;
227 value_desc(const char *Str) : Desc(Str) {}
228 void apply(Option &O) const { O.setValueStr(Desc); }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000229};
230
231
Chris Lattner331de232002-07-22 02:07:59 +0000232// 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.
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000235//
Chris Lattner331de232002-07-22 02:07:59 +0000236template<class Ty>
237struct 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); }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000243};
244
Chris Lattner331de232002-07-22 02:07:59 +0000245template<class Ty>
246initializer<Ty> init(const Ty &Val) {
247 return initializer<Ty>(Val);
248}
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000249
Chris Lattner331de232002-07-22 02:07:59 +0000250
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.
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000254//
Chris Lattner331de232002-07-22 02:07:59 +0000255template<class Ty>
256struct LocationClass {
257 Ty &Loc;
258 LocationClass(Ty &L) : Loc(L) {}
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000259
Chris Lattner331de232002-07-22 02:07:59 +0000260 template<class Opt>
261 void apply(Opt &O) const { O.setLocation(O, Loc); }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000262};
263
Chris Lattner331de232002-07-22 02:07:59 +0000264template<class Ty>
265LocationClass<Ty> location(Ty &L) { return LocationClass<Ty>(L); }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000266
267
268//===----------------------------------------------------------------------===//
269// Enum valued command line option
270//
Chris Lattnerae1257a2002-07-23 00:44:37 +0000271#define clEnumVal(ENUMVAL, DESC) #ENUMVAL, (int)ENUMVAL, DESC
272#define clEnumValN(ENUMVAL, FLAGNAME, DESC) FLAGNAME, (int)ENUMVAL, DESC
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000273
Chris Lattner331de232002-07-22 02:07:59 +0000274// 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//
279template<class DataType>
280class ValuesClass {
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000281 // 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.
Chris Lattner331de232002-07-22 02:07:59 +0000284 std::vector<std::pair<const char *, std::pair<int, const char *> > > Values;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000285 void processValues(va_list Vals);
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000286public:
Chris Lattner331de232002-07-22 02:07:59 +0000287 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 *)) {
Chris Lattnerae1257a2002-07-23 00:44:37 +0000294 DataType EnumVal = (DataType)va_arg(ValueArgs, int);
Chris Lattner331de232002-07-22 02:07:59 +0000295 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 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000307};
308
Chris Lattner331de232002-07-22 02:07:59 +0000309template<class DataType>
310ValuesClass<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}
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000318
Chris Lattner331de232002-07-22 02:07:59 +0000319
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//
332struct 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;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000345
346 // Return the width of the option tag for printing...
Chris Lattner331de232002-07-22 02:07:59 +0000347 virtual unsigned getOptionWidth(const Option &O) const;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000348
349 // printOptionInfo - Print out information about this option. The
350 // to-be-maintained width is specified.
351 //
Chris Lattner331de232002-07-22 02:07:59 +0000352 virtual void printOptionInfo(const Option &O, unsigned GlobalWidth) const;
Chris Lattner71fb7162002-05-22 17:03:05 +0000353
Chris Lattner331de232002-07-22 02:07:59 +0000354 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
Chris Lattneraf7e8212002-07-23 17:15:09 +0000387 // 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
Chris Lattner331de232002-07-22 02:07:59 +0000392protected:
393 bool hasArgStr;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000394};
395
Chris Lattner331de232002-07-22 02:07:59 +0000396// 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//
402template <class DataType>
403class parser : public generic_parser_base {
Chris Lattneraf7e8212002-07-23 17:15:09 +0000404protected:
Chris Lattner331de232002-07-22 02:07:59 +0000405 std::vector<std::pair<const char *,
406 std::pair<DataType, const char *> > > Values;
Chris Lattneraf7e8212002-07-23 17:15:09 +0000407public:
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000408 typedef DataType parser_data_type;
Chris Lattner331de232002-07-22 02:07:59 +0000409
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
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000417 // parse - Return true on error.
418 bool parse(Option &O, const char *ArgName, const std::string &Arg,
419 DataType &V) {
Chris Lattner7f4dd472002-07-24 22:08:36 +0000420 std::string ArgVal;
Chris Lattner331de232002-07-22 02:07:59 +0000421 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) {
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000428 V = Values[i].second.first;
Chris Lattner331de232002-07-22 02:07:59 +0000429 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) {
Chris Lattneraf7e8212002-07-23 17:15:09 +0000438 assert(findOption(Name) == Values.size() && "Option already exists!");
Chris Lattner331de232002-07-22 02:07:59 +0000439 Values.push_back(std::make_pair(Name, std::make_pair((DataType)V,HelpStr)));
440 }
Chris Lattneraf7e8212002-07-23 17:15:09 +0000441
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 }
Chris Lattner331de232002-07-22 02:07:59 +0000449};
450
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000451//--------------------------------------------------
452// basic_parser - Super class of parsers to provide boilerplate code
453//
454struct 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//
479template<class DataType>
480struct basic_parser : public basic_parser_impl {
481 typedef DataType parser_data_type;
482};
483
Chris Lattner331de232002-07-22 02:07:59 +0000484
485//--------------------------------------------------
486// parser<bool>
487//
488template<>
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000489struct 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);
Chris Lattner331de232002-07-22 02:07:59 +0000493
494 enum ValueExpected getValueExpectedFlagDefault() const {
495 return ValueOptional;
496 }
497
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000498 // getValueName - Do not print =<value> at all
499 virtual const char *getValueName() const { return 0; }
Chris Lattner331de232002-07-22 02:07:59 +0000500};
501
502
503//--------------------------------------------------
504// parser<int>
505//
506template<>
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000507struct parser<int> : public basic_parser<int> {
Chris Lattner331de232002-07-22 02:07:59 +0000508
509 // parse - Return true on error.
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000510 bool parse(Option &O, const char *ArgName, const std::string &Arg, int &Val);
Chris Lattner331de232002-07-22 02:07:59 +0000511
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000512 // getValueName - Overload in subclass to provide a better default value.
513 virtual const char *getValueName() const { return "int"; }
Chris Lattner331de232002-07-22 02:07:59 +0000514};
515
516
517//--------------------------------------------------
Chris Lattnerd2a6fc32003-06-28 15:47:20 +0000518// parser<unsigned>
519//
520template<>
521struct 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//--------------------------------------------------
Chris Lattner331de232002-07-22 02:07:59 +0000533// parser<double>
534//
535template<>
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000536struct parser<double> : public basic_parser<double> {
Chris Lattner331de232002-07-22 02:07:59 +0000537 // parse - Return true on error.
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000538 bool parse(Option &O, const char *AN, const std::string &Arg, double &Val);
Chris Lattner331de232002-07-22 02:07:59 +0000539
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000540 // getValueName - Overload in subclass to provide a better default value.
541 virtual const char *getValueName() const { return "number"; }
Chris Lattner331de232002-07-22 02:07:59 +0000542};
543
Chris Lattner331de232002-07-22 02:07:59 +0000544
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000545//--------------------------------------------------
546// parser<float>
547//
548template<>
549struct 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};
Chris Lattner331de232002-07-22 02:07:59 +0000556
557
558//--------------------------------------------------
Chris Lattner7f4dd472002-07-24 22:08:36 +0000559// parser<std::string>
Chris Lattner331de232002-07-22 02:07:59 +0000560//
561template<>
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000562struct parser<std::string> : public basic_parser<std::string> {
Chris Lattner331de232002-07-22 02:07:59 +0000563 // parse - Return true on error.
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000564 bool parse(Option &O, const char *ArgName, const std::string &Arg,
565 std::string &Value) {
566 Value = Arg;
Chris Lattner331de232002-07-22 02:07:59 +0000567 return false;
568 }
Chris Lattner71fb7162002-05-22 17:03:05 +0000569
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000570 // getValueName - Overload in subclass to provide a better default value.
571 virtual const char *getValueName() const { return "string"; }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000572};
573
Chris Lattner71fb7162002-05-22 17:03:05 +0000574
Chris Lattner331de232002-07-22 02:07:59 +0000575
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//
582template<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...
588template<unsigned n> struct applicator<char[n]> {
589 template<class Opt>
590 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
591};
Chris Lattner40423322002-09-13 14:33:39 +0000592template<unsigned n> struct applicator<const char[n]> {
593 template<class Opt>
594 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
595};
Chris Lattner331de232002-07-22 02:07:59 +0000596template<> struct applicator<const char*> {
597 template<class Opt>
598 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
599};
600
601template<> struct applicator<NumOccurances> {
602 static void opt(NumOccurances NO, Option &O) { O.setNumOccurancesFlag(NO); }
603};
604template<> struct applicator<ValueExpected> {
605 static void opt(ValueExpected VE, Option &O) { O.setValueExpectedFlag(VE); }
606};
607template<> struct applicator<OptionHidden> {
608 static void opt(OptionHidden OH, Option &O) { O.setHiddenFlag(OH); }
609};
610template<> struct applicator<FormattingFlags> {
611 static void opt(FormattingFlags FF, Option &O) { O.setFormattingFlag(FF); }
612};
Chris Lattnerb3b729b2003-05-22 20:25:57 +0000613template<> struct applicator<MiscFlags> {
614 static void opt(MiscFlags MF, Option &O) { O.setMiscFlag(MF); }
615};
Chris Lattner331de232002-07-22 02:07:59 +0000616
617// apply method - Apply a modifier to an option in a type safe way.
618template<class Mod, class Opt>
619void 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//
631template<class DataType, bool ExternalStorage, bool isClass>
632class 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 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000639public:
Chris Lattner331de232002-07-22 02:07:59 +0000640 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;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000647 }
648
Chris Lattner331de232002-07-22 02:07:59 +0000649 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//
664template<class DataType>
665struct 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//
678template<class DataType>
679struct 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; }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000690};
691
692
693//===----------------------------------------------------------------------===//
Chris Lattner331de232002-07-22 02:07:59 +0000694// opt - A scalar command line option.
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000695//
Chris Lattner331de232002-07-22 02:07:59 +0000696template <class DataType, bool ExternalStorage = false,
697 class ParserClass = parser<DataType> >
698class opt : public Option,
699 public opt_storage<DataType, ExternalStorage,
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000700 ::boost::is_class<DataType>::value> {
Chris Lattner331de232002-07-22 02:07:59 +0000701 ParserClass Parser;
702
703 virtual bool handleOccurance(const char *ArgName, const std::string &Arg) {
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000704 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;
Chris Lattner331de232002-07-22 02:07:59 +0000709 }
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 }
725public:
726 // setInitialValue - Used by the cl::init modifier...
727 void setInitialValue(const DataType &V) { setValue(V); }
728
Chris Lattner331de232002-07-22 02:07:59 +0000729 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//
806template<class DataType, class StorageClass>
807class list_storage {
808 StorageClass *Location; // Where to store the object...
809
810public:
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//
833template<class DataType>
834struct 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//
844template <class DataType, class Storage = bool,
845 class ParserClass = parser<DataType> >
846class list : public Option, public list_storage<DataType, Storage> {
847 ParserClass Parser;
848
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000849 virtual enum NumOccurances getNumOccurancesFlagDefault() const {
850 return ZeroOrMore;
851 }
852 virtual enum ValueExpected getValueExpectedFlagDefault() const {
Chris Lattner331de232002-07-22 02:07:59 +0000853 return Parser.getValueExpectedFlagDefault();
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000854 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000855
Chris Lattner331de232002-07-22 02:07:59 +0000856 virtual bool handleOccurance(const char *ArgName, const std::string &Arg) {
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000857 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;
Chris Lattner331de232002-07-22 02:07:59 +0000862 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000863
Chris Lattner331de232002-07-22 02:07:59 +0000864 // 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 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000869
Chris Lattner331de232002-07-22 02:07:59 +0000870 void done() {
871 addArgument(ArgStr);
872 Parser.initialize(*this);
873 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000874public:
Chris Lattner331de232002-07-22 02:07:59 +0000875 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 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000936};
937
Chris Lattner331de232002-07-22 02:07:59 +0000938
939
940//===----------------------------------------------------------------------===//
941// Aliased command line option (alias this name to a preexisting name)
942//
943
944class alias : public Option {
945 Option *AliasFor;
946 virtual bool handleOccurance(const char *ArgName, const std::string &Arg) {
947 return AliasFor->handleOccurance(AliasFor->ArgStr, Arg);
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000948 }
Chris Lattner331de232002-07-22 02:07:59 +0000949 // 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 }
963public:
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.
998struct aliasopt {
999 Option &Opt;
1000 aliasopt(Option &O) : Opt(O) {}
1001 void apply(alias &A) const { A.setAliasFor(Opt); }
Chris Lattnercee8f9a2001-11-27 00:03:19 +00001002};
1003
1004} // End namespace cl
1005
1006#endif