llvm.org GIT mirror llvm / dbab15a
Initial checkin git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@278 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 18 years ago
2 changed file(s) with 798 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 //===-- CommandLine.cpp - Command line parser implementation --------------===//
1 //
2 // This class implements a command line argument processor that is useful when
3 // creating a tool. It provides a simple, minimalistic interface that is easily
4 // extensible and supports nonlocal (library) command line options.
5 //
6 //===----------------------------------------------------------------------===//
7
8 #include "llvm/Tools/CommandLine.h"
9 #include "llvm/Tools/STLExtras.h"
10 #include
11 #include
12 #include
13 #include
14 using namespace cl;
15
16 // Return the global command line option vector. Making it a function scoped
17 // static ensures that it will be initialized before its first use correctly.
18 //
19 static map &getOpts() {
20 static map CommandLineOptions;
21 return CommandLineOptions;
22 }
23
24 static void AddArgument(const string &ArgName, Option *Opt) {
25 if (getOpts().find(ArgName) != getOpts().end()) {
26 cerr << "CommandLine Error: Argument '" << ArgName
27 << "' specified more than once!\n";
28 } else {
29 getOpts()[ArgName] = Opt; // Add argument to the argument map!
30 }
31 }
32
33 static const char *ProgramName = 0;
34 static const char *ProgramOverview = 0;
35
36 void cl::ParseCommandLineOptions(int &argc, char **argv,
37 const char *Overview = 0) {
38 ProgramName = argv[0]; // Save this away safe and snug
39 ProgramOverview = Overview;
40 bool ErrorParsing = false;
41
42 // Loop over all of the arguments... processing them.
43 for (int i = 1; i < argc; ++i) {
44 Option *Handler = 0;
45 const char *Value = "";
46 const char *ArgName = "";
47 if (argv[i][0] != '-') { // Unnamed argument?
48 Handler = getOpts()[""];
49 Value = argv[i];
50 } else { // We start with a - or --, eat dashes
51 ArgName = argv[i]+1;
52 while (*ArgName == '-') ++ArgName; // Eat leading dashes
53
54 const char *ArgNameEnd = ArgName;
55 while (*ArgNameEnd && *ArgNameEnd != '=') ++ArgNameEnd; // Scan till end
56
57 Value = ArgNameEnd;
58 if (*Value) // If we have an equals sign...
59 ++Value; // Advance to value...
60
61 if (*ArgName != 0) {
62 string ArgNameStr(ArgName, ArgNameEnd); // Extract arg name part
63 Handler = getOpts()[ArgNameStr];
64 }
65 }
66
67 if (Handler == 0) {
68 cerr << "Unknown command line argument '" << argv[i] << "'. Try: "
69 << argv[0] << " --help\n'";
70 ErrorParsing = true;
71 continue;
72 }
73
74 // Enforce value requirements
75 switch (Handler->Flags & ValueMask) {
76 case ValueRequired:
77 if (Value == 0 || *Value == 0) { // No value specified?
78 if (i+1 < argc) { // Steal the next argument, like for '-o filename'
79 Value = argv[++i];
80 } else {
81 ErrorParsing = Handler->error(" requires a value!");
82 continue;
83 }
84 }
85 break;
86 case ValueDisallowed:
87 if (*Value != 0) {
88 ErrorParsing = Handler->error(" does not allow a value! '" +
89 string(Value) + "' specified.");
90 continue;
91 }
92 break;
93 case Optional: break;
94 default: cerr << "Bad ValueMask flag! CommandLine usage error!\n"; abort();
95 }
96
97 // Run the handler now!
98 ErrorParsing |= Handler->addOccurance(ArgName, Value);
99 }
100
101 // TODO: loop over args and make sure all required args are specified!
102
103 // Free all of the memory allocated to the vector. Command line options may
104 // only be processed once!
105 getOpts().clear();
106
107 // If we had an error processing our arguments, don't let the program execute
108 if (ErrorParsing) exit(1);
109 }
110
111 //===----------------------------------------------------------------------===//
112 // Option Base class implementation
113 //
114 Option::Option(const char *argStr, const char *helpStr, int flags)
115 : NumOccurances(0), ArgStr(argStr), HelpStr(helpStr), Flags(flags) {
116 AddArgument(ArgStr, this);
117 }
118
119 bool Option::error(string Message, const char *ArgName = 0) {
120 if (ArgName == 0) ArgName = ArgStr;
121 cerr << "-" << ArgName << " option" << Message << endl;
122 return true;
123 }
124
125 bool Option::addOccurance(const char *ArgName, const string &Value) {
126 NumOccurances++; // Increment the number of times we have been seen
127
128 switch (Flags & OccurancesMask) {
129 case Optional:
130 if (NumOccurances > 1)
131 return error(": may only occur zero or one times!", ArgName);
132 break;
133 case Required:
134 if (NumOccurances > 1)
135 return error(": must occur exactly one time!", ArgName);
136 // Fall through
137 case OneOrMore:
138 case ZeroOrMore: break;
139 default: return error(": bad num occurances flag value!");
140 }
141
142 return handleOccurance(ArgName, Value);
143 }
144
145 // Return the width of the option tag for printing...
146 unsigned Option::getOptionWidth() const {
147 return std::strlen(ArgStr)+6;
148 }
149
150 void Option::printOptionInfo(unsigned GlobalWidth) const {
151 unsigned L = std::strlen(ArgStr);
152 if (L == 0) return; // Don't print the empty arg like this!
153 cerr << " -" << ArgStr << string(GlobalWidth-L-6, ' ') << " - "
154 << HelpStr << endl;
155 }
156
157
158 //===----------------------------------------------------------------------===//
159 // Boolean/flag command line option implementation
160 //
161
162 bool Flag::handleOccurance(const char *ArgName, const string &Arg) {
163 if (Arg == "" || Arg == "true" || Arg == "TRUE" || Arg == "True" ||
164 Arg == "1") {
165 Value = true;
166 } else if (Arg == "false" || Arg == "FALSE" || Arg == "False" || Arg == "0") {
167 Value = false;
168 } else {
169 return error(": '" + Arg + "' is invalid value for boolean argument! Try 0 or 1");
170 }
171
172 return false;
173 }
174
175 //===----------------------------------------------------------------------===//
176 // Integer valued command line option implementation
177 //
178 bool Int::handleOccurance(const char *ArgName, const string &Arg) {
179 const char *ArgStart = Arg.c_str();
180 char *End;
181 Value = (int)strtol(ArgStart, &End, 0);
182 if (*End != 0)
183 return error(": '" + Arg + "' value invalid for integer argument!");
184 return false;
185 }
186
187 //===----------------------------------------------------------------------===//
188 // String valued command line option implementation
189 //
190 bool String::handleOccurance(const char *ArgName, const string &Arg) {
191 Value = Arg;
192 return false;
193 }
194
195 //===----------------------------------------------------------------------===//
196 // Enum valued command line option implementation
197 //
198 void EnumBase::processValues(va_list Vals) {
199 while (const char *EnumName = va_arg(Vals, const char *)) {
200 int EnumVal = va_arg(Vals, int);
201 const char *EnumDesc = va_arg(Vals, const char *);
202 ValueMap.push_back(make_pair(EnumName, // Add value to value map
203 make_pair(EnumVal, EnumDesc)));
204 }
205 }
206
207 // registerArgs - notify the system about these new arguments
208 void EnumBase::registerArgs() {
209 for (unsigned i = 0; i < ValueMap.size(); ++i)
210 AddArgument(ValueMap[i].first, this);
211 }
212
213 const char *EnumBase::getArgName(int ID) const {
214 for (unsigned i = 0; i < ValueMap.size(); ++i)
215 if (ID == ValueMap[i].second.first) return ValueMap[i].first;
216 return "";
217 }
218 const char *EnumBase::getArgDescription(int ID) const {
219 for (unsigned i = 0; i < ValueMap.size(); ++i)
220 if (ID == ValueMap[i].second.first) return ValueMap[i].second.second;
221 return "";
222 }
223
224
225
226 bool EnumValueBase::handleOccurance(const char *ArgName, const string &Arg) {
227 unsigned i;
228 for (i = 0; i < ValueMap.size(); ++i)
229 if (ValueMap[i].first == Arg) break;
230 if (i == ValueMap.size())
231 return error(": unrecognized alternative '"+Arg+"'!");
232 Value = ValueMap[i].second.first;
233 return false;
234 }
235
236 // Return the width of the option tag for printing...
237 unsigned EnumValueBase::getOptionWidth() const {
238 unsigned BaseSize = Option::getOptionWidth();
239 for (unsigned i = 0; i < ValueMap.size(); ++i)
240 BaseSize = max(BaseSize, std::strlen(ValueMap[i].first)+8);
241 return BaseSize;
242 }
243
244 // printOptionInfo - Print out information about this option. The
245 // to-be-maintained width is specified.
246 //
247 void EnumValueBase::printOptionInfo(unsigned GlobalWidth) const {
248 Option::printOptionInfo(GlobalWidth);
249 for (unsigned i = 0; i < ValueMap.size(); ++i) {
250 unsigned NumSpaces = GlobalWidth-strlen(ValueMap[i].first)-8;
251 cerr << " =" << ValueMap[i].first << string(NumSpaces, ' ') << " - "
252 << ValueMap[i].second.second;
253
254 if (i == 0) cerr << " (default)";
255 cerr << endl;
256 }
257 }
258
259 //===----------------------------------------------------------------------===//
260 // Enum flags command line option implementation
261 //
262
263 bool EnumFlagsBase::handleOccurance(const char *ArgName, const string &Arg) {
264 return EnumValueBase::handleOccurance("", ArgName);
265 }
266
267 unsigned EnumFlagsBase::getOptionWidth() const {
268 unsigned BaseSize = 0;
269 for (unsigned i = 0; i < ValueMap.size(); ++i)
270 BaseSize = max(BaseSize, std::strlen(ValueMap[i].first)+6);
271 return BaseSize;
272 }
273
274 void EnumFlagsBase::printOptionInfo(unsigned GlobalWidth) const {
275 for (unsigned i = 0; i < ValueMap.size(); ++i) {
276 unsigned L = std::strlen(ValueMap[i].first);
277 cerr << " -" << ValueMap[i].first << string(GlobalWidth-L-6, ' ') << " - "
278 << ValueMap[i].second.second;
279 if (i == 0) cerr << " (default)";
280 cerr << endl;
281 }
282 }
283
284
285 //===----------------------------------------------------------------------===//
286 // Enum list command line option implementation
287 //
288
289 bool EnumListBase::handleOccurance(const char *ArgName, const string &Arg) {
290 unsigned i;
291 for (i = 0; i < ValueMap.size(); ++i)
292 if (ValueMap[i].first == string(ArgName)) break;
293 if (i == ValueMap.size())
294 return error(": CommandLine INTERNAL ERROR", ArgName);
295 Values.push_back(ValueMap[i].second.first);
296 return false;
297 }
298
299 // Return the width of the option tag for printing...
300 unsigned EnumListBase::getOptionWidth() const {
301 unsigned BaseSize = 0;
302 for (unsigned i = 0; i < ValueMap.size(); ++i)
303 BaseSize = max(BaseSize, std::strlen(ValueMap[i].first)+6);
304 return BaseSize;
305 }
306
307
308 // printOptionInfo - Print out information about this option. The
309 // to-be-maintained width is specified.
310 //
311 void EnumListBase::printOptionInfo(unsigned GlobalWidth) const {
312 for (unsigned i = 0; i < ValueMap.size(); ++i) {
313 unsigned L = std::strlen(ValueMap[i].first);
314 cerr << " -" << ValueMap[i].first << string(GlobalWidth-L-6, ' ') << " - "
315 << ValueMap[i].second.second << endl;
316 }
317 }
318
319
320 //===----------------------------------------------------------------------===//
321 // Help option... always automatically provided.
322 //
323 namespace {
324
325 // isHidden/isReallyHidden - Predicates to be used to filter down arg lists.
326 inline bool isHidden(pair &OptPair) {
327 return (OptPair.second->Flags & HiddenMask) == Hidden;
328 }
329 inline bool isReallyHidden(pair &OptPair) {
330 return (OptPair.second->Flags & HiddenMask) == ReallyHidden;
331 }
332
333 class Help : public Option {
334 unsigned MaxArgLen;
335 const Option *EmptyArg;
336 const bool ShowHidden;
337
338 virtual bool handleOccurance(const char *ArgName, const string &Arg) {
339 // Copy Options into a vector so we can sort them as we like...
340 vector > Options;
341 copy(getOpts().begin(), getOpts().end(), back_inserter(Options));
342
343 // Eliminate Hidden or ReallyHidden arguments, depending on ShowHidden
344 Options.erase(remove_if(Options.begin(), Options.end(),
345 ptr_fun(ShowHidden ? isReallyHidden : isHidden)),
346 Options.end());
347
348 // Eliminate duplicate entries in table (from enum flags options, f.e.)
349 set OptionSet;
350 for (unsigned i = 0; i < Options.size(); )
351 if (OptionSet.count(Options[i].second) == 0)
352 OptionSet.insert(Options[i++].second); // Add to set
353 else
354 Options.erase(Options.begin()+i); // Erase duplicate
355
356
357 if (ProgramOverview)
358 cerr << "OVERVIEW:" << ProgramOverview << endl;
359 // TODO: Sort options by some criteria
360
361 cerr << "USAGE: " << ProgramName << " [options]\n\n";
362 // TODO: print usage nicer
363
364 // Compute the maximum argument length...
365 MaxArgLen = 0;
366 for_each(Options.begin(), Options.end(),
367 bind_obj(this, &Help::getMaxArgLen));
368
369 cerr << "OPTIONS:\n";
370 for_each(Options.begin(), Options.end(),
371 bind_obj(this, &Help::printOption));
372
373 return true; // Displaying help is cause to terminate the program
374 }
375
376 void getMaxArgLen(pair OptPair) {
377 const Option *Opt = OptPair.second;
378 if (Opt->ArgStr[0] == 0) EmptyArg = Opt; // Capture the empty arg if exists
379 MaxArgLen = max(MaxArgLen, Opt->getOptionWidth());
380 }
381
382 void printOption(pair OptPair) {
383 const Option *Opt = OptPair.second;
384 Opt->printOptionInfo(MaxArgLen);
385 }
386
387 public:
388 inline Help(const char *ArgVal, const char *HelpVal, bool showHidden)
389 : Option(ArgVal, HelpVal, showHidden ? Hidden : 0), ShowHidden(showHidden) {
390 EmptyArg = 0;
391 }
392 };
393
394 Help HelpOp("help", "display available options"
395 " (-help-hidden for more)", false);
396 Help HelpHiddenOpt("help-hidden", "display all available options", true);
397
398 } // End anonymous namespace
0 //===-- CommandLine.cpp - Command line parser implementation --------------===//
1 //
2 // This class implements a command line argument processor that is useful when
3 // creating a tool. It provides a simple, minimalistic interface that is easily
4 // extensible and supports nonlocal (library) command line options.
5 //
6 //===----------------------------------------------------------------------===//
7
8 #include "llvm/Tools/CommandLine.h"
9 #include "llvm/Tools/STLExtras.h"
10 #include
11 #include
12 #include
13 #include
14 using namespace cl;
15
16 // Return the global command line option vector. Making it a function scoped
17 // static ensures that it will be initialized before its first use correctly.
18 //
19 static map &getOpts() {
20 static map CommandLineOptions;
21 return CommandLineOptions;
22 }
23
24 static void AddArgument(const string &ArgName, Option *Opt) {
25 if (getOpts().find(ArgName) != getOpts().end()) {
26 cerr << "CommandLine Error: Argument '" << ArgName
27 << "' specified more than once!\n";
28 } else {
29 getOpts()[ArgName] = Opt; // Add argument to the argument map!
30 }
31 }
32
33 static const char *ProgramName = 0;
34 static const char *ProgramOverview = 0;
35
36 void cl::ParseCommandLineOptions(int &argc, char **argv,
37 const char *Overview = 0) {
38 ProgramName = argv[0]; // Save this away safe and snug
39 ProgramOverview = Overview;
40 bool ErrorParsing = false;
41
42 // Loop over all of the arguments... processing them.
43 for (int i = 1; i < argc; ++i) {
44 Option *Handler = 0;
45 const char *Value = "";
46 const char *ArgName = "";
47 if (argv[i][0] != '-') { // Unnamed argument?
48 Handler = getOpts()[""];
49 Value = argv[i];
50 } else { // We start with a - or --, eat dashes
51 ArgName = argv[i]+1;
52 while (*ArgName == '-') ++ArgName; // Eat leading dashes
53
54 const char *ArgNameEnd = ArgName;
55 while (*ArgNameEnd && *ArgNameEnd != '=') ++ArgNameEnd; // Scan till end
56
57 Value = ArgNameEnd;
58 if (*Value) // If we have an equals sign...
59 ++Value; // Advance to value...
60
61 if (*ArgName != 0) {
62 string ArgNameStr(ArgName, ArgNameEnd); // Extract arg name part
63 Handler = getOpts()[ArgNameStr];
64 }
65 }
66
67 if (Handler == 0) {
68 cerr << "Unknown command line argument '" << argv[i] << "'. Try: "
69 << argv[0] << " --help\n'";
70 ErrorParsing = true;
71 continue;
72 }
73
74 // Enforce value requirements
75 switch (Handler->Flags & ValueMask) {
76 case ValueRequired:
77 if (Value == 0 || *Value == 0) { // No value specified?
78 if (i+1 < argc) { // Steal the next argument, like for '-o filename'
79 Value = argv[++i];
80 } else {
81 ErrorParsing = Handler->error(" requires a value!");
82 continue;
83 }
84 }
85 break;
86 case ValueDisallowed:
87 if (*Value != 0) {
88 ErrorParsing = Handler->error(" does not allow a value! '" +
89 string(Value) + "' specified.");
90 continue;
91 }
92 break;
93 case Optional: break;
94 default: cerr << "Bad ValueMask flag! CommandLine usage error!\n"; abort();
95 }
96
97 // Run the handler now!
98 ErrorParsing |= Handler->addOccurance(ArgName, Value);
99 }
100
101 // TODO: loop over args and make sure all required args are specified!
102
103 // Free all of the memory allocated to the vector. Command line options may
104 // only be processed once!
105 getOpts().clear();
106
107 // If we had an error processing our arguments, don't let the program execute
108 if (ErrorParsing) exit(1);
109 }
110
111 //===----------------------------------------------------------------------===//
112 // Option Base class implementation
113 //
114 Option::Option(const char *argStr, const char *helpStr, int flags)
115 : NumOccurances(0), ArgStr(argStr), HelpStr(helpStr), Flags(flags) {
116 AddArgument(ArgStr, this);
117 }
118
119 bool Option::error(string Message, const char *ArgName = 0) {
120 if (ArgName == 0) ArgName = ArgStr;
121 cerr << "-" << ArgName << " option" << Message << endl;
122 return true;
123 }
124
125 bool Option::addOccurance(const char *ArgName, const string &Value) {
126 NumOccurances++; // Increment the number of times we have been seen
127
128 switch (Flags & OccurancesMask) {
129 case Optional:
130 if (NumOccurances > 1)
131 return error(": may only occur zero or one times!", ArgName);
132 break;
133 case Required:
134 if (NumOccurances > 1)
135 return error(": must occur exactly one time!", ArgName);
136 // Fall through
137 case OneOrMore:
138 case ZeroOrMore: break;
139 default: return error(": bad num occurances flag value!");
140 }
141
142 return handleOccurance(ArgName, Value);
143 }
144
145 // Return the width of the option tag for printing...
146 unsigned Option::getOptionWidth() const {
147 return std::strlen(ArgStr)+6;
148 }
149
150 void Option::printOptionInfo(unsigned GlobalWidth) const {
151 unsigned L = std::strlen(ArgStr);
152 if (L == 0) return; // Don't print the empty arg like this!
153 cerr << " -" << ArgStr << string(GlobalWidth-L-6, ' ') << " - "
154 << HelpStr << endl;
155 }
156
157
158 //===----------------------------------------------------------------------===//
159 // Boolean/flag command line option implementation
160 //
161
162 bool Flag::handleOccurance(const char *ArgName, const string &Arg) {
163 if (Arg == "" || Arg == "true" || Arg == "TRUE" || Arg == "True" ||
164 Arg == "1") {
165 Value = true;
166 } else if (Arg == "false" || Arg == "FALSE" || Arg == "False" || Arg == "0") {
167 Value = false;
168 } else {
169 return error(": '" + Arg + "' is invalid value for boolean argument! Try 0 or 1");
170 }
171
172 return false;
173 }
174
175 //===----------------------------------------------------------------------===//
176 // Integer valued command line option implementation
177 //
178 bool Int::handleOccurance(const char *ArgName, const string &Arg) {
179 const char *ArgStart = Arg.c_str();
180 char *End;
181 Value = (int)strtol(ArgStart, &End, 0);
182 if (*End != 0)
183 return error(": '" + Arg + "' value invalid for integer argument!");
184 return false;
185 }
186
187 //===----------------------------------------------------------------------===//
188 // String valued command line option implementation
189 //
190 bool String::handleOccurance(const char *ArgName, const string &Arg) {
191 Value = Arg;
192 return false;
193 }
194
195 //===----------------------------------------------------------------------===//
196 // Enum valued command line option implementation
197 //
198 void EnumBase::processValues(va_list Vals) {
199 while (const char *EnumName = va_arg(Vals, const char *)) {
200 int EnumVal = va_arg(Vals, int);
201 const char *EnumDesc = va_arg(Vals, const char *);
202 ValueMap.push_back(make_pair(EnumName, // Add value to value map
203 make_pair(EnumVal, EnumDesc)));
204 }
205 }
206
207 // registerArgs - notify the system about these new arguments
208 void EnumBase::registerArgs() {
209 for (unsigned i = 0; i < ValueMap.size(); ++i)
210 AddArgument(ValueMap[i].first, this);
211 }
212
213 const char *EnumBase::getArgName(int ID) const {
214 for (unsigned i = 0; i < ValueMap.size(); ++i)
215 if (ID == ValueMap[i].second.first) return ValueMap[i].first;
216 return "";
217 }
218 const char *EnumBase::getArgDescription(int ID) const {
219 for (unsigned i = 0; i < ValueMap.size(); ++i)
220 if (ID == ValueMap[i].second.first) return ValueMap[i].second.second;
221 return "";
222 }
223
224
225
226 bool EnumValueBase::handleOccurance(const char *ArgName, const string &Arg) {
227 unsigned i;
228 for (i = 0; i < ValueMap.size(); ++i)
229 if (ValueMap[i].first == Arg) break;
230 if (i == ValueMap.size())
231 return error(": unrecognized alternative '"+Arg+"'!");
232 Value = ValueMap[i].second.first;
233 return false;
234 }
235
236 // Return the width of the option tag for printing...
237 unsigned EnumValueBase::getOptionWidth() const {
238 unsigned BaseSize = Option::getOptionWidth();
239 for (unsigned i = 0; i < ValueMap.size(); ++i)
240 BaseSize = max(BaseSize, std::strlen(ValueMap[i].first)+8);
241 return BaseSize;
242 }
243
244 // printOptionInfo - Print out information about this option. The
245 // to-be-maintained width is specified.
246 //
247 void EnumValueBase::printOptionInfo(unsigned GlobalWidth) const {
248 Option::printOptionInfo(GlobalWidth);
249 for (unsigned i = 0; i < ValueMap.size(); ++i) {
250 unsigned NumSpaces = GlobalWidth-strlen(ValueMap[i].first)-8;
251 cerr << " =" << ValueMap[i].first << string(NumSpaces, ' ') << " - "
252 << ValueMap[i].second.second;
253
254 if (i == 0) cerr << " (default)";
255 cerr << endl;
256 }
257 }
258
259 //===----------------------------------------------------------------------===//
260 // Enum flags command line option implementation
261 //
262
263 bool EnumFlagsBase::handleOccurance(const char *ArgName, const string &Arg) {
264 return EnumValueBase::handleOccurance("", ArgName);
265 }
266
267 unsigned EnumFlagsBase::getOptionWidth() const {
268 unsigned BaseSize = 0;
269 for (unsigned i = 0; i < ValueMap.size(); ++i)
270 BaseSize = max(BaseSize, std::strlen(ValueMap[i].first)+6);
271 return BaseSize;
272 }
273
274 void EnumFlagsBase::printOptionInfo(unsigned GlobalWidth) const {
275 for (unsigned i = 0; i < ValueMap.size(); ++i) {
276 unsigned L = std::strlen(ValueMap[i].first);
277 cerr << " -" << ValueMap[i].first << string(GlobalWidth-L-6, ' ') << " - "
278 << ValueMap[i].second.second;
279 if (i == 0) cerr << " (default)";
280 cerr << endl;
281 }
282 }
283
284
285 //===----------------------------------------------------------------------===//
286 // Enum list command line option implementation
287 //
288
289 bool EnumListBase::handleOccurance(const char *ArgName, const string &Arg) {
290 unsigned i;
291 for (i = 0; i < ValueMap.size(); ++i)
292 if (ValueMap[i].first == string(ArgName)) break;
293 if (i == ValueMap.size())
294 return error(": CommandLine INTERNAL ERROR", ArgName);
295 Values.push_back(ValueMap[i].second.first);
296 return false;
297 }
298
299 // Return the width of the option tag for printing...
300 unsigned EnumListBase::getOptionWidth() const {
301 unsigned BaseSize = 0;
302 for (unsigned i = 0; i < ValueMap.size(); ++i)
303 BaseSize = max(BaseSize, std::strlen(ValueMap[i].first)+6);
304 return BaseSize;
305 }
306
307
308 // printOptionInfo - Print out information about this option. The
309 // to-be-maintained width is specified.
310 //
311 void EnumListBase::printOptionInfo(unsigned GlobalWidth) const {
312 for (unsigned i = 0; i < ValueMap.size(); ++i) {
313 unsigned L = std::strlen(ValueMap[i].first);
314 cerr << " -" << ValueMap[i].first << string(GlobalWidth-L-6, ' ') << " - "
315 << ValueMap[i].second.second << endl;
316 }
317 }
318
319
320 //===----------------------------------------------------------------------===//
321 // Help option... always automatically provided.
322 //
323 namespace {
324
325 // isHidden/isReallyHidden - Predicates to be used to filter down arg lists.
326 inline bool isHidden(pair &OptPair) {
327 return (OptPair.second->Flags & HiddenMask) == Hidden;
328 }
329 inline bool isReallyHidden(pair &OptPair) {
330 return (OptPair.second->Flags & HiddenMask) == ReallyHidden;
331 }
332
333 class Help : public Option {
334 unsigned MaxArgLen;
335 const Option *EmptyArg;
336 const bool ShowHidden;
337
338 virtual bool handleOccurance(const char *ArgName, const string &Arg) {
339 // Copy Options into a vector so we can sort them as we like...
340 vector > Options;
341 copy(getOpts().begin(), getOpts().end(), back_inserter(Options));
342
343 // Eliminate Hidden or ReallyHidden arguments, depending on ShowHidden
344 Options.erase(remove_if(Options.begin(), Options.end(),
345 ptr_fun(ShowHidden ? isReallyHidden : isHidden)),
346 Options.end());
347
348 // Eliminate duplicate entries in table (from enum flags options, f.e.)
349 set OptionSet;
350 for (unsigned i = 0; i < Options.size(); )
351 if (OptionSet.count(Options[i].second) == 0)
352 OptionSet.insert(Options[i++].second); // Add to set
353 else
354 Options.erase(Options.begin()+i); // Erase duplicate
355
356
357 if (ProgramOverview)
358 cerr << "OVERVIEW:" << ProgramOverview << endl;
359 // TODO: Sort options by some criteria
360
361 cerr << "USAGE: " << ProgramName << " [options]\n\n";
362 // TODO: print usage nicer
363
364 // Compute the maximum argument length...
365 MaxArgLen = 0;
366 for_each(Options.begin(), Options.end(),
367 bind_obj(this, &Help::getMaxArgLen));
368
369 cerr << "OPTIONS:\n";
370 for_each(Options.begin(), Options.end(),
371 bind_obj(this, &Help::printOption));
372
373 return true; // Displaying help is cause to terminate the program
374 }
375
376 void getMaxArgLen(pair OptPair) {
377 const Option *Opt = OptPair.second;
378 if (Opt->ArgStr[0] == 0) EmptyArg = Opt; // Capture the empty arg if exists
379 MaxArgLen = max(MaxArgLen, Opt->getOptionWidth());
380 }
381
382 void printOption(pair OptPair) {
383 const Option *Opt = OptPair.second;
384 Opt->printOptionInfo(MaxArgLen);
385 }
386
387 public:
388 inline Help(const char *ArgVal, const char *HelpVal, bool showHidden)
389 : Option(ArgVal, HelpVal, showHidden ? Hidden : 0), ShowHidden(showHidden) {
390 EmptyArg = 0;
391 }
392 };
393
394 Help HelpOp("help", "display available options"
395 " (-help-hidden for more)", false);
396 Help HelpHiddenOpt("help-hidden", "display all available options", true);
397
398 } // End anonymous namespace