1 //===-- llvm/Tools/CommandLine.h - Command line parser for tools -*- C++ -*--=//
3 // This class implements a command line argument processor that is useful when
4 // creating a tool. It provides a simple, minimalistic interface that is easily
5 // extensible and supports nonlocal (library) command line options.
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_TOOLS_COMMANDLINE_H
10 #define LLVM_TOOLS_COMMANDLINE_H
17 namespace cl { // Short namespace to make usage concise
19 //===----------------------------------------------------------------------===//
20 // ParseCommandLineOptions - Minimalistic command line option processing entry
22 void cl::ParseCommandLineOptions(int &argc, char **argv,
23 const char *Overview = 0);
26 //===----------------------------------------------------------------------===//
27 // Global flags permitted to be passed to command line arguments
30 NoFlags = 0x00, // Marker to make explicit that we have no flags
32 // Flags for the number of occurances allowed...
33 Optional = 0x00, // Zero or One occurance
34 ZeroOrMore = 0x01, // Zero or more occurances allowed
35 Required = 0x02, // One occurance required
36 OneOrMore = 0x03, // One or more occurances required
37 OccurancesMask = 0x07,
39 // Number of arguments to a value expected...
40 //Optional = 0x00, // The value can oppear... or not
41 ValueRequired = 0x08, // The value is required to appear!
42 ValueDisallowed = 0x10, // A value may not be specified (for flags)
45 // Control whether -help shows the command line option...
46 Hidden = 0x20, // -help doesn't -help-hidden does
47 ReallyHidden = 0x60, // Neither -help nor -help-hidden show this arg
52 //===----------------------------------------------------------------------===//
56 friend void cl::ParseCommandLineOptions(int &, char **, const char *Overview);
58 // handleOccurances - Overriden by subclasses to handle the value passed into
59 // an argument. Should return true if there was an error processing the
60 // argument and the program should exit.
62 virtual bool handleOccurance(const char *ArgName, const string &Arg) = 0;
64 int NumOccurances; // The number of times specified
66 const char * const ArgStr; // The argument string itself (ex: "help", "o")
67 const char * const HelpStr; // The descriptive text message for --help
68 const int Flags; // Flags for the argument
71 Option(const char *ArgStr, const char *Message, int Flags);
72 Option(int flags) : ArgStr(""), HelpStr(""), Flags(flags) {}
74 // Prints option name followed by message. Always returns true.
75 bool error(string Message, const char *ArgName = 0);
77 // addOccurance - Wrapper around handleOccurance that enforces Flags
79 bool addOccurance(const char *ArgName, const string &Value);
82 // Return the width of the option tag for printing...
83 virtual unsigned getOptionWidth() const;
85 // printOptionInfo - Print out information about this option. The
86 // to-be-maintained width is specified.
88 virtual void printOptionInfo(unsigned GlobalWidth) const;
91 inline int getNumOccurances() const { return NumOccurances; }
96 //===----------------------------------------------------------------------===//
97 // Boolean/flag command line option
99 class Flag : public Option {
101 virtual bool handleOccurance(const char *ArgName, const string &Arg);
103 inline Flag(const char *ArgStr, const char *Message, int Flags = 0,
104 bool DefaultVal = 0) : Option(ArgStr, Message, Flags),
106 operator bool() const { return Value; }
107 inline bool getValue() const { return Value; }
108 inline void setValue(bool Val) { Value = Val; }
113 //===----------------------------------------------------------------------===//
114 // Integer valued command line option
116 class Int : public Option {
118 virtual bool handleOccurance(const char *ArgName, const string &Arg);
120 inline Int(const char *ArgStr, const char *Help, int Flags = 0,
121 int DefaultVal = 0) : Option(ArgStr, Help, Flags | ValueRequired),
123 inline operator int() const { return Value; }
124 inline int getValue() const { return Value; }
125 inline void setValue(int Val) { Value = Val; }
129 //===----------------------------------------------------------------------===//
130 // String valued command line option
132 class String : public Option {
134 virtual bool handleOccurance(const char *ArgName, const string &Arg);
136 inline String(const char *ArgStr, const char *Help, int Flags = 0,
137 const char *DefaultVal = "")
138 : Option(ArgStr, Help, Flags | ValueRequired), Value(DefaultVal) {}
140 inline const string &getValue() const { return Value; }
141 inline void setValue(const string &Val) { Value = Val; }
145 //===----------------------------------------------------------------------===//
146 // Enum valued command line option
148 #define clEnumVal(ENUMVAL, DESC) #ENUMVAL, ENUMVAL, DESC
149 #define clEnumValN(ENUMVAL, FLAGNAME, DESC) FLAGNAME, ENUMVAL, DESC
151 // EnumBase - Base class for all enum/varargs related argument types...
152 class EnumBase : public Option {
154 // Use a vector instead of a map, because the lists should be short,
155 // the overhead is less, and most importantly, it keeps them in the order
156 // inserted so we can print our option out nicely.
157 vector<pair<const char *, pair<int, const char *> > > ValueMap;
159 inline EnumBase(const char *ArgStr, const char *Help, int Flags)
160 : Option(ArgStr, Help, Flags) {}
161 inline EnumBase(int Flags) : Option(Flags) {}
163 // processValues - Incorporate the specifed varargs arglist into the
166 void processValues(va_list Vals);
168 // registerArgs - notify the system about these new arguments
172 // Turn an enum into the arg name that activates it
173 const char *getArgName(int ID) const;
174 const char *getArgDescription(int ID) const;
177 class EnumValueBase : public EnumBase {
180 inline EnumValueBase(const char *ArgStr, const char *Help, int Flags)
181 : EnumBase(ArgStr, Help, Flags) {}
182 inline EnumValueBase(int Flags) : EnumBase(Flags) {}
184 // handleOccurance - Set Value to the enum value specified by Arg
185 virtual bool handleOccurance(const char *ArgName, const string &Arg);
187 // Return the width of the option tag for printing...
188 virtual unsigned getOptionWidth() const;
190 // printOptionInfo - Print out information about this option. The
191 // to-be-maintained width is specified.
193 virtual void printOptionInfo(unsigned GlobalWidth) const;
196 template <class E> // The enum we are representing
197 class Enum : public EnumValueBase {
199 inline Enum(const char *ArgStr, int Flags, const char *Help, ...)
200 : EnumValueBase(ArgStr, Help, Flags | ValueRequired) {
202 va_start(Values, Help);
203 processValues(Values);
205 Value = ValueMap.front().second.first; // Grab default value
208 inline E getValue() const { return (E)Value; }
209 inline void setValue(E Val) { Value = (E)Val; }
213 //===----------------------------------------------------------------------===//
214 // Enum flags command line option
216 class EnumFlagsBase : public EnumValueBase {
218 virtual bool handleOccurance(const char *ArgName, const string &Arg);
219 inline EnumFlagsBase(int Flags) : EnumValueBase(Flags | ValueDisallowed) {}
221 // Return the width of the option tag for printing...
222 virtual unsigned getOptionWidth() const;
224 // printOptionInfo - Print out information about this option. The
225 // to-be-maintained width is specified.
227 virtual void printOptionInfo(unsigned GlobalWidth) const;
230 template <class E> // The enum we are representing
231 class EnumFlags : public EnumFlagsBase {
233 inline EnumFlags(int Flags, ...) : EnumFlagsBase(Flags) {
235 va_start(Values, Flags);
236 processValues(Values);
239 Value = ValueMap.front().second.first; // Grab default value
241 inline E getValue() const { return (E)Value; }
242 inline void setValue(E Val) { Value = (E)Val; }
246 //===----------------------------------------------------------------------===//
247 // Enum list command line option
249 class EnumListBase : public EnumBase {
251 vector<int> Values; // The options specified so far.
253 inline EnumListBase(int Flags)
254 : EnumBase(Flags | ValueDisallowed | ZeroOrMore) {}
255 virtual bool handleOccurance(const char *ArgName, const string &Arg);
257 // Return the width of the option tag for printing...
258 virtual unsigned getOptionWidth() const;
260 // printOptionInfo - Print out information about this option. The
261 // to-be-maintained width is specified.
263 virtual void printOptionInfo(unsigned GlobalWidth) const;
265 inline unsigned size() { return Values.size(); }
268 template <class E> // The enum we are representing
269 class EnumList : public EnumListBase {
271 inline EnumList(int Flags, ...) : EnumListBase(Flags) {
273 va_start(Values, Flags);
274 processValues(Values);
278 inline E getValue(unsigned i) const { return (E)Values[i]; }
279 inline E operator[](unsigned i) const { return (E)Values[i]; }
282 } // End namespace cl