blob: 23a12855439ec6fcb4e6791e93f28df04edfe4b6 [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
13#ifndef LLVM_SUPPORT_COMMANDLINE_H
14#define LLVM_SUPPORT_COMMANDLINE_H
15
16#include <string>
17#include <vector>
18#include <utility>
19#include <stdarg.h>
Chris Lattner331de232002-07-22 02:07:59 +000020#include "boost/type_traits/object_traits.hpp"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000021
22namespace cl { // Short namespace to make usage concise
23
24//===----------------------------------------------------------------------===//
Chris Lattner331de232002-07-22 02:07:59 +000025// ParseCommandLineOptions - Command line option processing entry point.
Chris Lattnercee8f9a2001-11-27 00:03:19 +000026//
27void cl::ParseCommandLineOptions(int &argc, char **argv,
Chris Lattner331de232002-07-22 02:07:59 +000028 const char *Overview = 0);
Chris Lattnercee8f9a2001-11-27 00:03:19 +000029
30//===----------------------------------------------------------------------===//
Chris Lattner331de232002-07-22 02:07:59 +000031// Flags permitted to be passed to command line arguments
32//
Chris Lattnercee8f9a2001-11-27 00:03:19 +000033
34enum NumOccurances { // Flags for the number of occurances allowed...
35 Optional = 0x01, // Zero or One occurance
36 ZeroOrMore = 0x02, // Zero or more occurances allowed
37 Required = 0x03, // One occurance required
38 OneOrMore = 0x04, // One or more occurances required
39
Chris Lattner331de232002-07-22 02:07:59 +000040 // ConsumeAfter - Indicates that this option is fed anything that follows the
41 // last positional argument required by the application (it is an error if
42 // there are zero positional arguments, and a ConsumeAfter option is used).
43 // Thus, for example, all arguments to LLI are processed until a filename is
44 // found. Once a filename is found, all of the succeeding arguments are
45 // passed, unprocessed, to the ConsumeAfter option.
Chris Lattnercee8f9a2001-11-27 00:03:19 +000046 //
47 ConsumeAfter = 0x05,
48
49 OccurancesMask = 0x07,
50};
51
52enum ValueExpected { // Is a value required for the option?
53 ValueOptional = 0x08, // The value can oppear... or not
54 ValueRequired = 0x10, // The value is required to appear!
55 ValueDisallowed = 0x18, // A value may not be specified (for flags)
56 ValueMask = 0x18,
57};
58
59enum OptionHidden { // Control whether -help shows this option
60 NotHidden = 0x20, // Option included in --help & --help-hidden
61 Hidden = 0x40, // -help doesn't, but --help-hidden does
62 ReallyHidden = 0x60, // Neither --help nor --help-hidden show this arg
63 HiddenMask = 0x60,
64};
65
Chris Lattner331de232002-07-22 02:07:59 +000066// Formatting flags - This controls special features that the option might have
67// that cause it to be parsed differently...
68//
69// Prefix - This option allows arguments that are otherwise unrecognized to be
70// matched by options that are a prefix of the actual value. This is useful for
71// cases like a linker, where options are typically of the form '-lfoo' or
72// '-L../../include' where -l or -L are the actual flags. When prefix is
73// enabled, and used, the value for the flag comes from the suffix of the
74// argument.
75//
76// Grouping - With this option enabled, multiple letter options are allowed to
77// bunch together with only a single hyphen for the whole group. This allows
78// emulation of the behavior that ls uses for example: ls -la === ls -l -a
79//
80
81enum FormattingFlags {
82 NormalFormatting = 0x000, // Nothing special
83 Positional = 0x080, // Is a positional argument, no '-' required
84 Prefix = 0x100, // Can this option directly prefix its value?
85 Grouping = 0x180, // Can this option group with other options?
86 FormattingMask = 0x180,
87};
88
Chris Lattnercee8f9a2001-11-27 00:03:19 +000089
90//===----------------------------------------------------------------------===//
91// Option Base class
92//
Chris Lattner331de232002-07-22 02:07:59 +000093class alias;
Chris Lattnercee8f9a2001-11-27 00:03:19 +000094class Option {
95 friend void cl::ParseCommandLineOptions(int &, char **, const char *, int);
Chris Lattner331de232002-07-22 02:07:59 +000096 friend class alias;
Chris Lattnercee8f9a2001-11-27 00:03:19 +000097
98 // handleOccurances - Overriden by subclasses to handle the value passed into
99 // an argument. Should return true if there was an error processing the
100 // argument and the program should exit.
101 //
Chris Lattner697954c2002-01-20 22:54:45 +0000102 virtual bool handleOccurance(const char *ArgName, const std::string &Arg) = 0;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000103
104 virtual enum NumOccurances getNumOccurancesFlagDefault() const {
105 return Optional;
106 }
107 virtual enum ValueExpected getValueExpectedFlagDefault() const {
108 return ValueOptional;
109 }
110 virtual enum OptionHidden getOptionHiddenFlagDefault() const {
111 return NotHidden;
112 }
Chris Lattner331de232002-07-22 02:07:59 +0000113 virtual enum FormattingFlags getFormattingFlagDefault() const {
114 return NormalFormatting;
115 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000116
Chris Lattner331de232002-07-22 02:07:59 +0000117 int NumOccurances; // The number of times specified
118 int Flags; // Flags for the argument
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000119public:
Chris Lattner331de232002-07-22 02:07:59 +0000120 const char *ArgStr; // The argument string itself (ex: "help", "o")
121 const char *HelpStr; // The descriptive text message for --help
122 const char *ValueStr; // String describing what the value of this option is
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000123
124 inline enum NumOccurances getNumOccurancesFlag() const {
125 int NO = Flags & OccurancesMask;
126 return NO ? (enum NumOccurances)NO : getNumOccurancesFlagDefault();
127 }
128 inline enum ValueExpected getValueExpectedFlag() const {
129 int VE = Flags & ValueMask;
130 return VE ? (enum ValueExpected)VE : getValueExpectedFlagDefault();
131 }
132 inline enum OptionHidden getOptionHiddenFlag() const {
133 int OH = Flags & HiddenMask;
134 return OH ? (enum OptionHidden)OH : getOptionHiddenFlagDefault();
135 }
Chris Lattner331de232002-07-22 02:07:59 +0000136 inline enum FormattingFlags getFormattingFlag() const {
137 int OH = Flags & FormattingMask;
138 return OH ? (enum FormattingFlags)OH : getFormattingFlagDefault();
139 }
140
141 // hasArgStr - Return true if the argstr != ""
142 bool hasArgStr() const { return ArgStr[0] != 0; }
143
144 //-------------------------------------------------------------------------===
145 // Accessor functions set by OptionModifiers
146 //
147 void setArgStr(const char *S) { ArgStr = S; }
148 void setDescription(const char *S) { HelpStr = S; }
149 void setValueStr(const char *S) { ValueStr = S; }
150
151 void setFlag(unsigned Flag, unsigned FlagMask) {
152 if (Flags & FlagMask) {
153 error(": Specified two settings for the same option!");
154 exit(1);
155 }
156
157 Flags |= Flag;
158 }
159
160 void setNumOccurancesFlag(enum NumOccurances Val) {
161 setFlag(Val, OccurancesMask);
162 }
163 void setValueExpectedFlag(enum ValueExpected Val) { setFlag(Val, ValueMask); }
164 void setHiddenFlag(enum OptionHidden Val) { setFlag(Val, HiddenMask); }
165 void setFormattingFlag(enum FormattingFlags V) { setFlag(V, FormattingMask); }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000166
167protected:
Chris Lattner331de232002-07-22 02:07:59 +0000168 Option() : NumOccurances(0), Flags(0),
169 ArgStr(""), HelpStr(""), ValueStr("") {}
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000170
171public:
Chris Lattner331de232002-07-22 02:07:59 +0000172 // addArgument - Tell the system that this Option subclass will handle all
173 // occurances of -ArgStr on the command line.
174 //
175 void addArgument(const char *ArgStr);
Chris Lattnerae1257a2002-07-23 00:44:37 +0000176 void removeArgument(const char *ArgStr);
Chris Lattner331de232002-07-22 02:07:59 +0000177
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000178 // Return the width of the option tag for printing...
Chris Lattner331de232002-07-22 02:07:59 +0000179 virtual unsigned getOptionWidth() const = 0;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000180
181 // printOptionInfo - Print out information about this option. The
182 // to-be-maintained width is specified.
183 //
Chris Lattner331de232002-07-22 02:07:59 +0000184 virtual void printOptionInfo(unsigned GlobalWidth) const = 0;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000185
186 // addOccurance - Wrapper around handleOccurance that enforces Flags
187 //
Chris Lattner697954c2002-01-20 22:54:45 +0000188 bool addOccurance(const char *ArgName, const std::string &Value);
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000189
190 // Prints option name followed by message. Always returns true.
Chris Lattner697954c2002-01-20 22:54:45 +0000191 bool error(std::string Message, const char *ArgName = 0);
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000192
193public:
194 inline int getNumOccurances() const { return NumOccurances; }
195 virtual ~Option() {}
196};
197
198
199//===----------------------------------------------------------------------===//
Chris Lattner331de232002-07-22 02:07:59 +0000200// Command line option modifiers that can be used to modify the behavior of
201// command line option parsers...
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000202//
Chris Lattner331de232002-07-22 02:07:59 +0000203
204// desc - Modifier to set the description shown in the --help output...
205struct desc {
206 const char *Desc;
207 desc(const char *Str) : Desc(Str) {}
208 void apply(Option &O) const { O.setDescription(Desc); }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000209};
210
Chris Lattner331de232002-07-22 02:07:59 +0000211// value_desc - Modifier to set the value description shown in the --help
212// output...
213struct value_desc {
214 const char *Desc;
215 value_desc(const char *Str) : Desc(Str) {}
216 void apply(Option &O) const { O.setValueStr(Desc); }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000217};
218
219
Chris Lattner331de232002-07-22 02:07:59 +0000220// init - Specify a default (initial) value for the command line argument, if
221// the default constructor for the argument type does not give you what you
222// want. This is only valid on "opt" arguments, not on "list" arguments.
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000223//
Chris Lattner331de232002-07-22 02:07:59 +0000224template<class Ty>
225struct initializer {
226 const Ty &Init;
227 initializer(const Ty &Val) : Init(Val) {}
228
229 template<class Opt>
230 void apply(Opt &O) const { O.setInitialValue(Init); }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000231};
232
Chris Lattner331de232002-07-22 02:07:59 +0000233template<class Ty>
234initializer<Ty> init(const Ty &Val) {
235 return initializer<Ty>(Val);
236}
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000237
Chris Lattner331de232002-07-22 02:07:59 +0000238
239// location - Allow the user to specify which external variable they want to
240// store the results of the command line argument processing into, if they don't
241// want to store it in the option itself.
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000242//
Chris Lattner331de232002-07-22 02:07:59 +0000243template<class Ty>
244struct LocationClass {
245 Ty &Loc;
246 LocationClass(Ty &L) : Loc(L) {}
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000247
Chris Lattner331de232002-07-22 02:07:59 +0000248 template<class Opt>
249 void apply(Opt &O) const { O.setLocation(O, Loc); }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000250};
251
Chris Lattner331de232002-07-22 02:07:59 +0000252template<class Ty>
253LocationClass<Ty> location(Ty &L) { return LocationClass<Ty>(L); }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000254
255
256//===----------------------------------------------------------------------===//
257// Enum valued command line option
258//
Chris Lattnerae1257a2002-07-23 00:44:37 +0000259#define clEnumVal(ENUMVAL, DESC) #ENUMVAL, (int)ENUMVAL, DESC
260#define clEnumValN(ENUMVAL, FLAGNAME, DESC) FLAGNAME, (int)ENUMVAL, DESC
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000261
Chris Lattner331de232002-07-22 02:07:59 +0000262// values - For custom data types, allow specifying a group of values together
263// as the values that go into the mapping that the option handler uses. Note
264// that the values list must always have a 0 at the end of the list to indicate
265// that the list has ended.
266//
267template<class DataType>
268class ValuesClass {
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000269 // Use a vector instead of a map, because the lists should be short,
270 // the overhead is less, and most importantly, it keeps them in the order
271 // inserted so we can print our option out nicely.
Chris Lattner331de232002-07-22 02:07:59 +0000272 std::vector<std::pair<const char *, std::pair<int, const char *> > > Values;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000273 void processValues(va_list Vals);
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000274public:
Chris Lattner331de232002-07-22 02:07:59 +0000275 ValuesClass(const char *EnumName, DataType Val, const char *Desc,
276 va_list ValueArgs) {
277 // Insert the first value, which is required.
278 Values.push_back(std::make_pair(EnumName, std::make_pair(Val, Desc)));
279
280 // Process the varargs portion of the values...
281 while (const char *EnumName = va_arg(ValueArgs, const char *)) {
Chris Lattnerae1257a2002-07-23 00:44:37 +0000282 DataType EnumVal = (DataType)va_arg(ValueArgs, int);
Chris Lattner331de232002-07-22 02:07:59 +0000283 const char *EnumDesc = va_arg(ValueArgs, const char *);
284 Values.push_back(std::make_pair(EnumName, // Add value to value map
285 std::make_pair(EnumVal, EnumDesc)));
286 }
287 }
288
289 template<class Opt>
290 void apply(Opt &O) const {
291 for (unsigned i = 0, e = Values.size(); i != e; ++i)
292 O.getParser().addLiteralOption(Values[i].first, Values[i].second.first,
293 Values[i].second.second);
294 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000295};
296
Chris Lattner331de232002-07-22 02:07:59 +0000297template<class DataType>
298ValuesClass<DataType> values(const char *Arg, DataType Val, const char *Desc,
299 ...) {
300 va_list ValueArgs;
301 va_start(ValueArgs, Desc);
302 ValuesClass<DataType> Vals(Arg, Val, Desc, ValueArgs);
303 va_end(ValueArgs);
304 return Vals;
305}
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000306
Chris Lattner331de232002-07-22 02:07:59 +0000307
308//===----------------------------------------------------------------------===//
309// parser class - Parameterizable parser for different data types. By default,
310// known data types (string, int, bool) have specialized parsers, that do what
311// you would expect. The default parser, used for data types that are not
312// built-in, uses a mapping table to map specific options to values, which is
313// used, among other things, to handle enum types.
314
315//--------------------------------------------------
316// generic_parser_base - This class holds all the non-generic code that we do
317// not need replicated for every instance of the generic parser. This also
318// allows us to put stuff into CommandLine.cpp
319//
320struct generic_parser_base {
321 virtual ~generic_parser_base() {} // Base class should have virtual-dtor
322
323 // getNumOptions - Virtual function implemented by generic subclass to
324 // indicate how many entries are in Values.
325 //
326 virtual unsigned getNumOptions() const = 0;
327
328 // getOption - Return option name N.
329 virtual const char *getOption(unsigned N) const = 0;
330
331 // getDescription - Return description N
332 virtual const char *getDescription(unsigned N) const = 0;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000333
334 // Return the width of the option tag for printing...
Chris Lattner331de232002-07-22 02:07:59 +0000335 virtual unsigned getOptionWidth(const Option &O) const;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000336
337 // printOptionInfo - Print out information about this option. The
338 // to-be-maintained width is specified.
339 //
Chris Lattner331de232002-07-22 02:07:59 +0000340 virtual void printOptionInfo(const Option &O, unsigned GlobalWidth) const;
Chris Lattner71fb7162002-05-22 17:03:05 +0000341
Chris Lattner331de232002-07-22 02:07:59 +0000342 void initialize(Option &O) {
343 // All of the modifiers for the option have been processed by now, so the
344 // argstr field should be stable, copy it down now.
345 //
346 hasArgStr = O.hasArgStr();
347
348 // If there has been no argstr specified, that means that we need to add an
349 // argument for every possible option. This ensures that our options are
350 // vectored to us.
351 //
352 if (!hasArgStr)
353 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
354 O.addArgument(getOption(i));
355 }
356
357 enum ValueExpected getValueExpectedFlagDefault() const {
358 // If there is an ArgStr specified, then we are of the form:
359 //
360 // -opt=O2 or -opt O2 or -optO2
361 //
362 // In which case, the value is required. Otherwise if an arg str has not
363 // been specified, we are of the form:
364 //
365 // -O2 or O2 or -la (where -l and -a are seperate options)
366 //
367 // If this is the case, we cannot allow a value.
368 //
369 if (hasArgStr)
370 return ValueRequired;
371 else
372 return ValueDisallowed;
373 }
374
Chris Lattneraf7e8212002-07-23 17:15:09 +0000375 // findOption - Return the option number corresponding to the specified
376 // argument string. If the option is not found, getNumOptions() is returned.
377 //
378 unsigned findOption(const char *Name);
379
Chris Lattner331de232002-07-22 02:07:59 +0000380protected:
381 bool hasArgStr;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000382};
383
Chris Lattner331de232002-07-22 02:07:59 +0000384// Default parser implementation - This implementation depends on having a
385// mapping of recognized options to values of some sort. In addition to this,
386// each entry in the mapping also tracks a help message that is printed with the
387// command line option for --help. Because this is a simple mapping parser, the
388// data type can be any unsupported type.
389//
390template <class DataType>
391class parser : public generic_parser_base {
Chris Lattneraf7e8212002-07-23 17:15:09 +0000392protected:
Chris Lattner331de232002-07-22 02:07:59 +0000393 std::vector<std::pair<const char *,
394 std::pair<DataType, const char *> > > Values;
Chris Lattneraf7e8212002-07-23 17:15:09 +0000395public:
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000396 typedef DataType parser_data_type;
Chris Lattner331de232002-07-22 02:07:59 +0000397
398 // Implement virtual functions needed by generic_parser_base
399 unsigned getNumOptions() const { return Values.size(); }
400 const char *getOption(unsigned N) const { return Values[N].first; }
401 const char *getDescription(unsigned N) const {
402 return Values[N].second.second;
403 }
404
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000405 // parse - Return true on error.
406 bool parse(Option &O, const char *ArgName, const std::string &Arg,
407 DataType &V) {
Chris Lattner7f4dd472002-07-24 22:08:36 +0000408 std::string ArgVal;
Chris Lattner331de232002-07-22 02:07:59 +0000409 if (hasArgStr)
410 ArgVal = Arg;
411 else
412 ArgVal = ArgName;
413
414 for (unsigned i = 0, e = Values.size(); i != e; ++i)
415 if (ArgVal == Values[i].first) {
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000416 V = Values[i].second.first;
Chris Lattner331de232002-07-22 02:07:59 +0000417 return false;
418 }
419
420 return O.error(": Cannot find option named '" + ArgVal + "'!");
421 }
422
423 // addLiteralOption - Add an entry to the mapping table...
424 template <class DT>
425 void addLiteralOption(const char *Name, const DT &V, const char *HelpStr) {
Chris Lattneraf7e8212002-07-23 17:15:09 +0000426 assert(findOption(Name) == Values.size() && "Option already exists!");
Chris Lattner331de232002-07-22 02:07:59 +0000427 Values.push_back(std::make_pair(Name, std::make_pair((DataType)V,HelpStr)));
428 }
Chris Lattneraf7e8212002-07-23 17:15:09 +0000429
430 // removeLiteralOption - Remove the specified option.
431 //
432 void removeLiteralOption(const char *Name) {
433 unsigned N = findOption(Name);
434 assert(N != Values.size() && "Option not found!");
435 Values.erase(Values.begin()+N);
436 }
Chris Lattner331de232002-07-22 02:07:59 +0000437};
438
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000439//--------------------------------------------------
440// basic_parser - Super class of parsers to provide boilerplate code
441//
442struct basic_parser_impl { // non-template implementation of basic_parser<t>
443 virtual ~basic_parser_impl() {}
444
445 enum ValueExpected getValueExpectedFlagDefault() const {
446 return ValueRequired;
447 }
448
449 void initialize(Option &O) {}
450
451 // Return the width of the option tag for printing...
452 unsigned getOptionWidth(const Option &O) const;
453
454 // printOptionInfo - Print out information about this option. The
455 // to-be-maintained width is specified.
456 //
457 void printOptionInfo(const Option &O, unsigned GlobalWidth) const;
458
459
460 // getValueName - Overload in subclass to provide a better default value.
461 virtual const char *getValueName() const { return "value"; }
462};
463
464// basic_parser - The real basic parser is just a template wrapper that provides
465// a typedef for the provided data type.
466//
467template<class DataType>
468struct basic_parser : public basic_parser_impl {
469 typedef DataType parser_data_type;
470};
471
Chris Lattner331de232002-07-22 02:07:59 +0000472
473//--------------------------------------------------
474// parser<bool>
475//
476template<>
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000477struct parser<bool> : public basic_parser<bool> {
478
479 // parse - Return true on error.
480 bool parse(Option &O, const char *ArgName, const std::string &Arg, bool &Val);
Chris Lattner331de232002-07-22 02:07:59 +0000481
482 enum ValueExpected getValueExpectedFlagDefault() const {
483 return ValueOptional;
484 }
485
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000486 // getValueName - Do not print =<value> at all
487 virtual const char *getValueName() const { return 0; }
Chris Lattner331de232002-07-22 02:07:59 +0000488};
489
490
491//--------------------------------------------------
492// parser<int>
493//
494template<>
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000495struct parser<int> : public basic_parser<int> {
Chris Lattner331de232002-07-22 02:07:59 +0000496
497 // parse - Return true on error.
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000498 bool parse(Option &O, const char *ArgName, const std::string &Arg, int &Val);
Chris Lattner331de232002-07-22 02:07:59 +0000499
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000500 // getValueName - Overload in subclass to provide a better default value.
501 virtual const char *getValueName() const { return "int"; }
Chris Lattner331de232002-07-22 02:07:59 +0000502};
503
504
505//--------------------------------------------------
506// parser<double>
507//
508template<>
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000509struct parser<double> : public basic_parser<double> {
Chris Lattner331de232002-07-22 02:07:59 +0000510 // parse - Return true on error.
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000511 bool parse(Option &O, const char *AN, const std::string &Arg, double &Val);
Chris Lattner331de232002-07-22 02:07:59 +0000512
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000513 // getValueName - Overload in subclass to provide a better default value.
514 virtual const char *getValueName() const { return "number"; }
Chris Lattner331de232002-07-22 02:07:59 +0000515};
516
Chris Lattner331de232002-07-22 02:07:59 +0000517
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000518//--------------------------------------------------
519// parser<float>
520//
521template<>
522struct parser<float> : public basic_parser<float> {
523 // parse - Return true on error.
524 bool parse(Option &O, const char *AN, const std::string &Arg, float &Val);
525
526 // getValueName - Overload in subclass to provide a better default value.
527 virtual const char *getValueName() const { return "number"; }
528};
Chris Lattner331de232002-07-22 02:07:59 +0000529
530
531//--------------------------------------------------
Chris Lattner7f4dd472002-07-24 22:08:36 +0000532// parser<std::string>
Chris Lattner331de232002-07-22 02:07:59 +0000533//
534template<>
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000535struct parser<std::string> : public basic_parser<std::string> {
Chris Lattner331de232002-07-22 02:07:59 +0000536 // parse - Return true on error.
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000537 bool parse(Option &O, const char *ArgName, const std::string &Arg,
538 std::string &Value) {
539 Value = Arg;
Chris Lattner331de232002-07-22 02:07:59 +0000540 return false;
541 }
Chris Lattner71fb7162002-05-22 17:03:05 +0000542
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000543 // getValueName - Overload in subclass to provide a better default value.
544 virtual const char *getValueName() const { return "string"; }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000545};
546
Chris Lattner71fb7162002-05-22 17:03:05 +0000547
Chris Lattner331de232002-07-22 02:07:59 +0000548
549//===----------------------------------------------------------------------===//
550// applicator class - This class is used because we must use partial
551// specialization to handle literal string arguments specially (const char* does
552// not correctly respond to the apply method). Because the syntax to use this
553// is a pain, we have the 'apply' method below to handle the nastiness...
554//
555template<class Mod> struct applicator {
556 template<class Opt>
557 static void opt(const Mod &M, Opt &O) { M.apply(O); }
558};
559
560// Handle const char* as a special case...
561template<unsigned n> struct applicator<char[n]> {
562 template<class Opt>
563 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
564};
Chris Lattner40423322002-09-13 14:33:39 +0000565template<unsigned n> struct applicator<const char[n]> {
566 template<class Opt>
567 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
568};
Chris Lattner331de232002-07-22 02:07:59 +0000569template<> struct applicator<const char*> {
570 template<class Opt>
571 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
572};
573
574template<> struct applicator<NumOccurances> {
575 static void opt(NumOccurances NO, Option &O) { O.setNumOccurancesFlag(NO); }
576};
577template<> struct applicator<ValueExpected> {
578 static void opt(ValueExpected VE, Option &O) { O.setValueExpectedFlag(VE); }
579};
580template<> struct applicator<OptionHidden> {
581 static void opt(OptionHidden OH, Option &O) { O.setHiddenFlag(OH); }
582};
583template<> struct applicator<FormattingFlags> {
584 static void opt(FormattingFlags FF, Option &O) { O.setFormattingFlag(FF); }
585};
586
587// apply method - Apply a modifier to an option in a type safe way.
588template<class Mod, class Opt>
589void apply(const Mod &M, Opt *O) {
590 applicator<Mod>::opt(M, *O);
591}
592
593
594//===----------------------------------------------------------------------===//
595// opt_storage class
596
597// Default storage class definition: external storage. This implementation
598// assumes the user will specify a variable to store the data into with the
599// cl::location(x) modifier.
600//
601template<class DataType, bool ExternalStorage, bool isClass>
602class opt_storage {
603 DataType *Location; // Where to store the object...
604
605 void check() {
606 assert(Location != 0 && "cl::location(...) not specified for a command "
607 "line option with external storage!");
608 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000609public:
Chris Lattner331de232002-07-22 02:07:59 +0000610 opt_storage() : Location(0) {}
611
612 bool setLocation(Option &O, DataType &L) {
613 if (Location)
614 return O.error(": cl::location(x) specified more than once!");
615 Location = &L;
616 return false;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000617 }
618
Chris Lattner331de232002-07-22 02:07:59 +0000619 template<class T>
620 void setValue(const T &V) {
621 check();
622 *Location = V;
623 }
624
625 DataType &getValue() { check(); return *Location; }
626 const DataType &getValue() const { check(); return *Location; }
627};
628
629
630// Define how to hold a class type object, such as a string. Since we can
631// inherit from a class, we do so. This makes us exactly compatible with the
632// object in all cases that it is used.
633//
634template<class DataType>
635struct opt_storage<DataType,false,true> : public DataType {
636
637 template<class T>
638 void setValue(const T &V) { DataType::operator=(V); }
639
640 DataType &getValue() { return *this; }
641 const DataType &getValue() const { return *this; }
642};
643
644// Define a partial specialization to handle things we cannot inherit from. In
645// this case, we store an instance through containment, and overload operators
646// to get at the value.
647//
648template<class DataType>
649struct opt_storage<DataType, false, false> {
650 DataType Value;
651
652 // Make sure we initialize the value with the default constructor for the
653 // type.
654 opt_storage() : Value(DataType()) {}
655
656 template<class T>
657 void setValue(const T &V) { Value = V; }
658 DataType &getValue() { return Value; }
659 DataType getValue() const { return Value; }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000660};
661
662
663//===----------------------------------------------------------------------===//
Chris Lattner331de232002-07-22 02:07:59 +0000664// opt - A scalar command line option.
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000665//
Chris Lattner331de232002-07-22 02:07:59 +0000666template <class DataType, bool ExternalStorage = false,
667 class ParserClass = parser<DataType> >
668class opt : public Option,
669 public opt_storage<DataType, ExternalStorage,
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000670 ::boost::is_class<DataType>::value> {
Chris Lattner331de232002-07-22 02:07:59 +0000671 ParserClass Parser;
672
673 virtual bool handleOccurance(const char *ArgName, const std::string &Arg) {
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000674 typename ParserClass::parser_data_type Val;
675 if (Parser.parse(*this, ArgName, Arg, Val))
676 return true; // Parse error!
677 setValue(Val);
678 return false;
Chris Lattner331de232002-07-22 02:07:59 +0000679 }
680
681 virtual enum ValueExpected getValueExpectedFlagDefault() const {
682 return Parser.getValueExpectedFlagDefault();
683 }
684
685 // Forward printing stuff to the parser...
686 virtual unsigned getOptionWidth() const {return Parser.getOptionWidth(*this);}
687 virtual void printOptionInfo(unsigned GlobalWidth) const {
688 Parser.printOptionInfo(*this, GlobalWidth);
689 }
690
691 void done() {
692 addArgument(ArgStr);
693 Parser.initialize(*this);
694 }
695public:
696 // setInitialValue - Used by the cl::init modifier...
697 void setInitialValue(const DataType &V) { setValue(V); }
698
Chris Lattner331de232002-07-22 02:07:59 +0000699 ParserClass &getParser() { return Parser; }
700
701 operator DataType() const { return getValue(); }
702
703 template<class T>
704 DataType &operator=(const T &Val) { setValue(Val); return getValue(); }
705
706 // One option...
707 template<class M0t>
708 opt(const M0t &M0) {
709 apply(M0, this);
710 done();
711 }
712
713 // Two options...
714 template<class M0t, class M1t>
715 opt(const M0t &M0, const M1t &M1) {
716 apply(M0, this); apply(M1, this);
717 done();
718 }
719
720 // Three options...
721 template<class M0t, class M1t, class M2t>
722 opt(const M0t &M0, const M1t &M1, const M2t &M2) {
723 apply(M0, this); apply(M1, this); apply(M2, this);
724 done();
725 }
726 // Four options...
727 template<class M0t, class M1t, class M2t, class M3t>
728 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3) {
729 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
730 done();
731 }
732 // Five options...
733 template<class M0t, class M1t, class M2t, class M3t, class M4t>
734 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
735 const M4t &M4) {
736 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
737 apply(M4, this);
738 done();
739 }
740 // Six options...
741 template<class M0t, class M1t, class M2t, class M3t,
742 class M4t, class M5t>
743 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
744 const M4t &M4, const M5t &M5) {
745 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
746 apply(M4, this); apply(M5, this);
747 done();
748 }
749 // Seven options...
750 template<class M0t, class M1t, class M2t, class M3t,
751 class M4t, class M5t, class M6t>
752 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
753 const M4t &M4, const M5t &M5, const M6t &M6) {
754 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
755 apply(M4, this); apply(M5, this); apply(M6, this);
756 done();
757 }
758 // Eight options...
759 template<class M0t, class M1t, class M2t, class M3t,
760 class M4t, class M5t, class M6t, class M7t>
761 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
762 const M4t &M4, const M5t &M5, const M6t &M6, const M7t &M7) {
763 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
764 apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
765 done();
766 }
767};
768
769//===----------------------------------------------------------------------===//
770// list_storage class
771
772// Default storage class definition: external storage. This implementation
773// assumes the user will specify a variable to store the data into with the
774// cl::location(x) modifier.
775//
776template<class DataType, class StorageClass>
777class list_storage {
778 StorageClass *Location; // Where to store the object...
779
780public:
781 list_storage() : Location(0) {}
782
783 bool setLocation(Option &O, StorageClass &L) {
784 if (Location)
785 return O.error(": cl::location(x) specified more than once!");
786 Location = &L;
787 return false;
788 }
789
790 template<class T>
791 void addValue(const T &V) {
792 assert(Location != 0 && "cl::location(...) not specified for a command "
793 "line option with external storage!");
794 Location->push_back(V);
795 }
796};
797
798
799// Define how to hold a class type object, such as a string. Since we can
800// inherit from a class, we do so. This makes us exactly compatible with the
801// object in all cases that it is used.
802//
803template<class DataType>
804struct list_storage<DataType, bool> : public std::vector<DataType> {
805
806 template<class T>
807 void addValue(const T &V) { push_back(V); }
808};
809
810
811//===----------------------------------------------------------------------===//
812// list - A list of command line options.
813//
814template <class DataType, class Storage = bool,
815 class ParserClass = parser<DataType> >
816class list : public Option, public list_storage<DataType, Storage> {
817 ParserClass Parser;
818
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000819 virtual enum NumOccurances getNumOccurancesFlagDefault() const {
820 return ZeroOrMore;
821 }
822 virtual enum ValueExpected getValueExpectedFlagDefault() const {
Chris Lattner331de232002-07-22 02:07:59 +0000823 return Parser.getValueExpectedFlagDefault();
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000824 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000825
Chris Lattner331de232002-07-22 02:07:59 +0000826 virtual bool handleOccurance(const char *ArgName, const std::string &Arg) {
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000827 typename ParserClass::parser_data_type Val;
828 if (Parser.parse(*this, ArgName, Arg, Val))
829 return true; // Parse Error!
830 addValue(Val);
831 return false;
Chris Lattner331de232002-07-22 02:07:59 +0000832 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000833
Chris Lattner331de232002-07-22 02:07:59 +0000834 // Forward printing stuff to the parser...
835 virtual unsigned getOptionWidth() const {return Parser.getOptionWidth(*this);}
836 virtual void printOptionInfo(unsigned GlobalWidth) const {
837 Parser.printOptionInfo(*this, GlobalWidth);
838 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000839
Chris Lattner331de232002-07-22 02:07:59 +0000840 void done() {
841 addArgument(ArgStr);
842 Parser.initialize(*this);
843 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000844public:
Chris Lattner331de232002-07-22 02:07:59 +0000845 ParserClass &getParser() { return Parser; }
846
847 // One option...
848 template<class M0t>
849 list(const M0t &M0) {
850 apply(M0, this);
851 done();
852 }
853 // Two options...
854 template<class M0t, class M1t>
855 list(const M0t &M0, const M1t &M1) {
856 apply(M0, this); apply(M1, this);
857 done();
858 }
859 // Three options...
860 template<class M0t, class M1t, class M2t>
861 list(const M0t &M0, const M1t &M1, const M2t &M2) {
862 apply(M0, this); apply(M1, this); apply(M2, this);
863 done();
864 }
865 // Four options...
866 template<class M0t, class M1t, class M2t, class M3t>
867 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3) {
868 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
869 done();
870 }
871 // Five options...
872 template<class M0t, class M1t, class M2t, class M3t, class M4t>
873 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
874 const M4t &M4) {
875 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
876 apply(M4, this);
877 done();
878 }
879 // Six options...
880 template<class M0t, class M1t, class M2t, class M3t,
881 class M4t, class M5t>
882 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
883 const M4t &M4, const M5t &M5) {
884 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
885 apply(M4, this); apply(M5, this);
886 done();
887 }
888 // Seven options...
889 template<class M0t, class M1t, class M2t, class M3t,
890 class M4t, class M5t, class M6t>
891 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
892 const M4t &M4, const M5t &M5, const M6t &M6) {
893 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
894 apply(M4, this); apply(M5, this); apply(M6, this);
895 done();
896 }
897 // Eight options...
898 template<class M0t, class M1t, class M2t, class M3t,
899 class M4t, class M5t, class M6t, class M7t>
900 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
901 const M4t &M4, const M5t &M5, const M6t &M6, const M7t &M7) {
902 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
903 apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
904 done();
905 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000906};
907
Chris Lattner331de232002-07-22 02:07:59 +0000908
909
910//===----------------------------------------------------------------------===//
911// Aliased command line option (alias this name to a preexisting name)
912//
913
914class alias : public Option {
915 Option *AliasFor;
916 virtual bool handleOccurance(const char *ArgName, const std::string &Arg) {
917 return AliasFor->handleOccurance(AliasFor->ArgStr, Arg);
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000918 }
Chris Lattner331de232002-07-22 02:07:59 +0000919 // Aliases default to be hidden...
920 virtual enum OptionHidden getOptionHiddenFlagDefault() const {return Hidden;}
921
922 // Handle printing stuff...
923 virtual unsigned getOptionWidth() const;
924 virtual void printOptionInfo(unsigned GlobalWidth) const;
925
926 void done() {
927 if (!hasArgStr())
928 error(": cl::alias must have argument name specified!");
929 if (AliasFor == 0)
930 error(": cl::alias must have an cl::aliasopt(option) specified!");
931 addArgument(ArgStr);
932 }
933public:
934 void setAliasFor(Option &O) {
935 if (AliasFor)
936 error(": cl::alias must only have one cl::aliasopt(...) specified!");
937 AliasFor = &O;
938 }
939
940 // One option...
941 template<class M0t>
942 alias(const M0t &M0) : AliasFor(0) {
943 apply(M0, this);
944 done();
945 }
946 // Two options...
947 template<class M0t, class M1t>
948 alias(const M0t &M0, const M1t &M1) : AliasFor(0) {
949 apply(M0, this); apply(M1, this);
950 done();
951 }
952 // Three options...
953 template<class M0t, class M1t, class M2t>
954 alias(const M0t &M0, const M1t &M1, const M2t &M2) : AliasFor(0) {
955 apply(M0, this); apply(M1, this); apply(M2, this);
956 done();
957 }
958 // Four options...
959 template<class M0t, class M1t, class M2t, class M3t>
960 alias(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3)
961 : AliasFor(0) {
962 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
963 done();
964 }
965};
966
967// aliasfor - Modifier to set the option an alias aliases.
968struct aliasopt {
969 Option &Opt;
970 aliasopt(Option &O) : Opt(O) {}
971 void apply(alias &A) const { A.setAliasFor(Opt); }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000972};
973
974} // End namespace cl
975
976#endif