LLVM API Documentation
00001 //===- llvm/Support/CommandLine.h - Command line handler --------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This class implements a command line argument processor that is useful when 00011 // creating a tool. It provides a simple, minimalistic interface that is easily 00012 // extensible and supports nonlocal (library) command line options. 00013 // 00014 // Note that rather than trying to figure out what this code does, you should 00015 // read the library documentation located in docs/CommandLine.html or looks at 00016 // the many example usages in tools/*/*.cpp 00017 // 00018 //===----------------------------------------------------------------------===// 00019 00020 #ifndef LLVM_SUPPORT_COMMANDLINE_H 00021 #define LLVM_SUPPORT_COMMANDLINE_H 00022 00023 #include "llvm/Support/type_traits.h" 00024 #include "llvm/Support/DataTypes.h" 00025 #include "llvm/Support/Compiler.h" 00026 #include "llvm/ADT/SmallVector.h" 00027 #include <cassert> 00028 #include <climits> 00029 #include <cstdarg> 00030 #include <string> 00031 #include <utility> 00032 #include <vector> 00033 00034 namespace llvm { 00035 00036 /// cl Namespace - This namespace contains all of the command line option 00037 /// processing machinery. It is intentionally a short name to make qualified 00038 /// usage concise. 00039 namespace cl { 00040 00041 //===----------------------------------------------------------------------===// 00042 // ParseCommandLineOptions - Command line option processing entry point. 00043 // 00044 void ParseCommandLineOptions(int argc, char **argv, 00045 const char *Overview = 0, 00046 bool ReadResponseFiles = false); 00047 00048 //===----------------------------------------------------------------------===// 00049 // ParseEnvironmentOptions - Environment variable option processing alternate 00050 // entry point. 00051 // 00052 void ParseEnvironmentOptions(const char *progName, const char *envvar, 00053 const char *Overview = 0, 00054 bool ReadResponseFiles = false); 00055 00056 ///===---------------------------------------------------------------------===// 00057 /// SetVersionPrinter - Override the default (LLVM specific) version printer 00058 /// used to print out the version when --version is given 00059 /// on the command line. This allows other systems using the 00060 /// CommandLine utilities to print their own version string. 00061 void SetVersionPrinter(void (*func)()); 00062 00063 00064 // MarkOptionsChanged - Internal helper function. 00065 void MarkOptionsChanged(); 00066 00067 //===----------------------------------------------------------------------===// 00068 // Flags permitted to be passed to command line arguments 00069 // 00070 00071 enum NumOccurrences { // Flags for the number of occurrences allowed 00072 Optional = 0x01, // Zero or One occurrence 00073 ZeroOrMore = 0x02, // Zero or more occurrences allowed 00074 Required = 0x03, // One occurrence required 00075 OneOrMore = 0x04, // One or more occurrences required 00076 00077 // ConsumeAfter - Indicates that this option is fed anything that follows the 00078 // last positional argument required by the application (it is an error if 00079 // there are zero positional arguments, and a ConsumeAfter option is used). 00080 // Thus, for example, all arguments to LLI are processed until a filename is 00081 // found. Once a filename is found, all of the succeeding arguments are 00082 // passed, unprocessed, to the ConsumeAfter option. 00083 // 00084 ConsumeAfter = 0x05, 00085 00086 OccurrencesMask = 0x07 00087 }; 00088 00089 enum ValueExpected { // Is a value required for the option? 00090 ValueOptional = 0x08, // The value can appear... or not 00091 ValueRequired = 0x10, // The value is required to appear! 00092 ValueDisallowed = 0x18, // A value may not be specified (for flags) 00093 ValueMask = 0x18 00094 }; 00095 00096 enum OptionHidden { // Control whether -help shows this option 00097 NotHidden = 0x20, // Option included in --help & --help-hidden 00098 Hidden = 0x40, // -help doesn't, but --help-hidden does 00099 ReallyHidden = 0x60, // Neither --help nor --help-hidden show this arg 00100 HiddenMask = 0x60 00101 }; 00102 00103 // Formatting flags - This controls special features that the option might have 00104 // that cause it to be parsed differently... 00105 // 00106 // Prefix - This option allows arguments that are otherwise unrecognized to be 00107 // matched by options that are a prefix of the actual value. This is useful for 00108 // cases like a linker, where options are typically of the form '-lfoo' or 00109 // '-L../../include' where -l or -L are the actual flags. When prefix is 00110 // enabled, and used, the value for the flag comes from the suffix of the 00111 // argument. 00112 // 00113 // Grouping - With this option enabled, multiple letter options are allowed to 00114 // bunch together with only a single hyphen for the whole group. This allows 00115 // emulation of the behavior that ls uses for example: ls -la === ls -l -a 00116 // 00117 00118 enum FormattingFlags { 00119 NormalFormatting = 0x000, // Nothing special 00120 Positional = 0x080, // Is a positional argument, no '-' required 00121 Prefix = 0x100, // Can this option directly prefix its value? 00122 Grouping = 0x180, // Can this option group with other options? 00123 FormattingMask = 0x180 // Union of the above flags. 00124 }; 00125 00126 enum MiscFlags { // Miscellaneous flags to adjust argument 00127 CommaSeparated = 0x200, // Should this cl::list split between commas? 00128 PositionalEatsArgs = 0x400, // Should this positional cl::list eat -args? 00129 Sink = 0x800, // Should this cl::list eat all unknown options? 00130 MiscMask = 0xE00 // Union of the above flags. 00131 }; 00132 00133 00134 00135 //===----------------------------------------------------------------------===// 00136 // Option Base class 00137 // 00138 class alias; 00139 class Option { 00140 friend class alias; 00141 00142 // handleOccurrences - Overriden by subclasses to handle the value passed into 00143 // an argument. Should return true if there was an error processing the 00144 // argument and the program should exit. 00145 // 00146 virtual bool handleOccurrence(unsigned pos, const char *ArgName, 00147 const std::string &Arg) = 0; 00148 00149 virtual enum ValueExpected getValueExpectedFlagDefault() const { 00150 return ValueOptional; 00151 } 00152 00153 // Out of line virtual function to provide home for the class. 00154 virtual void anchor(); 00155 00156 int NumOccurrences; // The number of times specified 00157 int Flags; // Flags for the argument 00158 unsigned Position; // Position of last occurrence of the option 00159 unsigned AdditionalVals;// Greater than 0 for multi-valued option. 00160 Option *NextRegistered; // Singly linked list of registered options. 00161 public: 00162 const char *ArgStr; // The argument string itself (ex: "help", "o") 00163 const char *HelpStr; // The descriptive text message for --help 00164 const char *ValueStr; // String describing what the value of this option is 00165 00166 inline enum NumOccurrences getNumOccurrencesFlag() const { 00167 return static_cast<enum NumOccurrences>(Flags & OccurrencesMask); 00168 } 00169 inline enum ValueExpected getValueExpectedFlag() const { 00170 int VE = Flags & ValueMask; 00171 return VE ? static_cast<enum ValueExpected>(VE) 00172 : getValueExpectedFlagDefault(); 00173 } 00174 inline enum OptionHidden getOptionHiddenFlag() const { 00175 return static_cast<enum OptionHidden>(Flags & HiddenMask); 00176 } 00177 inline enum FormattingFlags getFormattingFlag() const { 00178 return static_cast<enum FormattingFlags>(Flags & FormattingMask); 00179 } 00180 inline unsigned getMiscFlags() const { 00181 return Flags & MiscMask; 00182 } 00183 inline unsigned getPosition() const { return Position; } 00184 inline unsigned getNumAdditionalVals() const { return AdditionalVals; } 00185 00186 // hasArgStr - Return true if the argstr != "" 00187 bool hasArgStr() const { return ArgStr[0] != 0; } 00188 00189 //-------------------------------------------------------------------------=== 00190 // Accessor functions set by OptionModifiers 00191 // 00192 void setArgStr(const char *S) { ArgStr = S; } 00193 void setDescription(const char *S) { HelpStr = S; } 00194 void setValueStr(const char *S) { ValueStr = S; } 00195 00196 void setFlag(unsigned Flag, unsigned FlagMask) { 00197 Flags &= ~FlagMask; 00198 Flags |= Flag; 00199 } 00200 00201 void setNumOccurrencesFlag(enum NumOccurrences Val) { 00202 setFlag(Val, OccurrencesMask); 00203 } 00204 void setValueExpectedFlag(enum ValueExpected Val) { setFlag(Val, ValueMask); } 00205 void setHiddenFlag(enum OptionHidden Val) { setFlag(Val, HiddenMask); } 00206 void setFormattingFlag(enum FormattingFlags V) { setFlag(V, FormattingMask); } 00207 void setMiscFlag(enum MiscFlags M) { setFlag(M, M); } 00208 void setPosition(unsigned pos) { Position = pos; } 00209 protected: 00210 explicit Option(unsigned DefaultFlags) 00211 : NumOccurrences(0), Flags(DefaultFlags | NormalFormatting), Position(0), 00212 AdditionalVals(0), NextRegistered(0), 00213 ArgStr(""), HelpStr(""), ValueStr("") { 00214 assert(getNumOccurrencesFlag() != 0 && 00215 getOptionHiddenFlag() != 0 && "Not all default flags specified!"); 00216 } 00217 00218 inline void setNumAdditionalVals(unsigned n) 00219 { AdditionalVals = n; } 00220 public: 00221 // addArgument - Register this argument with the commandline system. 00222 // 00223 void addArgument(); 00224 00225 Option *getNextRegisteredOption() const { return NextRegistered; } 00226 00227 // Return the width of the option tag for printing... 00228 virtual size_t getOptionWidth() const = 0; 00229 00230 // printOptionInfo - Print out information about this option. The 00231 // to-be-maintained width is specified. 00232 // 00233 virtual void printOptionInfo(size_t GlobalWidth) const = 0; 00234 00235 virtual void getExtraOptionNames(std::vector<const char*> &) {} 00236 00237 // addOccurrence - Wrapper around handleOccurrence that enforces Flags 00238 // 00239 bool addOccurrence(unsigned pos, const char *ArgName, 00240 const std::string &Value, bool MultiArg = false); 00241 00242 // Prints option name followed by message. Always returns true. 00243 bool error(std::string Message, const char *ArgName = 0); 00244 00245 public: 00246 inline int getNumOccurrences() const { return NumOccurrences; } 00247 virtual ~Option() {} 00248 }; 00249 00250 00251 //===----------------------------------------------------------------------===// 00252 // Command line option modifiers that can be used to modify the behavior of 00253 // command line option parsers... 00254 // 00255 00256 // desc - Modifier to set the description shown in the --help output... 00257 struct desc { 00258 const char *Desc; 00259 desc(const char *Str) : Desc(Str) {} 00260 void apply(Option &O) const { O.setDescription(Desc); } 00261 }; 00262 00263 // value_desc - Modifier to set the value description shown in the --help 00264 // output... 00265 struct value_desc { 00266 const char *Desc; 00267 value_desc(const char *Str) : Desc(Str) {} 00268 void apply(Option &O) const { O.setValueStr(Desc); } 00269 }; 00270 00271 // init - Specify a default (initial) value for the command line argument, if 00272 // the default constructor for the argument type does not give you what you 00273 // want. This is only valid on "opt" arguments, not on "list" arguments. 00274 // 00275 template<class Ty> 00276 struct initializer { 00277 const Ty &Init; 00278 initializer(const Ty &Val) : Init(Val) {} 00279 00280 template<class Opt> 00281 void apply(Opt &O) const { O.setInitialValue(Init); } 00282 }; 00283 00284 template<class Ty> 00285 initializer<Ty> init(const Ty &Val) { 00286 return initializer<Ty>(Val); 00287 } 00288 00289 00290 // location - Allow the user to specify which external variable they want to 00291 // store the results of the command line argument processing into, if they don't 00292 // want to store it in the option itself. 00293 // 00294 template<class Ty> 00295 struct LocationClass { 00296 Ty &Loc; 00297 LocationClass(Ty &L) : Loc(L) {} 00298 00299 template<class Opt> 00300 void apply(Opt &O) const { O.setLocation(O, Loc); } 00301 }; 00302 00303 template<class Ty> 00304 LocationClass<Ty> location(Ty &L) { return LocationClass<Ty>(L); } 00305 00306 00307 //===----------------------------------------------------------------------===// 00308 // Enum valued command line option 00309 // 00310 #define clEnumVal(ENUMVAL, DESC) #ENUMVAL, int(ENUMVAL), DESC 00311 #define clEnumValN(ENUMVAL, FLAGNAME, DESC) FLAGNAME, int(ENUMVAL), DESC 00312 #define clEnumValEnd (reinterpret_cast<void*>(0)) 00313 00314 // values - For custom data types, allow specifying a group of values together 00315 // as the values that go into the mapping that the option handler uses. Note 00316 // that the values list must always have a 0 at the end of the list to indicate 00317 // that the list has ended. 00318 // 00319 template<class DataType> 00320 class ValuesClass { 00321 // Use a vector instead of a map, because the lists should be short, 00322 // the overhead is less, and most importantly, it keeps them in the order 00323 // inserted so we can print our option out nicely. 00324 SmallVector<std::pair<const char *, std::pair<int, const char *> >,4> Values; 00325 void processValues(va_list Vals); 00326 public: 00327 ValuesClass(const char *EnumName, DataType Val, const char *Desc, 00328 va_list ValueArgs) { 00329 // Insert the first value, which is required. 00330 Values.push_back(std::make_pair(EnumName, std::make_pair(Val, Desc))); 00331 00332 // Process the varargs portion of the values... 00333 while (const char *enumName = va_arg(ValueArgs, const char *)) { 00334 DataType EnumVal = static_cast<DataType>(va_arg(ValueArgs, int)); 00335 const char *EnumDesc = va_arg(ValueArgs, const char *); 00336 Values.push_back(std::make_pair(enumName, // Add value to value map 00337 std::make_pair(EnumVal, EnumDesc))); 00338 } 00339 } 00340 00341 template<class Opt> 00342 void apply(Opt &O) const { 00343 for (unsigned i = 0, e = static_cast<unsigned>(Values.size()); 00344 i != e; ++i) 00345 O.getParser().addLiteralOption(Values[i].first, Values[i].second.first, 00346 Values[i].second.second); 00347 } 00348 }; 00349 00350 template<class DataType> 00351 ValuesClass<DataType> END_WITH_NULL values(const char *Arg, DataType Val, 00352 const char *Desc, ...) { 00353 va_list ValueArgs; 00354 va_start(ValueArgs, Desc); 00355 ValuesClass<DataType> Vals(Arg, Val, Desc, ValueArgs); 00356 va_end(ValueArgs); 00357 return Vals; 00358 } 00359 00360 00361 //===----------------------------------------------------------------------===// 00362 // parser class - Parameterizable parser for different data types. By default, 00363 // known data types (string, int, bool) have specialized parsers, that do what 00364 // you would expect. The default parser, used for data types that are not 00365 // built-in, uses a mapping table to map specific options to values, which is 00366 // used, among other things, to handle enum types. 00367 00368 //-------------------------------------------------- 00369 // generic_parser_base - This class holds all the non-generic code that we do 00370 // not need replicated for every instance of the generic parser. This also 00371 // allows us to put stuff into CommandLine.cpp 00372 // 00373 struct generic_parser_base { 00374 virtual ~generic_parser_base() {} // Base class should have virtual-dtor 00375 00376 // getNumOptions - Virtual function implemented by generic subclass to 00377 // indicate how many entries are in Values. 00378 // 00379 virtual unsigned getNumOptions() const = 0; 00380 00381 // getOption - Return option name N. 00382 virtual const char *getOption(unsigned N) const = 0; 00383 00384 // getDescription - Return description N 00385 virtual const char *getDescription(unsigned N) const = 0; 00386 00387 // Return the width of the option tag for printing... 00388 virtual size_t getOptionWidth(const Option &O) const; 00389 00390 // printOptionInfo - Print out information about this option. The 00391 // to-be-maintained width is specified. 00392 // 00393 virtual void printOptionInfo(const Option &O, size_t GlobalWidth) const; 00394 00395 void initialize(Option &O) { 00396 // All of the modifiers for the option have been processed by now, so the 00397 // argstr field should be stable, copy it down now. 00398 // 00399 hasArgStr = O.hasArgStr(); 00400 } 00401 00402 void getExtraOptionNames(std::vector<const char*> &OptionNames) { 00403 // If there has been no argstr specified, that means that we need to add an 00404 // argument for every possible option. This ensures that our options are 00405 // vectored to us. 00406 if (!hasArgStr) 00407 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) 00408 OptionNames.push_back(getOption(i)); 00409 } 00410 00411 00412 enum ValueExpected getValueExpectedFlagDefault() const { 00413 // If there is an ArgStr specified, then we are of the form: 00414 // 00415 // -opt=O2 or -opt O2 or -optO2 00416 // 00417 // In which case, the value is required. Otherwise if an arg str has not 00418 // been specified, we are of the form: 00419 // 00420 // -O2 or O2 or -la (where -l and -a are separate options) 00421 // 00422 // If this is the case, we cannot allow a value. 00423 // 00424 if (hasArgStr) 00425 return ValueRequired; 00426 else 00427 return ValueDisallowed; 00428 } 00429 00430 // findOption - Return the option number corresponding to the specified 00431 // argument string. If the option is not found, getNumOptions() is returned. 00432 // 00433 unsigned findOption(const char *Name); 00434 00435 protected: 00436 bool hasArgStr; 00437 }; 00438 00439 // Default parser implementation - This implementation depends on having a 00440 // mapping of recognized options to values of some sort. In addition to this, 00441 // each entry in the mapping also tracks a help message that is printed with the 00442 // command line option for --help. Because this is a simple mapping parser, the 00443 // data type can be any unsupported type. 00444 // 00445 template <class DataType> 00446 class parser : public generic_parser_base { 00447 protected: 00448 SmallVector<std::pair<const char *, 00449 std::pair<DataType, const char *> >, 8> Values; 00450 public: 00451 typedef DataType parser_data_type; 00452 00453 // Implement virtual functions needed by generic_parser_base 00454 unsigned getNumOptions() const { return unsigned(Values.size()); } 00455 const char *getOption(unsigned N) const { return Values[N].first; } 00456 const char *getDescription(unsigned N) const { 00457 return Values[N].second.second; 00458 } 00459 00460 // parse - Return true on error. 00461 bool parse(Option &O, const char *ArgName, const std::string &Arg, 00462 DataType &V) { 00463 std::string ArgVal; 00464 if (hasArgStr) 00465 ArgVal = Arg; 00466 else 00467 ArgVal = ArgName; 00468 00469 for (unsigned i = 0, e = static_cast<unsigned>(Values.size()); 00470 i != e; ++i) 00471 if (ArgVal == Values[i].first) { 00472 V = Values[i].second.first; 00473 return false; 00474 } 00475 00476 return O.error("Cannot find option named '" + ArgVal + "'!"); 00477 } 00478 00479 /// addLiteralOption - Add an entry to the mapping table. 00480 /// 00481 template <class DT> 00482 void addLiteralOption(const char *Name, const DT &V, const char *HelpStr) { 00483 assert(findOption(Name) == Values.size() && "Option already exists!"); 00484 Values.push_back(std::make_pair(Name, 00485 std::make_pair(static_cast<DataType>(V),HelpStr))); 00486 MarkOptionsChanged(); 00487 } 00488 00489 /// removeLiteralOption - Remove the specified option. 00490 /// 00491 void removeLiteralOption(const char *Name) { 00492 unsigned N = findOption(Name); 00493 assert(N != Values.size() && "Option not found!"); 00494 Values.erase(Values.begin()+N); 00495 } 00496 }; 00497 00498 //-------------------------------------------------- 00499 // basic_parser - Super class of parsers to provide boilerplate code 00500 // 00501 struct basic_parser_impl { // non-template implementation of basic_parser<t> 00502 virtual ~basic_parser_impl() {} 00503 00504 enum ValueExpected getValueExpectedFlagDefault() const { 00505 return ValueRequired; 00506 } 00507 00508 void getExtraOptionNames(std::vector<const char*> &) {} 00509 00510 void initialize(Option &) {} 00511 00512 // Return the width of the option tag for printing... 00513 size_t getOptionWidth(const Option &O) const; 00514 00515 // printOptionInfo - Print out information about this option. The 00516 // to-be-maintained width is specified. 00517 // 00518 void printOptionInfo(const Option &O, size_t GlobalWidth) const; 00519 00520 // getValueName - Overload in subclass to provide a better default value. 00521 virtual const char *getValueName() const { return "value"; } 00522 00523 // An out-of-line virtual method to provide a 'home' for this class. 00524 virtual void anchor(); 00525 }; 00526 00527 // basic_parser - The real basic parser is just a template wrapper that provides 00528 // a typedef for the provided data type. 00529 // 00530 template<class DataType> 00531 struct basic_parser : public basic_parser_impl { 00532 typedef DataType parser_data_type; 00533 }; 00534 00535 //-------------------------------------------------- 00536 // parser<bool> 00537 // 00538 template<> 00539 class parser<bool> : public basic_parser<bool> { 00540 const char *ArgStr; 00541 public: 00542 00543 // parse - Return true on error. 00544 bool parse(Option &O, const char *ArgName, const std::string &Arg, bool &Val); 00545 00546 template <class Opt> 00547 void initialize(Opt &O) { 00548 ArgStr = O.ArgStr; 00549 } 00550 00551 enum ValueExpected getValueExpectedFlagDefault() const { 00552 return ValueOptional; 00553 } 00554 00555 // getValueName - Do not print =<value> at all. 00556 virtual const char *getValueName() const { return 0; } 00557 00558 // An out-of-line virtual method to provide a 'home' for this class. 00559 virtual void anchor(); 00560 }; 00561 00562 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<bool>); 00563 00564 //-------------------------------------------------- 00565 // parser<boolOrDefault> 00566 enum boolOrDefault { BOU_UNSET, BOU_TRUE, BOU_FALSE }; 00567 template<> 00568 class parser<boolOrDefault> : public basic_parser<boolOrDefault> { 00569 public: 00570 // parse - Return true on error. 00571 bool parse(Option &O, const char *ArgName, const std::string &Arg, 00572 boolOrDefault &Val); 00573 00574 enum ValueExpected getValueExpectedFlagDefault() const { 00575 return ValueOptional; 00576 } 00577 00578 // getValueName - Do not print =<value> at all. 00579 virtual const char *getValueName() const { return 0; } 00580 00581 // An out-of-line virtual method to provide a 'home' for this class. 00582 virtual void anchor(); 00583 }; 00584 00585 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<boolOrDefault>); 00586 00587 //-------------------------------------------------- 00588 // parser<int> 00589 // 00590 template<> 00591 class parser<int> : public basic_parser<int> { 00592 public: 00593 // parse - Return true on error. 00594 bool parse(Option &O, const char *ArgName, const std::string &Arg, int &Val); 00595 00596 // getValueName - Overload in subclass to provide a better default value. 00597 virtual const char *getValueName() const { return "int"; } 00598 00599 // An out-of-line virtual method to provide a 'home' for this class. 00600 virtual void anchor(); 00601 }; 00602 00603 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<int>); 00604 00605 00606 //-------------------------------------------------- 00607 // parser<unsigned> 00608 // 00609 template<> 00610 class parser<unsigned> : public basic_parser<unsigned> { 00611 public: 00612 // parse - Return true on error. 00613 bool parse(Option &O, const char *AN, const std::string &Arg, unsigned &Val); 00614 00615 // getValueName - Overload in subclass to provide a better default value. 00616 virtual const char *getValueName() const { return "uint"; } 00617 00618 // An out-of-line virtual method to provide a 'home' for this class. 00619 virtual void anchor(); 00620 }; 00621 00622 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<unsigned>); 00623 00624 //-------------------------------------------------- 00625 // parser<double> 00626 // 00627 template<> 00628 class parser<double> : public basic_parser<double> { 00629 public: 00630 // parse - Return true on error. 00631 bool parse(Option &O, const char *AN, const std::string &Arg, double &Val); 00632 00633 // getValueName - Overload in subclass to provide a better default value. 00634 virtual const char *getValueName() const { return "number"; } 00635 00636 // An out-of-line virtual method to provide a 'home' for this class. 00637 virtual void anchor(); 00638 }; 00639 00640 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<double>); 00641 00642 //-------------------------------------------------- 00643 // parser<float> 00644 // 00645 template<> 00646 class parser<float> : public basic_parser<float> { 00647 public: 00648 // parse - Return true on error. 00649 bool parse(Option &O, const char *AN, const std::string &Arg, float &Val); 00650 00651 // getValueName - Overload in subclass to provide a better default value. 00652 virtual const char *getValueName() const { return "number"; } 00653 00654 // An out-of-line virtual method to provide a 'home' for this class. 00655 virtual void anchor(); 00656 }; 00657 00658 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<float>); 00659 00660 //-------------------------------------------------- 00661 // parser<std::string> 00662 // 00663 template<> 00664 class parser<std::string> : public basic_parser<std::string> { 00665 public: 00666 // parse - Return true on error. 00667 bool parse(Option &, const char *, const std::string &Arg, 00668 std::string &Value) { 00669 Value = Arg; 00670 return false; 00671 } 00672 00673 // getValueName - Overload in subclass to provide a better default value. 00674 virtual const char *getValueName() const { return "string"; } 00675 00676 // An out-of-line virtual method to provide a 'home' for this class. 00677 virtual void anchor(); 00678 }; 00679 00680 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<std::string>); 00681 00682 //-------------------------------------------------- 00683 // parser<char> 00684 // 00685 template<> 00686 class parser<char> : public basic_parser<char> { 00687 public: 00688 // parse - Return true on error. 00689 bool parse(Option &, const char *, const std::string &Arg, 00690 char &Value) { 00691 Value = Arg[0]; 00692 return false; 00693 } 00694 00695 // getValueName - Overload in subclass to provide a better default value. 00696 virtual const char *getValueName() const { return "char"; } 00697 00698 // An out-of-line virtual method to provide a 'home' for this class. 00699 virtual void anchor(); 00700 }; 00701 00702 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<char>); 00703 00704 //===----------------------------------------------------------------------===// 00705 // applicator class - This class is used because we must use partial 00706 // specialization to handle literal string arguments specially (const char* does 00707 // not correctly respond to the apply method). Because the syntax to use this 00708 // is a pain, we have the 'apply' method below to handle the nastiness... 00709 // 00710 template<class Mod> struct applicator { 00711 template<class Opt> 00712 static void opt(const Mod &M, Opt &O) { M.apply(O); } 00713 }; 00714 00715 // Handle const char* as a special case... 00716 template<unsigned n> struct applicator<char[n]> { 00717 template<class Opt> 00718 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); } 00719 }; 00720 template<unsigned n> struct applicator<const char[n]> { 00721 template<class Opt> 00722 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); } 00723 }; 00724 template<> struct applicator<const char*> { 00725 template<class Opt> 00726 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); } 00727 }; 00728 00729 template<> struct applicator<NumOccurrences> { 00730 static void opt(NumOccurrences NO, Option &O) { O.setNumOccurrencesFlag(NO); } 00731 }; 00732 template<> struct applicator<ValueExpected> { 00733 static void opt(ValueExpected VE, Option &O) { O.setValueExpectedFlag(VE); } 00734 }; 00735 template<> struct applicator<OptionHidden> { 00736 static void opt(OptionHidden OH, Option &O) { O.setHiddenFlag(OH); } 00737 }; 00738 template<> struct applicator<FormattingFlags> { 00739 static void opt(FormattingFlags FF, Option &O) { O.setFormattingFlag(FF); } 00740 }; 00741 template<> struct applicator<MiscFlags> { 00742 static void opt(MiscFlags MF, Option &O) { O.setMiscFlag(MF); } 00743 }; 00744 00745 // apply method - Apply a modifier to an option in a type safe way. 00746 template<class Mod, class Opt> 00747 void apply(const Mod &M, Opt *O) { 00748 applicator<Mod>::opt(M, *O); 00749 } 00750 00751 00752 //===----------------------------------------------------------------------===// 00753 // opt_storage class 00754 00755 // Default storage class definition: external storage. This implementation 00756 // assumes the user will specify a variable to store the data into with the 00757 // cl::location(x) modifier. 00758 // 00759 template<class DataType, bool ExternalStorage, bool isClass> 00760 class opt_storage { 00761 DataType *Location; // Where to store the object... 00762 00763 void check() const { 00764 assert(Location != 0 && "cl::location(...) not specified for a command " 00765 "line option with external storage, " 00766 "or cl::init specified before cl::location()!!"); 00767 } 00768 public: 00769 opt_storage() : Location(0) {} 00770 00771 bool setLocation(Option &O, DataType &L) { 00772 if (Location) 00773 return O.error("cl::location(x) specified more than once!"); 00774 Location = &L; 00775 return false; 00776 } 00777 00778 template<class T> 00779 void setValue(const T &V) { 00780 check(); 00781 *Location = V; 00782 } 00783 00784 DataType &getValue() { check(); return *Location; } 00785 const DataType &getValue() const { check(); return *Location; } 00786 }; 00787 00788 00789 // Define how to hold a class type object, such as a string. Since we can 00790 // inherit from a class, we do so. This makes us exactly compatible with the 00791 // object in all cases that it is used. 00792 // 00793 template<class DataType> 00794 class opt_storage<DataType,false,true> : public DataType { 00795 public: 00796 template<class T> 00797 void setValue(const T &V) { DataType::operator=(V); } 00798 00799 DataType &getValue() { return *this; } 00800 const DataType &getValue() const { return *this; } 00801 }; 00802 00803 // Define a partial specialization to handle things we cannot inherit from. In 00804 // this case, we store an instance through containment, and overload operators 00805 // to get at the value. 00806 // 00807 template<class DataType> 00808 class opt_storage<DataType, false, false> { 00809 public: 00810 DataType Value; 00811 00812 // Make sure we initialize the value with the default constructor for the 00813 // type. 00814 opt_storage() : Value(DataType()) {} 00815 00816 template<class T> 00817 void setValue(const T &V) { Value = V; } 00818 DataType &getValue() { return Value; } 00819 DataType getValue() const { return Value; } 00820 00821 // If the datatype is a pointer, support -> on it. 00822 DataType operator->() const { return Value; } 00823 }; 00824 00825 00826 //===----------------------------------------------------------------------===// 00827 // opt - A scalar command line option. 00828 // 00829 template <class DataType, bool ExternalStorage = false, 00830 class ParserClass = parser<DataType> > 00831 class opt : public Option, 00832 public opt_storage<DataType, ExternalStorage, 00833 is_class<DataType>::value> { 00834 ParserClass Parser; 00835 00836 virtual bool handleOccurrence(unsigned pos, const char *ArgName, 00837 const std::string &Arg) { 00838 typename ParserClass::parser_data_type Val = 00839 typename ParserClass::parser_data_type(); 00840 if (Parser.parse(*this, ArgName, Arg, Val)) 00841 return true; // Parse error! 00842 this->setValue(Val); 00843 this->setPosition(pos); 00844 return false; 00845 } 00846 00847 virtual enum ValueExpected getValueExpectedFlagDefault() const { 00848 return Parser.getValueExpectedFlagDefault(); 00849 } 00850 virtual void getExtraOptionNames(std::vector<const char*> &OptionNames) { 00851 return Parser.getExtraOptionNames(OptionNames); 00852 } 00853 00854 // Forward printing stuff to the parser... 00855 virtual size_t getOptionWidth() const {return Parser.getOptionWidth(*this);} 00856 virtual void printOptionInfo(size_t GlobalWidth) const { 00857 Parser.printOptionInfo(*this, GlobalWidth); 00858 } 00859 00860 void done() { 00861 addArgument(); 00862 Parser.initialize(*this); 00863 } 00864 public: 00865 // setInitialValue - Used by the cl::init modifier... 00866 void setInitialValue(const DataType &V) { this->setValue(V); } 00867 00868 ParserClass &getParser() { return Parser; } 00869 00870 operator DataType() const { return this->getValue(); } 00871 00872 template<class T> 00873 DataType &operator=(const T &Val) { 00874 this->setValue(Val); 00875 return this->getValue(); 00876 } 00877 00878 // One option... 00879 template<class M0t> 00880 explicit opt(const M0t &M0) : Option(Optional | NotHidden) { 00881 apply(M0, this); 00882 done(); 00883 } 00884 00885 // Two options... 00886 template<class M0t, class M1t> 00887 opt(const M0t &M0, const M1t &M1) : Option(Optional | NotHidden) { 00888 apply(M0, this); apply(M1, this); 00889 done(); 00890 } 00891 00892 // Three options... 00893 template<class M0t, class M1t, class M2t> 00894 opt(const M0t &M0, const M1t &M1, 00895 const M2t &M2) : Option(Optional | NotHidden) { 00896 apply(M0, this); apply(M1, this); apply(M2, this); 00897 done(); 00898 } 00899 // Four options... 00900 template<class M0t, class M1t, class M2t, class M3t> 00901 opt(const M0t &M0, const M1t &M1, const M2t &M2, 00902 const M3t &M3) : Option(Optional | NotHidden) { 00903 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 00904 done(); 00905 } 00906 // Five options... 00907 template<class M0t, class M1t, class M2t, class M3t, class M4t> 00908 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, 00909 const M4t &M4) : Option(Optional | NotHidden) { 00910 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 00911 apply(M4, this); 00912 done(); 00913 } 00914 // Six options... 00915 template<class M0t, class M1t, class M2t, class M3t, 00916 class M4t, class M5t> 00917 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, 00918 const M4t &M4, const M5t &M5) : Option(Optional | NotHidden) { 00919 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 00920 apply(M4, this); apply(M5, this); 00921 done(); 00922 } 00923 // Seven options... 00924 template<class M0t, class M1t, class M2t, class M3t, 00925 class M4t, class M5t, class M6t> 00926 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, 00927 const M4t &M4, const M5t &M5, 00928 const M6t &M6) : Option(Optional | NotHidden) { 00929 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 00930 apply(M4, this); apply(M5, this); apply(M6, this); 00931 done(); 00932 } 00933 // Eight options... 00934 template<class M0t, class M1t, class M2t, class M3t, 00935 class M4t, class M5t, class M6t, class M7t> 00936 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, 00937 const M4t &M4, const M5t &M5, const M6t &M6, 00938 const M7t &M7) : Option(Optional | NotHidden) { 00939 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 00940 apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this); 00941 done(); 00942 } 00943 }; 00944 00945 EXTERN_TEMPLATE_INSTANTIATION(class opt<unsigned>); 00946 EXTERN_TEMPLATE_INSTANTIATION(class opt<int>); 00947 EXTERN_TEMPLATE_INSTANTIATION(class opt<std::string>); 00948 EXTERN_TEMPLATE_INSTANTIATION(class opt<char>); 00949 EXTERN_TEMPLATE_INSTANTIATION(class opt<bool>); 00950 00951 //===----------------------------------------------------------------------===// 00952 // list_storage class 00953 00954 // Default storage class definition: external storage. This implementation 00955 // assumes the user will specify a variable to store the data into with the 00956 // cl::location(x) modifier. 00957 // 00958 template<class DataType, class StorageClass> 00959 class list_storage { 00960 StorageClass *Location; // Where to store the object... 00961 00962 public: 00963 list_storage() : Location(0) {} 00964 00965 bool setLocation(Option &O, StorageClass &L) { 00966 if (Location) 00967 return O.error("cl::location(x) specified more than once!"); 00968 Location = &L; 00969 return false; 00970 } 00971 00972 template<class T> 00973 void addValue(const T &V) { 00974 assert(Location != 0 && "cl::location(...) not specified for a command " 00975 "line option with external storage!"); 00976 Location->push_back(V); 00977 } 00978 }; 00979 00980 00981 // Define how to hold a class type object, such as a string. Since we can 00982 // inherit from a class, we do so. This makes us exactly compatible with the 00983 // object in all cases that it is used. 00984 // 00985 template<class DataType> 00986 class list_storage<DataType, bool> : public std::vector<DataType> { 00987 public: 00988 template<class T> 00989 void addValue(const T &V) { push_back(V); } 00990 }; 00991 00992 00993 //===----------------------------------------------------------------------===// 00994 // list - A list of command line options. 00995 // 00996 template <class DataType, class Storage = bool, 00997 class ParserClass = parser<DataType> > 00998 class list : public Option, public list_storage<DataType, Storage> { 00999 std::vector<unsigned> Positions; 01000 ParserClass Parser; 01001 01002 virtual enum ValueExpected getValueExpectedFlagDefault() const { 01003 return Parser.getValueExpectedFlagDefault(); 01004 } 01005 virtual void getExtraOptionNames(std::vector<const char*> &OptionNames) { 01006 return Parser.getExtraOptionNames(OptionNames); 01007 } 01008 01009 virtual bool handleOccurrence(unsigned pos, const char *ArgName, 01010 const std::string &Arg) { 01011 typename ParserClass::parser_data_type Val = 01012 typename ParserClass::parser_data_type(); 01013 if (Parser.parse(*this, ArgName, Arg, Val)) 01014 return true; // Parse Error! 01015 addValue(Val); 01016 setPosition(pos); 01017 Positions.push_back(pos); 01018 return false; 01019 } 01020 01021 // Forward printing stuff to the parser... 01022 virtual size_t getOptionWidth() const {return Parser.getOptionWidth(*this);} 01023 virtual void printOptionInfo(size_t GlobalWidth) const { 01024 Parser.printOptionInfo(*this, GlobalWidth); 01025 } 01026 01027 void done() { 01028 addArgument(); 01029 Parser.initialize(*this); 01030 } 01031 public: 01032 ParserClass &getParser() { return Parser; } 01033 01034 unsigned getPosition(unsigned optnum) const { 01035 assert(optnum < this->size() && "Invalid option index"); 01036 return Positions[optnum]; 01037 } 01038 01039 void setNumAdditionalVals(unsigned n) { 01040 Option::setNumAdditionalVals(n); 01041 } 01042 01043 // One option... 01044 template<class M0t> 01045 explicit list(const M0t &M0) : Option(ZeroOrMore | NotHidden) { 01046 apply(M0, this); 01047 done(); 01048 } 01049 // Two options... 01050 template<class M0t, class M1t> 01051 list(const M0t &M0, const M1t &M1) : Option(ZeroOrMore | NotHidden) { 01052 apply(M0, this); apply(M1, this); 01053 done(); 01054 } 01055 // Three options... 01056 template<class M0t, class M1t, class M2t> 01057 list(const M0t &M0, const M1t &M1, const M2t &M2) 01058 : Option(ZeroOrMore | NotHidden) { 01059 apply(M0, this); apply(M1, this); apply(M2, this); 01060 done(); 01061 } 01062 // Four options... 01063 template<class M0t, class M1t, class M2t, class M3t> 01064 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3) 01065 : Option(ZeroOrMore | NotHidden) { 01066 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 01067 done(); 01068 } 01069 // Five options... 01070 template<class M0t, class M1t, class M2t, class M3t, class M4t> 01071 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, 01072 const M4t &M4) : Option(ZeroOrMore | NotHidden) { 01073 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 01074 apply(M4, this); 01075 done(); 01076 } 01077 // Six options... 01078 template<class M0t, class M1t, class M2t, class M3t, 01079 class M4t, class M5t> 01080 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, 01081 const M4t &M4, const M5t &M5) : Option(ZeroOrMore | NotHidden) { 01082 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 01083 apply(M4, this); apply(M5, this); 01084 done(); 01085 } 01086 // Seven options... 01087 template<class M0t, class M1t, class M2t, class M3t, 01088 class M4t, class M5t, class M6t> 01089 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, 01090 const M4t &M4, const M5t &M5, const M6t &M6) 01091 : Option(ZeroOrMore | NotHidden) { 01092 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 01093 apply(M4, this); apply(M5, this); apply(M6, this); 01094 done(); 01095 } 01096 // Eight options... 01097 template<class M0t, class M1t, class M2t, class M3t, 01098 class M4t, class M5t, class M6t, class M7t> 01099 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, 01100 const M4t &M4, const M5t &M5, const M6t &M6, 01101 const M7t &M7) : Option(ZeroOrMore | NotHidden) { 01102 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 01103 apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this); 01104 done(); 01105 } 01106 }; 01107 01108 // multi_val - Modifier to set the number of additional values. 01109 struct multi_val { 01110 unsigned AdditionalVals; 01111 explicit multi_val(unsigned N) : AdditionalVals(N) {} 01112 01113 template <typename D, typename S, typename P> 01114 void apply(list<D, S, P> &L) const { L.setNumAdditionalVals(AdditionalVals); } 01115 }; 01116 01117 01118 //===----------------------------------------------------------------------===// 01119 // bits_storage class 01120 01121 // Default storage class definition: external storage. This implementation 01122 // assumes the user will specify a variable to store the data into with the 01123 // cl::location(x) modifier. 01124 // 01125 template<class DataType, class StorageClass> 01126 class bits_storage { 01127 unsigned *Location; // Where to store the bits... 01128 01129 template<class T> 01130 static unsigned Bit(const T &V) { 01131 unsigned BitPos = reinterpret_cast<unsigned>(V); 01132 assert(BitPos < sizeof(unsigned) * CHAR_BIT && 01133 "enum exceeds width of bit vector!"); 01134 return 1 << BitPos; 01135 } 01136 01137 public: 01138 bits_storage() : Location(0) {} 01139 01140 bool setLocation(Option &O, unsigned &L) { 01141 if (Location) 01142 return O.error("cl::location(x) specified more than once!"); 01143 Location = &L; 01144 return false; 01145 } 01146 01147 template<class T> 01148 void addValue(const T &V) { 01149 assert(Location != 0 && "cl::location(...) not specified for a command " 01150 "line option with external storage!"); 01151 *Location |= Bit(V); 01152 } 01153 01154 unsigned getBits() { return *Location; } 01155 01156 template<class T> 01157 bool isSet(const T &V) { 01158 return (*Location & Bit(V)) != 0; 01159 } 01160 }; 01161 01162 01163 // Define how to hold bits. Since we can inherit from a class, we do so. 01164 // This makes us exactly compatible with the bits in all cases that it is used. 01165 // 01166 template<class DataType> 01167 class bits_storage<DataType, bool> { 01168 unsigned Bits; // Where to store the bits... 01169 01170 template<class T> 01171 static unsigned Bit(const T &V) { 01172 unsigned BitPos = reinterpret_cast<unsigned>(V); 01173 assert(BitPos < sizeof(unsigned) * CHAR_BIT && 01174 "enum exceeds width of bit vector!"); 01175 return 1 << BitPos; 01176 } 01177 01178 public: 01179 template<class T> 01180 void addValue(const T &V) { 01181 Bits |= Bit(V); 01182 } 01183 01184 unsigned getBits() { return Bits; } 01185 01186 template<class T> 01187 bool isSet(const T &V) { 01188 return (Bits & Bit(V)) != 0; 01189 } 01190 }; 01191 01192 01193 //===----------------------------------------------------------------------===// 01194 // bits - A bit vector of command options. 01195 // 01196 template <class DataType, class Storage = bool, 01197 class ParserClass = parser<DataType> > 01198 class bits : public Option, public bits_storage<DataType, Storage> { 01199 std::vector<unsigned> Positions; 01200 ParserClass Parser; 01201 01202 virtual enum ValueExpected getValueExpectedFlagDefault() const { 01203 return Parser.getValueExpectedFlagDefault(); 01204 } 01205 virtual void getExtraOptionNames(std::vector<const char*> &OptionNames) { 01206 return Parser.getExtraOptionNames(OptionNames); 01207 } 01208 01209 virtual bool handleOccurrence(unsigned pos, const char *ArgName, 01210 const std::string &Arg) { 01211 typename ParserClass::parser_data_type Val = 01212 typename ParserClass::parser_data_type(); 01213 if (Parser.parse(*this, ArgName, Arg, Val)) 01214 return true; // Parse Error! 01215 addValue(Val); 01216 setPosition(pos); 01217 Positions.push_back(pos); 01218 return false; 01219 } 01220 01221 // Forward printing stuff to the parser... 01222 virtual size_t getOptionWidth() const {return Parser.getOptionWidth(*this);} 01223 virtual void printOptionInfo(size_t GlobalWidth) const { 01224 Parser.printOptionInfo(*this, GlobalWidth); 01225 } 01226 01227 void done() { 01228 addArgument(); 01229 Parser.initialize(*this); 01230 } 01231 public: 01232 ParserClass &getParser() { return Parser; } 01233 01234 unsigned getPosition(unsigned optnum) const { 01235 assert(optnum < this->size() && "Invalid option index"); 01236 return Positions[optnum]; 01237 } 01238 01239 // One option... 01240 template<class M0t> 01241 explicit bits(const M0t &M0) : Option(ZeroOrMore | NotHidden) { 01242 apply(M0, this); 01243 done(); 01244 } 01245 // Two options... 01246 template<class M0t, class M1t> 01247 bits(const M0t &M0, const M1t &M1) : Option(ZeroOrMore | NotHidden) { 01248 apply(M0, this); apply(M1, this); 01249 done(); 01250 } 01251 // Three options... 01252 template<class M0t, class M1t, class M2t> 01253 bits(const M0t &M0, const M1t &M1, const M2t &M2) 01254 : Option(ZeroOrMore | NotHidden) { 01255 apply(M0, this); apply(M1, this); apply(M2, this); 01256 done(); 01257 } 01258 // Four options... 01259 template<class M0t, class M1t, class M2t, class M3t> 01260 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3) 01261 : Option(ZeroOrMore | NotHidden) { 01262 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 01263 done(); 01264 } 01265 // Five options... 01266 template<class M0t, class M1t, class M2t, class M3t, class M4t> 01267 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, 01268 const M4t &M4) : Option(ZeroOrMore | NotHidden) { 01269 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 01270 apply(M4, this); 01271 done(); 01272 } 01273 // Six options... 01274 template<class M0t, class M1t, class M2t, class M3t, 01275 class M4t, class M5t> 01276 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, 01277 const M4t &M4, const M5t &M5) : Option(ZeroOrMore | NotHidden) { 01278 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 01279 apply(M4, this); apply(M5, this); 01280 done(); 01281 } 01282 // Seven options... 01283 template<class M0t, class M1t, class M2t, class M3t, 01284 class M4t, class M5t, class M6t> 01285 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, 01286 const M4t &M4, const M5t &M5, const M6t &M6) 01287 : Option(ZeroOrMore | NotHidden) { 01288 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 01289 apply(M4, this); apply(M5, this); apply(M6, this); 01290 done(); 01291 } 01292 // Eight options... 01293 template<class M0t, class M1t, class M2t, class M3t, 01294 class M4t, class M5t, class M6t, class M7t> 01295 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3, 01296 const M4t &M4, const M5t &M5, const M6t &M6, 01297 const M7t &M7) : Option(ZeroOrMore | NotHidden) { 01298 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 01299 apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this); 01300 done(); 01301 } 01302 }; 01303 01304 //===----------------------------------------------------------------------===// 01305 // Aliased command line option (alias this name to a preexisting name) 01306 // 01307 01308 class alias : public Option { 01309 Option *AliasFor; 01310 virtual bool handleOccurrence(unsigned pos, const char * /*ArgName*/, 01311 const std::string &Arg) { 01312 return AliasFor->handleOccurrence(pos, AliasFor->ArgStr, Arg); 01313 } 01314 // Handle printing stuff... 01315 virtual size_t getOptionWidth() const; 01316 virtual void printOptionInfo(size_t GlobalWidth) const; 01317 01318 void done() { 01319 if (!hasArgStr()) 01320 error("cl::alias must have argument name specified!"); 01321 if (AliasFor == 0) 01322 error("cl::alias must have an cl::aliasopt(option) specified!"); 01323 addArgument(); 01324 } 01325 public: 01326 void setAliasFor(Option &O) { 01327 if (AliasFor) 01328 error("cl::alias must only have one cl::aliasopt(...) specified!"); 01329 AliasFor = &O; 01330 } 01331 01332 // One option... 01333 template<class M0t> 01334 explicit alias(const M0t &M0) : Option(Optional | Hidden), AliasFor(0) { 01335 apply(M0, this); 01336 done(); 01337 } 01338 // Two options... 01339 template<class M0t, class M1t> 01340 alias(const M0t &M0, const M1t &M1) : Option(Optional | Hidden), AliasFor(0) { 01341 apply(M0, this); apply(M1, this); 01342 done(); 01343 } 01344 // Three options... 01345 template<class M0t, class M1t, class M2t> 01346 alias(const M0t &M0, const M1t &M1, const M2t &M2) 01347 : Option(Optional | Hidden), AliasFor(0) { 01348 apply(M0, this); apply(M1, this); apply(M2, this); 01349 done(); 01350 } 01351 // Four options... 01352 template<class M0t, class M1t, class M2t, class M3t> 01353 alias(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3) 01354 : Option(Optional | Hidden), AliasFor(0) { 01355 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this); 01356 done(); 01357 } 01358 }; 01359 01360 // aliasfor - Modifier to set the option an alias aliases. 01361 struct aliasopt { 01362 Option &Opt; 01363 explicit aliasopt(Option &O) : Opt(O) {} 01364 void apply(alias &A) const { A.setAliasFor(Opt); } 01365 }; 01366 01367 // extrahelp - provide additional help at the end of the normal help 01368 // output. All occurrences of cl::extrahelp will be accumulated and 01369 // printed to stderr at the end of the regular help, just before 01370 // exit is called. 01371 struct extrahelp { 01372 const char * morehelp; 01373 explicit extrahelp(const char* help); 01374 }; 01375 01376 void PrintVersionMessage(); 01377 // This function just prints the help message, exactly the same way as if the 01378 // --help option had been given on the command line. 01379 // NOTE: THIS FUNCTION TERMINATES THE PROGRAM! 01380 void PrintHelpMessage(); 01381 01382 } // End namespace cl 01383 01384 } // End namespace llvm 01385 01386 #endif