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