blob: aafef95f56ff3474dd95e0be402d1894b9f3dada [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
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//--------------------------------------------------
518// parser<double>
519//
520template<>
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000521struct parser<double> : public basic_parser<double> {
Chris Lattner331de232002-07-22 02:07:59 +0000522 // parse - Return true on error.
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000523 bool parse(Option &O, const char *AN, const std::string &Arg, double &Val);
Chris Lattner331de232002-07-22 02:07:59 +0000524
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000525 // getValueName - Overload in subclass to provide a better default value.
526 virtual const char *getValueName() const { return "number"; }
Chris Lattner331de232002-07-22 02:07:59 +0000527};
528
Chris Lattner331de232002-07-22 02:07:59 +0000529
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000530//--------------------------------------------------
531// parser<float>
532//
533template<>
534struct parser<float> : public basic_parser<float> {
535 // parse - Return true on error.
536 bool parse(Option &O, const char *AN, const std::string &Arg, float &Val);
537
538 // getValueName - Overload in subclass to provide a better default value.
539 virtual const char *getValueName() const { return "number"; }
540};
Chris Lattner331de232002-07-22 02:07:59 +0000541
542
543//--------------------------------------------------
Chris Lattner7f4dd472002-07-24 22:08:36 +0000544// parser<std::string>
Chris Lattner331de232002-07-22 02:07:59 +0000545//
546template<>
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000547struct parser<std::string> : public basic_parser<std::string> {
Chris Lattner331de232002-07-22 02:07:59 +0000548 // parse - Return true on error.
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000549 bool parse(Option &O, const char *ArgName, const std::string &Arg,
550 std::string &Value) {
551 Value = Arg;
Chris Lattner331de232002-07-22 02:07:59 +0000552 return false;
553 }
Chris Lattner71fb7162002-05-22 17:03:05 +0000554
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000555 // getValueName - Overload in subclass to provide a better default value.
556 virtual const char *getValueName() const { return "string"; }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000557};
558
Chris Lattner71fb7162002-05-22 17:03:05 +0000559
Chris Lattner331de232002-07-22 02:07:59 +0000560
561//===----------------------------------------------------------------------===//
562// applicator class - This class is used because we must use partial
563// specialization to handle literal string arguments specially (const char* does
564// not correctly respond to the apply method). Because the syntax to use this
565// is a pain, we have the 'apply' method below to handle the nastiness...
566//
567template<class Mod> struct applicator {
568 template<class Opt>
569 static void opt(const Mod &M, Opt &O) { M.apply(O); }
570};
571
572// Handle const char* as a special case...
573template<unsigned n> struct applicator<char[n]> {
574 template<class Opt>
575 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
576};
Chris Lattner40423322002-09-13 14:33:39 +0000577template<unsigned n> struct applicator<const char[n]> {
578 template<class Opt>
579 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
580};
Chris Lattner331de232002-07-22 02:07:59 +0000581template<> struct applicator<const char*> {
582 template<class Opt>
583 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
584};
585
586template<> struct applicator<NumOccurances> {
587 static void opt(NumOccurances NO, Option &O) { O.setNumOccurancesFlag(NO); }
588};
589template<> struct applicator<ValueExpected> {
590 static void opt(ValueExpected VE, Option &O) { O.setValueExpectedFlag(VE); }
591};
592template<> struct applicator<OptionHidden> {
593 static void opt(OptionHidden OH, Option &O) { O.setHiddenFlag(OH); }
594};
595template<> struct applicator<FormattingFlags> {
596 static void opt(FormattingFlags FF, Option &O) { O.setFormattingFlag(FF); }
597};
Chris Lattnerb3b729b2003-05-22 20:25:57 +0000598template<> struct applicator<MiscFlags> {
599 static void opt(MiscFlags MF, Option &O) { O.setMiscFlag(MF); }
600};
Chris Lattner331de232002-07-22 02:07:59 +0000601
602// apply method - Apply a modifier to an option in a type safe way.
603template<class Mod, class Opt>
604void apply(const Mod &M, Opt *O) {
605 applicator<Mod>::opt(M, *O);
606}
607
608
609//===----------------------------------------------------------------------===//
610// opt_storage class
611
612// Default storage class definition: external storage. This implementation
613// assumes the user will specify a variable to store the data into with the
614// cl::location(x) modifier.
615//
616template<class DataType, bool ExternalStorage, bool isClass>
617class opt_storage {
618 DataType *Location; // Where to store the object...
619
620 void check() {
621 assert(Location != 0 && "cl::location(...) not specified for a command "
622 "line option with external storage!");
623 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000624public:
Chris Lattner331de232002-07-22 02:07:59 +0000625 opt_storage() : Location(0) {}
626
627 bool setLocation(Option &O, DataType &L) {
628 if (Location)
629 return O.error(": cl::location(x) specified more than once!");
630 Location = &L;
631 return false;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000632 }
633
Chris Lattner331de232002-07-22 02:07:59 +0000634 template<class T>
635 void setValue(const T &V) {
636 check();
637 *Location = V;
638 }
639
640 DataType &getValue() { check(); return *Location; }
641 const DataType &getValue() const { check(); return *Location; }
642};
643
644
645// Define how to hold a class type object, such as a string. Since we can
646// inherit from a class, we do so. This makes us exactly compatible with the
647// object in all cases that it is used.
648//
649template<class DataType>
650struct opt_storage<DataType,false,true> : public DataType {
651
652 template<class T>
653 void setValue(const T &V) { DataType::operator=(V); }
654
655 DataType &getValue() { return *this; }
656 const DataType &getValue() const { return *this; }
657};
658
659// Define a partial specialization to handle things we cannot inherit from. In
660// this case, we store an instance through containment, and overload operators
661// to get at the value.
662//
663template<class DataType>
664struct opt_storage<DataType, false, false> {
665 DataType Value;
666
667 // Make sure we initialize the value with the default constructor for the
668 // type.
669 opt_storage() : Value(DataType()) {}
670
671 template<class T>
672 void setValue(const T &V) { Value = V; }
673 DataType &getValue() { return Value; }
674 DataType getValue() const { return Value; }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000675};
676
677
678//===----------------------------------------------------------------------===//
Chris Lattner331de232002-07-22 02:07:59 +0000679// opt - A scalar command line option.
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000680//
Chris Lattner331de232002-07-22 02:07:59 +0000681template <class DataType, bool ExternalStorage = false,
682 class ParserClass = parser<DataType> >
683class opt : public Option,
684 public opt_storage<DataType, ExternalStorage,
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000685 ::boost::is_class<DataType>::value> {
Chris Lattner331de232002-07-22 02:07:59 +0000686 ParserClass Parser;
687
688 virtual bool handleOccurance(const char *ArgName, const std::string &Arg) {
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000689 typename ParserClass::parser_data_type Val;
690 if (Parser.parse(*this, ArgName, Arg, Val))
691 return true; // Parse error!
692 setValue(Val);
693 return false;
Chris Lattner331de232002-07-22 02:07:59 +0000694 }
695
696 virtual enum ValueExpected getValueExpectedFlagDefault() const {
697 return Parser.getValueExpectedFlagDefault();
698 }
699
700 // Forward printing stuff to the parser...
701 virtual unsigned getOptionWidth() const {return Parser.getOptionWidth(*this);}
702 virtual void printOptionInfo(unsigned GlobalWidth) const {
703 Parser.printOptionInfo(*this, GlobalWidth);
704 }
705
706 void done() {
707 addArgument(ArgStr);
708 Parser.initialize(*this);
709 }
710public:
711 // setInitialValue - Used by the cl::init modifier...
712 void setInitialValue(const DataType &V) { setValue(V); }
713
Chris Lattner331de232002-07-22 02:07:59 +0000714 ParserClass &getParser() { return Parser; }
715
716 operator DataType() const { return getValue(); }
717
718 template<class T>
719 DataType &operator=(const T &Val) { setValue(Val); return getValue(); }
720
721 // One option...
722 template<class M0t>
723 opt(const M0t &M0) {
724 apply(M0, this);
725 done();
726 }
727
728 // Two options...
729 template<class M0t, class M1t>
730 opt(const M0t &M0, const M1t &M1) {
731 apply(M0, this); apply(M1, this);
732 done();
733 }
734
735 // Three options...
736 template<class M0t, class M1t, class M2t>
737 opt(const M0t &M0, const M1t &M1, const M2t &M2) {
738 apply(M0, this); apply(M1, this); apply(M2, this);
739 done();
740 }
741 // Four options...
742 template<class M0t, class M1t, class M2t, class M3t>
743 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3) {
744 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
745 done();
746 }
747 // Five options...
748 template<class M0t, class M1t, class M2t, class M3t, class M4t>
749 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
750 const M4t &M4) {
751 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
752 apply(M4, this);
753 done();
754 }
755 // Six options...
756 template<class M0t, class M1t, class M2t, class M3t,
757 class M4t, class M5t>
758 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
759 const M4t &M4, const M5t &M5) {
760 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
761 apply(M4, this); apply(M5, this);
762 done();
763 }
764 // Seven options...
765 template<class M0t, class M1t, class M2t, class M3t,
766 class M4t, class M5t, class M6t>
767 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
768 const M4t &M4, const M5t &M5, const M6t &M6) {
769 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
770 apply(M4, this); apply(M5, this); apply(M6, this);
771 done();
772 }
773 // Eight options...
774 template<class M0t, class M1t, class M2t, class M3t,
775 class M4t, class M5t, class M6t, class M7t>
776 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
777 const M4t &M4, const M5t &M5, const M6t &M6, const M7t &M7) {
778 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
779 apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
780 done();
781 }
782};
783
784//===----------------------------------------------------------------------===//
785// list_storage class
786
787// Default storage class definition: external storage. This implementation
788// assumes the user will specify a variable to store the data into with the
789// cl::location(x) modifier.
790//
791template<class DataType, class StorageClass>
792class list_storage {
793 StorageClass *Location; // Where to store the object...
794
795public:
796 list_storage() : Location(0) {}
797
798 bool setLocation(Option &O, StorageClass &L) {
799 if (Location)
800 return O.error(": cl::location(x) specified more than once!");
801 Location = &L;
802 return false;
803 }
804
805 template<class T>
806 void addValue(const T &V) {
807 assert(Location != 0 && "cl::location(...) not specified for a command "
808 "line option with external storage!");
809 Location->push_back(V);
810 }
811};
812
813
814// Define how to hold a class type object, such as a string. Since we can
815// inherit from a class, we do so. This makes us exactly compatible with the
816// object in all cases that it is used.
817//
818template<class DataType>
819struct list_storage<DataType, bool> : public std::vector<DataType> {
820
821 template<class T>
822 void addValue(const T &V) { push_back(V); }
823};
824
825
826//===----------------------------------------------------------------------===//
827// list - A list of command line options.
828//
829template <class DataType, class Storage = bool,
830 class ParserClass = parser<DataType> >
831class list : public Option, public list_storage<DataType, Storage> {
832 ParserClass Parser;
833
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000834 virtual enum NumOccurances getNumOccurancesFlagDefault() const {
835 return ZeroOrMore;
836 }
837 virtual enum ValueExpected getValueExpectedFlagDefault() const {
Chris Lattner331de232002-07-22 02:07:59 +0000838 return Parser.getValueExpectedFlagDefault();
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000839 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000840
Chris Lattner331de232002-07-22 02:07:59 +0000841 virtual bool handleOccurance(const char *ArgName, const std::string &Arg) {
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000842 typename ParserClass::parser_data_type Val;
843 if (Parser.parse(*this, ArgName, Arg, Val))
844 return true; // Parse Error!
845 addValue(Val);
846 return false;
Chris Lattner331de232002-07-22 02:07:59 +0000847 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000848
Chris Lattner331de232002-07-22 02:07:59 +0000849 // Forward printing stuff to the parser...
850 virtual unsigned getOptionWidth() const {return Parser.getOptionWidth(*this);}
851 virtual void printOptionInfo(unsigned GlobalWidth) const {
852 Parser.printOptionInfo(*this, GlobalWidth);
853 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000854
Chris Lattner331de232002-07-22 02:07:59 +0000855 void done() {
856 addArgument(ArgStr);
857 Parser.initialize(*this);
858 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000859public:
Chris Lattner331de232002-07-22 02:07:59 +0000860 ParserClass &getParser() { return Parser; }
861
862 // One option...
863 template<class M0t>
864 list(const M0t &M0) {
865 apply(M0, this);
866 done();
867 }
868 // Two options...
869 template<class M0t, class M1t>
870 list(const M0t &M0, const M1t &M1) {
871 apply(M0, this); apply(M1, this);
872 done();
873 }
874 // Three options...
875 template<class M0t, class M1t, class M2t>
876 list(const M0t &M0, const M1t &M1, const M2t &M2) {
877 apply(M0, this); apply(M1, this); apply(M2, this);
878 done();
879 }
880 // Four options...
881 template<class M0t, class M1t, class M2t, class M3t>
882 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3) {
883 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
884 done();
885 }
886 // Five options...
887 template<class M0t, class M1t, class M2t, class M3t, class M4t>
888 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
889 const M4t &M4) {
890 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
891 apply(M4, this);
892 done();
893 }
894 // Six options...
895 template<class M0t, class M1t, class M2t, class M3t,
896 class M4t, class M5t>
897 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
898 const M4t &M4, const M5t &M5) {
899 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
900 apply(M4, this); apply(M5, this);
901 done();
902 }
903 // Seven options...
904 template<class M0t, class M1t, class M2t, class M3t,
905 class M4t, class M5t, class M6t>
906 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
907 const M4t &M4, const M5t &M5, const M6t &M6) {
908 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
909 apply(M4, this); apply(M5, this); apply(M6, this);
910 done();
911 }
912 // Eight options...
913 template<class M0t, class M1t, class M2t, class M3t,
914 class M4t, class M5t, class M6t, class M7t>
915 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
916 const M4t &M4, const M5t &M5, const M6t &M6, const M7t &M7) {
917 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
918 apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
919 done();
920 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000921};
922
Chris Lattner331de232002-07-22 02:07:59 +0000923
924
925//===----------------------------------------------------------------------===//
926// Aliased command line option (alias this name to a preexisting name)
927//
928
929class alias : public Option {
930 Option *AliasFor;
931 virtual bool handleOccurance(const char *ArgName, const std::string &Arg) {
932 return AliasFor->handleOccurance(AliasFor->ArgStr, Arg);
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000933 }
Chris Lattner331de232002-07-22 02:07:59 +0000934 // Aliases default to be hidden...
935 virtual enum OptionHidden getOptionHiddenFlagDefault() const {return Hidden;}
936
937 // Handle printing stuff...
938 virtual unsigned getOptionWidth() const;
939 virtual void printOptionInfo(unsigned GlobalWidth) const;
940
941 void done() {
942 if (!hasArgStr())
943 error(": cl::alias must have argument name specified!");
944 if (AliasFor == 0)
945 error(": cl::alias must have an cl::aliasopt(option) specified!");
946 addArgument(ArgStr);
947 }
948public:
949 void setAliasFor(Option &O) {
950 if (AliasFor)
951 error(": cl::alias must only have one cl::aliasopt(...) specified!");
952 AliasFor = &O;
953 }
954
955 // One option...
956 template<class M0t>
957 alias(const M0t &M0) : AliasFor(0) {
958 apply(M0, this);
959 done();
960 }
961 // Two options...
962 template<class M0t, class M1t>
963 alias(const M0t &M0, const M1t &M1) : AliasFor(0) {
964 apply(M0, this); apply(M1, this);
965 done();
966 }
967 // Three options...
968 template<class M0t, class M1t, class M2t>
969 alias(const M0t &M0, const M1t &M1, const M2t &M2) : AliasFor(0) {
970 apply(M0, this); apply(M1, this); apply(M2, this);
971 done();
972 }
973 // Four options...
974 template<class M0t, class M1t, class M2t, class M3t>
975 alias(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3)
976 : AliasFor(0) {
977 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
978 done();
979 }
980};
981
982// aliasfor - Modifier to set the option an alias aliases.
983struct aliasopt {
984 Option &Opt;
985 aliasopt(Option &O) : Opt(O) {}
986 void apply(alias &A) const { A.setAliasFor(Opt); }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000987};
988
989} // End namespace cl
990
991#endif