blob: 6c7a9fc2aec7446ad18586ae66b343bc12b22baf [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>
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"
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?
Chris Lattnerb3b729b2003-05-22 20:25:57 +000053 ValueOptional = 0x08, // The value can appear... or not
Chris Lattnercee8f9a2001-11-27 00:03:19 +000054 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 Lattnerb3b729b2003-05-22 20:25:57 +000089enum MiscFlags { // Miscellaneous flags to adjust argument
90 CommaSeparated = 0x200, // Should this cl::list split between commas?
91 MiscMask = 0x200,
92};
93
94
Chris Lattnercee8f9a2001-11-27 00:03:19 +000095
96//===----------------------------------------------------------------------===//
97// Option Base class
98//
Chris Lattner331de232002-07-22 02:07:59 +000099class alias;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000100class Option {
101 friend void cl::ParseCommandLineOptions(int &, char **, const char *, int);
Chris Lattner331de232002-07-22 02:07:59 +0000102 friend class alias;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000103
104 // handleOccurances - Overriden by subclasses to handle the value passed into
105 // an argument. Should return true if there was an error processing the
106 // argument and the program should exit.
107 //
Chris Lattner697954c2002-01-20 22:54:45 +0000108 virtual bool handleOccurance(const char *ArgName, const std::string &Arg) = 0;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000109
110 virtual enum NumOccurances getNumOccurancesFlagDefault() const {
111 return Optional;
112 }
113 virtual enum ValueExpected getValueExpectedFlagDefault() const {
114 return ValueOptional;
115 }
116 virtual enum OptionHidden getOptionHiddenFlagDefault() const {
117 return NotHidden;
118 }
Chris Lattner331de232002-07-22 02:07:59 +0000119 virtual enum FormattingFlags getFormattingFlagDefault() const {
120 return NormalFormatting;
121 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000122
Chris Lattner331de232002-07-22 02:07:59 +0000123 int NumOccurances; // The number of times specified
124 int Flags; // Flags for the argument
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000125public:
Chris Lattner331de232002-07-22 02:07:59 +0000126 const char *ArgStr; // The argument string itself (ex: "help", "o")
127 const char *HelpStr; // The descriptive text message for --help
128 const char *ValueStr; // String describing what the value of this option is
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000129
130 inline enum NumOccurances getNumOccurancesFlag() const {
131 int NO = Flags & OccurancesMask;
132 return NO ? (enum NumOccurances)NO : getNumOccurancesFlagDefault();
133 }
134 inline enum ValueExpected getValueExpectedFlag() const {
135 int VE = Flags & ValueMask;
136 return VE ? (enum ValueExpected)VE : getValueExpectedFlagDefault();
137 }
138 inline enum OptionHidden getOptionHiddenFlag() const {
139 int OH = Flags & HiddenMask;
140 return OH ? (enum OptionHidden)OH : getOptionHiddenFlagDefault();
141 }
Chris Lattner331de232002-07-22 02:07:59 +0000142 inline enum FormattingFlags getFormattingFlag() const {
143 int OH = Flags & FormattingMask;
144 return OH ? (enum FormattingFlags)OH : getFormattingFlagDefault();
145 }
Chris Lattnerb3b729b2003-05-22 20:25:57 +0000146 inline unsigned getMiscFlags() const {
147 return Flags & MiscMask;
148 }
Chris Lattner331de232002-07-22 02:07:59 +0000149
150 // hasArgStr - Return true if the argstr != ""
151 bool hasArgStr() const { return ArgStr[0] != 0; }
152
153 //-------------------------------------------------------------------------===
154 // Accessor functions set by OptionModifiers
155 //
156 void setArgStr(const char *S) { ArgStr = S; }
157 void setDescription(const char *S) { HelpStr = S; }
158 void setValueStr(const char *S) { ValueStr = S; }
159
160 void setFlag(unsigned Flag, unsigned FlagMask) {
161 if (Flags & FlagMask) {
162 error(": Specified two settings for the same option!");
163 exit(1);
164 }
165
166 Flags |= Flag;
167 }
168
169 void setNumOccurancesFlag(enum NumOccurances Val) {
170 setFlag(Val, OccurancesMask);
171 }
172 void setValueExpectedFlag(enum ValueExpected Val) { setFlag(Val, ValueMask); }
173 void setHiddenFlag(enum OptionHidden Val) { setFlag(Val, HiddenMask); }
174 void setFormattingFlag(enum FormattingFlags V) { setFlag(V, FormattingMask); }
Chris Lattnerb3b729b2003-05-22 20:25:57 +0000175 void setMiscFlag(enum MiscFlags M) { setFlag(M, M); }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000176protected:
Chris Lattner331de232002-07-22 02:07:59 +0000177 Option() : NumOccurances(0), Flags(0),
178 ArgStr(""), HelpStr(""), ValueStr("") {}
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000179
180public:
Chris Lattner331de232002-07-22 02:07:59 +0000181 // addArgument - Tell the system that this Option subclass will handle all
182 // occurances of -ArgStr on the command line.
183 //
184 void addArgument(const char *ArgStr);
Chris Lattnerae1257a2002-07-23 00:44:37 +0000185 void removeArgument(const char *ArgStr);
Chris Lattner331de232002-07-22 02:07:59 +0000186
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000187 // Return the width of the option tag for printing...
Chris Lattner331de232002-07-22 02:07:59 +0000188 virtual unsigned getOptionWidth() const = 0;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000189
190 // printOptionInfo - Print out information about this option. The
191 // to-be-maintained width is specified.
192 //
Chris Lattner331de232002-07-22 02:07:59 +0000193 virtual void printOptionInfo(unsigned GlobalWidth) const = 0;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000194
195 // addOccurance - Wrapper around handleOccurance that enforces Flags
196 //
Chris Lattner697954c2002-01-20 22:54:45 +0000197 bool addOccurance(const char *ArgName, const std::string &Value);
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000198
199 // Prints option name followed by message. Always returns true.
Chris Lattner697954c2002-01-20 22:54:45 +0000200 bool error(std::string Message, const char *ArgName = 0);
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000201
202public:
203 inline int getNumOccurances() const { return NumOccurances; }
204 virtual ~Option() {}
205};
206
207
208//===----------------------------------------------------------------------===//
Chris Lattner331de232002-07-22 02:07:59 +0000209// Command line option modifiers that can be used to modify the behavior of
210// command line option parsers...
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000211//
Chris Lattner331de232002-07-22 02:07:59 +0000212
213// desc - Modifier to set the description shown in the --help output...
214struct desc {
215 const char *Desc;
216 desc(const char *Str) : Desc(Str) {}
217 void apply(Option &O) const { O.setDescription(Desc); }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000218};
219
Chris Lattner331de232002-07-22 02:07:59 +0000220// value_desc - Modifier to set the value description shown in the --help
221// output...
222struct value_desc {
223 const char *Desc;
224 value_desc(const char *Str) : Desc(Str) {}
225 void apply(Option &O) const { O.setValueStr(Desc); }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000226};
227
228
Chris Lattner331de232002-07-22 02:07:59 +0000229// init - Specify a default (initial) value for the command line argument, if
230// the default constructor for the argument type does not give you what you
231// want. This is only valid on "opt" arguments, not on "list" arguments.
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000232//
Chris Lattner331de232002-07-22 02:07:59 +0000233template<class Ty>
234struct initializer {
235 const Ty &Init;
236 initializer(const Ty &Val) : Init(Val) {}
237
238 template<class Opt>
239 void apply(Opt &O) const { O.setInitialValue(Init); }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000240};
241
Chris Lattner331de232002-07-22 02:07:59 +0000242template<class Ty>
243initializer<Ty> init(const Ty &Val) {
244 return initializer<Ty>(Val);
245}
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000246
Chris Lattner331de232002-07-22 02:07:59 +0000247
248// location - Allow the user to specify which external variable they want to
249// store the results of the command line argument processing into, if they don't
250// want to store it in the option itself.
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000251//
Chris Lattner331de232002-07-22 02:07:59 +0000252template<class Ty>
253struct LocationClass {
254 Ty &Loc;
255 LocationClass(Ty &L) : Loc(L) {}
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000256
Chris Lattner331de232002-07-22 02:07:59 +0000257 template<class Opt>
258 void apply(Opt &O) const { O.setLocation(O, Loc); }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000259};
260
Chris Lattner331de232002-07-22 02:07:59 +0000261template<class Ty>
262LocationClass<Ty> location(Ty &L) { return LocationClass<Ty>(L); }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000263
264
265//===----------------------------------------------------------------------===//
266// Enum valued command line option
267//
Chris Lattnerae1257a2002-07-23 00:44:37 +0000268#define clEnumVal(ENUMVAL, DESC) #ENUMVAL, (int)ENUMVAL, DESC
269#define clEnumValN(ENUMVAL, FLAGNAME, DESC) FLAGNAME, (int)ENUMVAL, DESC
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000270
Chris Lattner331de232002-07-22 02:07:59 +0000271// values - For custom data types, allow specifying a group of values together
272// as the values that go into the mapping that the option handler uses. Note
273// that the values list must always have a 0 at the end of the list to indicate
274// that the list has ended.
275//
276template<class DataType>
277class ValuesClass {
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000278 // Use a vector instead of a map, because the lists should be short,
279 // the overhead is less, and most importantly, it keeps them in the order
280 // inserted so we can print our option out nicely.
Chris Lattner331de232002-07-22 02:07:59 +0000281 std::vector<std::pair<const char *, std::pair<int, const char *> > > Values;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000282 void processValues(va_list Vals);
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000283public:
Chris Lattner331de232002-07-22 02:07:59 +0000284 ValuesClass(const char *EnumName, DataType Val, const char *Desc,
285 va_list ValueArgs) {
286 // Insert the first value, which is required.
287 Values.push_back(std::make_pair(EnumName, std::make_pair(Val, Desc)));
288
289 // Process the varargs portion of the values...
290 while (const char *EnumName = va_arg(ValueArgs, const char *)) {
Chris Lattnerae1257a2002-07-23 00:44:37 +0000291 DataType EnumVal = (DataType)va_arg(ValueArgs, int);
Chris Lattner331de232002-07-22 02:07:59 +0000292 const char *EnumDesc = va_arg(ValueArgs, const char *);
293 Values.push_back(std::make_pair(EnumName, // Add value to value map
294 std::make_pair(EnumVal, EnumDesc)));
295 }
296 }
297
298 template<class Opt>
299 void apply(Opt &O) const {
300 for (unsigned i = 0, e = Values.size(); i != e; ++i)
301 O.getParser().addLiteralOption(Values[i].first, Values[i].second.first,
302 Values[i].second.second);
303 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000304};
305
Chris Lattner331de232002-07-22 02:07:59 +0000306template<class DataType>
307ValuesClass<DataType> values(const char *Arg, DataType Val, const char *Desc,
308 ...) {
309 va_list ValueArgs;
310 va_start(ValueArgs, Desc);
311 ValuesClass<DataType> Vals(Arg, Val, Desc, ValueArgs);
312 va_end(ValueArgs);
313 return Vals;
314}
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000315
Chris Lattner331de232002-07-22 02:07:59 +0000316
317//===----------------------------------------------------------------------===//
318// parser class - Parameterizable parser for different data types. By default,
319// known data types (string, int, bool) have specialized parsers, that do what
320// you would expect. The default parser, used for data types that are not
321// built-in, uses a mapping table to map specific options to values, which is
322// used, among other things, to handle enum types.
323
324//--------------------------------------------------
325// generic_parser_base - This class holds all the non-generic code that we do
326// not need replicated for every instance of the generic parser. This also
327// allows us to put stuff into CommandLine.cpp
328//
329struct generic_parser_base {
330 virtual ~generic_parser_base() {} // Base class should have virtual-dtor
331
332 // getNumOptions - Virtual function implemented by generic subclass to
333 // indicate how many entries are in Values.
334 //
335 virtual unsigned getNumOptions() const = 0;
336
337 // getOption - Return option name N.
338 virtual const char *getOption(unsigned N) const = 0;
339
340 // getDescription - Return description N
341 virtual const char *getDescription(unsigned N) const = 0;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000342
343 // Return the width of the option tag for printing...
Chris Lattner331de232002-07-22 02:07:59 +0000344 virtual unsigned getOptionWidth(const Option &O) const;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000345
346 // printOptionInfo - Print out information about this option. The
347 // to-be-maintained width is specified.
348 //
Chris Lattner331de232002-07-22 02:07:59 +0000349 virtual void printOptionInfo(const Option &O, unsigned GlobalWidth) const;
Chris Lattner71fb7162002-05-22 17:03:05 +0000350
Chris Lattner331de232002-07-22 02:07:59 +0000351 void initialize(Option &O) {
352 // All of the modifiers for the option have been processed by now, so the
353 // argstr field should be stable, copy it down now.
354 //
355 hasArgStr = O.hasArgStr();
356
357 // If there has been no argstr specified, that means that we need to add an
358 // argument for every possible option. This ensures that our options are
359 // vectored to us.
360 //
361 if (!hasArgStr)
362 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
363 O.addArgument(getOption(i));
364 }
365
366 enum ValueExpected getValueExpectedFlagDefault() const {
367 // If there is an ArgStr specified, then we are of the form:
368 //
369 // -opt=O2 or -opt O2 or -optO2
370 //
371 // In which case, the value is required. Otherwise if an arg str has not
372 // been specified, we are of the form:
373 //
374 // -O2 or O2 or -la (where -l and -a are seperate options)
375 //
376 // If this is the case, we cannot allow a value.
377 //
378 if (hasArgStr)
379 return ValueRequired;
380 else
381 return ValueDisallowed;
382 }
383
Chris Lattneraf7e8212002-07-23 17:15:09 +0000384 // findOption - Return the option number corresponding to the specified
385 // argument string. If the option is not found, getNumOptions() is returned.
386 //
387 unsigned findOption(const char *Name);
388
Chris Lattner331de232002-07-22 02:07:59 +0000389protected:
390 bool hasArgStr;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000391};
392
Chris Lattner331de232002-07-22 02:07:59 +0000393// Default parser implementation - This implementation depends on having a
394// mapping of recognized options to values of some sort. In addition to this,
395// each entry in the mapping also tracks a help message that is printed with the
396// command line option for --help. Because this is a simple mapping parser, the
397// data type can be any unsupported type.
398//
399template <class DataType>
400class parser : public generic_parser_base {
Chris Lattneraf7e8212002-07-23 17:15:09 +0000401protected:
Chris Lattner331de232002-07-22 02:07:59 +0000402 std::vector<std::pair<const char *,
403 std::pair<DataType, const char *> > > Values;
Chris Lattneraf7e8212002-07-23 17:15:09 +0000404public:
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000405 typedef DataType parser_data_type;
Chris Lattner331de232002-07-22 02:07:59 +0000406
407 // Implement virtual functions needed by generic_parser_base
408 unsigned getNumOptions() const { return Values.size(); }
409 const char *getOption(unsigned N) const { return Values[N].first; }
410 const char *getDescription(unsigned N) const {
411 return Values[N].second.second;
412 }
413
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000414 // parse - Return true on error.
415 bool parse(Option &O, const char *ArgName, const std::string &Arg,
416 DataType &V) {
Chris Lattner7f4dd472002-07-24 22:08:36 +0000417 std::string ArgVal;
Chris Lattner331de232002-07-22 02:07:59 +0000418 if (hasArgStr)
419 ArgVal = Arg;
420 else
421 ArgVal = ArgName;
422
423 for (unsigned i = 0, e = Values.size(); i != e; ++i)
424 if (ArgVal == Values[i].first) {
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000425 V = Values[i].second.first;
Chris Lattner331de232002-07-22 02:07:59 +0000426 return false;
427 }
428
429 return O.error(": Cannot find option named '" + ArgVal + "'!");
430 }
431
432 // addLiteralOption - Add an entry to the mapping table...
433 template <class DT>
434 void addLiteralOption(const char *Name, const DT &V, const char *HelpStr) {
Chris Lattneraf7e8212002-07-23 17:15:09 +0000435 assert(findOption(Name) == Values.size() && "Option already exists!");
Chris Lattner331de232002-07-22 02:07:59 +0000436 Values.push_back(std::make_pair(Name, std::make_pair((DataType)V,HelpStr)));
437 }
Chris Lattneraf7e8212002-07-23 17:15:09 +0000438
439 // removeLiteralOption - Remove the specified option.
440 //
441 void removeLiteralOption(const char *Name) {
442 unsigned N = findOption(Name);
443 assert(N != Values.size() && "Option not found!");
444 Values.erase(Values.begin()+N);
445 }
Chris Lattner331de232002-07-22 02:07:59 +0000446};
447
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000448//--------------------------------------------------
449// basic_parser - Super class of parsers to provide boilerplate code
450//
451struct basic_parser_impl { // non-template implementation of basic_parser<t>
452 virtual ~basic_parser_impl() {}
453
454 enum ValueExpected getValueExpectedFlagDefault() const {
455 return ValueRequired;
456 }
457
458 void initialize(Option &O) {}
459
460 // Return the width of the option tag for printing...
461 unsigned getOptionWidth(const Option &O) const;
462
463 // printOptionInfo - Print out information about this option. The
464 // to-be-maintained width is specified.
465 //
466 void printOptionInfo(const Option &O, unsigned GlobalWidth) const;
467
468
469 // getValueName - Overload in subclass to provide a better default value.
470 virtual const char *getValueName() const { return "value"; }
471};
472
473// basic_parser - The real basic parser is just a template wrapper that provides
474// a typedef for the provided data type.
475//
476template<class DataType>
477struct basic_parser : public basic_parser_impl {
478 typedef DataType parser_data_type;
479};
480
Chris Lattner331de232002-07-22 02:07:59 +0000481
482//--------------------------------------------------
483// parser<bool>
484//
485template<>
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000486struct parser<bool> : public basic_parser<bool> {
487
488 // parse - Return true on error.
489 bool parse(Option &O, const char *ArgName, const std::string &Arg, bool &Val);
Chris Lattner331de232002-07-22 02:07:59 +0000490
491 enum ValueExpected getValueExpectedFlagDefault() const {
492 return ValueOptional;
493 }
494
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000495 // getValueName - Do not print =<value> at all
496 virtual const char *getValueName() const { return 0; }
Chris Lattner331de232002-07-22 02:07:59 +0000497};
498
499
500//--------------------------------------------------
501// parser<int>
502//
503template<>
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000504struct parser<int> : public basic_parser<int> {
Chris Lattner331de232002-07-22 02:07:59 +0000505
506 // parse - Return true on error.
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000507 bool parse(Option &O, const char *ArgName, const std::string &Arg, int &Val);
Chris Lattner331de232002-07-22 02:07:59 +0000508
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000509 // getValueName - Overload in subclass to provide a better default value.
510 virtual const char *getValueName() const { return "int"; }
Chris Lattner331de232002-07-22 02:07:59 +0000511};
512
513
514//--------------------------------------------------
515// parser<double>
516//
517template<>
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000518struct parser<double> : public basic_parser<double> {
Chris Lattner331de232002-07-22 02:07:59 +0000519 // parse - Return true on error.
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000520 bool parse(Option &O, const char *AN, const std::string &Arg, double &Val);
Chris Lattner331de232002-07-22 02:07:59 +0000521
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000522 // getValueName - Overload in subclass to provide a better default value.
523 virtual const char *getValueName() const { return "number"; }
Chris Lattner331de232002-07-22 02:07:59 +0000524};
525
Chris Lattner331de232002-07-22 02:07:59 +0000526
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000527//--------------------------------------------------
528// parser<float>
529//
530template<>
531struct parser<float> : public basic_parser<float> {
532 // parse - Return true on error.
533 bool parse(Option &O, const char *AN, const std::string &Arg, float &Val);
534
535 // getValueName - Overload in subclass to provide a better default value.
536 virtual const char *getValueName() const { return "number"; }
537};
Chris Lattner331de232002-07-22 02:07:59 +0000538
539
540//--------------------------------------------------
Chris Lattner7f4dd472002-07-24 22:08:36 +0000541// parser<std::string>
Chris Lattner331de232002-07-22 02:07:59 +0000542//
543template<>
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000544struct parser<std::string> : public basic_parser<std::string> {
Chris Lattner331de232002-07-22 02:07:59 +0000545 // parse - Return true on error.
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000546 bool parse(Option &O, const char *ArgName, const std::string &Arg,
547 std::string &Value) {
548 Value = Arg;
Chris Lattner331de232002-07-22 02:07:59 +0000549 return false;
550 }
Chris Lattner71fb7162002-05-22 17:03:05 +0000551
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000552 // getValueName - Overload in subclass to provide a better default value.
553 virtual const char *getValueName() const { return "string"; }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000554};
555
Chris Lattner71fb7162002-05-22 17:03:05 +0000556
Chris Lattner331de232002-07-22 02:07:59 +0000557
558//===----------------------------------------------------------------------===//
559// applicator class - This class is used because we must use partial
560// specialization to handle literal string arguments specially (const char* does
561// not correctly respond to the apply method). Because the syntax to use this
562// is a pain, we have the 'apply' method below to handle the nastiness...
563//
564template<class Mod> struct applicator {
565 template<class Opt>
566 static void opt(const Mod &M, Opt &O) { M.apply(O); }
567};
568
569// Handle const char* as a special case...
570template<unsigned n> struct applicator<char[n]> {
571 template<class Opt>
572 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
573};
Chris Lattner40423322002-09-13 14:33:39 +0000574template<unsigned n> struct applicator<const char[n]> {
575 template<class Opt>
576 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
577};
Chris Lattner331de232002-07-22 02:07:59 +0000578template<> struct applicator<const char*> {
579 template<class Opt>
580 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
581};
582
583template<> struct applicator<NumOccurances> {
584 static void opt(NumOccurances NO, Option &O) { O.setNumOccurancesFlag(NO); }
585};
586template<> struct applicator<ValueExpected> {
587 static void opt(ValueExpected VE, Option &O) { O.setValueExpectedFlag(VE); }
588};
589template<> struct applicator<OptionHidden> {
590 static void opt(OptionHidden OH, Option &O) { O.setHiddenFlag(OH); }
591};
592template<> struct applicator<FormattingFlags> {
593 static void opt(FormattingFlags FF, Option &O) { O.setFormattingFlag(FF); }
594};
Chris Lattnerb3b729b2003-05-22 20:25:57 +0000595template<> struct applicator<MiscFlags> {
596 static void opt(MiscFlags MF, Option &O) { O.setMiscFlag(MF); }
597};
Chris Lattner331de232002-07-22 02:07:59 +0000598
599// apply method - Apply a modifier to an option in a type safe way.
600template<class Mod, class Opt>
601void apply(const Mod &M, Opt *O) {
602 applicator<Mod>::opt(M, *O);
603}
604
605
606//===----------------------------------------------------------------------===//
607// opt_storage class
608
609// Default storage class definition: external storage. This implementation
610// assumes the user will specify a variable to store the data into with the
611// cl::location(x) modifier.
612//
613template<class DataType, bool ExternalStorage, bool isClass>
614class opt_storage {
615 DataType *Location; // Where to store the object...
616
617 void check() {
618 assert(Location != 0 && "cl::location(...) not specified for a command "
619 "line option with external storage!");
620 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000621public:
Chris Lattner331de232002-07-22 02:07:59 +0000622 opt_storage() : Location(0) {}
623
624 bool setLocation(Option &O, DataType &L) {
625 if (Location)
626 return O.error(": cl::location(x) specified more than once!");
627 Location = &L;
628 return false;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000629 }
630
Chris Lattner331de232002-07-22 02:07:59 +0000631 template<class T>
632 void setValue(const T &V) {
633 check();
634 *Location = V;
635 }
636
637 DataType &getValue() { check(); return *Location; }
638 const DataType &getValue() const { check(); return *Location; }
639};
640
641
642// Define how to hold a class type object, such as a string. Since we can
643// inherit from a class, we do so. This makes us exactly compatible with the
644// object in all cases that it is used.
645//
646template<class DataType>
647struct opt_storage<DataType,false,true> : public DataType {
648
649 template<class T>
650 void setValue(const T &V) { DataType::operator=(V); }
651
652 DataType &getValue() { return *this; }
653 const DataType &getValue() const { return *this; }
654};
655
656// Define a partial specialization to handle things we cannot inherit from. In
657// this case, we store an instance through containment, and overload operators
658// to get at the value.
659//
660template<class DataType>
661struct opt_storage<DataType, false, false> {
662 DataType Value;
663
664 // Make sure we initialize the value with the default constructor for the
665 // type.
666 opt_storage() : Value(DataType()) {}
667
668 template<class T>
669 void setValue(const T &V) { Value = V; }
670 DataType &getValue() { return Value; }
671 DataType getValue() const { return Value; }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000672};
673
674
675//===----------------------------------------------------------------------===//
Chris Lattner331de232002-07-22 02:07:59 +0000676// opt - A scalar command line option.
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000677//
Chris Lattner331de232002-07-22 02:07:59 +0000678template <class DataType, bool ExternalStorage = false,
679 class ParserClass = parser<DataType> >
680class opt : public Option,
681 public opt_storage<DataType, ExternalStorage,
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000682 ::boost::is_class<DataType>::value> {
Chris Lattner331de232002-07-22 02:07:59 +0000683 ParserClass Parser;
684
685 virtual bool handleOccurance(const char *ArgName, const std::string &Arg) {
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000686 typename ParserClass::parser_data_type Val;
687 if (Parser.parse(*this, ArgName, Arg, Val))
688 return true; // Parse error!
689 setValue(Val);
690 return false;
Chris Lattner331de232002-07-22 02:07:59 +0000691 }
692
693 virtual enum ValueExpected getValueExpectedFlagDefault() const {
694 return Parser.getValueExpectedFlagDefault();
695 }
696
697 // Forward printing stuff to the parser...
698 virtual unsigned getOptionWidth() const {return Parser.getOptionWidth(*this);}
699 virtual void printOptionInfo(unsigned GlobalWidth) const {
700 Parser.printOptionInfo(*this, GlobalWidth);
701 }
702
703 void done() {
704 addArgument(ArgStr);
705 Parser.initialize(*this);
706 }
707public:
708 // setInitialValue - Used by the cl::init modifier...
709 void setInitialValue(const DataType &V) { setValue(V); }
710
Chris Lattner331de232002-07-22 02:07:59 +0000711 ParserClass &getParser() { return Parser; }
712
713 operator DataType() const { return getValue(); }
714
715 template<class T>
716 DataType &operator=(const T &Val) { setValue(Val); return getValue(); }
717
718 // One option...
719 template<class M0t>
720 opt(const M0t &M0) {
721 apply(M0, this);
722 done();
723 }
724
725 // Two options...
726 template<class M0t, class M1t>
727 opt(const M0t &M0, const M1t &M1) {
728 apply(M0, this); apply(M1, this);
729 done();
730 }
731
732 // Three options...
733 template<class M0t, class M1t, class M2t>
734 opt(const M0t &M0, const M1t &M1, const M2t &M2) {
735 apply(M0, this); apply(M1, this); apply(M2, this);
736 done();
737 }
738 // Four options...
739 template<class M0t, class M1t, class M2t, class M3t>
740 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3) {
741 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
742 done();
743 }
744 // Five options...
745 template<class M0t, class M1t, class M2t, class M3t, class M4t>
746 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
747 const M4t &M4) {
748 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
749 apply(M4, this);
750 done();
751 }
752 // Six options...
753 template<class M0t, class M1t, class M2t, class M3t,
754 class M4t, class M5t>
755 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
756 const M4t &M4, const M5t &M5) {
757 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
758 apply(M4, this); apply(M5, this);
759 done();
760 }
761 // Seven options...
762 template<class M0t, class M1t, class M2t, class M3t,
763 class M4t, class M5t, class M6t>
764 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
765 const M4t &M4, const M5t &M5, const M6t &M6) {
766 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
767 apply(M4, this); apply(M5, this); apply(M6, this);
768 done();
769 }
770 // Eight options...
771 template<class M0t, class M1t, class M2t, class M3t,
772 class M4t, class M5t, class M6t, class M7t>
773 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
774 const M4t &M4, const M5t &M5, const M6t &M6, const M7t &M7) {
775 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
776 apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
777 done();
778 }
779};
780
781//===----------------------------------------------------------------------===//
782// list_storage class
783
784// Default storage class definition: external storage. This implementation
785// assumes the user will specify a variable to store the data into with the
786// cl::location(x) modifier.
787//
788template<class DataType, class StorageClass>
789class list_storage {
790 StorageClass *Location; // Where to store the object...
791
792public:
793 list_storage() : Location(0) {}
794
795 bool setLocation(Option &O, StorageClass &L) {
796 if (Location)
797 return O.error(": cl::location(x) specified more than once!");
798 Location = &L;
799 return false;
800 }
801
802 template<class T>
803 void addValue(const T &V) {
804 assert(Location != 0 && "cl::location(...) not specified for a command "
805 "line option with external storage!");
806 Location->push_back(V);
807 }
808};
809
810
811// Define how to hold a class type object, such as a string. Since we can
812// inherit from a class, we do so. This makes us exactly compatible with the
813// object in all cases that it is used.
814//
815template<class DataType>
816struct list_storage<DataType, bool> : public std::vector<DataType> {
817
818 template<class T>
819 void addValue(const T &V) { push_back(V); }
820};
821
822
823//===----------------------------------------------------------------------===//
824// list - A list of command line options.
825//
826template <class DataType, class Storage = bool,
827 class ParserClass = parser<DataType> >
828class list : public Option, public list_storage<DataType, Storage> {
829 ParserClass Parser;
830
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000831 virtual enum NumOccurances getNumOccurancesFlagDefault() const {
832 return ZeroOrMore;
833 }
834 virtual enum ValueExpected getValueExpectedFlagDefault() const {
Chris Lattner331de232002-07-22 02:07:59 +0000835 return Parser.getValueExpectedFlagDefault();
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000836 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000837
Chris Lattner331de232002-07-22 02:07:59 +0000838 virtual bool handleOccurance(const char *ArgName, const std::string &Arg) {
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000839 typename ParserClass::parser_data_type Val;
840 if (Parser.parse(*this, ArgName, Arg, Val))
841 return true; // Parse Error!
842 addValue(Val);
843 return false;
Chris Lattner331de232002-07-22 02:07:59 +0000844 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000845
Chris Lattner331de232002-07-22 02:07:59 +0000846 // Forward printing stuff to the parser...
847 virtual unsigned getOptionWidth() const {return Parser.getOptionWidth(*this);}
848 virtual void printOptionInfo(unsigned GlobalWidth) const {
849 Parser.printOptionInfo(*this, GlobalWidth);
850 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000851
Chris Lattner331de232002-07-22 02:07:59 +0000852 void done() {
853 addArgument(ArgStr);
854 Parser.initialize(*this);
855 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000856public:
Chris Lattner331de232002-07-22 02:07:59 +0000857 ParserClass &getParser() { return Parser; }
858
859 // One option...
860 template<class M0t>
861 list(const M0t &M0) {
862 apply(M0, this);
863 done();
864 }
865 // Two options...
866 template<class M0t, class M1t>
867 list(const M0t &M0, const M1t &M1) {
868 apply(M0, this); apply(M1, this);
869 done();
870 }
871 // Three options...
872 template<class M0t, class M1t, class M2t>
873 list(const M0t &M0, const M1t &M1, const M2t &M2) {
874 apply(M0, this); apply(M1, this); apply(M2, this);
875 done();
876 }
877 // Four options...
878 template<class M0t, class M1t, class M2t, class M3t>
879 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3) {
880 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
881 done();
882 }
883 // Five options...
884 template<class M0t, class M1t, class M2t, class M3t, class M4t>
885 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
886 const M4t &M4) {
887 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
888 apply(M4, this);
889 done();
890 }
891 // Six options...
892 template<class M0t, class M1t, class M2t, class M3t,
893 class M4t, class M5t>
894 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
895 const M4t &M4, const M5t &M5) {
896 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
897 apply(M4, this); apply(M5, this);
898 done();
899 }
900 // Seven options...
901 template<class M0t, class M1t, class M2t, class M3t,
902 class M4t, class M5t, class M6t>
903 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
904 const M4t &M4, const M5t &M5, const M6t &M6) {
905 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
906 apply(M4, this); apply(M5, this); apply(M6, this);
907 done();
908 }
909 // Eight options...
910 template<class M0t, class M1t, class M2t, class M3t,
911 class M4t, class M5t, class M6t, class M7t>
912 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
913 const M4t &M4, const M5t &M5, const M6t &M6, const M7t &M7) {
914 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
915 apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
916 done();
917 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000918};
919
Chris Lattner331de232002-07-22 02:07:59 +0000920
921
922//===----------------------------------------------------------------------===//
923// Aliased command line option (alias this name to a preexisting name)
924//
925
926class alias : public Option {
927 Option *AliasFor;
928 virtual bool handleOccurance(const char *ArgName, const std::string &Arg) {
929 return AliasFor->handleOccurance(AliasFor->ArgStr, Arg);
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000930 }
Chris Lattner331de232002-07-22 02:07:59 +0000931 // Aliases default to be hidden...
932 virtual enum OptionHidden getOptionHiddenFlagDefault() const {return Hidden;}
933
934 // Handle printing stuff...
935 virtual unsigned getOptionWidth() const;
936 virtual void printOptionInfo(unsigned GlobalWidth) const;
937
938 void done() {
939 if (!hasArgStr())
940 error(": cl::alias must have argument name specified!");
941 if (AliasFor == 0)
942 error(": cl::alias must have an cl::aliasopt(option) specified!");
943 addArgument(ArgStr);
944 }
945public:
946 void setAliasFor(Option &O) {
947 if (AliasFor)
948 error(": cl::alias must only have one cl::aliasopt(...) specified!");
949 AliasFor = &O;
950 }
951
952 // One option...
953 template<class M0t>
954 alias(const M0t &M0) : AliasFor(0) {
955 apply(M0, this);
956 done();
957 }
958 // Two options...
959 template<class M0t, class M1t>
960 alias(const M0t &M0, const M1t &M1) : AliasFor(0) {
961 apply(M0, this); apply(M1, this);
962 done();
963 }
964 // Three options...
965 template<class M0t, class M1t, class M2t>
966 alias(const M0t &M0, const M1t &M1, const M2t &M2) : AliasFor(0) {
967 apply(M0, this); apply(M1, this); apply(M2, this);
968 done();
969 }
970 // Four options...
971 template<class M0t, class M1t, class M2t, class M3t>
972 alias(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3)
973 : AliasFor(0) {
974 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
975 done();
976 }
977};
978
979// aliasfor - Modifier to set the option an alias aliases.
980struct aliasopt {
981 Option &Opt;
982 aliasopt(Option &O) : Opt(O) {}
983 void apply(alias &A) const { A.setAliasFor(Opt); }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000984};
985
986} // End namespace cl
987
988#endif