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"
32 /// cl Namespace - This namespace contains all of the command line option
33 /// processing machinery. It is intentionally a short name to make qualified
37 //===----------------------------------------------------------------------===//
38 // ParseCommandLineOptions - Command line option processing entry point.
40 void ParseCommandLineOptions(int &argc, char **argv,
41 const char *Overview = 0);
43 //===----------------------------------------------------------------------===//
44 // ParseEnvironmentOptions - Environment variable option processing alternate
47 void ParseEnvironmentOptions(const char *progName, const char *envvar,
48 const char *Overview = 0);
50 //===----------------------------------------------------------------------===//
51 // Flags permitted to be passed to command line arguments
54 enum 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
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.
69 OccurrencesMask = 0x07,
72 enum 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)
79 enum 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
86 // Formatting flags - This controls special features that the option might have
87 // that cause it to be parsed differently...
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
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
101 enum 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.
109 enum 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.
117 //===----------------------------------------------------------------------===//
122 friend void cl::ParseCommandLineOptions(int &, char **, const char *);
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.
129 virtual bool handleOccurrence(unsigned pos, const char *ArgName,
130 const std::string &Arg) = 0;
132 virtual enum NumOccurrences getNumOccurrencesFlagDefault() const {
135 virtual enum ValueExpected getValueExpectedFlagDefault() const {
136 return ValueOptional;
138 virtual enum OptionHidden getOptionHiddenFlagDefault() const {
141 virtual enum FormattingFlags getFormattingFlagDefault() const {
142 return NormalFormatting;
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
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
153 inline enum NumOccurrences getNumOccurrencesFlag() const {
154 int NO = Flags & OccurrencesMask;
155 return NO ? static_cast<enum NumOccurrences>(NO)
156 : getNumOccurrencesFlagDefault();
158 inline enum ValueExpected getValueExpectedFlag() const {
159 int VE = Flags & ValueMask;
160 return VE ? static_cast<enum ValueExpected>(VE)
161 : getValueExpectedFlagDefault();
163 inline enum OptionHidden getOptionHiddenFlag() const {
164 int OH = Flags & HiddenMask;
165 return OH ? static_cast<enum OptionHidden>(OH)
166 : getOptionHiddenFlagDefault();
168 inline enum FormattingFlags getFormattingFlag() const {
169 int OH = Flags & FormattingMask;
170 return OH ? static_cast<enum FormattingFlags>(OH)
171 : getFormattingFlagDefault();
173 inline unsigned getMiscFlags() const {
174 return Flags & MiscMask;
176 inline unsigned getPosition() const { return Position; }
178 // hasArgStr - Return true if the argstr != ""
179 bool hasArgStr() const { return ArgStr[0] != 0; }
181 //-------------------------------------------------------------------------===
182 // Accessor functions set by OptionModifiers
184 void setArgStr(const char *S) { ArgStr = S; }
185 void setDescription(const char *S) { HelpStr = S; }
186 void setValueStr(const char *S) { ValueStr = S; }
188 void setFlag(unsigned Flag, unsigned FlagMask) {
189 if (Flags & FlagMask) {
190 error(": Specified two settings for the same option!");
197 void setNumOccurrencesFlag(enum NumOccurrences Val) {
198 setFlag(Val, OccurrencesMask);
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; }
206 Option() : NumOccurrences(0), Flags(0), Position(0),
207 ArgStr(""), HelpStr(""), ValueStr("") {}
210 // addArgument - Tell the system that this Option subclass will handle all
211 // occurrences of -ArgStr on the command line.
213 void addArgument(const char *ArgStr);
214 void removeArgument(const char *ArgStr);
216 // Return the width of the option tag for printing...
217 virtual unsigned getOptionWidth() const = 0;
219 // printOptionInfo - Print out information about this option. The
220 // to-be-maintained width is specified.
222 virtual void printOptionInfo(unsigned GlobalWidth) const = 0;
224 // addOccurrence - Wrapper around handleOccurrence that enforces Flags
226 bool addOccurrence(unsigned pos, const char *ArgName,
227 const std::string &Value);
229 // Prints option name followed by message. Always returns true.
230 bool error(std::string Message, const char *ArgName = 0);
233 inline int getNumOccurrences() const { return NumOccurrences; }
238 //===----------------------------------------------------------------------===//
239 // Command line option modifiers that can be used to modify the behavior of
240 // command line option parsers...
243 // desc - Modifier to set the description shown in the --help output...
246 desc(const char *Str) : Desc(Str) {}
247 void apply(Option &O) const { O.setDescription(Desc); }
250 // value_desc - Modifier to set the value description shown in the --help
254 value_desc(const char *Str) : Desc(Str) {}
255 void apply(Option &O) const { O.setValueStr(Desc); }
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.
265 initializer(const Ty &Val) : Init(Val) {}
268 void apply(Opt &O) const { O.setInitialValue(Init); }
272 initializer<Ty> init(const Ty &Val) {
273 return initializer<Ty>(Val);
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.
282 struct LocationClass {
284 LocationClass(Ty &L) : Loc(L) {}
287 void apply(Opt &O) const { O.setLocation(O, Loc); }
291 LocationClass<Ty> location(Ty &L) { return LocationClass<Ty>(L); }
294 //===----------------------------------------------------------------------===//
295 // Enum valued command line option
297 #define clEnumVal(ENUMVAL, DESC) #ENUMVAL, (int)ENUMVAL, DESC
298 #define clEnumValN(ENUMVAL, FLAGNAME, DESC) FLAGNAME, (int)ENUMVAL, DESC
299 #define clEnumValEnd ((void*)0)
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.
306 template<class DataType>
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);
314 ValuesClass(const char *EnumName, DataType Val, const char *Desc,
316 // Insert the first value, which is required.
317 Values.push_back(std::make_pair(EnumName, std::make_pair(Val, Desc)));
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)));
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);
336 template<class DataType>
337 ValuesClass<DataType> values(const char *Arg, DataType Val, const char *Desc,
340 va_start(ValueArgs, Desc);
341 ValuesClass<DataType> Vals(Arg, Val, Desc, ValueArgs);
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.
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
359 struct generic_parser_base {
360 virtual ~generic_parser_base() {} // Base class should have virtual-dtor
362 // getNumOptions - Virtual function implemented by generic subclass to
363 // indicate how many entries are in Values.
365 virtual unsigned getNumOptions() const = 0;
367 // getOption - Return option name N.
368 virtual const char *getOption(unsigned N) const = 0;
370 // getDescription - Return description N
371 virtual const char *getDescription(unsigned N) const = 0;
373 // Return the width of the option tag for printing...
374 virtual unsigned getOptionWidth(const Option &O) const;
376 // printOptionInfo - Print out information about this option. The
377 // to-be-maintained width is specified.
379 virtual void printOptionInfo(const Option &O, unsigned GlobalWidth) const;
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.
385 hasArgStr = O.hasArgStr();
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
392 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
393 O.addArgument(getOption(i));
396 enum ValueExpected getValueExpectedFlagDefault() const {
397 // If there is an ArgStr specified, then we are of the form:
399 // -opt=O2 or -opt O2 or -optO2
401 // In which case, the value is required. Otherwise if an arg str has not
402 // been specified, we are of the form:
404 // -O2 or O2 or -la (where -l and -a are separate options)
406 // If this is the case, we cannot allow a value.
409 return ValueRequired;
411 return ValueDisallowed;
414 // findOption - Return the option number corresponding to the specified
415 // argument string. If the option is not found, getNumOptions() is returned.
417 unsigned findOption(const char *Name);
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.
429 template <class DataType>
430 class parser : public generic_parser_base {
432 std::vector<std::pair<const char *,
433 std::pair<DataType, const char *> > > Values;
435 typedef DataType parser_data_type;
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;
444 // parse - Return true on error.
445 bool parse(Option &O, const char *ArgName, const std::string &Arg,
453 for (unsigned i = 0, e = Values.size(); i != e; ++i)
454 if (ArgVal == Values[i].first) {
455 V = Values[i].second.first;
459 return O.error(": Cannot find option named '" + ArgVal + "'!");
462 // addLiteralOption - Add an entry to the mapping table...
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)));
470 // removeLiteralOption - Remove the specified option.
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);
479 //--------------------------------------------------
480 // basic_parser - Super class of parsers to provide boilerplate code
482 struct basic_parser_impl { // non-template implementation of basic_parser<t>
483 virtual ~basic_parser_impl() {}
485 enum ValueExpected getValueExpectedFlagDefault() const {
486 return ValueRequired;
489 void initialize(Option &O) {}
491 // Return the width of the option tag for printing...
492 unsigned getOptionWidth(const Option &O) const;
494 // printOptionInfo - Print out information about this option. The
495 // to-be-maintained width is specified.
497 void printOptionInfo(const Option &O, unsigned GlobalWidth) const;
499 // getValueName - Overload in subclass to provide a better default value.
500 virtual const char *getValueName() const { return "value"; }
503 // basic_parser - The real basic parser is just a template wrapper that provides
504 // a typedef for the provided data type.
506 template<class DataType>
507 struct basic_parser : public basic_parser_impl {
508 typedef DataType parser_data_type;
512 //--------------------------------------------------
516 class parser<bool> : public basic_parser<bool> {
518 // parse - Return true on error.
519 bool parse(Option &O, const char *ArgName, const std::string &Arg, bool &Val);
521 enum ValueExpected getValueExpectedFlagDefault() const {
522 return ValueOptional;
525 // getValueName - Do not print =<value> at all
526 virtual const char *getValueName() const { return 0; }
530 //--------------------------------------------------
534 class parser<int> : public basic_parser<int> {
536 // parse - Return true on error.
537 bool parse(Option &O, const char *ArgName, const std::string &Arg, int &Val);
539 // getValueName - Overload in subclass to provide a better default value.
540 virtual const char *getValueName() const { return "int"; }
544 //--------------------------------------------------
548 class parser<unsigned> : public basic_parser<unsigned> {
550 // parse - Return true on error.
551 bool parse(Option &O, const char *AN, const std::string &Arg, unsigned &Val);
553 // getValueName - Overload in subclass to provide a better default value.
554 virtual const char *getValueName() const { return "uint"; }
558 //--------------------------------------------------
562 class parser<double> : public basic_parser<double> {
564 // parse - Return true on error.
565 bool parse(Option &O, const char *AN, const std::string &Arg, double &Val);
567 // getValueName - Overload in subclass to provide a better default value.
568 virtual const char *getValueName() const { return "number"; }
572 //--------------------------------------------------
576 class parser<float> : public basic_parser<float> {
578 // parse - Return true on error.
579 bool parse(Option &O, const char *AN, const std::string &Arg, float &Val);
581 // getValueName - Overload in subclass to provide a better default value.
582 virtual const char *getValueName() const { return "number"; }
586 //--------------------------------------------------
587 // parser<std::string>
590 class parser<std::string> : public basic_parser<std::string> {
592 // parse - Return true on error.
593 bool parse(Option &O, const char *AN, const std::string &Arg,
594 std::string &Value) {
599 // getValueName - Overload in subclass to provide a better default value.
600 virtual const char *getValueName() const { return "string"; }
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...
609 template<class Mod> struct applicator {
611 static void opt(const Mod &M, Opt &O) { M.apply(O); }
614 // Handle const char* as a special case...
615 template<unsigned n> struct applicator<char[n]> {
617 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
619 template<unsigned n> struct applicator<const char[n]> {
621 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
623 template<> struct applicator<const char*> {
625 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
628 template<> struct applicator<NumOccurrences> {
629 static void opt(NumOccurrences NO, Option &O) { O.setNumOccurrencesFlag(NO); }
631 template<> struct applicator<ValueExpected> {
632 static void opt(ValueExpected VE, Option &O) { O.setValueExpectedFlag(VE); }
634 template<> struct applicator<OptionHidden> {
635 static void opt(OptionHidden OH, Option &O) { O.setHiddenFlag(OH); }
637 template<> struct applicator<FormattingFlags> {
638 static void opt(FormattingFlags FF, Option &O) { O.setFormattingFlag(FF); }
640 template<> struct applicator<MiscFlags> {
641 static void opt(MiscFlags MF, Option &O) { O.setMiscFlag(MF); }
644 // apply method - Apply a modifier to an option in a type safe way.
645 template<class Mod, class Opt>
646 void apply(const Mod &M, Opt *O) {
647 applicator<Mod>::opt(M, *O);
651 //===----------------------------------------------------------------------===//
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.
658 template<class DataType, bool ExternalStorage, bool isClass>
660 DataType *Location; // Where to store the object...
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()!!");
668 opt_storage() : Location(0) {}
670 bool setLocation(Option &O, DataType &L) {
672 return O.error(": cl::location(x) specified more than once!");
678 void setValue(const T &V) {
683 DataType &getValue() { check(); return *Location; }
684 const DataType &getValue() const { check(); return *Location; }
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.
692 template<class DataType>
693 class opt_storage<DataType,false,true> : public DataType {
696 void setValue(const T &V) { DataType::operator=(V); }
698 DataType &getValue() { return *this; }
699 const DataType &getValue() const { return *this; }
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.
706 template<class DataType>
707 class opt_storage<DataType, false, false> {
711 // Make sure we initialize the value with the default constructor for the
713 opt_storage() : Value(DataType()) {}
716 void setValue(const T &V) { Value = V; }
717 DataType &getValue() { return Value; }
718 DataType getValue() const { return Value; }
720 // If the datatype is a pointer, support -> on it.
721 DataType operator->() const { return Value; }
725 //===----------------------------------------------------------------------===//
726 // opt - A scalar command line option.
728 template <class DataType, bool ExternalStorage = false,
729 class ParserClass = parser<DataType> >
730 class opt : public Option,
731 public opt_storage<DataType, ExternalStorage,
732 is_class<DataType>::value> {
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!
745 virtual enum ValueExpected getValueExpectedFlagDefault() const {
746 return Parser.getValueExpectedFlagDefault();
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);
757 Parser.initialize(*this);
760 // setInitialValue - Used by the cl::init modifier...
761 void setInitialValue(const DataType &V) { this->setValue(V); }
763 ParserClass &getParser() { return Parser; }
765 operator DataType() const { return this->getValue(); }
768 DataType &operator=(const T &Val) {
770 return this->getValue();
781 template<class M0t, class M1t>
782 opt(const M0t &M0, const M1t &M1) {
783 apply(M0, this); apply(M1, this);
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);
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);
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,
803 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
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);
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);
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);
836 //===----------------------------------------------------------------------===//
837 // list_storage class
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.
843 template<class DataType, class StorageClass>
845 StorageClass *Location; // Where to store the object...
848 list_storage() : Location(0) {}
850 bool setLocation(Option &O, StorageClass &L) {
852 return O.error(": cl::location(x) specified more than once!");
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);
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.
870 template<class DataType>
871 class list_storage<DataType, bool> : public std::vector<DataType> {
874 void addValue(const T &V) { push_back(V); }
878 //===----------------------------------------------------------------------===//
879 // list - A list of command line options.
881 template <class DataType, class Storage = bool,
882 class ParserClass = parser<DataType> >
883 class list : public Option, public list_storage<DataType, Storage> {
884 std::vector<unsigned> Positions;
887 virtual enum NumOccurrences getNumOccurrencesFlagDefault() const {
890 virtual enum ValueExpected getValueExpectedFlagDefault() const {
891 return Parser.getValueExpectedFlagDefault();
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!
901 Positions.push_back(pos);
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);
913 Parser.initialize(*this);
916 ParserClass &getParser() { return Parser; }
918 unsigned getPosition(unsigned optnum) {
919 assert(optnum < this->size() && "Invalid option index");
920 return Positions[optnum];
925 list(const M0t &M0) {
930 template<class M0t, class M1t>
931 list(const M0t &M0, const M1t &M1) {
932 apply(M0, this); apply(M1, this);
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);
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);
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,
951 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
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);
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);
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);
984 //===----------------------------------------------------------------------===//
985 // Aliased command line option (alias this name to a preexisting name)
988 class alias : public Option {
990 virtual bool handleOccurrence(unsigned pos, const char *ArgName,
991 const std::string &Arg) {
992 return AliasFor->handleOccurrence(pos, AliasFor->ArgStr, Arg);
994 // Aliases default to be hidden...
995 virtual enum OptionHidden getOptionHiddenFlagDefault() const {return Hidden;}
997 // Handle printing stuff...
998 virtual unsigned getOptionWidth() const;
999 virtual void printOptionInfo(unsigned GlobalWidth) const;
1003 error(": cl::alias must have argument name specified!");
1005 error(": cl::alias must have an cl::aliasopt(option) specified!");
1006 addArgument(ArgStr);
1009 void setAliasFor(Option &O) {
1011 error(": cl::alias must only have one cl::aliasopt(...) specified!");
1017 alias(const M0t &M0) : AliasFor(0) {
1022 template<class M0t, class M1t>
1023 alias(const M0t &M0, const M1t &M1) : AliasFor(0) {
1024 apply(M0, this); apply(M1, this);
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);
1034 template<class M0t, class M1t, class M2t, class M3t>
1035 alias(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3)
1037 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1042 // aliasfor - Modifier to set the option an alias aliases.
1045 aliasopt(Option &O) : Opt(O) {}
1046 void apply(alias &A) const { A.setAliasFor(Opt); }
1049 // extrahelp - provide additional help at the end of the normal help
1050 // output. All occurrences of cl::extrahelp will be accumulated and
1051 // printed to std::cerr at the end of the regular help, just before
1054 const char * morehelp;
1055 extrahelp(const char* help);
1058 // This function just prints the help message, exactly the same way as if the
1059 // --help option had been given on the command line.
1060 // NOTE: THIS FUNCTION TERMINATES THE PROGRAM!
1061 void PrintHelpMessage();
1063 } // End namespace cl
1065 } // End namespace llvm