Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 1 | //===- 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 Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 7 | // 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 Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #ifndef LLVM_SUPPORT_COMMANDLINE_H |
| 14 | #define LLVM_SUPPORT_COMMANDLINE_H |
| 15 | |
| 16 | #include <string> |
| 17 | #include <vector> |
| 18 | #include <utility> |
| 19 | #include <stdarg.h> |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 20 | #include "boost/type_traits/object_traits.hpp" |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 21 | |
| 22 | namespace cl { // Short namespace to make usage concise |
| 23 | |
| 24 | //===----------------------------------------------------------------------===// |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 25 | // ParseCommandLineOptions - Command line option processing entry point. |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 26 | // |
| 27 | void cl::ParseCommandLineOptions(int &argc, char **argv, |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 28 | const char *Overview = 0); |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 29 | |
| 30 | //===----------------------------------------------------------------------===// |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 31 | // Flags permitted to be passed to command line arguments |
| 32 | // |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 33 | |
| 34 | enum NumOccurances { // Flags for the number of occurances allowed... |
| 35 | Optional = 0x01, // Zero or One occurance |
| 36 | ZeroOrMore = 0x02, // Zero or more occurances allowed |
| 37 | Required = 0x03, // One occurance required |
| 38 | OneOrMore = 0x04, // One or more occurances required |
| 39 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 40 | // ConsumeAfter - Indicates that this option is fed anything that follows the |
| 41 | // last positional argument required by the application (it is an error if |
| 42 | // there are zero positional arguments, and a ConsumeAfter option is used). |
| 43 | // Thus, for example, all arguments to LLI are processed until a filename is |
| 44 | // found. Once a filename is found, all of the succeeding arguments are |
| 45 | // passed, unprocessed, to the ConsumeAfter option. |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 46 | // |
| 47 | ConsumeAfter = 0x05, |
| 48 | |
| 49 | OccurancesMask = 0x07, |
| 50 | }; |
| 51 | |
| 52 | enum ValueExpected { // Is a value required for the option? |
| 53 | ValueOptional = 0x08, // The value can oppear... or not |
| 54 | ValueRequired = 0x10, // The value is required to appear! |
| 55 | ValueDisallowed = 0x18, // A value may not be specified (for flags) |
| 56 | ValueMask = 0x18, |
| 57 | }; |
| 58 | |
| 59 | enum OptionHidden { // Control whether -help shows this option |
| 60 | NotHidden = 0x20, // Option included in --help & --help-hidden |
| 61 | Hidden = 0x40, // -help doesn't, but --help-hidden does |
| 62 | ReallyHidden = 0x60, // Neither --help nor --help-hidden show this arg |
| 63 | HiddenMask = 0x60, |
| 64 | }; |
| 65 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 66 | // Formatting flags - This controls special features that the option might have |
| 67 | // that cause it to be parsed differently... |
| 68 | // |
| 69 | // Prefix - This option allows arguments that are otherwise unrecognized to be |
| 70 | // matched by options that are a prefix of the actual value. This is useful for |
| 71 | // cases like a linker, where options are typically of the form '-lfoo' or |
| 72 | // '-L../../include' where -l or -L are the actual flags. When prefix is |
| 73 | // enabled, and used, the value for the flag comes from the suffix of the |
| 74 | // argument. |
| 75 | // |
| 76 | // Grouping - With this option enabled, multiple letter options are allowed to |
| 77 | // bunch together with only a single hyphen for the whole group. This allows |
| 78 | // emulation of the behavior that ls uses for example: ls -la === ls -l -a |
| 79 | // |
| 80 | |
| 81 | enum FormattingFlags { |
| 82 | NormalFormatting = 0x000, // Nothing special |
| 83 | Positional = 0x080, // Is a positional argument, no '-' required |
| 84 | Prefix = 0x100, // Can this option directly prefix its value? |
| 85 | Grouping = 0x180, // Can this option group with other options? |
| 86 | FormattingMask = 0x180, |
| 87 | }; |
| 88 | |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 89 | |
| 90 | //===----------------------------------------------------------------------===// |
| 91 | // Option Base class |
| 92 | // |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 93 | class alias; |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 94 | class Option { |
| 95 | friend void cl::ParseCommandLineOptions(int &, char **, const char *, int); |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 96 | friend class alias; |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 97 | |
| 98 | // handleOccurances - Overriden by subclasses to handle the value passed into |
| 99 | // an argument. Should return true if there was an error processing the |
| 100 | // argument and the program should exit. |
| 101 | // |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 102 | virtual bool handleOccurance(const char *ArgName, const std::string &Arg) = 0; |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 103 | |
| 104 | virtual enum NumOccurances getNumOccurancesFlagDefault() const { |
| 105 | return Optional; |
| 106 | } |
| 107 | virtual enum ValueExpected getValueExpectedFlagDefault() const { |
| 108 | return ValueOptional; |
| 109 | } |
| 110 | virtual enum OptionHidden getOptionHiddenFlagDefault() const { |
| 111 | return NotHidden; |
| 112 | } |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 113 | virtual enum FormattingFlags getFormattingFlagDefault() const { |
| 114 | return NormalFormatting; |
| 115 | } |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 116 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 117 | int NumOccurances; // The number of times specified |
| 118 | int Flags; // Flags for the argument |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 119 | public: |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 120 | const char *ArgStr; // The argument string itself (ex: "help", "o") |
| 121 | const char *HelpStr; // The descriptive text message for --help |
| 122 | const char *ValueStr; // String describing what the value of this option is |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 123 | |
| 124 | inline enum NumOccurances getNumOccurancesFlag() const { |
| 125 | int NO = Flags & OccurancesMask; |
| 126 | return NO ? (enum NumOccurances)NO : getNumOccurancesFlagDefault(); |
| 127 | } |
| 128 | inline enum ValueExpected getValueExpectedFlag() const { |
| 129 | int VE = Flags & ValueMask; |
| 130 | return VE ? (enum ValueExpected)VE : getValueExpectedFlagDefault(); |
| 131 | } |
| 132 | inline enum OptionHidden getOptionHiddenFlag() const { |
| 133 | int OH = Flags & HiddenMask; |
| 134 | return OH ? (enum OptionHidden)OH : getOptionHiddenFlagDefault(); |
| 135 | } |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 136 | inline enum FormattingFlags getFormattingFlag() const { |
| 137 | int OH = Flags & FormattingMask; |
| 138 | return OH ? (enum FormattingFlags)OH : getFormattingFlagDefault(); |
| 139 | } |
| 140 | |
| 141 | // hasArgStr - Return true if the argstr != "" |
| 142 | bool hasArgStr() const { return ArgStr[0] != 0; } |
| 143 | |
| 144 | //-------------------------------------------------------------------------=== |
| 145 | // Accessor functions set by OptionModifiers |
| 146 | // |
| 147 | void setArgStr(const char *S) { ArgStr = S; } |
| 148 | void setDescription(const char *S) { HelpStr = S; } |
| 149 | void setValueStr(const char *S) { ValueStr = S; } |
| 150 | |
| 151 | void setFlag(unsigned Flag, unsigned FlagMask) { |
| 152 | if (Flags & FlagMask) { |
| 153 | error(": Specified two settings for the same option!"); |
| 154 | exit(1); |
| 155 | } |
| 156 | |
| 157 | Flags |= Flag; |
| 158 | } |
| 159 | |
| 160 | void setNumOccurancesFlag(enum NumOccurances Val) { |
| 161 | setFlag(Val, OccurancesMask); |
| 162 | } |
| 163 | void setValueExpectedFlag(enum ValueExpected Val) { setFlag(Val, ValueMask); } |
| 164 | void setHiddenFlag(enum OptionHidden Val) { setFlag(Val, HiddenMask); } |
| 165 | void setFormattingFlag(enum FormattingFlags V) { setFlag(V, FormattingMask); } |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 166 | |
| 167 | protected: |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 168 | Option() : NumOccurances(0), Flags(0), |
| 169 | ArgStr(""), HelpStr(""), ValueStr("") {} |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 170 | |
| 171 | public: |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 172 | // addArgument - Tell the system that this Option subclass will handle all |
| 173 | // occurances of -ArgStr on the command line. |
| 174 | // |
| 175 | void addArgument(const char *ArgStr); |
Chris Lattner | ae1257a | 2002-07-23 00:44:37 +0000 | [diff] [blame] | 176 | void removeArgument(const char *ArgStr); |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 177 | |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 178 | // Return the width of the option tag for printing... |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 179 | virtual unsigned getOptionWidth() const = 0; |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 180 | |
| 181 | // printOptionInfo - Print out information about this option. The |
| 182 | // to-be-maintained width is specified. |
| 183 | // |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 184 | virtual void printOptionInfo(unsigned GlobalWidth) const = 0; |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 185 | |
| 186 | // addOccurance - Wrapper around handleOccurance that enforces Flags |
| 187 | // |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 188 | bool addOccurance(const char *ArgName, const std::string &Value); |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 189 | |
| 190 | // Prints option name followed by message. Always returns true. |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 191 | bool error(std::string Message, const char *ArgName = 0); |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 192 | |
| 193 | public: |
| 194 | inline int getNumOccurances() const { return NumOccurances; } |
| 195 | virtual ~Option() {} |
| 196 | }; |
| 197 | |
| 198 | |
| 199 | //===----------------------------------------------------------------------===// |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 200 | // Command line option modifiers that can be used to modify the behavior of |
| 201 | // command line option parsers... |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 202 | // |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 203 | |
| 204 | // desc - Modifier to set the description shown in the --help output... |
| 205 | struct desc { |
| 206 | const char *Desc; |
| 207 | desc(const char *Str) : Desc(Str) {} |
| 208 | void apply(Option &O) const { O.setDescription(Desc); } |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 209 | }; |
| 210 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 211 | // value_desc - Modifier to set the value description shown in the --help |
| 212 | // output... |
| 213 | struct value_desc { |
| 214 | const char *Desc; |
| 215 | value_desc(const char *Str) : Desc(Str) {} |
| 216 | void apply(Option &O) const { O.setValueStr(Desc); } |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 217 | }; |
| 218 | |
| 219 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 220 | // init - Specify a default (initial) value for the command line argument, if |
| 221 | // the default constructor for the argument type does not give you what you |
| 222 | // want. This is only valid on "opt" arguments, not on "list" arguments. |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 223 | // |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 224 | template<class Ty> |
| 225 | struct initializer { |
| 226 | const Ty &Init; |
| 227 | initializer(const Ty &Val) : Init(Val) {} |
| 228 | |
| 229 | template<class Opt> |
| 230 | void apply(Opt &O) const { O.setInitialValue(Init); } |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 231 | }; |
| 232 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 233 | template<class Ty> |
| 234 | initializer<Ty> init(const Ty &Val) { |
| 235 | return initializer<Ty>(Val); |
| 236 | } |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 237 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 238 | |
| 239 | // location - Allow the user to specify which external variable they want to |
| 240 | // store the results of the command line argument processing into, if they don't |
| 241 | // want to store it in the option itself. |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 242 | // |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 243 | template<class Ty> |
| 244 | struct LocationClass { |
| 245 | Ty &Loc; |
| 246 | LocationClass(Ty &L) : Loc(L) {} |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 247 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 248 | template<class Opt> |
| 249 | void apply(Opt &O) const { O.setLocation(O, Loc); } |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 250 | }; |
| 251 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 252 | template<class Ty> |
| 253 | LocationClass<Ty> location(Ty &L) { return LocationClass<Ty>(L); } |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 254 | |
| 255 | |
| 256 | //===----------------------------------------------------------------------===// |
| 257 | // Enum valued command line option |
| 258 | // |
Chris Lattner | ae1257a | 2002-07-23 00:44:37 +0000 | [diff] [blame] | 259 | #define clEnumVal(ENUMVAL, DESC) #ENUMVAL, (int)ENUMVAL, DESC |
| 260 | #define clEnumValN(ENUMVAL, FLAGNAME, DESC) FLAGNAME, (int)ENUMVAL, DESC |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 261 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 262 | // values - For custom data types, allow specifying a group of values together |
| 263 | // as the values that go into the mapping that the option handler uses. Note |
| 264 | // that the values list must always have a 0 at the end of the list to indicate |
| 265 | // that the list has ended. |
| 266 | // |
| 267 | template<class DataType> |
| 268 | class ValuesClass { |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 269 | // Use a vector instead of a map, because the lists should be short, |
| 270 | // the overhead is less, and most importantly, it keeps them in the order |
| 271 | // inserted so we can print our option out nicely. |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 272 | std::vector<std::pair<const char *, std::pair<int, const char *> > > Values; |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 273 | void processValues(va_list Vals); |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 274 | public: |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 275 | ValuesClass(const char *EnumName, DataType Val, const char *Desc, |
| 276 | va_list ValueArgs) { |
| 277 | // Insert the first value, which is required. |
| 278 | Values.push_back(std::make_pair(EnumName, std::make_pair(Val, Desc))); |
| 279 | |
| 280 | // Process the varargs portion of the values... |
| 281 | while (const char *EnumName = va_arg(ValueArgs, const char *)) { |
Chris Lattner | ae1257a | 2002-07-23 00:44:37 +0000 | [diff] [blame] | 282 | DataType EnumVal = (DataType)va_arg(ValueArgs, int); |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 283 | const char *EnumDesc = va_arg(ValueArgs, const char *); |
| 284 | Values.push_back(std::make_pair(EnumName, // Add value to value map |
| 285 | std::make_pair(EnumVal, EnumDesc))); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | template<class Opt> |
| 290 | void apply(Opt &O) const { |
| 291 | for (unsigned i = 0, e = Values.size(); i != e; ++i) |
| 292 | O.getParser().addLiteralOption(Values[i].first, Values[i].second.first, |
| 293 | Values[i].second.second); |
| 294 | } |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 295 | }; |
| 296 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 297 | template<class DataType> |
| 298 | ValuesClass<DataType> values(const char *Arg, DataType Val, const char *Desc, |
| 299 | ...) { |
| 300 | va_list ValueArgs; |
| 301 | va_start(ValueArgs, Desc); |
| 302 | ValuesClass<DataType> Vals(Arg, Val, Desc, ValueArgs); |
| 303 | va_end(ValueArgs); |
| 304 | return Vals; |
| 305 | } |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 306 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 307 | |
| 308 | //===----------------------------------------------------------------------===// |
| 309 | // parser class - Parameterizable parser for different data types. By default, |
| 310 | // known data types (string, int, bool) have specialized parsers, that do what |
| 311 | // you would expect. The default parser, used for data types that are not |
| 312 | // built-in, uses a mapping table to map specific options to values, which is |
| 313 | // used, among other things, to handle enum types. |
| 314 | |
| 315 | //-------------------------------------------------- |
| 316 | // generic_parser_base - This class holds all the non-generic code that we do |
| 317 | // not need replicated for every instance of the generic parser. This also |
| 318 | // allows us to put stuff into CommandLine.cpp |
| 319 | // |
| 320 | struct generic_parser_base { |
| 321 | virtual ~generic_parser_base() {} // Base class should have virtual-dtor |
| 322 | |
| 323 | // getNumOptions - Virtual function implemented by generic subclass to |
| 324 | // indicate how many entries are in Values. |
| 325 | // |
| 326 | virtual unsigned getNumOptions() const = 0; |
| 327 | |
| 328 | // getOption - Return option name N. |
| 329 | virtual const char *getOption(unsigned N) const = 0; |
| 330 | |
| 331 | // getDescription - Return description N |
| 332 | virtual const char *getDescription(unsigned N) const = 0; |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 333 | |
| 334 | // Return the width of the option tag for printing... |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 335 | virtual unsigned getOptionWidth(const Option &O) const; |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 336 | |
| 337 | // printOptionInfo - Print out information about this option. The |
| 338 | // to-be-maintained width is specified. |
| 339 | // |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 340 | virtual void printOptionInfo(const Option &O, unsigned GlobalWidth) const; |
Chris Lattner | 71fb716 | 2002-05-22 17:03:05 +0000 | [diff] [blame] | 341 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 342 | void initialize(Option &O) { |
| 343 | // All of the modifiers for the option have been processed by now, so the |
| 344 | // argstr field should be stable, copy it down now. |
| 345 | // |
| 346 | hasArgStr = O.hasArgStr(); |
| 347 | |
| 348 | // If there has been no argstr specified, that means that we need to add an |
| 349 | // argument for every possible option. This ensures that our options are |
| 350 | // vectored to us. |
| 351 | // |
| 352 | if (!hasArgStr) |
| 353 | for (unsigned i = 0, e = getNumOptions(); i != e; ++i) |
| 354 | O.addArgument(getOption(i)); |
| 355 | } |
| 356 | |
| 357 | enum ValueExpected getValueExpectedFlagDefault() const { |
| 358 | // If there is an ArgStr specified, then we are of the form: |
| 359 | // |
| 360 | // -opt=O2 or -opt O2 or -optO2 |
| 361 | // |
| 362 | // In which case, the value is required. Otherwise if an arg str has not |
| 363 | // been specified, we are of the form: |
| 364 | // |
| 365 | // -O2 or O2 or -la (where -l and -a are seperate options) |
| 366 | // |
| 367 | // If this is the case, we cannot allow a value. |
| 368 | // |
| 369 | if (hasArgStr) |
| 370 | return ValueRequired; |
| 371 | else |
| 372 | return ValueDisallowed; |
| 373 | } |
| 374 | |
Chris Lattner | af7e821 | 2002-07-23 17:15:09 +0000 | [diff] [blame] | 375 | // findOption - Return the option number corresponding to the specified |
| 376 | // argument string. If the option is not found, getNumOptions() is returned. |
| 377 | // |
| 378 | unsigned findOption(const char *Name); |
| 379 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 380 | protected: |
| 381 | bool hasArgStr; |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 382 | }; |
| 383 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 384 | // Default parser implementation - This implementation depends on having a |
| 385 | // mapping of recognized options to values of some sort. In addition to this, |
| 386 | // each entry in the mapping also tracks a help message that is printed with the |
| 387 | // command line option for --help. Because this is a simple mapping parser, the |
| 388 | // data type can be any unsupported type. |
| 389 | // |
| 390 | template <class DataType> |
| 391 | class parser : public generic_parser_base { |
Chris Lattner | af7e821 | 2002-07-23 17:15:09 +0000 | [diff] [blame] | 392 | protected: |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 393 | std::vector<std::pair<const char *, |
| 394 | std::pair<DataType, const char *> > > Values; |
Chris Lattner | af7e821 | 2002-07-23 17:15:09 +0000 | [diff] [blame] | 395 | public: |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 396 | |
| 397 | // Implement virtual functions needed by generic_parser_base |
| 398 | unsigned getNumOptions() const { return Values.size(); } |
| 399 | const char *getOption(unsigned N) const { return Values[N].first; } |
| 400 | const char *getDescription(unsigned N) const { |
| 401 | return Values[N].second.second; |
| 402 | } |
| 403 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 404 | // Default implementation, requires user to populate it with values somehow. |
| 405 | template<class Opt> // parse - Return true on error. |
Chris Lattner | 7f4dd47 | 2002-07-24 22:08:36 +0000 | [diff] [blame^] | 406 | bool parse(Opt &O, const char *ArgName, const std::string &Arg) { |
| 407 | std::string ArgVal; |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 408 | if (hasArgStr) |
| 409 | ArgVal = Arg; |
| 410 | else |
| 411 | ArgVal = ArgName; |
| 412 | |
| 413 | for (unsigned i = 0, e = Values.size(); i != e; ++i) |
| 414 | if (ArgVal == Values[i].first) { |
| 415 | O.addValue(Values[i].second.first); |
| 416 | return false; |
| 417 | } |
| 418 | |
| 419 | return O.error(": Cannot find option named '" + ArgVal + "'!"); |
| 420 | } |
| 421 | |
| 422 | // addLiteralOption - Add an entry to the mapping table... |
| 423 | template <class DT> |
| 424 | void addLiteralOption(const char *Name, const DT &V, const char *HelpStr) { |
Chris Lattner | af7e821 | 2002-07-23 17:15:09 +0000 | [diff] [blame] | 425 | assert(findOption(Name) == Values.size() && "Option already exists!"); |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 426 | Values.push_back(std::make_pair(Name, std::make_pair((DataType)V,HelpStr))); |
| 427 | } |
Chris Lattner | af7e821 | 2002-07-23 17:15:09 +0000 | [diff] [blame] | 428 | |
| 429 | // removeLiteralOption - Remove the specified option. |
| 430 | // |
| 431 | void removeLiteralOption(const char *Name) { |
| 432 | unsigned N = findOption(Name); |
| 433 | assert(N != Values.size() && "Option not found!"); |
| 434 | Values.erase(Values.begin()+N); |
| 435 | } |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 436 | }; |
| 437 | |
| 438 | |
| 439 | //-------------------------------------------------- |
| 440 | // parser<bool> |
| 441 | // |
| 442 | template<> |
| 443 | class parser<bool> { |
Chris Lattner | 7f4dd47 | 2002-07-24 22:08:36 +0000 | [diff] [blame^] | 444 | static bool parseImpl(Option &O, const std::string &Arg, bool &Val); |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 445 | public: |
| 446 | |
| 447 | template<class Opt> // parse - Return true on error. |
Chris Lattner | 7f4dd47 | 2002-07-24 22:08:36 +0000 | [diff] [blame^] | 448 | bool parse(Opt &O, const char *ArgName, const std::string &Arg) { |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 449 | bool Val; |
| 450 | bool Error = parseImpl(O, Arg, Val); |
| 451 | if (!Error) O.addValue(Val); |
| 452 | return Error; |
| 453 | } |
| 454 | |
| 455 | enum ValueExpected getValueExpectedFlagDefault() const { |
| 456 | return ValueOptional; |
| 457 | } |
| 458 | |
| 459 | void initialize(Option &O) {} |
| 460 | |
| 461 | // Return the width of the option tag for printing... |
| 462 | virtual unsigned getOptionWidth(const Option &O) const; |
| 463 | |
| 464 | // printOptionInfo - Print out information about this option. The |
| 465 | // to-be-maintained width is specified. |
| 466 | // |
| 467 | virtual void printOptionInfo(const Option &O, unsigned GlobalWidth) const; |
| 468 | }; |
| 469 | |
| 470 | |
| 471 | //-------------------------------------------------- |
| 472 | // parser<int> |
| 473 | // |
| 474 | template<> |
| 475 | class parser<int> { |
Chris Lattner | 7f4dd47 | 2002-07-24 22:08:36 +0000 | [diff] [blame^] | 476 | static bool parseImpl(Option &O, const std::string &Arg, int &Val); |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 477 | public: |
| 478 | |
| 479 | // parse - Return true on error. |
| 480 | template<class Opt> |
Chris Lattner | 7f4dd47 | 2002-07-24 22:08:36 +0000 | [diff] [blame^] | 481 | bool parse(Opt &O, const char *ArgName, const std::string &Arg) { |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 482 | int Val; |
| 483 | bool Error = parseImpl(O, Arg, Val); |
| 484 | if (!Error) O.addValue(Val); |
| 485 | return Error; |
| 486 | } |
| 487 | |
| 488 | enum ValueExpected getValueExpectedFlagDefault() const { |
| 489 | return ValueRequired; |
| 490 | } |
| 491 | |
| 492 | void initialize(Option &O) {} |
| 493 | |
| 494 | // Return the width of the option tag for printing... |
| 495 | virtual unsigned getOptionWidth(const Option &O) const; |
| 496 | |
| 497 | // printOptionInfo - Print out information about this option. The |
| 498 | // to-be-maintained width is specified. |
| 499 | // |
| 500 | virtual void printOptionInfo(const Option &O, unsigned GlobalWidth) const; |
| 501 | }; |
| 502 | |
| 503 | |
| 504 | //-------------------------------------------------- |
| 505 | // parser<double> |
| 506 | // |
| 507 | template<> |
| 508 | class parser<double> { |
Chris Lattner | 7f4dd47 | 2002-07-24 22:08:36 +0000 | [diff] [blame^] | 509 | static bool parseImpl(Option &O, const std::string &Arg, double &Val); |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 510 | public: |
| 511 | |
| 512 | // parse - Return true on error. |
| 513 | template<class Opt> |
Chris Lattner | 7f4dd47 | 2002-07-24 22:08:36 +0000 | [diff] [blame^] | 514 | bool parse(Opt &O, const char *ArgName, const std::string &Arg) { |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 515 | double Val; |
| 516 | bool Error = parseImpl(O, Arg, Val); |
| 517 | if (!Error) O.addValue(Val); |
| 518 | return Error; |
| 519 | } |
| 520 | |
| 521 | enum ValueExpected getValueExpectedFlagDefault() const { |
| 522 | return ValueRequired; |
| 523 | } |
| 524 | |
| 525 | void initialize(Option &O) {} |
| 526 | |
| 527 | // Return the width of the option tag for printing... |
| 528 | virtual unsigned getOptionWidth(const Option &O) const; |
| 529 | |
| 530 | // printOptionInfo - Print out information about this option. The |
| 531 | // to-be-maintained width is specified. |
| 532 | // |
| 533 | virtual void printOptionInfo(const Option &O, unsigned GlobalWidth) const; |
| 534 | }; |
| 535 | |
| 536 | // Parser<float> is the same as parser<double> |
| 537 | template<> struct parser<float> : public parser<double> {}; |
| 538 | |
| 539 | |
| 540 | |
| 541 | //-------------------------------------------------- |
Chris Lattner | 7f4dd47 | 2002-07-24 22:08:36 +0000 | [diff] [blame^] | 542 | // parser<std::string> |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 543 | // |
| 544 | template<> |
Chris Lattner | 7f4dd47 | 2002-07-24 22:08:36 +0000 | [diff] [blame^] | 545 | struct parser<std::string> { |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 546 | // parse - Return true on error. |
| 547 | template<class Opt> |
Chris Lattner | 7f4dd47 | 2002-07-24 22:08:36 +0000 | [diff] [blame^] | 548 | bool parse(Opt &O, const char *ArgName, const std::string &Arg) { |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 549 | O.addValue(Arg); |
| 550 | return false; |
| 551 | } |
| 552 | enum ValueExpected getValueExpectedFlagDefault() const { |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 553 | return ValueRequired; |
| 554 | } |
Chris Lattner | 71fb716 | 2002-05-22 17:03:05 +0000 | [diff] [blame] | 555 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 556 | void initialize(Option &O) {} |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 557 | |
| 558 | // Return the width of the option tag for printing... |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 559 | virtual unsigned getOptionWidth(const Option &O) const; |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 560 | |
| 561 | // printOptionInfo - Print out information about this option. The |
| 562 | // to-be-maintained width is specified. |
| 563 | // |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 564 | virtual void printOptionInfo(const Option &O, unsigned GlobalWidth) const; |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 565 | }; |
| 566 | |
Chris Lattner | 71fb716 | 2002-05-22 17:03:05 +0000 | [diff] [blame] | 567 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 568 | |
| 569 | //===----------------------------------------------------------------------===// |
| 570 | // applicator class - This class is used because we must use partial |
| 571 | // specialization to handle literal string arguments specially (const char* does |
| 572 | // not correctly respond to the apply method). Because the syntax to use this |
| 573 | // is a pain, we have the 'apply' method below to handle the nastiness... |
| 574 | // |
| 575 | template<class Mod> struct applicator { |
| 576 | template<class Opt> |
| 577 | static void opt(const Mod &M, Opt &O) { M.apply(O); } |
| 578 | }; |
| 579 | |
| 580 | // Handle const char* as a special case... |
| 581 | template<unsigned n> struct applicator<char[n]> { |
| 582 | template<class Opt> |
| 583 | static void opt(const char *Str, Opt &O) { O.setArgStr(Str); } |
| 584 | }; |
| 585 | template<> struct applicator<const char*> { |
| 586 | template<class Opt> |
| 587 | static void opt(const char *Str, Opt &O) { O.setArgStr(Str); } |
| 588 | }; |
| 589 | |
| 590 | template<> struct applicator<NumOccurances> { |
| 591 | static void opt(NumOccurances NO, Option &O) { O.setNumOccurancesFlag(NO); } |
| 592 | }; |
| 593 | template<> struct applicator<ValueExpected> { |
| 594 | static void opt(ValueExpected VE, Option &O) { O.setValueExpectedFlag(VE); } |
| 595 | }; |
| 596 | template<> struct applicator<OptionHidden> { |
| 597 | static void opt(OptionHidden OH, Option &O) { O.setHiddenFlag(OH); } |
| 598 | }; |
| 599 | template<> struct applicator<FormattingFlags> { |
| 600 | static void opt(FormattingFlags FF, Option &O) { O.setFormattingFlag(FF); } |
| 601 | }; |
| 602 | |
| 603 | // apply method - Apply a modifier to an option in a type safe way. |
| 604 | template<class Mod, class Opt> |
| 605 | void apply(const Mod &M, Opt *O) { |
| 606 | applicator<Mod>::opt(M, *O); |
| 607 | } |
| 608 | |
| 609 | |
| 610 | //===----------------------------------------------------------------------===// |
| 611 | // opt_storage class |
| 612 | |
| 613 | // Default storage class definition: external storage. This implementation |
| 614 | // assumes the user will specify a variable to store the data into with the |
| 615 | // cl::location(x) modifier. |
| 616 | // |
| 617 | template<class DataType, bool ExternalStorage, bool isClass> |
| 618 | class opt_storage { |
| 619 | DataType *Location; // Where to store the object... |
| 620 | |
| 621 | void check() { |
| 622 | assert(Location != 0 && "cl::location(...) not specified for a command " |
| 623 | "line option with external storage!"); |
| 624 | } |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 625 | public: |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 626 | opt_storage() : Location(0) {} |
| 627 | |
| 628 | bool setLocation(Option &O, DataType &L) { |
| 629 | if (Location) |
| 630 | return O.error(": cl::location(x) specified more than once!"); |
| 631 | Location = &L; |
| 632 | return false; |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 633 | } |
| 634 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 635 | template<class T> |
| 636 | void setValue(const T &V) { |
| 637 | check(); |
| 638 | *Location = V; |
| 639 | } |
| 640 | |
| 641 | DataType &getValue() { check(); return *Location; } |
| 642 | const DataType &getValue() const { check(); return *Location; } |
| 643 | }; |
| 644 | |
| 645 | |
| 646 | // Define how to hold a class type object, such as a string. Since we can |
| 647 | // inherit from a class, we do so. This makes us exactly compatible with the |
| 648 | // object in all cases that it is used. |
| 649 | // |
| 650 | template<class DataType> |
| 651 | struct opt_storage<DataType,false,true> : public DataType { |
| 652 | |
| 653 | template<class T> |
| 654 | void setValue(const T &V) { DataType::operator=(V); } |
| 655 | |
| 656 | DataType &getValue() { return *this; } |
| 657 | const DataType &getValue() const { return *this; } |
| 658 | }; |
| 659 | |
| 660 | // Define a partial specialization to handle things we cannot inherit from. In |
| 661 | // this case, we store an instance through containment, and overload operators |
| 662 | // to get at the value. |
| 663 | // |
| 664 | template<class DataType> |
| 665 | struct opt_storage<DataType, false, false> { |
| 666 | DataType Value; |
| 667 | |
| 668 | // Make sure we initialize the value with the default constructor for the |
| 669 | // type. |
| 670 | opt_storage() : Value(DataType()) {} |
| 671 | |
| 672 | template<class T> |
| 673 | void setValue(const T &V) { Value = V; } |
| 674 | DataType &getValue() { return Value; } |
| 675 | DataType getValue() const { return Value; } |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 676 | }; |
| 677 | |
| 678 | |
| 679 | //===----------------------------------------------------------------------===// |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 680 | // opt - A scalar command line option. |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 681 | // |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 682 | template <class DataType, bool ExternalStorage = false, |
| 683 | class ParserClass = parser<DataType> > |
| 684 | class opt : public Option, |
| 685 | public opt_storage<DataType, ExternalStorage, |
| 686 | ::boost::is_class<DataType>::value>{ |
| 687 | ParserClass Parser; |
| 688 | |
| 689 | virtual bool handleOccurance(const char *ArgName, const std::string &Arg) { |
| 690 | return Parser.parse(*this, ArgName, Arg); |
| 691 | } |
| 692 | |
| 693 | virtual enum ValueExpected getValueExpectedFlagDefault() const { |
| 694 | return Parser.getValueExpectedFlagDefault(); |
| 695 | } |
| 696 | |
| 697 | // Forward printing stuff to the parser... |
| 698 | virtual unsigned getOptionWidth() const {return Parser.getOptionWidth(*this);} |
| 699 | virtual void printOptionInfo(unsigned GlobalWidth) const { |
| 700 | Parser.printOptionInfo(*this, GlobalWidth); |
| 701 | } |
| 702 | |
| 703 | void done() { |
| 704 | addArgument(ArgStr); |
| 705 | Parser.initialize(*this); |
| 706 | } |
| 707 | public: |
| 708 | // setInitialValue - Used by the cl::init modifier... |
| 709 | void setInitialValue(const DataType &V) { setValue(V); } |
| 710 | |
| 711 | // addValue - Used by the parser to add a value to the option |
| 712 | template<class T> |
| 713 | void addValue(const T &V) { setValue(V); } |
| 714 | 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 | // |
| 791 | template<class DataType, class StorageClass> |
| 792 | class list_storage { |
| 793 | StorageClass *Location; // Where to store the object... |
| 794 | |
| 795 | public: |
| 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 | // |
| 818 | template<class DataType> |
| 819 | struct 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 | // |
| 829 | template <class DataType, class Storage = bool, |
| 830 | class ParserClass = parser<DataType> > |
| 831 | class list : public Option, public list_storage<DataType, Storage> { |
| 832 | ParserClass Parser; |
| 833 | |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 834 | virtual enum NumOccurances getNumOccurancesFlagDefault() const { |
| 835 | return ZeroOrMore; |
| 836 | } |
| 837 | virtual enum ValueExpected getValueExpectedFlagDefault() const { |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 838 | return Parser.getValueExpectedFlagDefault(); |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 839 | } |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 840 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 841 | virtual bool handleOccurance(const char *ArgName, const std::string &Arg) { |
| 842 | return Parser.parse(*this, ArgName, Arg); |
| 843 | } |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 844 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 845 | // Forward printing stuff to the parser... |
| 846 | virtual unsigned getOptionWidth() const {return Parser.getOptionWidth(*this);} |
| 847 | virtual void printOptionInfo(unsigned GlobalWidth) const { |
| 848 | Parser.printOptionInfo(*this, GlobalWidth); |
| 849 | } |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 850 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 851 | void done() { |
| 852 | addArgument(ArgStr); |
| 853 | Parser.initialize(*this); |
| 854 | } |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 855 | public: |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 856 | ParserClass &getParser() { return Parser; } |
| 857 | |
| 858 | // One option... |
| 859 | template<class M0t> |
| 860 | list(const M0t &M0) { |
| 861 | apply(M0, this); |
| 862 | done(); |
| 863 | } |
| 864 | // Two options... |
| 865 | template<class M0t, class M1t> |
| 866 | list(const M0t &M0, const M1t &M1) { |
| 867 | apply(M0, this); apply(M1, this); |
| 868 | done(); |
| 869 | } |
| 870 | // Three options... |
| 871 | template<class M0t, class M1t, class M2t> |
| 872 | list(const M0t &M0, const M1t &M1, const M2t &M2) { |
| 873 | apply(M0, this); apply(M1, this); apply(M2, this); |
| 874 | done(); |
| 875 | } |
| 876 | // Four options... |
| 877 | template<class M0t, class M1t, class M2t, class M3t> |
| 878 | list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3) { |
| 879 | apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); |
| 880 | done(); |
| 881 | } |
| 882 | // Five options... |
| 883 | template<class M0t, class M1t, class M2t, class M3t, class M4t> |
| 884 | list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, |
| 885 | const M4t &M4) { |
| 886 | apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); |
| 887 | apply(M4, this); |
| 888 | done(); |
| 889 | } |
| 890 | // Six options... |
| 891 | template<class M0t, class M1t, class M2t, class M3t, |
| 892 | class M4t, class M5t> |
| 893 | list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, |
| 894 | const M4t &M4, const M5t &M5) { |
| 895 | apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); |
| 896 | apply(M4, this); apply(M5, this); |
| 897 | done(); |
| 898 | } |
| 899 | // Seven options... |
| 900 | template<class M0t, class M1t, class M2t, class M3t, |
| 901 | class M4t, class M5t, class M6t> |
| 902 | list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, |
| 903 | const M4t &M4, const M5t &M5, const M6t &M6) { |
| 904 | apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); |
| 905 | apply(M4, this); apply(M5, this); apply(M6, this); |
| 906 | done(); |
| 907 | } |
| 908 | // Eight options... |
| 909 | template<class M0t, class M1t, class M2t, class M3t, |
| 910 | class M4t, class M5t, class M6t, class M7t> |
| 911 | list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, |
| 912 | const M4t &M4, const M5t &M5, const M6t &M6, const M7t &M7) { |
| 913 | apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); |
| 914 | apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this); |
| 915 | done(); |
| 916 | } |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 917 | }; |
| 918 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 919 | |
| 920 | |
| 921 | //===----------------------------------------------------------------------===// |
| 922 | // Aliased command line option (alias this name to a preexisting name) |
| 923 | // |
| 924 | |
| 925 | class alias : public Option { |
| 926 | Option *AliasFor; |
| 927 | virtual bool handleOccurance(const char *ArgName, const std::string &Arg) { |
| 928 | return AliasFor->handleOccurance(AliasFor->ArgStr, Arg); |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 929 | } |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 930 | // Aliases default to be hidden... |
| 931 | virtual enum OptionHidden getOptionHiddenFlagDefault() const {return Hidden;} |
| 932 | |
| 933 | // Handle printing stuff... |
| 934 | virtual unsigned getOptionWidth() const; |
| 935 | virtual void printOptionInfo(unsigned GlobalWidth) const; |
| 936 | |
| 937 | void done() { |
| 938 | if (!hasArgStr()) |
| 939 | error(": cl::alias must have argument name specified!"); |
| 940 | if (AliasFor == 0) |
| 941 | error(": cl::alias must have an cl::aliasopt(option) specified!"); |
| 942 | addArgument(ArgStr); |
| 943 | } |
| 944 | public: |
| 945 | void setAliasFor(Option &O) { |
| 946 | if (AliasFor) |
| 947 | error(": cl::alias must only have one cl::aliasopt(...) specified!"); |
| 948 | AliasFor = &O; |
| 949 | } |
| 950 | |
| 951 | // One option... |
| 952 | template<class M0t> |
| 953 | alias(const M0t &M0) : AliasFor(0) { |
| 954 | apply(M0, this); |
| 955 | done(); |
| 956 | } |
| 957 | // Two options... |
| 958 | template<class M0t, class M1t> |
| 959 | alias(const M0t &M0, const M1t &M1) : AliasFor(0) { |
| 960 | apply(M0, this); apply(M1, this); |
| 961 | done(); |
| 962 | } |
| 963 | // Three options... |
| 964 | template<class M0t, class M1t, class M2t> |
| 965 | alias(const M0t &M0, const M1t &M1, const M2t &M2) : AliasFor(0) { |
| 966 | apply(M0, this); apply(M1, this); apply(M2, this); |
| 967 | done(); |
| 968 | } |
| 969 | // Four options... |
| 970 | template<class M0t, class M1t, class M2t, class M3t> |
| 971 | alias(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3) |
| 972 | : AliasFor(0) { |
| 973 | apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); |
| 974 | done(); |
| 975 | } |
| 976 | }; |
| 977 | |
| 978 | // aliasfor - Modifier to set the option an alias aliases. |
| 979 | struct aliasopt { |
| 980 | Option &Opt; |
| 981 | aliasopt(Option &O) : Opt(O) {} |
| 982 | void apply(alias &A) const { A.setAliasFor(Opt); } |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 983 | }; |
| 984 | |
| 985 | } // End namespace cl |
| 986 | |
| 987 | #endif |