blob: 728bb1467835f71e9a886e6f92e4dc27b161e035 [file] [log] [blame]
Chris Lattnercee8f9a2001-11-27 00:03:19 +00001//===- Support/CommandLine.h - Flexible Command line parser ------*- C++ -*--=//
2//
3// This class implements a command line argument processor that is useful when
4// creating a tool. It provides a simple, minimalistic interface that is easily
5// extensible and supports nonlocal (library) command line options.
6//
Chris Lattner331de232002-07-22 02:07:59 +00007// Note that rather than trying to figure out what this code does, you should
8// read the library documentation located in docs/CommandLine.html or looks at
9// the many example usages in tools/*/*.cpp
Chris Lattnercee8f9a2001-11-27 00:03:19 +000010//
11//===----------------------------------------------------------------------===//
12
Brian Gaekea9f6e4a2003-06-17 00:35:55 +000013#ifndef SUPPORT_COMMANDLINE_H
14#define SUPPORT_COMMANDLINE_H
Chris Lattnercee8f9a2001-11-27 00:03:19 +000015
16#include <string>
17#include <vector>
18#include <utility>
Chris Lattnerb3b729b2003-05-22 20:25:57 +000019#include <cstdarg>
Chris Lattner331de232002-07-22 02:07:59 +000020#include "boost/type_traits/object_traits.hpp"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000021
John Criswellbe583b92003-06-11 14:01:36 +000022#include <assert.h>
23
Chris Lattnerbf0ac3f2003-06-03 15:30:37 +000024/// cl Namespace - This namespace contains all of the command line option
25/// processing machinery. It is intentionally a short name to make qualified
26/// usage concise.
27namespace cl {
Chris Lattnercee8f9a2001-11-27 00:03:19 +000028
29//===----------------------------------------------------------------------===//
Chris Lattner331de232002-07-22 02:07:59 +000030// ParseCommandLineOptions - Command line option processing entry point.
Chris Lattnercee8f9a2001-11-27 00:03:19 +000031//
32void cl::ParseCommandLineOptions(int &argc, char **argv,
Chris Lattner331de232002-07-22 02:07:59 +000033 const char *Overview = 0);
Chris Lattnercee8f9a2001-11-27 00:03:19 +000034
35//===----------------------------------------------------------------------===//
Chris Lattner331de232002-07-22 02:07:59 +000036// Flags permitted to be passed to command line arguments
37//
Chris Lattnercee8f9a2001-11-27 00:03:19 +000038
39enum NumOccurances { // Flags for the number of occurances allowed...
40 Optional = 0x01, // Zero or One occurance
41 ZeroOrMore = 0x02, // Zero or more occurances allowed
42 Required = 0x03, // One occurance required
43 OneOrMore = 0x04, // One or more occurances required
44
Chris Lattner331de232002-07-22 02:07:59 +000045 // ConsumeAfter - Indicates that this option is fed anything that follows the
46 // last positional argument required by the application (it is an error if
47 // there are zero positional arguments, and a ConsumeAfter option is used).
48 // Thus, for example, all arguments to LLI are processed until a filename is
49 // found. Once a filename is found, all of the succeeding arguments are
50 // passed, unprocessed, to the ConsumeAfter option.
Chris Lattnercee8f9a2001-11-27 00:03:19 +000051 //
52 ConsumeAfter = 0x05,
53
54 OccurancesMask = 0x07,
55};
56
57enum ValueExpected { // Is a value required for the option?
Chris Lattnerb3b729b2003-05-22 20:25:57 +000058 ValueOptional = 0x08, // The value can appear... or not
Chris Lattnercee8f9a2001-11-27 00:03:19 +000059 ValueRequired = 0x10, // The value is required to appear!
60 ValueDisallowed = 0x18, // A value may not be specified (for flags)
61 ValueMask = 0x18,
62};
63
64enum OptionHidden { // Control whether -help shows this option
65 NotHidden = 0x20, // Option included in --help & --help-hidden
66 Hidden = 0x40, // -help doesn't, but --help-hidden does
67 ReallyHidden = 0x60, // Neither --help nor --help-hidden show this arg
68 HiddenMask = 0x60,
69};
70
Chris Lattner331de232002-07-22 02:07:59 +000071// Formatting flags - This controls special features that the option might have
72// that cause it to be parsed differently...
73//
74// Prefix - This option allows arguments that are otherwise unrecognized to be
75// matched by options that are a prefix of the actual value. This is useful for
76// cases like a linker, where options are typically of the form '-lfoo' or
77// '-L../../include' where -l or -L are the actual flags. When prefix is
78// enabled, and used, the value for the flag comes from the suffix of the
79// argument.
80//
81// Grouping - With this option enabled, multiple letter options are allowed to
82// bunch together with only a single hyphen for the whole group. This allows
83// emulation of the behavior that ls uses for example: ls -la === ls -l -a
84//
85
86enum FormattingFlags {
87 NormalFormatting = 0x000, // Nothing special
88 Positional = 0x080, // Is a positional argument, no '-' required
89 Prefix = 0x100, // Can this option directly prefix its value?
90 Grouping = 0x180, // Can this option group with other options?
91 FormattingMask = 0x180,
92};
93
Chris Lattnerb3b729b2003-05-22 20:25:57 +000094enum MiscFlags { // Miscellaneous flags to adjust argument
95 CommaSeparated = 0x200, // Should this cl::list split between commas?
96 MiscMask = 0x200,
97};
98
99
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000100
101//===----------------------------------------------------------------------===//
102// Option Base class
103//
Chris Lattner331de232002-07-22 02:07:59 +0000104class alias;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000105class Option {
106 friend void cl::ParseCommandLineOptions(int &, char **, const char *, int);
Chris Lattner331de232002-07-22 02:07:59 +0000107 friend class alias;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000108
109 // handleOccurances - Overriden by subclasses to handle the value passed into
110 // an argument. Should return true if there was an error processing the
111 // argument and the program should exit.
112 //
Chris Lattner697954c2002-01-20 22:54:45 +0000113 virtual bool handleOccurance(const char *ArgName, const std::string &Arg) = 0;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000114
115 virtual enum NumOccurances getNumOccurancesFlagDefault() const {
116 return Optional;
117 }
118 virtual enum ValueExpected getValueExpectedFlagDefault() const {
119 return ValueOptional;
120 }
121 virtual enum OptionHidden getOptionHiddenFlagDefault() const {
122 return NotHidden;
123 }
Chris Lattner331de232002-07-22 02:07:59 +0000124 virtual enum FormattingFlags getFormattingFlagDefault() const {
125 return NormalFormatting;
126 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000127
Chris Lattner331de232002-07-22 02:07:59 +0000128 int NumOccurances; // The number of times specified
129 int Flags; // Flags for the argument
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000130public:
Chris Lattner331de232002-07-22 02:07:59 +0000131 const char *ArgStr; // The argument string itself (ex: "help", "o")
132 const char *HelpStr; // The descriptive text message for --help
133 const char *ValueStr; // String describing what the value of this option is
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000134
135 inline enum NumOccurances getNumOccurancesFlag() const {
136 int NO = Flags & OccurancesMask;
137 return NO ? (enum NumOccurances)NO : getNumOccurancesFlagDefault();
138 }
139 inline enum ValueExpected getValueExpectedFlag() const {
140 int VE = Flags & ValueMask;
141 return VE ? (enum ValueExpected)VE : getValueExpectedFlagDefault();
142 }
143 inline enum OptionHidden getOptionHiddenFlag() const {
144 int OH = Flags & HiddenMask;
145 return OH ? (enum OptionHidden)OH : getOptionHiddenFlagDefault();
146 }
Chris Lattner331de232002-07-22 02:07:59 +0000147 inline enum FormattingFlags getFormattingFlag() const {
148 int OH = Flags & FormattingMask;
149 return OH ? (enum FormattingFlags)OH : getFormattingFlagDefault();
150 }
Chris Lattnerb3b729b2003-05-22 20:25:57 +0000151 inline unsigned getMiscFlags() const {
152 return Flags & MiscMask;
153 }
Chris Lattner331de232002-07-22 02:07:59 +0000154
155 // hasArgStr - Return true if the argstr != ""
156 bool hasArgStr() const { return ArgStr[0] != 0; }
157
158 //-------------------------------------------------------------------------===
159 // Accessor functions set by OptionModifiers
160 //
161 void setArgStr(const char *S) { ArgStr = S; }
162 void setDescription(const char *S) { HelpStr = S; }
163 void setValueStr(const char *S) { ValueStr = S; }
164
165 void setFlag(unsigned Flag, unsigned FlagMask) {
166 if (Flags & FlagMask) {
167 error(": Specified two settings for the same option!");
168 exit(1);
169 }
170
171 Flags |= Flag;
172 }
173
174 void setNumOccurancesFlag(enum NumOccurances Val) {
175 setFlag(Val, OccurancesMask);
176 }
177 void setValueExpectedFlag(enum ValueExpected Val) { setFlag(Val, ValueMask); }
178 void setHiddenFlag(enum OptionHidden Val) { setFlag(Val, HiddenMask); }
179 void setFormattingFlag(enum FormattingFlags V) { setFlag(V, FormattingMask); }
Chris Lattnerb3b729b2003-05-22 20:25:57 +0000180 void setMiscFlag(enum MiscFlags M) { setFlag(M, M); }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000181protected:
Chris Lattner331de232002-07-22 02:07:59 +0000182 Option() : NumOccurances(0), Flags(0),
183 ArgStr(""), HelpStr(""), ValueStr("") {}
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000184
185public:
Chris Lattner331de232002-07-22 02:07:59 +0000186 // addArgument - Tell the system that this Option subclass will handle all
187 // occurances of -ArgStr on the command line.
188 //
189 void addArgument(const char *ArgStr);
Chris Lattnerae1257a2002-07-23 00:44:37 +0000190 void removeArgument(const char *ArgStr);
Chris Lattner331de232002-07-22 02:07:59 +0000191
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000192 // Return the width of the option tag for printing...
Chris Lattner331de232002-07-22 02:07:59 +0000193 virtual unsigned getOptionWidth() const = 0;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000194
195 // printOptionInfo - Print out information about this option. The
196 // to-be-maintained width is specified.
197 //
Chris Lattner331de232002-07-22 02:07:59 +0000198 virtual void printOptionInfo(unsigned GlobalWidth) const = 0;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000199
200 // addOccurance - Wrapper around handleOccurance that enforces Flags
201 //
Chris Lattner697954c2002-01-20 22:54:45 +0000202 bool addOccurance(const char *ArgName, const std::string &Value);
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000203
204 // Prints option name followed by message. Always returns true.
Chris Lattner697954c2002-01-20 22:54:45 +0000205 bool error(std::string Message, const char *ArgName = 0);
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000206
207public:
208 inline int getNumOccurances() const { return NumOccurances; }
209 virtual ~Option() {}
210};
211
212
213//===----------------------------------------------------------------------===//
Chris Lattner331de232002-07-22 02:07:59 +0000214// Command line option modifiers that can be used to modify the behavior of
215// command line option parsers...
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000216//
Chris Lattner331de232002-07-22 02:07:59 +0000217
218// desc - Modifier to set the description shown in the --help output...
219struct desc {
220 const char *Desc;
221 desc(const char *Str) : Desc(Str) {}
222 void apply(Option &O) const { O.setDescription(Desc); }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000223};
224
Chris Lattner331de232002-07-22 02:07:59 +0000225// value_desc - Modifier to set the value description shown in the --help
226// output...
227struct value_desc {
228 const char *Desc;
229 value_desc(const char *Str) : Desc(Str) {}
230 void apply(Option &O) const { O.setValueStr(Desc); }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000231};
232
233
Chris Lattner331de232002-07-22 02:07:59 +0000234// init - Specify a default (initial) value for the command line argument, if
235// the default constructor for the argument type does not give you what you
236// want. This is only valid on "opt" arguments, not on "list" arguments.
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000237//
Chris Lattner331de232002-07-22 02:07:59 +0000238template<class Ty>
239struct initializer {
240 const Ty &Init;
241 initializer(const Ty &Val) : Init(Val) {}
242
243 template<class Opt>
244 void apply(Opt &O) const { O.setInitialValue(Init); }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000245};
246
Chris Lattner331de232002-07-22 02:07:59 +0000247template<class Ty>
248initializer<Ty> init(const Ty &Val) {
249 return initializer<Ty>(Val);
250}
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000251
Chris Lattner331de232002-07-22 02:07:59 +0000252
253// location - Allow the user to specify which external variable they want to
254// store the results of the command line argument processing into, if they don't
255// want to store it in the option itself.
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000256//
Chris Lattner331de232002-07-22 02:07:59 +0000257template<class Ty>
258struct LocationClass {
259 Ty &Loc;
260 LocationClass(Ty &L) : Loc(L) {}
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000261
Chris Lattner331de232002-07-22 02:07:59 +0000262 template<class Opt>
263 void apply(Opt &O) const { O.setLocation(O, Loc); }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000264};
265
Chris Lattner331de232002-07-22 02:07:59 +0000266template<class Ty>
267LocationClass<Ty> location(Ty &L) { return LocationClass<Ty>(L); }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000268
269
270//===----------------------------------------------------------------------===//
271// Enum valued command line option
272//
Chris Lattnerae1257a2002-07-23 00:44:37 +0000273#define clEnumVal(ENUMVAL, DESC) #ENUMVAL, (int)ENUMVAL, DESC
274#define clEnumValN(ENUMVAL, FLAGNAME, DESC) FLAGNAME, (int)ENUMVAL, DESC
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000275
Chris Lattner331de232002-07-22 02:07:59 +0000276// values - For custom data types, allow specifying a group of values together
277// as the values that go into the mapping that the option handler uses. Note
278// that the values list must always have a 0 at the end of the list to indicate
279// that the list has ended.
280//
281template<class DataType>
282class ValuesClass {
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000283 // Use a vector instead of a map, because the lists should be short,
284 // the overhead is less, and most importantly, it keeps them in the order
285 // inserted so we can print our option out nicely.
Chris Lattner331de232002-07-22 02:07:59 +0000286 std::vector<std::pair<const char *, std::pair<int, const char *> > > Values;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000287 void processValues(va_list Vals);
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000288public:
Chris Lattner331de232002-07-22 02:07:59 +0000289 ValuesClass(const char *EnumName, DataType Val, const char *Desc,
290 va_list ValueArgs) {
291 // Insert the first value, which is required.
292 Values.push_back(std::make_pair(EnumName, std::make_pair(Val, Desc)));
293
294 // Process the varargs portion of the values...
295 while (const char *EnumName = va_arg(ValueArgs, const char *)) {
Chris Lattnerae1257a2002-07-23 00:44:37 +0000296 DataType EnumVal = (DataType)va_arg(ValueArgs, int);
Chris Lattner331de232002-07-22 02:07:59 +0000297 const char *EnumDesc = va_arg(ValueArgs, const char *);
298 Values.push_back(std::make_pair(EnumName, // Add value to value map
299 std::make_pair(EnumVal, EnumDesc)));
300 }
301 }
302
303 template<class Opt>
304 void apply(Opt &O) const {
305 for (unsigned i = 0, e = Values.size(); i != e; ++i)
306 O.getParser().addLiteralOption(Values[i].first, Values[i].second.first,
307 Values[i].second.second);
308 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000309};
310
Chris Lattner331de232002-07-22 02:07:59 +0000311template<class DataType>
312ValuesClass<DataType> values(const char *Arg, DataType Val, const char *Desc,
313 ...) {
314 va_list ValueArgs;
315 va_start(ValueArgs, Desc);
316 ValuesClass<DataType> Vals(Arg, Val, Desc, ValueArgs);
317 va_end(ValueArgs);
318 return Vals;
319}
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000320
Chris Lattner331de232002-07-22 02:07:59 +0000321
322//===----------------------------------------------------------------------===//
323// parser class - Parameterizable parser for different data types. By default,
324// known data types (string, int, bool) have specialized parsers, that do what
325// you would expect. The default parser, used for data types that are not
326// built-in, uses a mapping table to map specific options to values, which is
327// used, among other things, to handle enum types.
328
329//--------------------------------------------------
330// generic_parser_base - This class holds all the non-generic code that we do
331// not need replicated for every instance of the generic parser. This also
332// allows us to put stuff into CommandLine.cpp
333//
334struct generic_parser_base {
335 virtual ~generic_parser_base() {} // Base class should have virtual-dtor
336
337 // getNumOptions - Virtual function implemented by generic subclass to
338 // indicate how many entries are in Values.
339 //
340 virtual unsigned getNumOptions() const = 0;
341
342 // getOption - Return option name N.
343 virtual const char *getOption(unsigned N) const = 0;
344
345 // getDescription - Return description N
346 virtual const char *getDescription(unsigned N) const = 0;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000347
348 // Return the width of the option tag for printing...
Chris Lattner331de232002-07-22 02:07:59 +0000349 virtual unsigned getOptionWidth(const Option &O) const;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000350
351 // printOptionInfo - Print out information about this option. The
352 // to-be-maintained width is specified.
353 //
Chris Lattner331de232002-07-22 02:07:59 +0000354 virtual void printOptionInfo(const Option &O, unsigned GlobalWidth) const;
Chris Lattner71fb7162002-05-22 17:03:05 +0000355
Chris Lattner331de232002-07-22 02:07:59 +0000356 void initialize(Option &O) {
357 // All of the modifiers for the option have been processed by now, so the
358 // argstr field should be stable, copy it down now.
359 //
360 hasArgStr = O.hasArgStr();
361
362 // If there has been no argstr specified, that means that we need to add an
363 // argument for every possible option. This ensures that our options are
364 // vectored to us.
365 //
366 if (!hasArgStr)
367 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
368 O.addArgument(getOption(i));
369 }
370
371 enum ValueExpected getValueExpectedFlagDefault() const {
372 // If there is an ArgStr specified, then we are of the form:
373 //
374 // -opt=O2 or -opt O2 or -optO2
375 //
376 // In which case, the value is required. Otherwise if an arg str has not
377 // been specified, we are of the form:
378 //
379 // -O2 or O2 or -la (where -l and -a are seperate options)
380 //
381 // If this is the case, we cannot allow a value.
382 //
383 if (hasArgStr)
384 return ValueRequired;
385 else
386 return ValueDisallowed;
387 }
388
Chris Lattneraf7e8212002-07-23 17:15:09 +0000389 // findOption - Return the option number corresponding to the specified
390 // argument string. If the option is not found, getNumOptions() is returned.
391 //
392 unsigned findOption(const char *Name);
393
Chris Lattner331de232002-07-22 02:07:59 +0000394protected:
395 bool hasArgStr;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000396};
397
Chris Lattner331de232002-07-22 02:07:59 +0000398// Default parser implementation - This implementation depends on having a
399// mapping of recognized options to values of some sort. In addition to this,
400// each entry in the mapping also tracks a help message that is printed with the
401// command line option for --help. Because this is a simple mapping parser, the
402// data type can be any unsupported type.
403//
404template <class DataType>
405class parser : public generic_parser_base {
Chris Lattneraf7e8212002-07-23 17:15:09 +0000406protected:
Chris Lattner331de232002-07-22 02:07:59 +0000407 std::vector<std::pair<const char *,
408 std::pair<DataType, const char *> > > Values;
Chris Lattneraf7e8212002-07-23 17:15:09 +0000409public:
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000410 typedef DataType parser_data_type;
Chris Lattner331de232002-07-22 02:07:59 +0000411
412 // Implement virtual functions needed by generic_parser_base
413 unsigned getNumOptions() const { return Values.size(); }
414 const char *getOption(unsigned N) const { return Values[N].first; }
415 const char *getDescription(unsigned N) const {
416 return Values[N].second.second;
417 }
418
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000419 // parse - Return true on error.
420 bool parse(Option &O, const char *ArgName, const std::string &Arg,
421 DataType &V) {
Chris Lattner7f4dd472002-07-24 22:08:36 +0000422 std::string ArgVal;
Chris Lattner331de232002-07-22 02:07:59 +0000423 if (hasArgStr)
424 ArgVal = Arg;
425 else
426 ArgVal = ArgName;
427
428 for (unsigned i = 0, e = Values.size(); i != e; ++i)
429 if (ArgVal == Values[i].first) {
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000430 V = Values[i].second.first;
Chris Lattner331de232002-07-22 02:07:59 +0000431 return false;
432 }
433
434 return O.error(": Cannot find option named '" + ArgVal + "'!");
435 }
436
437 // addLiteralOption - Add an entry to the mapping table...
438 template <class DT>
439 void addLiteralOption(const char *Name, const DT &V, const char *HelpStr) {
Chris Lattneraf7e8212002-07-23 17:15:09 +0000440 assert(findOption(Name) == Values.size() && "Option already exists!");
Chris Lattner331de232002-07-22 02:07:59 +0000441 Values.push_back(std::make_pair(Name, std::make_pair((DataType)V,HelpStr)));
442 }
Chris Lattneraf7e8212002-07-23 17:15:09 +0000443
444 // removeLiteralOption - Remove the specified option.
445 //
446 void removeLiteralOption(const char *Name) {
447 unsigned N = findOption(Name);
448 assert(N != Values.size() && "Option not found!");
449 Values.erase(Values.begin()+N);
450 }
Chris Lattner331de232002-07-22 02:07:59 +0000451};
452
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000453//--------------------------------------------------
454// basic_parser - Super class of parsers to provide boilerplate code
455//
456struct basic_parser_impl { // non-template implementation of basic_parser<t>
457 virtual ~basic_parser_impl() {}
458
459 enum ValueExpected getValueExpectedFlagDefault() const {
460 return ValueRequired;
461 }
462
463 void initialize(Option &O) {}
464
465 // Return the width of the option tag for printing...
466 unsigned getOptionWidth(const Option &O) const;
467
468 // printOptionInfo - Print out information about this option. The
469 // to-be-maintained width is specified.
470 //
471 void printOptionInfo(const Option &O, unsigned GlobalWidth) const;
472
473
474 // getValueName - Overload in subclass to provide a better default value.
475 virtual const char *getValueName() const { return "value"; }
476};
477
478// basic_parser - The real basic parser is just a template wrapper that provides
479// a typedef for the provided data type.
480//
481template<class DataType>
482struct basic_parser : public basic_parser_impl {
483 typedef DataType parser_data_type;
484};
485
Chris Lattner331de232002-07-22 02:07:59 +0000486
487//--------------------------------------------------
488// parser<bool>
489//
490template<>
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000491struct parser<bool> : public basic_parser<bool> {
492
493 // parse - Return true on error.
494 bool parse(Option &O, const char *ArgName, const std::string &Arg, bool &Val);
Chris Lattner331de232002-07-22 02:07:59 +0000495
496 enum ValueExpected getValueExpectedFlagDefault() const {
497 return ValueOptional;
498 }
499
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000500 // getValueName - Do not print =<value> at all
501 virtual const char *getValueName() const { return 0; }
Chris Lattner331de232002-07-22 02:07:59 +0000502};
503
504
505//--------------------------------------------------
506// parser<int>
507//
508template<>
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000509struct parser<int> : public basic_parser<int> {
Chris Lattner331de232002-07-22 02:07:59 +0000510
511 // parse - Return true on error.
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000512 bool parse(Option &O, const char *ArgName, const std::string &Arg, int &Val);
Chris Lattner331de232002-07-22 02:07:59 +0000513
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000514 // getValueName - Overload in subclass to provide a better default value.
515 virtual const char *getValueName() const { return "int"; }
Chris Lattner331de232002-07-22 02:07:59 +0000516};
517
518
519//--------------------------------------------------
520// parser<double>
521//
522template<>
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000523struct parser<double> : public basic_parser<double> {
Chris Lattner331de232002-07-22 02:07:59 +0000524 // parse - Return true on error.
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000525 bool parse(Option &O, const char *AN, const std::string &Arg, double &Val);
Chris Lattner331de232002-07-22 02:07:59 +0000526
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000527 // getValueName - Overload in subclass to provide a better default value.
528 virtual const char *getValueName() const { return "number"; }
Chris Lattner331de232002-07-22 02:07:59 +0000529};
530
Chris Lattner331de232002-07-22 02:07:59 +0000531
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000532//--------------------------------------------------
533// parser<float>
534//
535template<>
536struct parser<float> : public basic_parser<float> {
537 // parse - Return true on error.
538 bool parse(Option &O, const char *AN, const std::string &Arg, float &Val);
539
540 // getValueName - Overload in subclass to provide a better default value.
541 virtual const char *getValueName() const { return "number"; }
542};
Chris Lattner331de232002-07-22 02:07:59 +0000543
544
545//--------------------------------------------------
Chris Lattner7f4dd472002-07-24 22:08:36 +0000546// parser<std::string>
Chris Lattner331de232002-07-22 02:07:59 +0000547//
548template<>
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000549struct parser<std::string> : public basic_parser<std::string> {
Chris Lattner331de232002-07-22 02:07:59 +0000550 // parse - Return true on error.
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000551 bool parse(Option &O, const char *ArgName, const std::string &Arg,
552 std::string &Value) {
553 Value = Arg;
Chris Lattner331de232002-07-22 02:07:59 +0000554 return false;
555 }
Chris Lattner71fb7162002-05-22 17:03:05 +0000556
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000557 // getValueName - Overload in subclass to provide a better default value.
558 virtual const char *getValueName() const { return "string"; }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000559};
560
Chris Lattner71fb7162002-05-22 17:03:05 +0000561
Chris Lattner331de232002-07-22 02:07:59 +0000562
563//===----------------------------------------------------------------------===//
564// applicator class - This class is used because we must use partial
565// specialization to handle literal string arguments specially (const char* does
566// not correctly respond to the apply method). Because the syntax to use this
567// is a pain, we have the 'apply' method below to handle the nastiness...
568//
569template<class Mod> struct applicator {
570 template<class Opt>
571 static void opt(const Mod &M, Opt &O) { M.apply(O); }
572};
573
574// Handle const char* as a special case...
575template<unsigned n> struct applicator<char[n]> {
576 template<class Opt>
577 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
578};
Chris Lattner40423322002-09-13 14:33:39 +0000579template<unsigned n> struct applicator<const char[n]> {
580 template<class Opt>
581 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
582};
Chris Lattner331de232002-07-22 02:07:59 +0000583template<> struct applicator<const char*> {
584 template<class Opt>
585 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
586};
587
588template<> struct applicator<NumOccurances> {
589 static void opt(NumOccurances NO, Option &O) { O.setNumOccurancesFlag(NO); }
590};
591template<> struct applicator<ValueExpected> {
592 static void opt(ValueExpected VE, Option &O) { O.setValueExpectedFlag(VE); }
593};
594template<> struct applicator<OptionHidden> {
595 static void opt(OptionHidden OH, Option &O) { O.setHiddenFlag(OH); }
596};
597template<> struct applicator<FormattingFlags> {
598 static void opt(FormattingFlags FF, Option &O) { O.setFormattingFlag(FF); }
599};
Chris Lattnerb3b729b2003-05-22 20:25:57 +0000600template<> struct applicator<MiscFlags> {
601 static void opt(MiscFlags MF, Option &O) { O.setMiscFlag(MF); }
602};
Chris Lattner331de232002-07-22 02:07:59 +0000603
604// apply method - Apply a modifier to an option in a type safe way.
605template<class Mod, class Opt>
606void apply(const Mod &M, Opt *O) {
607 applicator<Mod>::opt(M, *O);
608}
609
610
611//===----------------------------------------------------------------------===//
612// opt_storage class
613
614// Default storage class definition: external storage. This implementation
615// assumes the user will specify a variable to store the data into with the
616// cl::location(x) modifier.
617//
618template<class DataType, bool ExternalStorage, bool isClass>
619class opt_storage {
620 DataType *Location; // Where to store the object...
621
622 void check() {
623 assert(Location != 0 && "cl::location(...) not specified for a command "
624 "line option with external storage!");
625 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000626public:
Chris Lattner331de232002-07-22 02:07:59 +0000627 opt_storage() : Location(0) {}
628
629 bool setLocation(Option &O, DataType &L) {
630 if (Location)
631 return O.error(": cl::location(x) specified more than once!");
632 Location = &L;
633 return false;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000634 }
635
Chris Lattner331de232002-07-22 02:07:59 +0000636 template<class T>
637 void setValue(const T &V) {
638 check();
639 *Location = V;
640 }
641
642 DataType &getValue() { check(); return *Location; }
643 const DataType &getValue() const { check(); return *Location; }
644};
645
646
647// Define how to hold a class type object, such as a string. Since we can
648// inherit from a class, we do so. This makes us exactly compatible with the
649// object in all cases that it is used.
650//
651template<class DataType>
652struct opt_storage<DataType,false,true> : public DataType {
653
654 template<class T>
655 void setValue(const T &V) { DataType::operator=(V); }
656
657 DataType &getValue() { return *this; }
658 const DataType &getValue() const { return *this; }
659};
660
661// Define a partial specialization to handle things we cannot inherit from. In
662// this case, we store an instance through containment, and overload operators
663// to get at the value.
664//
665template<class DataType>
666struct opt_storage<DataType, false, false> {
667 DataType Value;
668
669 // Make sure we initialize the value with the default constructor for the
670 // type.
671 opt_storage() : Value(DataType()) {}
672
673 template<class T>
674 void setValue(const T &V) { Value = V; }
675 DataType &getValue() { return Value; }
676 DataType getValue() const { return Value; }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000677};
678
679
680//===----------------------------------------------------------------------===//
Chris Lattner331de232002-07-22 02:07:59 +0000681// opt - A scalar command line option.
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000682//
Chris Lattner331de232002-07-22 02:07:59 +0000683template <class DataType, bool ExternalStorage = false,
684 class ParserClass = parser<DataType> >
685class opt : public Option,
686 public opt_storage<DataType, ExternalStorage,
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000687 ::boost::is_class<DataType>::value> {
Chris Lattner331de232002-07-22 02:07:59 +0000688 ParserClass Parser;
689
690 virtual bool handleOccurance(const char *ArgName, const std::string &Arg) {
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000691 typename ParserClass::parser_data_type Val;
692 if (Parser.parse(*this, ArgName, Arg, Val))
693 return true; // Parse error!
694 setValue(Val);
695 return false;
Chris Lattner331de232002-07-22 02:07:59 +0000696 }
697
698 virtual enum ValueExpected getValueExpectedFlagDefault() const {
699 return Parser.getValueExpectedFlagDefault();
700 }
701
702 // Forward printing stuff to the parser...
703 virtual unsigned getOptionWidth() const {return Parser.getOptionWidth(*this);}
704 virtual void printOptionInfo(unsigned GlobalWidth) const {
705 Parser.printOptionInfo(*this, GlobalWidth);
706 }
707
708 void done() {
709 addArgument(ArgStr);
710 Parser.initialize(*this);
711 }
712public:
713 // setInitialValue - Used by the cl::init modifier...
714 void setInitialValue(const DataType &V) { setValue(V); }
715
Chris Lattner331de232002-07-22 02:07:59 +0000716 ParserClass &getParser() { return Parser; }
717
718 operator DataType() const { return getValue(); }
719
720 template<class T>
721 DataType &operator=(const T &Val) { setValue(Val); return getValue(); }
722
723 // One option...
724 template<class M0t>
725 opt(const M0t &M0) {
726 apply(M0, this);
727 done();
728 }
729
730 // Two options...
731 template<class M0t, class M1t>
732 opt(const M0t &M0, const M1t &M1) {
733 apply(M0, this); apply(M1, this);
734 done();
735 }
736
737 // Three options...
738 template<class M0t, class M1t, class M2t>
739 opt(const M0t &M0, const M1t &M1, const M2t &M2) {
740 apply(M0, this); apply(M1, this); apply(M2, this);
741 done();
742 }
743 // Four options...
744 template<class M0t, class M1t, class M2t, class M3t>
745 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3) {
746 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
747 done();
748 }
749 // Five options...
750 template<class M0t, class M1t, class M2t, class M3t, class M4t>
751 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
752 const M4t &M4) {
753 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
754 apply(M4, this);
755 done();
756 }
757 // Six options...
758 template<class M0t, class M1t, class M2t, class M3t,
759 class M4t, class M5t>
760 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
761 const M4t &M4, const M5t &M5) {
762 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
763 apply(M4, this); apply(M5, this);
764 done();
765 }
766 // Seven options...
767 template<class M0t, class M1t, class M2t, class M3t,
768 class M4t, class M5t, class M6t>
769 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
770 const M4t &M4, const M5t &M5, const M6t &M6) {
771 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
772 apply(M4, this); apply(M5, this); apply(M6, this);
773 done();
774 }
775 // Eight options...
776 template<class M0t, class M1t, class M2t, class M3t,
777 class M4t, class M5t, class M6t, class M7t>
778 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
779 const M4t &M4, const M5t &M5, const M6t &M6, const M7t &M7) {
780 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
781 apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
782 done();
783 }
784};
785
786//===----------------------------------------------------------------------===//
787// list_storage class
788
789// Default storage class definition: external storage. This implementation
790// assumes the user will specify a variable to store the data into with the
791// cl::location(x) modifier.
792//
793template<class DataType, class StorageClass>
794class list_storage {
795 StorageClass *Location; // Where to store the object...
796
797public:
798 list_storage() : Location(0) {}
799
800 bool setLocation(Option &O, StorageClass &L) {
801 if (Location)
802 return O.error(": cl::location(x) specified more than once!");
803 Location = &L;
804 return false;
805 }
806
807 template<class T>
808 void addValue(const T &V) {
809 assert(Location != 0 && "cl::location(...) not specified for a command "
810 "line option with external storage!");
811 Location->push_back(V);
812 }
813};
814
815
816// Define how to hold a class type object, such as a string. Since we can
817// inherit from a class, we do so. This makes us exactly compatible with the
818// object in all cases that it is used.
819//
820template<class DataType>
821struct list_storage<DataType, bool> : public std::vector<DataType> {
822
823 template<class T>
824 void addValue(const T &V) { push_back(V); }
825};
826
827
828//===----------------------------------------------------------------------===//
829// list - A list of command line options.
830//
831template <class DataType, class Storage = bool,
832 class ParserClass = parser<DataType> >
833class list : public Option, public list_storage<DataType, Storage> {
834 ParserClass Parser;
835
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000836 virtual enum NumOccurances getNumOccurancesFlagDefault() const {
837 return ZeroOrMore;
838 }
839 virtual enum ValueExpected getValueExpectedFlagDefault() const {
Chris Lattner331de232002-07-22 02:07:59 +0000840 return Parser.getValueExpectedFlagDefault();
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000841 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000842
Chris Lattner331de232002-07-22 02:07:59 +0000843 virtual bool handleOccurance(const char *ArgName, const std::string &Arg) {
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000844 typename ParserClass::parser_data_type Val;
845 if (Parser.parse(*this, ArgName, Arg, Val))
846 return true; // Parse Error!
847 addValue(Val);
848 return false;
Chris Lattner331de232002-07-22 02:07:59 +0000849 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000850
Chris Lattner331de232002-07-22 02:07:59 +0000851 // Forward printing stuff to the parser...
852 virtual unsigned getOptionWidth() const {return Parser.getOptionWidth(*this);}
853 virtual void printOptionInfo(unsigned GlobalWidth) const {
854 Parser.printOptionInfo(*this, GlobalWidth);
855 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000856
Chris Lattner331de232002-07-22 02:07:59 +0000857 void done() {
858 addArgument(ArgStr);
859 Parser.initialize(*this);
860 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000861public:
Chris Lattner331de232002-07-22 02:07:59 +0000862 ParserClass &getParser() { return Parser; }
863
864 // One option...
865 template<class M0t>
866 list(const M0t &M0) {
867 apply(M0, this);
868 done();
869 }
870 // Two options...
871 template<class M0t, class M1t>
872 list(const M0t &M0, const M1t &M1) {
873 apply(M0, this); apply(M1, this);
874 done();
875 }
876 // Three options...
877 template<class M0t, class M1t, class M2t>
878 list(const M0t &M0, const M1t &M1, const M2t &M2) {
879 apply(M0, this); apply(M1, this); apply(M2, this);
880 done();
881 }
882 // Four options...
883 template<class M0t, class M1t, class M2t, class M3t>
884 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3) {
885 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
886 done();
887 }
888 // Five options...
889 template<class M0t, class M1t, class M2t, class M3t, class M4t>
890 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
891 const M4t &M4) {
892 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
893 apply(M4, this);
894 done();
895 }
896 // Six options...
897 template<class M0t, class M1t, class M2t, class M3t,
898 class M4t, class M5t>
899 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
900 const M4t &M4, const M5t &M5) {
901 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
902 apply(M4, this); apply(M5, this);
903 done();
904 }
905 // Seven options...
906 template<class M0t, class M1t, class M2t, class M3t,
907 class M4t, class M5t, class M6t>
908 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
909 const M4t &M4, const M5t &M5, const M6t &M6) {
910 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
911 apply(M4, this); apply(M5, this); apply(M6, this);
912 done();
913 }
914 // Eight options...
915 template<class M0t, class M1t, class M2t, class M3t,
916 class M4t, class M5t, class M6t, class M7t>
917 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
918 const M4t &M4, const M5t &M5, const M6t &M6, const M7t &M7) {
919 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
920 apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
921 done();
922 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000923};
924
Chris Lattner331de232002-07-22 02:07:59 +0000925
926
927//===----------------------------------------------------------------------===//
928// Aliased command line option (alias this name to a preexisting name)
929//
930
931class alias : public Option {
932 Option *AliasFor;
933 virtual bool handleOccurance(const char *ArgName, const std::string &Arg) {
934 return AliasFor->handleOccurance(AliasFor->ArgStr, Arg);
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000935 }
Chris Lattner331de232002-07-22 02:07:59 +0000936 // Aliases default to be hidden...
937 virtual enum OptionHidden getOptionHiddenFlagDefault() const {return Hidden;}
938
939 // Handle printing stuff...
940 virtual unsigned getOptionWidth() const;
941 virtual void printOptionInfo(unsigned GlobalWidth) const;
942
943 void done() {
944 if (!hasArgStr())
945 error(": cl::alias must have argument name specified!");
946 if (AliasFor == 0)
947 error(": cl::alias must have an cl::aliasopt(option) specified!");
948 addArgument(ArgStr);
949 }
950public:
951 void setAliasFor(Option &O) {
952 if (AliasFor)
953 error(": cl::alias must only have one cl::aliasopt(...) specified!");
954 AliasFor = &O;
955 }
956
957 // One option...
958 template<class M0t>
959 alias(const M0t &M0) : AliasFor(0) {
960 apply(M0, this);
961 done();
962 }
963 // Two options...
964 template<class M0t, class M1t>
965 alias(const M0t &M0, const M1t &M1) : AliasFor(0) {
966 apply(M0, this); apply(M1, this);
967 done();
968 }
969 // Three options...
970 template<class M0t, class M1t, class M2t>
971 alias(const M0t &M0, const M1t &M1, const M2t &M2) : AliasFor(0) {
972 apply(M0, this); apply(M1, this); apply(M2, this);
973 done();
974 }
975 // Four options...
976 template<class M0t, class M1t, class M2t, class M3t>
977 alias(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3)
978 : AliasFor(0) {
979 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
980 done();
981 }
982};
983
984// aliasfor - Modifier to set the option an alias aliases.
985struct aliasopt {
986 Option &Opt;
987 aliasopt(Option &O) : Opt(O) {}
988 void apply(alias &A) const { A.setAliasFor(Opt); }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000989};
990
991} // End namespace cl
992
993#endif