blob: dcd3b5ba511ee025096d059bb9f683fcfb732093 [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 Lattnera44a4cd2003-07-25 17:23:27 +000020#include <cassert>
Chris Lattner331de232002-07-22 02:07:59 +000021#include "boost/type_traits/object_traits.hpp"
John Criswellbe583b92003-06-11 14:01:36 +000022
Chris Lattnerbf0ac3f2003-06-03 15:30:37 +000023/// cl Namespace - This namespace contains all of the command line option
24/// processing machinery. It is intentionally a short name to make qualified
25/// usage concise.
26namespace cl {
Chris Lattnercee8f9a2001-11-27 00:03:19 +000027
28//===----------------------------------------------------------------------===//
Chris Lattner331de232002-07-22 02:07:59 +000029// ParseCommandLineOptions - Command line option processing entry point.
Chris Lattnercee8f9a2001-11-27 00:03:19 +000030//
Chris Lattner20968a22003-08-14 22:04:41 +000031void ParseCommandLineOptions(int &argc, char **argv,
32 const char *Overview = 0);
Chris Lattnercee8f9a2001-11-27 00:03:19 +000033
34//===----------------------------------------------------------------------===//
Brian Gaeke06b06c52003-08-14 22:00:59 +000035// ParseEnvironmentOptions - Environment variable option processing alternate
36// entry point.
37//
Brian Gaekec48ef2a2003-08-15 21:05:57 +000038void ParseEnvironmentOptions(const char *progName, const char *envvar,
Chris Lattner20968a22003-08-14 22:04:41 +000039 const char *Overview = 0);
Brian Gaeke06b06c52003-08-14 22:00:59 +000040
41//===----------------------------------------------------------------------===//
Chris Lattner331de232002-07-22 02:07:59 +000042// Flags permitted to be passed to command line arguments
43//
Chris Lattnercee8f9a2001-11-27 00:03:19 +000044
Misha Brukmanb5c520b2003-07-10 17:05:26 +000045enum NumOccurrences { // Flags for the number of occurrences allowed
46 Optional = 0x01, // Zero or One occurrence
47 ZeroOrMore = 0x02, // Zero or more occurrences allowed
48 Required = 0x03, // One occurrence required
49 OneOrMore = 0x04, // One or more occurrences required
Chris Lattnercee8f9a2001-11-27 00:03:19 +000050
Chris Lattner331de232002-07-22 02:07:59 +000051 // ConsumeAfter - Indicates that this option is fed anything that follows the
52 // last positional argument required by the application (it is an error if
53 // there are zero positional arguments, and a ConsumeAfter option is used).
54 // Thus, for example, all arguments to LLI are processed until a filename is
55 // found. Once a filename is found, all of the succeeding arguments are
56 // passed, unprocessed, to the ConsumeAfter option.
Chris Lattnercee8f9a2001-11-27 00:03:19 +000057 //
58 ConsumeAfter = 0x05,
59
Misha Brukmandd6cb6a2003-07-10 16:49:51 +000060 OccurrencesMask = 0x07,
Chris Lattnercee8f9a2001-11-27 00:03:19 +000061};
62
63enum ValueExpected { // Is a value required for the option?
Chris Lattnerb3b729b2003-05-22 20:25:57 +000064 ValueOptional = 0x08, // The value can appear... or not
Chris Lattnercee8f9a2001-11-27 00:03:19 +000065 ValueRequired = 0x10, // The value is required to appear!
66 ValueDisallowed = 0x18, // A value may not be specified (for flags)
67 ValueMask = 0x18,
68};
69
70enum OptionHidden { // Control whether -help shows this option
71 NotHidden = 0x20, // Option included in --help & --help-hidden
72 Hidden = 0x40, // -help doesn't, but --help-hidden does
73 ReallyHidden = 0x60, // Neither --help nor --help-hidden show this arg
74 HiddenMask = 0x60,
75};
76
Chris Lattner331de232002-07-22 02:07:59 +000077// Formatting flags - This controls special features that the option might have
78// that cause it to be parsed differently...
79//
80// Prefix - This option allows arguments that are otherwise unrecognized to be
81// matched by options that are a prefix of the actual value. This is useful for
82// cases like a linker, where options are typically of the form '-lfoo' or
83// '-L../../include' where -l or -L are the actual flags. When prefix is
84// enabled, and used, the value for the flag comes from the suffix of the
85// argument.
86//
87// Grouping - With this option enabled, multiple letter options are allowed to
88// bunch together with only a single hyphen for the whole group. This allows
89// emulation of the behavior that ls uses for example: ls -la === ls -l -a
90//
91
92enum FormattingFlags {
93 NormalFormatting = 0x000, // Nothing special
94 Positional = 0x080, // Is a positional argument, no '-' required
95 Prefix = 0x100, // Can this option directly prefix its value?
96 Grouping = 0x180, // Can this option group with other options?
97 FormattingMask = 0x180,
98};
99
Chris Lattnerb3b729b2003-05-22 20:25:57 +0000100enum MiscFlags { // Miscellaneous flags to adjust argument
101 CommaSeparated = 0x200, // Should this cl::list split between commas?
102 MiscMask = 0x200,
103};
104
105
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000106
107//===----------------------------------------------------------------------===//
108// Option Base class
109//
Chris Lattner331de232002-07-22 02:07:59 +0000110class alias;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000111class Option {
112 friend void cl::ParseCommandLineOptions(int &, char **, const char *, int);
Chris Lattner331de232002-07-22 02:07:59 +0000113 friend class alias;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000114
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000115 // handleOccurrences - Overriden by subclasses to handle the value passed into
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000116 // an argument. Should return true if there was an error processing the
117 // argument and the program should exit.
118 //
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000119 virtual bool handleOccurrence(const char *ArgName, const std::string &Arg) = 0;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000120
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000121 virtual enum NumOccurrences getNumOccurrencesFlagDefault() const {
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000122 return Optional;
123 }
124 virtual enum ValueExpected getValueExpectedFlagDefault() const {
125 return ValueOptional;
126 }
127 virtual enum OptionHidden getOptionHiddenFlagDefault() const {
128 return NotHidden;
129 }
Chris Lattner331de232002-07-22 02:07:59 +0000130 virtual enum FormattingFlags getFormattingFlagDefault() const {
131 return NormalFormatting;
132 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000133
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000134 int NumOccurrences; // The number of times specified
Chris Lattner331de232002-07-22 02:07:59 +0000135 int Flags; // Flags for the argument
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000136public:
Chris Lattner331de232002-07-22 02:07:59 +0000137 const char *ArgStr; // The argument string itself (ex: "help", "o")
138 const char *HelpStr; // The descriptive text message for --help
139 const char *ValueStr; // String describing what the value of this option is
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000140
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000141 inline enum NumOccurrences getNumOccurrencesFlag() const {
142 int NO = Flags & OccurrencesMask;
143 return NO ? (enum NumOccurrences)NO : getNumOccurrencesFlagDefault();
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000144 }
145 inline enum ValueExpected getValueExpectedFlag() const {
146 int VE = Flags & ValueMask;
147 return VE ? (enum ValueExpected)VE : getValueExpectedFlagDefault();
148 }
149 inline enum OptionHidden getOptionHiddenFlag() const {
150 int OH = Flags & HiddenMask;
151 return OH ? (enum OptionHidden)OH : getOptionHiddenFlagDefault();
152 }
Chris Lattner331de232002-07-22 02:07:59 +0000153 inline enum FormattingFlags getFormattingFlag() const {
154 int OH = Flags & FormattingMask;
155 return OH ? (enum FormattingFlags)OH : getFormattingFlagDefault();
156 }
Chris Lattnerb3b729b2003-05-22 20:25:57 +0000157 inline unsigned getMiscFlags() const {
158 return Flags & MiscMask;
159 }
Chris Lattner331de232002-07-22 02:07:59 +0000160
161 // hasArgStr - Return true if the argstr != ""
162 bool hasArgStr() const { return ArgStr[0] != 0; }
163
164 //-------------------------------------------------------------------------===
165 // Accessor functions set by OptionModifiers
166 //
167 void setArgStr(const char *S) { ArgStr = S; }
168 void setDescription(const char *S) { HelpStr = S; }
169 void setValueStr(const char *S) { ValueStr = S; }
170
171 void setFlag(unsigned Flag, unsigned FlagMask) {
172 if (Flags & FlagMask) {
173 error(": Specified two settings for the same option!");
174 exit(1);
175 }
176
177 Flags |= Flag;
178 }
179
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000180 void setNumOccurrencesFlag(enum NumOccurrences Val) {
181 setFlag(Val, OccurrencesMask);
Chris Lattner331de232002-07-22 02:07:59 +0000182 }
183 void setValueExpectedFlag(enum ValueExpected Val) { setFlag(Val, ValueMask); }
184 void setHiddenFlag(enum OptionHidden Val) { setFlag(Val, HiddenMask); }
185 void setFormattingFlag(enum FormattingFlags V) { setFlag(V, FormattingMask); }
Chris Lattnerb3b729b2003-05-22 20:25:57 +0000186 void setMiscFlag(enum MiscFlags M) { setFlag(M, M); }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000187protected:
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000188 Option() : NumOccurrences(0), Flags(0),
Chris Lattner331de232002-07-22 02:07:59 +0000189 ArgStr(""), HelpStr(""), ValueStr("") {}
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000190
191public:
Chris Lattner331de232002-07-22 02:07:59 +0000192 // addArgument - Tell the system that this Option subclass will handle all
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000193 // occurrences of -ArgStr on the command line.
Chris Lattner331de232002-07-22 02:07:59 +0000194 //
195 void addArgument(const char *ArgStr);
Chris Lattnerae1257a2002-07-23 00:44:37 +0000196 void removeArgument(const char *ArgStr);
Chris Lattner331de232002-07-22 02:07:59 +0000197
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000198 // Return the width of the option tag for printing...
Chris Lattner331de232002-07-22 02:07:59 +0000199 virtual unsigned getOptionWidth() const = 0;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000200
201 // printOptionInfo - Print out information about this option. The
202 // to-be-maintained width is specified.
203 //
Chris Lattner331de232002-07-22 02:07:59 +0000204 virtual void printOptionInfo(unsigned GlobalWidth) const = 0;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000205
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000206 // addOccurrence - Wrapper around handleOccurrence that enforces Flags
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000207 //
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000208 bool addOccurrence(const char *ArgName, const std::string &Value);
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000209
210 // Prints option name followed by message. Always returns true.
Chris Lattner697954c2002-01-20 22:54:45 +0000211 bool error(std::string Message, const char *ArgName = 0);
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000212
213public:
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000214 inline int getNumOccurrences() const { return NumOccurrences; }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000215 virtual ~Option() {}
216};
217
218
219//===----------------------------------------------------------------------===//
Chris Lattner331de232002-07-22 02:07:59 +0000220// Command line option modifiers that can be used to modify the behavior of
221// command line option parsers...
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000222//
Chris Lattner331de232002-07-22 02:07:59 +0000223
224// desc - Modifier to set the description shown in the --help output...
225struct desc {
226 const char *Desc;
227 desc(const char *Str) : Desc(Str) {}
228 void apply(Option &O) const { O.setDescription(Desc); }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000229};
230
Chris Lattner331de232002-07-22 02:07:59 +0000231// value_desc - Modifier to set the value description shown in the --help
232// output...
233struct value_desc {
234 const char *Desc;
235 value_desc(const char *Str) : Desc(Str) {}
236 void apply(Option &O) const { O.setValueStr(Desc); }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000237};
238
239
Chris Lattner331de232002-07-22 02:07:59 +0000240// init - Specify a default (initial) value for the command line argument, if
241// the default constructor for the argument type does not give you what you
242// want. This is only valid on "opt" arguments, not on "list" arguments.
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000243//
Chris Lattner331de232002-07-22 02:07:59 +0000244template<class Ty>
245struct initializer {
246 const Ty &Init;
247 initializer(const Ty &Val) : Init(Val) {}
248
249 template<class Opt>
250 void apply(Opt &O) const { O.setInitialValue(Init); }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000251};
252
Chris Lattner331de232002-07-22 02:07:59 +0000253template<class Ty>
254initializer<Ty> init(const Ty &Val) {
255 return initializer<Ty>(Val);
256}
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000257
Chris Lattner331de232002-07-22 02:07:59 +0000258
259// location - Allow the user to specify which external variable they want to
260// store the results of the command line argument processing into, if they don't
261// want to store it in the option itself.
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000262//
Chris Lattner331de232002-07-22 02:07:59 +0000263template<class Ty>
264struct LocationClass {
265 Ty &Loc;
266 LocationClass(Ty &L) : Loc(L) {}
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000267
Chris Lattner331de232002-07-22 02:07:59 +0000268 template<class Opt>
269 void apply(Opt &O) const { O.setLocation(O, Loc); }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000270};
271
Chris Lattner331de232002-07-22 02:07:59 +0000272template<class Ty>
273LocationClass<Ty> location(Ty &L) { return LocationClass<Ty>(L); }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000274
275
276//===----------------------------------------------------------------------===//
277// Enum valued command line option
278//
Chris Lattnerae1257a2002-07-23 00:44:37 +0000279#define clEnumVal(ENUMVAL, DESC) #ENUMVAL, (int)ENUMVAL, DESC
280#define clEnumValN(ENUMVAL, FLAGNAME, DESC) FLAGNAME, (int)ENUMVAL, DESC
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000281
Chris Lattner331de232002-07-22 02:07:59 +0000282// values - For custom data types, allow specifying a group of values together
283// as the values that go into the mapping that the option handler uses. Note
284// that the values list must always have a 0 at the end of the list to indicate
285// that the list has ended.
286//
287template<class DataType>
288class ValuesClass {
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000289 // Use a vector instead of a map, because the lists should be short,
290 // the overhead is less, and most importantly, it keeps them in the order
291 // inserted so we can print our option out nicely.
Chris Lattner331de232002-07-22 02:07:59 +0000292 std::vector<std::pair<const char *, std::pair<int, const char *> > > Values;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000293 void processValues(va_list Vals);
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000294public:
Chris Lattner331de232002-07-22 02:07:59 +0000295 ValuesClass(const char *EnumName, DataType Val, const char *Desc,
296 va_list ValueArgs) {
297 // Insert the first value, which is required.
298 Values.push_back(std::make_pair(EnumName, std::make_pair(Val, Desc)));
299
300 // Process the varargs portion of the values...
301 while (const char *EnumName = va_arg(ValueArgs, const char *)) {
Chris Lattnerae1257a2002-07-23 00:44:37 +0000302 DataType EnumVal = (DataType)va_arg(ValueArgs, int);
Chris Lattner331de232002-07-22 02:07:59 +0000303 const char *EnumDesc = va_arg(ValueArgs, const char *);
304 Values.push_back(std::make_pair(EnumName, // Add value to value map
305 std::make_pair(EnumVal, EnumDesc)));
306 }
307 }
308
309 template<class Opt>
310 void apply(Opt &O) const {
311 for (unsigned i = 0, e = Values.size(); i != e; ++i)
312 O.getParser().addLiteralOption(Values[i].first, Values[i].second.first,
313 Values[i].second.second);
314 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000315};
316
Chris Lattner331de232002-07-22 02:07:59 +0000317template<class DataType>
318ValuesClass<DataType> values(const char *Arg, DataType Val, const char *Desc,
319 ...) {
320 va_list ValueArgs;
321 va_start(ValueArgs, Desc);
322 ValuesClass<DataType> Vals(Arg, Val, Desc, ValueArgs);
323 va_end(ValueArgs);
324 return Vals;
325}
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000326
Chris Lattner331de232002-07-22 02:07:59 +0000327
328//===----------------------------------------------------------------------===//
329// parser class - Parameterizable parser for different data types. By default,
330// known data types (string, int, bool) have specialized parsers, that do what
331// you would expect. The default parser, used for data types that are not
332// built-in, uses a mapping table to map specific options to values, which is
333// used, among other things, to handle enum types.
334
335//--------------------------------------------------
336// generic_parser_base - This class holds all the non-generic code that we do
337// not need replicated for every instance of the generic parser. This also
338// allows us to put stuff into CommandLine.cpp
339//
340struct generic_parser_base {
341 virtual ~generic_parser_base() {} // Base class should have virtual-dtor
342
343 // getNumOptions - Virtual function implemented by generic subclass to
344 // indicate how many entries are in Values.
345 //
346 virtual unsigned getNumOptions() const = 0;
347
348 // getOption - Return option name N.
349 virtual const char *getOption(unsigned N) const = 0;
350
351 // getDescription - Return description N
352 virtual const char *getDescription(unsigned N) const = 0;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000353
354 // Return the width of the option tag for printing...
Chris Lattner331de232002-07-22 02:07:59 +0000355 virtual unsigned getOptionWidth(const Option &O) const;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000356
357 // printOptionInfo - Print out information about this option. The
358 // to-be-maintained width is specified.
359 //
Chris Lattner331de232002-07-22 02:07:59 +0000360 virtual void printOptionInfo(const Option &O, unsigned GlobalWidth) const;
Chris Lattner71fb7162002-05-22 17:03:05 +0000361
Chris Lattner331de232002-07-22 02:07:59 +0000362 void initialize(Option &O) {
363 // All of the modifiers for the option have been processed by now, so the
364 // argstr field should be stable, copy it down now.
365 //
366 hasArgStr = O.hasArgStr();
367
368 // If there has been no argstr specified, that means that we need to add an
369 // argument for every possible option. This ensures that our options are
370 // vectored to us.
371 //
372 if (!hasArgStr)
373 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
374 O.addArgument(getOption(i));
375 }
376
377 enum ValueExpected getValueExpectedFlagDefault() const {
378 // If there is an ArgStr specified, then we are of the form:
379 //
380 // -opt=O2 or -opt O2 or -optO2
381 //
382 // In which case, the value is required. Otherwise if an arg str has not
383 // been specified, we are of the form:
384 //
Misha Brukmanbc0e9982003-07-14 17:20:40 +0000385 // -O2 or O2 or -la (where -l and -a are separate options)
Chris Lattner331de232002-07-22 02:07:59 +0000386 //
387 // If this is the case, we cannot allow a value.
388 //
389 if (hasArgStr)
390 return ValueRequired;
391 else
392 return ValueDisallowed;
393 }
394
Chris Lattneraf7e8212002-07-23 17:15:09 +0000395 // findOption - Return the option number corresponding to the specified
396 // argument string. If the option is not found, getNumOptions() is returned.
397 //
398 unsigned findOption(const char *Name);
399
Chris Lattner331de232002-07-22 02:07:59 +0000400protected:
401 bool hasArgStr;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000402};
403
Chris Lattner331de232002-07-22 02:07:59 +0000404// Default parser implementation - This implementation depends on having a
405// mapping of recognized options to values of some sort. In addition to this,
406// each entry in the mapping also tracks a help message that is printed with the
407// command line option for --help. Because this is a simple mapping parser, the
408// data type can be any unsupported type.
409//
410template <class DataType>
411class parser : public generic_parser_base {
Chris Lattneraf7e8212002-07-23 17:15:09 +0000412protected:
Chris Lattner331de232002-07-22 02:07:59 +0000413 std::vector<std::pair<const char *,
414 std::pair<DataType, const char *> > > Values;
Chris Lattneraf7e8212002-07-23 17:15:09 +0000415public:
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000416 typedef DataType parser_data_type;
Chris Lattner331de232002-07-22 02:07:59 +0000417
418 // Implement virtual functions needed by generic_parser_base
419 unsigned getNumOptions() const { return Values.size(); }
420 const char *getOption(unsigned N) const { return Values[N].first; }
421 const char *getDescription(unsigned N) const {
422 return Values[N].second.second;
423 }
424
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000425 // parse - Return true on error.
426 bool parse(Option &O, const char *ArgName, const std::string &Arg,
427 DataType &V) {
Chris Lattner7f4dd472002-07-24 22:08:36 +0000428 std::string ArgVal;
Chris Lattner331de232002-07-22 02:07:59 +0000429 if (hasArgStr)
430 ArgVal = Arg;
431 else
432 ArgVal = ArgName;
433
434 for (unsigned i = 0, e = Values.size(); i != e; ++i)
435 if (ArgVal == Values[i].first) {
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000436 V = Values[i].second.first;
Chris Lattner331de232002-07-22 02:07:59 +0000437 return false;
438 }
439
440 return O.error(": Cannot find option named '" + ArgVal + "'!");
441 }
442
443 // addLiteralOption - Add an entry to the mapping table...
444 template <class DT>
445 void addLiteralOption(const char *Name, const DT &V, const char *HelpStr) {
Chris Lattneraf7e8212002-07-23 17:15:09 +0000446 assert(findOption(Name) == Values.size() && "Option already exists!");
Chris Lattner331de232002-07-22 02:07:59 +0000447 Values.push_back(std::make_pair(Name, std::make_pair((DataType)V,HelpStr)));
448 }
Chris Lattneraf7e8212002-07-23 17:15:09 +0000449
450 // removeLiteralOption - Remove the specified option.
451 //
452 void removeLiteralOption(const char *Name) {
453 unsigned N = findOption(Name);
454 assert(N != Values.size() && "Option not found!");
455 Values.erase(Values.begin()+N);
456 }
Chris Lattner331de232002-07-22 02:07:59 +0000457};
458
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000459//--------------------------------------------------
460// basic_parser - Super class of parsers to provide boilerplate code
461//
462struct basic_parser_impl { // non-template implementation of basic_parser<t>
463 virtual ~basic_parser_impl() {}
464
465 enum ValueExpected getValueExpectedFlagDefault() const {
466 return ValueRequired;
467 }
468
469 void initialize(Option &O) {}
470
471 // Return the width of the option tag for printing...
472 unsigned getOptionWidth(const Option &O) const;
473
474 // printOptionInfo - Print out information about this option. The
475 // to-be-maintained width is specified.
476 //
477 void printOptionInfo(const Option &O, unsigned GlobalWidth) const;
478
479
480 // getValueName - Overload in subclass to provide a better default value.
481 virtual const char *getValueName() const { return "value"; }
482};
483
484// basic_parser - The real basic parser is just a template wrapper that provides
485// a typedef for the provided data type.
486//
487template<class DataType>
488struct basic_parser : public basic_parser_impl {
489 typedef DataType parser_data_type;
490};
491
Chris Lattner331de232002-07-22 02:07:59 +0000492
493//--------------------------------------------------
494// parser<bool>
495//
496template<>
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000497struct parser<bool> : public basic_parser<bool> {
498
499 // parse - Return true on error.
500 bool parse(Option &O, const char *ArgName, const std::string &Arg, bool &Val);
Chris Lattner331de232002-07-22 02:07:59 +0000501
502 enum ValueExpected getValueExpectedFlagDefault() const {
503 return ValueOptional;
504 }
505
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000506 // getValueName - Do not print =<value> at all
507 virtual const char *getValueName() const { return 0; }
Chris Lattner331de232002-07-22 02:07:59 +0000508};
509
510
511//--------------------------------------------------
512// parser<int>
513//
514template<>
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000515struct parser<int> : public basic_parser<int> {
Chris Lattner331de232002-07-22 02:07:59 +0000516
517 // parse - Return true on error.
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000518 bool parse(Option &O, const char *ArgName, const std::string &Arg, int &Val);
Chris Lattner331de232002-07-22 02:07:59 +0000519
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000520 // getValueName - Overload in subclass to provide a better default value.
521 virtual const char *getValueName() const { return "int"; }
Chris Lattner331de232002-07-22 02:07:59 +0000522};
523
524
525//--------------------------------------------------
Chris Lattnerd2a6fc32003-06-28 15:47:20 +0000526// parser<unsigned>
527//
528template<>
529struct parser<unsigned> : public basic_parser<unsigned> {
530
531 // parse - Return true on error.
532 bool parse(Option &O, const char *ArgName, const std::string &Arg,
533 unsigned &Val);
534
535 // getValueName - Overload in subclass to provide a better default value.
536 virtual const char *getValueName() const { return "uint"; }
537};
538
539
540//--------------------------------------------------
Chris Lattner331de232002-07-22 02:07:59 +0000541// parser<double>
542//
543template<>
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000544struct parser<double> : public basic_parser<double> {
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 *AN, const std::string &Arg, double &Val);
Chris Lattner331de232002-07-22 02:07:59 +0000547
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000548 // getValueName - Overload in subclass to provide a better default value.
549 virtual const char *getValueName() const { return "number"; }
Chris Lattner331de232002-07-22 02:07:59 +0000550};
551
Chris Lattner331de232002-07-22 02:07:59 +0000552
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000553//--------------------------------------------------
554// parser<float>
555//
556template<>
557struct parser<float> : public basic_parser<float> {
558 // parse - Return true on error.
559 bool parse(Option &O, const char *AN, const std::string &Arg, float &Val);
560
561 // getValueName - Overload in subclass to provide a better default value.
562 virtual const char *getValueName() const { return "number"; }
563};
Chris Lattner331de232002-07-22 02:07:59 +0000564
565
566//--------------------------------------------------
Chris Lattner7f4dd472002-07-24 22:08:36 +0000567// parser<std::string>
Chris Lattner331de232002-07-22 02:07:59 +0000568//
569template<>
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000570struct parser<std::string> : public basic_parser<std::string> {
Chris Lattner331de232002-07-22 02:07:59 +0000571 // parse - Return true on error.
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000572 bool parse(Option &O, const char *ArgName, const std::string &Arg,
573 std::string &Value) {
574 Value = Arg;
Chris Lattner331de232002-07-22 02:07:59 +0000575 return false;
576 }
Chris Lattner71fb7162002-05-22 17:03:05 +0000577
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000578 // getValueName - Overload in subclass to provide a better default value.
579 virtual const char *getValueName() const { return "string"; }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000580};
581
Chris Lattner71fb7162002-05-22 17:03:05 +0000582
Chris Lattner331de232002-07-22 02:07:59 +0000583
584//===----------------------------------------------------------------------===//
585// applicator class - This class is used because we must use partial
586// specialization to handle literal string arguments specially (const char* does
587// not correctly respond to the apply method). Because the syntax to use this
588// is a pain, we have the 'apply' method below to handle the nastiness...
589//
590template<class Mod> struct applicator {
591 template<class Opt>
592 static void opt(const Mod &M, Opt &O) { M.apply(O); }
593};
594
595// Handle const char* as a special case...
596template<unsigned n> struct applicator<char[n]> {
597 template<class Opt>
598 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
599};
Chris Lattner40423322002-09-13 14:33:39 +0000600template<unsigned n> struct applicator<const char[n]> {
601 template<class Opt>
602 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
603};
Chris Lattner331de232002-07-22 02:07:59 +0000604template<> struct applicator<const char*> {
605 template<class Opt>
606 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
607};
608
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000609template<> struct applicator<NumOccurrences> {
610 static void opt(NumOccurrences NO, Option &O) { O.setNumOccurrencesFlag(NO); }
Chris Lattner331de232002-07-22 02:07:59 +0000611};
612template<> struct applicator<ValueExpected> {
613 static void opt(ValueExpected VE, Option &O) { O.setValueExpectedFlag(VE); }
614};
615template<> struct applicator<OptionHidden> {
616 static void opt(OptionHidden OH, Option &O) { O.setHiddenFlag(OH); }
617};
618template<> struct applicator<FormattingFlags> {
619 static void opt(FormattingFlags FF, Option &O) { O.setFormattingFlag(FF); }
620};
Chris Lattnerb3b729b2003-05-22 20:25:57 +0000621template<> struct applicator<MiscFlags> {
622 static void opt(MiscFlags MF, Option &O) { O.setMiscFlag(MF); }
623};
Chris Lattner331de232002-07-22 02:07:59 +0000624
625// apply method - Apply a modifier to an option in a type safe way.
626template<class Mod, class Opt>
627void apply(const Mod &M, Opt *O) {
628 applicator<Mod>::opt(M, *O);
629}
630
631
632//===----------------------------------------------------------------------===//
633// opt_storage class
634
635// Default storage class definition: external storage. This implementation
636// assumes the user will specify a variable to store the data into with the
637// cl::location(x) modifier.
638//
639template<class DataType, bool ExternalStorage, bool isClass>
640class opt_storage {
641 DataType *Location; // Where to store the object...
642
643 void check() {
644 assert(Location != 0 && "cl::location(...) not specified for a command "
Chris Lattner3c7eb1f2003-08-19 21:57:00 +0000645 "line option with external storage, "
646 "or cl::init specified before cl::location()!!");
Chris Lattner331de232002-07-22 02:07:59 +0000647 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000648public:
Chris Lattner331de232002-07-22 02:07:59 +0000649 opt_storage() : Location(0) {}
650
651 bool setLocation(Option &O, DataType &L) {
652 if (Location)
653 return O.error(": cl::location(x) specified more than once!");
654 Location = &L;
655 return false;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000656 }
657
Chris Lattner331de232002-07-22 02:07:59 +0000658 template<class T>
659 void setValue(const T &V) {
660 check();
661 *Location = V;
662 }
663
664 DataType &getValue() { check(); return *Location; }
665 const DataType &getValue() const { check(); return *Location; }
666};
667
668
669// Define how to hold a class type object, such as a string. Since we can
670// inherit from a class, we do so. This makes us exactly compatible with the
671// object in all cases that it is used.
672//
673template<class DataType>
674struct opt_storage<DataType,false,true> : public DataType {
675
676 template<class T>
677 void setValue(const T &V) { DataType::operator=(V); }
678
679 DataType &getValue() { return *this; }
680 const DataType &getValue() const { return *this; }
681};
682
683// Define a partial specialization to handle things we cannot inherit from. In
684// this case, we store an instance through containment, and overload operators
685// to get at the value.
686//
687template<class DataType>
688struct opt_storage<DataType, false, false> {
689 DataType Value;
690
691 // Make sure we initialize the value with the default constructor for the
692 // type.
693 opt_storage() : Value(DataType()) {}
694
695 template<class T>
696 void setValue(const T &V) { Value = V; }
697 DataType &getValue() { return Value; }
698 DataType getValue() const { return Value; }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000699};
700
701
702//===----------------------------------------------------------------------===//
Chris Lattner331de232002-07-22 02:07:59 +0000703// opt - A scalar command line option.
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000704//
Chris Lattner331de232002-07-22 02:07:59 +0000705template <class DataType, bool ExternalStorage = false,
706 class ParserClass = parser<DataType> >
707class opt : public Option,
708 public opt_storage<DataType, ExternalStorage,
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000709 ::boost::is_class<DataType>::value> {
Chris Lattner331de232002-07-22 02:07:59 +0000710 ParserClass Parser;
711
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000712 virtual bool handleOccurrence(const char *ArgName, const std::string &Arg) {
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000713 typename ParserClass::parser_data_type Val;
714 if (Parser.parse(*this, ArgName, Arg, Val))
715 return true; // Parse error!
716 setValue(Val);
717 return false;
Chris Lattner331de232002-07-22 02:07:59 +0000718 }
719
720 virtual enum ValueExpected getValueExpectedFlagDefault() const {
721 return Parser.getValueExpectedFlagDefault();
722 }
723
724 // Forward printing stuff to the parser...
725 virtual unsigned getOptionWidth() const {return Parser.getOptionWidth(*this);}
726 virtual void printOptionInfo(unsigned GlobalWidth) const {
727 Parser.printOptionInfo(*this, GlobalWidth);
728 }
729
730 void done() {
731 addArgument(ArgStr);
732 Parser.initialize(*this);
733 }
734public:
735 // setInitialValue - Used by the cl::init modifier...
736 void setInitialValue(const DataType &V) { setValue(V); }
737
Chris Lattner331de232002-07-22 02:07:59 +0000738 ParserClass &getParser() { return Parser; }
739
740 operator DataType() const { return getValue(); }
741
742 template<class T>
743 DataType &operator=(const T &Val) { setValue(Val); return getValue(); }
744
745 // One option...
746 template<class M0t>
747 opt(const M0t &M0) {
748 apply(M0, this);
749 done();
750 }
751
752 // Two options...
753 template<class M0t, class M1t>
754 opt(const M0t &M0, const M1t &M1) {
755 apply(M0, this); apply(M1, this);
756 done();
757 }
758
759 // Three options...
760 template<class M0t, class M1t, class M2t>
761 opt(const M0t &M0, const M1t &M1, const M2t &M2) {
762 apply(M0, this); apply(M1, this); apply(M2, this);
763 done();
764 }
765 // Four options...
766 template<class M0t, class M1t, class M2t, class M3t>
767 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3) {
768 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
769 done();
770 }
771 // Five options...
772 template<class M0t, class M1t, class M2t, class M3t, class M4t>
773 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
774 const M4t &M4) {
775 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
776 apply(M4, this);
777 done();
778 }
779 // Six options...
780 template<class M0t, class M1t, class M2t, class M3t,
781 class M4t, class M5t>
782 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
783 const M4t &M4, const M5t &M5) {
784 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
785 apply(M4, this); apply(M5, this);
786 done();
787 }
788 // Seven options...
789 template<class M0t, class M1t, class M2t, class M3t,
790 class M4t, class M5t, class M6t>
791 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
792 const M4t &M4, const M5t &M5, const M6t &M6) {
793 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
794 apply(M4, this); apply(M5, this); apply(M6, this);
795 done();
796 }
797 // Eight options...
798 template<class M0t, class M1t, class M2t, class M3t,
799 class M4t, class M5t, class M6t, class M7t>
800 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
801 const M4t &M4, const M5t &M5, const M6t &M6, const M7t &M7) {
802 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
803 apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
804 done();
805 }
806};
807
808//===----------------------------------------------------------------------===//
809// list_storage class
810
811// Default storage class definition: external storage. This implementation
812// assumes the user will specify a variable to store the data into with the
813// cl::location(x) modifier.
814//
815template<class DataType, class StorageClass>
816class list_storage {
817 StorageClass *Location; // Where to store the object...
818
819public:
820 list_storage() : Location(0) {}
821
822 bool setLocation(Option &O, StorageClass &L) {
823 if (Location)
824 return O.error(": cl::location(x) specified more than once!");
825 Location = &L;
826 return false;
827 }
828
829 template<class T>
830 void addValue(const T &V) {
831 assert(Location != 0 && "cl::location(...) not specified for a command "
832 "line option with external storage!");
833 Location->push_back(V);
834 }
835};
836
837
838// Define how to hold a class type object, such as a string. Since we can
839// inherit from a class, we do so. This makes us exactly compatible with the
840// object in all cases that it is used.
841//
842template<class DataType>
843struct list_storage<DataType, bool> : public std::vector<DataType> {
844
845 template<class T>
846 void addValue(const T &V) { push_back(V); }
847};
848
849
850//===----------------------------------------------------------------------===//
851// list - A list of command line options.
852//
853template <class DataType, class Storage = bool,
854 class ParserClass = parser<DataType> >
855class list : public Option, public list_storage<DataType, Storage> {
856 ParserClass Parser;
857
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000858 virtual enum NumOccurrences getNumOccurrencesFlagDefault() const {
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000859 return ZeroOrMore;
860 }
861 virtual enum ValueExpected getValueExpectedFlagDefault() const {
Chris Lattner331de232002-07-22 02:07:59 +0000862 return Parser.getValueExpectedFlagDefault();
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000863 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000864
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000865 virtual bool handleOccurrence(const char *ArgName, const std::string &Arg) {
Chris Lattnerd23a35b2002-08-07 18:36:27 +0000866 typename ParserClass::parser_data_type Val;
867 if (Parser.parse(*this, ArgName, Arg, Val))
868 return true; // Parse Error!
869 addValue(Val);
870 return false;
Chris Lattner331de232002-07-22 02:07:59 +0000871 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000872
Chris Lattner331de232002-07-22 02:07:59 +0000873 // Forward printing stuff to the parser...
874 virtual unsigned getOptionWidth() const {return Parser.getOptionWidth(*this);}
875 virtual void printOptionInfo(unsigned GlobalWidth) const {
876 Parser.printOptionInfo(*this, GlobalWidth);
877 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000878
Chris Lattner331de232002-07-22 02:07:59 +0000879 void done() {
880 addArgument(ArgStr);
881 Parser.initialize(*this);
882 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000883public:
Chris Lattner331de232002-07-22 02:07:59 +0000884 ParserClass &getParser() { return Parser; }
885
886 // One option...
887 template<class M0t>
888 list(const M0t &M0) {
889 apply(M0, this);
890 done();
891 }
892 // Two options...
893 template<class M0t, class M1t>
894 list(const M0t &M0, const M1t &M1) {
895 apply(M0, this); apply(M1, this);
896 done();
897 }
898 // Three options...
899 template<class M0t, class M1t, class M2t>
900 list(const M0t &M0, const M1t &M1, const M2t &M2) {
901 apply(M0, this); apply(M1, this); apply(M2, this);
902 done();
903 }
904 // Four options...
905 template<class M0t, class M1t, class M2t, class M3t>
906 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3) {
907 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
908 done();
909 }
910 // Five options...
911 template<class M0t, class M1t, class M2t, class M3t, class M4t>
912 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
913 const M4t &M4) {
914 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
915 apply(M4, this);
916 done();
917 }
918 // Six options...
919 template<class M0t, class M1t, class M2t, class M3t,
920 class M4t, class M5t>
921 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
922 const M4t &M4, const M5t &M5) {
923 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
924 apply(M4, this); apply(M5, this);
925 done();
926 }
927 // Seven options...
928 template<class M0t, class M1t, class M2t, class M3t,
929 class M4t, class M5t, class M6t>
930 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
931 const M4t &M4, const M5t &M5, const M6t &M6) {
932 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
933 apply(M4, this); apply(M5, this); apply(M6, this);
934 done();
935 }
936 // Eight options...
937 template<class M0t, class M1t, class M2t, class M3t,
938 class M4t, class M5t, class M6t, class M7t>
939 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
940 const M4t &M4, const M5t &M5, const M6t &M6, const M7t &M7) {
941 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
942 apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
943 done();
944 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000945};
946
Chris Lattner331de232002-07-22 02:07:59 +0000947
948
949//===----------------------------------------------------------------------===//
950// Aliased command line option (alias this name to a preexisting name)
951//
952
953class alias : public Option {
954 Option *AliasFor;
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000955 virtual bool handleOccurrence(const char *ArgName, const std::string &Arg) {
956 return AliasFor->handleOccurrence(AliasFor->ArgStr, Arg);
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000957 }
Chris Lattner331de232002-07-22 02:07:59 +0000958 // Aliases default to be hidden...
959 virtual enum OptionHidden getOptionHiddenFlagDefault() const {return Hidden;}
960
961 // Handle printing stuff...
962 virtual unsigned getOptionWidth() const;
963 virtual void printOptionInfo(unsigned GlobalWidth) const;
964
965 void done() {
966 if (!hasArgStr())
967 error(": cl::alias must have argument name specified!");
968 if (AliasFor == 0)
969 error(": cl::alias must have an cl::aliasopt(option) specified!");
970 addArgument(ArgStr);
971 }
972public:
973 void setAliasFor(Option &O) {
974 if (AliasFor)
975 error(": cl::alias must only have one cl::aliasopt(...) specified!");
976 AliasFor = &O;
977 }
978
979 // One option...
980 template<class M0t>
981 alias(const M0t &M0) : AliasFor(0) {
982 apply(M0, this);
983 done();
984 }
985 // Two options...
986 template<class M0t, class M1t>
987 alias(const M0t &M0, const M1t &M1) : AliasFor(0) {
988 apply(M0, this); apply(M1, this);
989 done();
990 }
991 // Three options...
992 template<class M0t, class M1t, class M2t>
993 alias(const M0t &M0, const M1t &M1, const M2t &M2) : AliasFor(0) {
994 apply(M0, this); apply(M1, this); apply(M2, this);
995 done();
996 }
997 // Four options...
998 template<class M0t, class M1t, class M2t, class M3t>
999 alias(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3)
1000 : AliasFor(0) {
1001 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1002 done();
1003 }
1004};
1005
1006// aliasfor - Modifier to set the option an alias aliases.
1007struct aliasopt {
1008 Option &Opt;
1009 aliasopt(Option &O) : Opt(O) {}
1010 void apply(alias &A) const { A.setAliasFor(Opt); }
Chris Lattnercee8f9a2001-11-27 00:03:19 +00001011};
1012
1013} // End namespace cl
1014
1015#endif