1 //===- llvm/Support/CommandLine.h - Command line handler --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
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.
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
18 //===----------------------------------------------------------------------===//
20 #ifndef LLVM_SUPPORT_COMMANDLINE_H
21 #define LLVM_SUPPORT_COMMANDLINE_H
23 #include "llvm/Support/type_traits.h"
24 #include "llvm/Support/DataTypes.h"
25 #include "llvm/Support/Compiler.h"
26 #include "llvm/ADT/SmallVector.h"
35 /// cl Namespace - This namespace contains all of the command line option
36 /// processing machinery. It is intentionally a short name to make qualified
40 //===----------------------------------------------------------------------===//
41 // ParseCommandLineOptions - Command line option processing entry point.
43 void ParseCommandLineOptions(int &argc, char **argv,
44 const char *Overview = 0);
46 //===----------------------------------------------------------------------===//
47 // ParseEnvironmentOptions - Environment variable option processing alternate
50 void ParseEnvironmentOptions(const char *progName, const char *envvar,
51 const char *Overview = 0);
53 ///===---------------------------------------------------------------------===//
54 /// SetVersionPrinter - Override the default (LLVM specific) version printer
55 /// used to print out the version when --version is given
56 /// on the command line. This gives other systems using the
57 /// CommandLine utilities to print their own version string.
58 void SetVersionPrinter(void (*func)());
61 // MarkOptionsChanged - Internal helper function.
62 void MarkOptionsChanged();
64 //===----------------------------------------------------------------------===//
65 // Flags permitted to be passed to command line arguments
68 enum NumOccurrences { // Flags for the number of occurrences allowed
69 Optional = 0x01, // Zero or One occurrence
70 ZeroOrMore = 0x02, // Zero or more occurrences allowed
71 Required = 0x03, // One occurrence required
72 OneOrMore = 0x04, // One or more occurrences required
74 // ConsumeAfter - Indicates that this option is fed anything that follows the
75 // last positional argument required by the application (it is an error if
76 // there are zero positional arguments, and a ConsumeAfter option is used).
77 // Thus, for example, all arguments to LLI are processed until a filename is
78 // found. Once a filename is found, all of the succeeding arguments are
79 // passed, unprocessed, to the ConsumeAfter option.
83 OccurrencesMask = 0x07
86 enum ValueExpected { // Is a value required for the option?
87 ValueOptional = 0x08, // The value can appear... or not
88 ValueRequired = 0x10, // The value is required to appear!
89 ValueDisallowed = 0x18, // A value may not be specified (for flags)
93 enum OptionHidden { // Control whether -help shows this option
94 NotHidden = 0x20, // Option included in --help & --help-hidden
95 Hidden = 0x40, // -help doesn't, but --help-hidden does
96 ReallyHidden = 0x60, // Neither --help nor --help-hidden show this arg
100 // Formatting flags - This controls special features that the option might have
101 // that cause it to be parsed differently...
103 // Prefix - This option allows arguments that are otherwise unrecognized to be
104 // matched by options that are a prefix of the actual value. This is useful for
105 // cases like a linker, where options are typically of the form '-lfoo' or
106 // '-L../../include' where -l or -L are the actual flags. When prefix is
107 // enabled, and used, the value for the flag comes from the suffix of the
110 // Grouping - With this option enabled, multiple letter options are allowed to
111 // bunch together with only a single hyphen for the whole group. This allows
112 // emulation of the behavior that ls uses for example: ls -la === ls -l -a
115 enum FormattingFlags {
116 NormalFormatting = 0x000, // Nothing special
117 Positional = 0x080, // Is a positional argument, no '-' required
118 Prefix = 0x100, // Can this option directly prefix its value?
119 Grouping = 0x180, // Can this option group with other options?
120 FormattingMask = 0x180 // Union of the above flags.
123 enum MiscFlags { // Miscellaneous flags to adjust argument
124 CommaSeparated = 0x200, // Should this cl::list split between commas?
125 PositionalEatsArgs = 0x400, // Should this positional cl::list eat -args?
126 MiscMask = 0x600 // Union of the above flags.
131 //===----------------------------------------------------------------------===//
136 friend void cl::ParseCommandLineOptions(int &, char **, const char *);
139 // handleOccurrences - Overriden by subclasses to handle the value passed into
140 // an argument. Should return true if there was an error processing the
141 // argument and the program should exit.
143 virtual bool handleOccurrence(unsigned pos, const char *ArgName,
144 const std::string &Arg) = 0;
146 virtual enum ValueExpected getValueExpectedFlagDefault() const {
147 return ValueOptional;
150 // Out of line virtual function to provide home for the class.
151 virtual void anchor();
153 int NumOccurrences; // The number of times specified
154 int Flags; // Flags for the argument
155 unsigned Position; // Position of last occurrence of the option
156 Option *NextRegistered; // Singly linked list of registered options.
158 const char *ArgStr; // The argument string itself (ex: "help", "o")
159 const char *HelpStr; // The descriptive text message for --help
160 const char *ValueStr; // String describing what the value of this option is
162 inline enum NumOccurrences getNumOccurrencesFlag() const {
163 return static_cast<enum NumOccurrences>(Flags & OccurrencesMask);
165 inline enum ValueExpected getValueExpectedFlag() const {
166 int VE = Flags & ValueMask;
167 return VE ? static_cast<enum ValueExpected>(VE)
168 : getValueExpectedFlagDefault();
170 inline enum OptionHidden getOptionHiddenFlag() const {
171 return static_cast<enum OptionHidden>(Flags & HiddenMask);
173 inline enum FormattingFlags getFormattingFlag() const {
174 return static_cast<enum FormattingFlags>(Flags & FormattingMask);
176 inline unsigned getMiscFlags() const {
177 return Flags & MiscMask;
179 inline unsigned getPosition() const { return Position; }
181 // hasArgStr - Return true if the argstr != ""
182 bool hasArgStr() const { return ArgStr[0] != 0; }
184 //-------------------------------------------------------------------------===
185 // Accessor functions set by OptionModifiers
187 void setArgStr(const char *S) { ArgStr = S; }
188 void setDescription(const char *S) { HelpStr = S; }
189 void setValueStr(const char *S) { ValueStr = S; }
191 void setFlag(unsigned Flag, unsigned FlagMask) {
196 void setNumOccurrencesFlag(enum NumOccurrences Val) {
197 setFlag(Val, OccurrencesMask);
199 void setValueExpectedFlag(enum ValueExpected Val) { setFlag(Val, ValueMask); }
200 void setHiddenFlag(enum OptionHidden Val) { setFlag(Val, HiddenMask); }
201 void setFormattingFlag(enum FormattingFlags V) { setFlag(V, FormattingMask); }
202 void setMiscFlag(enum MiscFlags M) { setFlag(M, M); }
203 void setPosition(unsigned pos) { Position = pos; }
205 explicit Option(unsigned DefaultFlags)
206 : NumOccurrences(0), Flags(DefaultFlags | NormalFormatting), Position(0),
207 NextRegistered(0), ArgStr(""), HelpStr(""), ValueStr("") {
208 assert(getNumOccurrencesFlag() != 0 &&
209 getOptionHiddenFlag() != 0 && "Not all default flags specified!");
213 // addArgument - Register this argument with the commandline system.
217 Option *getNextRegisteredOption() const { return NextRegistered; }
219 // Return the width of the option tag for printing...
220 virtual unsigned getOptionWidth() const = 0;
222 // printOptionInfo - Print out information about this option. The
223 // to-be-maintained width is specified.
225 virtual void printOptionInfo(unsigned GlobalWidth) const = 0;
227 virtual void getExtraOptionNames(std::vector<const char*> &OptionNames) {}
229 // addOccurrence - Wrapper around handleOccurrence that enforces Flags
231 bool addOccurrence(unsigned pos, const char *ArgName,
232 const std::string &Value);
234 // Prints option name followed by message. Always returns true.
235 bool error(std::string Message, const char *ArgName = 0);
238 inline int getNumOccurrences() const { return NumOccurrences; }
243 //===----------------------------------------------------------------------===//
244 // Command line option modifiers that can be used to modify the behavior of
245 // command line option parsers...
248 // desc - Modifier to set the description shown in the --help output...
251 desc(const char *Str) : Desc(Str) {}
252 void apply(Option &O) const { O.setDescription(Desc); }
255 // value_desc - Modifier to set the value description shown in the --help
259 value_desc(const char *Str) : Desc(Str) {}
260 void apply(Option &O) const { O.setValueStr(Desc); }
263 // init - Specify a default (initial) value for the command line argument, if
264 // the default constructor for the argument type does not give you what you
265 // want. This is only valid on "opt" arguments, not on "list" arguments.
270 initializer(const Ty &Val) : Init(Val) {}
273 void apply(Opt &O) const { O.setInitialValue(Init); }
277 initializer<Ty> init(const Ty &Val) {
278 return initializer<Ty>(Val);
282 // location - Allow the user to specify which external variable they want to
283 // store the results of the command line argument processing into, if they don't
284 // want to store it in the option itself.
287 struct LocationClass {
289 LocationClass(Ty &L) : Loc(L) {}
292 void apply(Opt &O) const { O.setLocation(O, Loc); }
296 LocationClass<Ty> location(Ty &L) { return LocationClass<Ty>(L); }
299 //===----------------------------------------------------------------------===//
300 // Enum valued command line option
302 #define clEnumVal(ENUMVAL, DESC) #ENUMVAL, int(ENUMVAL), DESC
303 #define clEnumValN(ENUMVAL, FLAGNAME, DESC) FLAGNAME, int(ENUMVAL), DESC
304 #define clEnumValEnd (reinterpret_cast<void*>(0))
306 // values - For custom data types, allow specifying a group of values together
307 // as the values that go into the mapping that the option handler uses. Note
308 // that the values list must always have a 0 at the end of the list to indicate
309 // that the list has ended.
311 template<class DataType>
313 // Use a vector instead of a map, because the lists should be short,
314 // the overhead is less, and most importantly, it keeps them in the order
315 // inserted so we can print our option out nicely.
316 SmallVector<std::pair<const char *, std::pair<int, const char *> >,4> Values;
317 void processValues(va_list Vals);
319 ValuesClass(const char *EnumName, DataType Val, const char *Desc,
321 // Insert the first value, which is required.
322 Values.push_back(std::make_pair(EnumName, std::make_pair(Val, Desc)));
324 // Process the varargs portion of the values...
325 while (const char *EnumName = va_arg(ValueArgs, const char *)) {
326 DataType EnumVal = static_cast<DataType>(va_arg(ValueArgs, int));
327 const char *EnumDesc = va_arg(ValueArgs, const char *);
328 Values.push_back(std::make_pair(EnumName, // Add value to value map
329 std::make_pair(EnumVal, EnumDesc)));
334 void apply(Opt &O) const {
335 for (unsigned i = 0, e = Values.size(); i != e; ++i)
336 O.getParser().addLiteralOption(Values[i].first, Values[i].second.first,
337 Values[i].second.second);
341 template<class DataType>
342 ValuesClass<DataType> END_WITH_NULL values(const char *Arg, DataType Val,
343 const char *Desc, ...) {
345 va_start(ValueArgs, Desc);
346 ValuesClass<DataType> Vals(Arg, Val, Desc, ValueArgs);
352 //===----------------------------------------------------------------------===//
353 // parser class - Parameterizable parser for different data types. By default,
354 // known data types (string, int, bool) have specialized parsers, that do what
355 // you would expect. The default parser, used for data types that are not
356 // built-in, uses a mapping table to map specific options to values, which is
357 // used, among other things, to handle enum types.
359 //--------------------------------------------------
360 // generic_parser_base - This class holds all the non-generic code that we do
361 // not need replicated for every instance of the generic parser. This also
362 // allows us to put stuff into CommandLine.cpp
364 struct generic_parser_base {
365 virtual ~generic_parser_base() {} // Base class should have virtual-dtor
367 // getNumOptions - Virtual function implemented by generic subclass to
368 // indicate how many entries are in Values.
370 virtual unsigned getNumOptions() const = 0;
372 // getOption - Return option name N.
373 virtual const char *getOption(unsigned N) const = 0;
375 // getDescription - Return description N
376 virtual const char *getDescription(unsigned N) const = 0;
378 // Return the width of the option tag for printing...
379 virtual unsigned getOptionWidth(const Option &O) const;
381 // printOptionInfo - Print out information about this option. The
382 // to-be-maintained width is specified.
384 virtual void printOptionInfo(const Option &O, unsigned GlobalWidth) const;
386 void initialize(Option &O) {
387 // All of the modifiers for the option have been processed by now, so the
388 // argstr field should be stable, copy it down now.
390 hasArgStr = O.hasArgStr();
393 void getExtraOptionNames(std::vector<const char*> &OptionNames) {
394 // If there has been no argstr specified, that means that we need to add an
395 // argument for every possible option. This ensures that our options are
398 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
399 OptionNames.push_back(getOption(i));
403 enum ValueExpected getValueExpectedFlagDefault() const {
404 // If there is an ArgStr specified, then we are of the form:
406 // -opt=O2 or -opt O2 or -optO2
408 // In which case, the value is required. Otherwise if an arg str has not
409 // been specified, we are of the form:
411 // -O2 or O2 or -la (where -l and -a are separate options)
413 // If this is the case, we cannot allow a value.
416 return ValueRequired;
418 return ValueDisallowed;
421 // findOption - Return the option number corresponding to the specified
422 // argument string. If the option is not found, getNumOptions() is returned.
424 unsigned findOption(const char *Name);
430 // Default parser implementation - This implementation depends on having a
431 // mapping of recognized options to values of some sort. In addition to this,
432 // each entry in the mapping also tracks a help message that is printed with the
433 // command line option for --help. Because this is a simple mapping parser, the
434 // data type can be any unsupported type.
436 template <class DataType>
437 class parser : public generic_parser_base {
439 SmallVector<std::pair<const char *,
440 std::pair<DataType, const char *> >, 8> Values;
442 typedef DataType parser_data_type;
444 // Implement virtual functions needed by generic_parser_base
445 unsigned getNumOptions() const { return unsigned(Values.size()); }
446 const char *getOption(unsigned N) const { return Values[N].first; }
447 const char *getDescription(unsigned N) const {
448 return Values[N].second.second;
451 // parse - Return true on error.
452 bool parse(Option &O, const char *ArgName, const std::string &Arg,
460 for (unsigned i = 0, e = Values.size(); i != e; ++i)
461 if (ArgVal == Values[i].first) {
462 V = Values[i].second.first;
466 return O.error(": Cannot find option named '" + ArgVal + "'!");
469 /// addLiteralOption - Add an entry to the mapping table.
472 void addLiteralOption(const char *Name, const DT &V, const char *HelpStr) {
473 assert(findOption(Name) == Values.size() && "Option already exists!");
474 Values.push_back(std::make_pair(Name,
475 std::make_pair(static_cast<DataType>(V),HelpStr)));
476 MarkOptionsChanged();
479 /// removeLiteralOption - Remove the specified option.
481 void removeLiteralOption(const char *Name) {
482 unsigned N = findOption(Name);
483 assert(N != Values.size() && "Option not found!");
484 Values.erase(Values.begin()+N);
488 //--------------------------------------------------
489 // basic_parser - Super class of parsers to provide boilerplate code
491 struct basic_parser_impl { // non-template implementation of basic_parser<t>
492 virtual ~basic_parser_impl() {}
494 enum ValueExpected getValueExpectedFlagDefault() const {
495 return ValueRequired;
498 void getExtraOptionNames(std::vector<const char*> &OptionNames) {}
500 void initialize(Option &O) {}
502 // Return the width of the option tag for printing...
503 unsigned getOptionWidth(const Option &O) const;
505 // printOptionInfo - Print out information about this option. The
506 // to-be-maintained width is specified.
508 void printOptionInfo(const Option &O, unsigned GlobalWidth) const;
510 // getValueName - Overload in subclass to provide a better default value.
511 virtual const char *getValueName() const { return "value"; }
513 // An out-of-line virtual method to provide a 'home' for this class.
514 virtual void anchor();
517 // basic_parser - The real basic parser is just a template wrapper that provides
518 // a typedef for the provided data type.
520 template<class DataType>
521 struct basic_parser : public basic_parser_impl {
522 typedef DataType parser_data_type;
525 //--------------------------------------------------
529 class parser<bool> : public basic_parser<bool> {
531 // parse - Return true on error.
532 bool parse(Option &O, const char *ArgName, const std::string &Arg, bool &Val);
534 enum ValueExpected getValueExpectedFlagDefault() const {
535 return ValueOptional;
538 // getValueName - Do not print =<value> at all.
539 virtual const char *getValueName() const { return 0; }
541 // An out-of-line virtual method to provide a 'home' for this class.
542 virtual void anchor();
545 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<bool>);
547 //--------------------------------------------------
548 // parser<boolOrDefault>
549 enum boolOrDefault { BOU_UNSET, BOU_TRUE, BOU_FALSE };
551 class parser<boolOrDefault> : public basic_parser<boolOrDefault> {
553 // parse - Return true on error.
554 bool parse(Option &O, const char *ArgName, const std::string &Arg,
557 enum ValueExpected getValueExpectedFlagDefault() const {
558 return ValueOptional;
561 // getValueName - Do not print =<value> at all.
562 virtual const char *getValueName() const { return 0; }
564 // An out-of-line virtual method to provide a 'home' for this class.
565 virtual void anchor();
568 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<boolOrDefault>);
570 //--------------------------------------------------
574 class parser<int> : public basic_parser<int> {
576 // parse - Return true on error.
577 bool parse(Option &O, const char *ArgName, const std::string &Arg, int &Val);
579 // getValueName - Overload in subclass to provide a better default value.
580 virtual const char *getValueName() const { return "int"; }
582 // An out-of-line virtual method to provide a 'home' for this class.
583 virtual void anchor();
586 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<int>);
589 //--------------------------------------------------
593 class parser<unsigned> : public basic_parser<unsigned> {
595 // parse - Return true on error.
596 bool parse(Option &O, const char *AN, const std::string &Arg, unsigned &Val);
598 // getValueName - Overload in subclass to provide a better default value.
599 virtual const char *getValueName() const { return "uint"; }
601 // An out-of-line virtual method to provide a 'home' for this class.
602 virtual void anchor();
605 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<unsigned>);
607 //--------------------------------------------------
611 class parser<double> : public basic_parser<double> {
613 // parse - Return true on error.
614 bool parse(Option &O, const char *AN, const std::string &Arg, double &Val);
616 // getValueName - Overload in subclass to provide a better default value.
617 virtual const char *getValueName() const { return "number"; }
619 // An out-of-line virtual method to provide a 'home' for this class.
620 virtual void anchor();
623 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<double>);
625 //--------------------------------------------------
629 class parser<float> : public basic_parser<float> {
631 // parse - Return true on error.
632 bool parse(Option &O, const char *AN, const std::string &Arg, float &Val);
634 // getValueName - Overload in subclass to provide a better default value.
635 virtual const char *getValueName() const { return "number"; }
637 // An out-of-line virtual method to provide a 'home' for this class.
638 virtual void anchor();
641 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<float>);
643 //--------------------------------------------------
644 // parser<std::string>
647 class parser<std::string> : public basic_parser<std::string> {
649 // parse - Return true on error.
650 bool parse(Option &O, const char *AN, const std::string &Arg,
651 std::string &Value) {
656 // getValueName - Overload in subclass to provide a better default value.
657 virtual const char *getValueName() const { return "string"; }
659 // An out-of-line virtual method to provide a 'home' for this class.
660 virtual void anchor();
663 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<std::string>);
665 //===----------------------------------------------------------------------===//
666 // applicator class - This class is used because we must use partial
667 // specialization to handle literal string arguments specially (const char* does
668 // not correctly respond to the apply method). Because the syntax to use this
669 // is a pain, we have the 'apply' method below to handle the nastiness...
671 template<class Mod> struct applicator {
673 static void opt(const Mod &M, Opt &O) { M.apply(O); }
676 // Handle const char* as a special case...
677 template<unsigned n> struct applicator<char[n]> {
679 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
681 template<unsigned n> struct applicator<const char[n]> {
683 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
685 template<> struct applicator<const char*> {
687 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
690 template<> struct applicator<NumOccurrences> {
691 static void opt(NumOccurrences NO, Option &O) { O.setNumOccurrencesFlag(NO); }
693 template<> struct applicator<ValueExpected> {
694 static void opt(ValueExpected VE, Option &O) { O.setValueExpectedFlag(VE); }
696 template<> struct applicator<OptionHidden> {
697 static void opt(OptionHidden OH, Option &O) { O.setHiddenFlag(OH); }
699 template<> struct applicator<FormattingFlags> {
700 static void opt(FormattingFlags FF, Option &O) { O.setFormattingFlag(FF); }
702 template<> struct applicator<MiscFlags> {
703 static void opt(MiscFlags MF, Option &O) { O.setMiscFlag(MF); }
706 // apply method - Apply a modifier to an option in a type safe way.
707 template<class Mod, class Opt>
708 void apply(const Mod &M, Opt *O) {
709 applicator<Mod>::opt(M, *O);
713 //===----------------------------------------------------------------------===//
716 // Default storage class definition: external storage. This implementation
717 // assumes the user will specify a variable to store the data into with the
718 // cl::location(x) modifier.
720 template<class DataType, bool ExternalStorage, bool isClass>
722 DataType *Location; // Where to store the object...
725 assert(Location != 0 && "cl::location(...) not specified for a command "
726 "line option with external storage, "
727 "or cl::init specified before cl::location()!!");
730 opt_storage() : Location(0) {}
732 bool setLocation(Option &O, DataType &L) {
734 return O.error(": cl::location(x) specified more than once!");
740 void setValue(const T &V) {
745 DataType &getValue() { check(); return *Location; }
746 const DataType &getValue() const { check(); return *Location; }
750 // Define how to hold a class type object, such as a string. Since we can
751 // inherit from a class, we do so. This makes us exactly compatible with the
752 // object in all cases that it is used.
754 template<class DataType>
755 class opt_storage<DataType,false,true> : public DataType {
758 void setValue(const T &V) { DataType::operator=(V); }
760 DataType &getValue() { return *this; }
761 const DataType &getValue() const { return *this; }
764 // Define a partial specialization to handle things we cannot inherit from. In
765 // this case, we store an instance through containment, and overload operators
766 // to get at the value.
768 template<class DataType>
769 class opt_storage<DataType, false, false> {
773 // Make sure we initialize the value with the default constructor for the
775 opt_storage() : Value(DataType()) {}
778 void setValue(const T &V) { Value = V; }
779 DataType &getValue() { return Value; }
780 DataType getValue() const { return Value; }
782 // If the datatype is a pointer, support -> on it.
783 DataType operator->() const { return Value; }
787 //===----------------------------------------------------------------------===//
788 // opt - A scalar command line option.
790 template <class DataType, bool ExternalStorage = false,
791 class ParserClass = parser<DataType> >
792 class opt : public Option,
793 public opt_storage<DataType, ExternalStorage,
794 is_class<DataType>::value> {
797 virtual bool handleOccurrence(unsigned pos, const char *ArgName,
798 const std::string &Arg) {
799 typename ParserClass::parser_data_type Val =
800 typename ParserClass::parser_data_type();
801 if (Parser.parse(*this, ArgName, Arg, Val))
802 return true; // Parse error!
808 virtual enum ValueExpected getValueExpectedFlagDefault() const {
809 return Parser.getValueExpectedFlagDefault();
811 virtual void getExtraOptionNames(std::vector<const char*> &OptionNames) {
812 return Parser.getExtraOptionNames(OptionNames);
815 // Forward printing stuff to the parser...
816 virtual unsigned getOptionWidth() const {return Parser.getOptionWidth(*this);}
817 virtual void printOptionInfo(unsigned GlobalWidth) const {
818 Parser.printOptionInfo(*this, GlobalWidth);
823 Parser.initialize(*this);
826 // setInitialValue - Used by the cl::init modifier...
827 void setInitialValue(const DataType &V) { this->setValue(V); }
829 ParserClass &getParser() { return Parser; }
831 operator DataType() const { return this->getValue(); }
834 DataType &operator=(const T &Val) {
836 return this->getValue();
841 opt(const M0t &M0) : Option(Optional | NotHidden) {
847 template<class M0t, class M1t>
848 opt(const M0t &M0, const M1t &M1) : Option(Optional | NotHidden) {
849 apply(M0, this); apply(M1, this);
854 template<class M0t, class M1t, class M2t>
855 opt(const M0t &M0, const M1t &M1,
856 const M2t &M2) : Option(Optional | NotHidden) {
857 apply(M0, this); apply(M1, this); apply(M2, this);
861 template<class M0t, class M1t, class M2t, class M3t>
862 opt(const M0t &M0, const M1t &M1, const M2t &M2,
863 const M3t &M3) : Option(Optional | NotHidden) {
864 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
868 template<class M0t, class M1t, class M2t, class M3t, class M4t>
869 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
870 const M4t &M4) : Option(Optional | NotHidden) {
871 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
876 template<class M0t, class M1t, class M2t, class M3t,
877 class M4t, class M5t>
878 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
879 const M4t &M4, const M5t &M5) : Option(Optional | NotHidden) {
880 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
881 apply(M4, this); apply(M5, this);
885 template<class M0t, class M1t, class M2t, class M3t,
886 class M4t, class M5t, class M6t>
887 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
888 const M4t &M4, const M5t &M5,
889 const M6t &M6) : Option(Optional | NotHidden) {
890 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
891 apply(M4, this); apply(M5, this); apply(M6, this);
895 template<class M0t, class M1t, class M2t, class M3t,
896 class M4t, class M5t, class M6t, class M7t>
897 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
898 const M4t &M4, const M5t &M5, const M6t &M6,
899 const M7t &M7) : Option(Optional | NotHidden) {
900 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
901 apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
906 EXTERN_TEMPLATE_INSTANTIATION(class opt<unsigned>);
907 EXTERN_TEMPLATE_INSTANTIATION(class opt<int>);
908 EXTERN_TEMPLATE_INSTANTIATION(class opt<std::string>);
909 EXTERN_TEMPLATE_INSTANTIATION(class opt<bool>);
911 //===----------------------------------------------------------------------===//
912 // list_storage class
914 // Default storage class definition: external storage. This implementation
915 // assumes the user will specify a variable to store the data into with the
916 // cl::location(x) modifier.
918 template<class DataType, class StorageClass>
920 StorageClass *Location; // Where to store the object...
923 list_storage() : Location(0) {}
925 bool setLocation(Option &O, StorageClass &L) {
927 return O.error(": cl::location(x) specified more than once!");
933 void addValue(const T &V) {
934 assert(Location != 0 && "cl::location(...) not specified for a command "
935 "line option with external storage!");
936 Location->push_back(V);
941 // Define how to hold a class type object, such as a string. Since we can
942 // inherit from a class, we do so. This makes us exactly compatible with the
943 // object in all cases that it is used.
945 template<class DataType>
946 class list_storage<DataType, bool> : public std::vector<DataType> {
949 void addValue(const T &V) { push_back(V); }
953 //===----------------------------------------------------------------------===//
954 // list - A list of command line options.
956 template <class DataType, class Storage = bool,
957 class ParserClass = parser<DataType> >
958 class list : public Option, public list_storage<DataType, Storage> {
959 std::vector<unsigned> Positions;
962 virtual enum ValueExpected getValueExpectedFlagDefault() const {
963 return Parser.getValueExpectedFlagDefault();
965 virtual void getExtraOptionNames(std::vector<const char*> &OptionNames) {
966 return Parser.getExtraOptionNames(OptionNames);
969 virtual bool handleOccurrence(unsigned pos, const char *ArgName,
970 const std::string &Arg) {
971 typename ParserClass::parser_data_type Val =
972 typename ParserClass::parser_data_type();
973 if (Parser.parse(*this, ArgName, Arg, Val))
974 return true; // Parse Error!
977 Positions.push_back(pos);
981 // Forward printing stuff to the parser...
982 virtual unsigned getOptionWidth() const {return Parser.getOptionWidth(*this);}
983 virtual void printOptionInfo(unsigned GlobalWidth) const {
984 Parser.printOptionInfo(*this, GlobalWidth);
989 Parser.initialize(*this);
992 ParserClass &getParser() { return Parser; }
994 unsigned getPosition(unsigned optnum) const {
995 assert(optnum < this->size() && "Invalid option index");
996 return Positions[optnum];
1001 list(const M0t &M0) : Option(ZeroOrMore | NotHidden) {
1006 template<class M0t, class M1t>
1007 list(const M0t &M0, const M1t &M1) : Option(ZeroOrMore | NotHidden) {
1008 apply(M0, this); apply(M1, this);
1012 template<class M0t, class M1t, class M2t>
1013 list(const M0t &M0, const M1t &M1, const M2t &M2)
1014 : Option(ZeroOrMore | NotHidden) {
1015 apply(M0, this); apply(M1, this); apply(M2, this);
1019 template<class M0t, class M1t, class M2t, class M3t>
1020 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3)
1021 : Option(ZeroOrMore | NotHidden) {
1022 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1026 template<class M0t, class M1t, class M2t, class M3t, class M4t>
1027 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1028 const M4t &M4) : Option(ZeroOrMore | NotHidden) {
1029 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1034 template<class M0t, class M1t, class M2t, class M3t,
1035 class M4t, class M5t>
1036 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1037 const M4t &M4, const M5t &M5) : Option(ZeroOrMore | NotHidden) {
1038 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1039 apply(M4, this); apply(M5, this);
1043 template<class M0t, class M1t, class M2t, class M3t,
1044 class M4t, class M5t, class M6t>
1045 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1046 const M4t &M4, const M5t &M5, const M6t &M6)
1047 : Option(ZeroOrMore | NotHidden) {
1048 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1049 apply(M4, this); apply(M5, this); apply(M6, this);
1053 template<class M0t, class M1t, class M2t, class M3t,
1054 class M4t, class M5t, class M6t, class M7t>
1055 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1056 const M4t &M4, const M5t &M5, const M6t &M6,
1057 const M7t &M7) : Option(ZeroOrMore | NotHidden) {
1058 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1059 apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
1064 //===----------------------------------------------------------------------===//
1065 // bits_storage class
1067 // Default storage class definition: external storage. This implementation
1068 // assumes the user will specify a variable to store the data into with the
1069 // cl::location(x) modifier.
1071 template<class DataType, class StorageClass>
1072 class bits_storage {
1073 unsigned *Location; // Where to store the bits...
1076 static unsigned Bit(const T &V) {
1077 unsigned BitPos = reinterpret_cast<unsigned>(V);
1078 assert(BitPos < sizeof(unsigned) * 8 &&
1079 "enum exceeds width of bit vector!");
1084 bits_storage() : Location(0) {}
1086 bool setLocation(Option &O, unsigned &L) {
1088 return O.error(": cl::location(x) specified more than once!");
1094 void addValue(const T &V) {
1095 assert(Location != 0 && "cl::location(...) not specified for a command "
1096 "line option with external storage!");
1097 *Location |= Bit(V);
1100 unsigned getBits() { return *Location; }
1103 bool isSet(const T &V) {
1104 return (*Location & Bit(V)) != 0;
1109 // Define how to hold bits. Since we can inherit from a class, we do so.
1110 // This makes us exactly compatible with the bits in all cases that it is used.
1112 template<class DataType>
1113 class bits_storage<DataType, bool> {
1114 unsigned Bits; // Where to store the bits...
1117 static unsigned Bit(const T &V) {
1118 unsigned BitPos = reinterpret_cast<unsigned>(V);
1119 assert(BitPos < sizeof(unsigned) * 8 &&
1120 "enum exceeds width of bit vector!");
1126 void addValue(const T &V) {
1130 unsigned getBits() { return Bits; }
1133 bool isSet(const T &V) {
1134 return (Bits & Bit(V)) != 0;
1139 //===----------------------------------------------------------------------===//
1140 // bits - A bit vector of command options.
1142 template <class DataType, class Storage = bool,
1143 class ParserClass = parser<DataType> >
1144 class bits : public Option, public bits_storage<DataType, Storage> {
1145 std::vector<unsigned> Positions;
1148 virtual enum ValueExpected getValueExpectedFlagDefault() const {
1149 return Parser.getValueExpectedFlagDefault();
1151 virtual void getExtraOptionNames(std::vector<const char*> &OptionNames) {
1152 return Parser.getExtraOptionNames(OptionNames);
1155 virtual bool handleOccurrence(unsigned pos, const char *ArgName,
1156 const std::string &Arg) {
1157 typename ParserClass::parser_data_type Val =
1158 typename ParserClass::parser_data_type();
1159 if (Parser.parse(*this, ArgName, Arg, Val))
1160 return true; // Parse Error!
1163 Positions.push_back(pos);
1167 // Forward printing stuff to the parser...
1168 virtual unsigned getOptionWidth() const {return Parser.getOptionWidth(*this);}
1169 virtual void printOptionInfo(unsigned GlobalWidth) const {
1170 Parser.printOptionInfo(*this, GlobalWidth);
1175 Parser.initialize(*this);
1178 ParserClass &getParser() { return Parser; }
1180 unsigned getPosition(unsigned optnum) const {
1181 assert(optnum < this->size() && "Invalid option index");
1182 return Positions[optnum];
1187 bits(const M0t &M0) : Option(ZeroOrMore | NotHidden) {
1192 template<class M0t, class M1t>
1193 bits(const M0t &M0, const M1t &M1) : Option(ZeroOrMore | NotHidden) {
1194 apply(M0, this); apply(M1, this);
1198 template<class M0t, class M1t, class M2t>
1199 bits(const M0t &M0, const M1t &M1, const M2t &M2)
1200 : Option(ZeroOrMore | NotHidden) {
1201 apply(M0, this); apply(M1, this); apply(M2, this);
1205 template<class M0t, class M1t, class M2t, class M3t>
1206 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3)
1207 : Option(ZeroOrMore | NotHidden) {
1208 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1212 template<class M0t, class M1t, class M2t, class M3t, class M4t>
1213 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1214 const M4t &M4) : Option(ZeroOrMore | NotHidden) {
1215 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1220 template<class M0t, class M1t, class M2t, class M3t,
1221 class M4t, class M5t>
1222 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1223 const M4t &M4, const M5t &M5) : Option(ZeroOrMore | NotHidden) {
1224 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1225 apply(M4, this); apply(M5, this);
1229 template<class M0t, class M1t, class M2t, class M3t,
1230 class M4t, class M5t, class M6t>
1231 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1232 const M4t &M4, const M5t &M5, const M6t &M6)
1233 : Option(ZeroOrMore | NotHidden) {
1234 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1235 apply(M4, this); apply(M5, this); apply(M6, this);
1239 template<class M0t, class M1t, class M2t, class M3t,
1240 class M4t, class M5t, class M6t, class M7t>
1241 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1242 const M4t &M4, const M5t &M5, const M6t &M6,
1243 const M7t &M7) : Option(ZeroOrMore | NotHidden) {
1244 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1245 apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
1250 //===----------------------------------------------------------------------===//
1251 // Aliased command line option (alias this name to a preexisting name)
1254 class alias : public Option {
1256 virtual bool handleOccurrence(unsigned pos, const char *ArgName,
1257 const std::string &Arg) {
1258 return AliasFor->handleOccurrence(pos, AliasFor->ArgStr, Arg);
1260 // Handle printing stuff...
1261 virtual unsigned getOptionWidth() const;
1262 virtual void printOptionInfo(unsigned GlobalWidth) const;
1266 error(": cl::alias must have argument name specified!");
1268 error(": cl::alias must have an cl::aliasopt(option) specified!");
1272 void setAliasFor(Option &O) {
1274 error(": cl::alias must only have one cl::aliasopt(...) specified!");
1280 alias(const M0t &M0) : Option(Optional | Hidden), AliasFor(0) {
1285 template<class M0t, class M1t>
1286 alias(const M0t &M0, const M1t &M1) : Option(Optional | Hidden), AliasFor(0) {
1287 apply(M0, this); apply(M1, this);
1291 template<class M0t, class M1t, class M2t>
1292 alias(const M0t &M0, const M1t &M1, const M2t &M2)
1293 : Option(Optional | Hidden), AliasFor(0) {
1294 apply(M0, this); apply(M1, this); apply(M2, this);
1298 template<class M0t, class M1t, class M2t, class M3t>
1299 alias(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3)
1300 : Option(Optional | Hidden), AliasFor(0) {
1301 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1306 // aliasfor - Modifier to set the option an alias aliases.
1309 aliasopt(Option &O) : Opt(O) {}
1310 void apply(alias &A) const { A.setAliasFor(Opt); }
1313 // extrahelp - provide additional help at the end of the normal help
1314 // output. All occurrences of cl::extrahelp will be accumulated and
1315 // printed to std::cerr at the end of the regular help, just before
1318 const char * morehelp;
1319 extrahelp(const char* help);
1322 void PrintVersionMessage();
1323 // This function just prints the help message, exactly the same way as if the
1324 // --help option had been given on the command line.
1325 // NOTE: THIS FUNCTION TERMINATES THE PROGRAM!
1326 void PrintHelpMessage();
1328 } // End namespace cl
1330 } // End namespace llvm