blob: f038d39154a1388382d44e7a44ea891c7598bf09 [file] [log] [blame]
Chris Lattnerdbab15a2001-07-23 17:17:47 +00001//===-- CommandLine.cpp - Command line parser implementation --------------===//
2//
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.
6//
Chris Lattner03fe1bd2001-07-23 23:04:07 +00007// Note that rather than trying to figure out what this code does, you could try
8// reading the library documentation located in docs/CommandLine.html
9//
Chris Lattnerdbab15a2001-07-23 17:17:47 +000010//===----------------------------------------------------------------------===//
11
Chris Lattnercee8f9a2001-11-27 00:03:19 +000012#include "Support/CommandLine.h"
Chris Lattnerdbab15a2001-07-23 17:17:47 +000013#include <algorithm>
14#include <map>
15#include <set>
Chris Lattner697954c2002-01-20 22:54:45 +000016#include <iostream>
Chris Lattner7f1576f2002-02-24 23:02:12 +000017
Chris Lattnerdbab15a2001-07-23 17:17:47 +000018using namespace cl;
19
Chris Lattner331de232002-07-22 02:07:59 +000020//===----------------------------------------------------------------------===//
21// Basic, shared command line option processing machinery...
22//
23
Chris Lattnerdbab15a2001-07-23 17:17:47 +000024// Return the global command line option vector. Making it a function scoped
Chris Lattnerf78032f2001-11-26 18:58:34 +000025// static ensures that it will be initialized correctly before its first use.
Chris Lattnerdbab15a2001-07-23 17:17:47 +000026//
Chris Lattnerca6433f2003-05-22 20:06:43 +000027static std::map<std::string, Option*> *CommandLineOptions = 0;
28static std::map<std::string, Option*> &getOpts() {
29 if (CommandLineOptions == 0)
30 CommandLineOptions = new std::map<std::string,Option*>();
Chris Lattnere8e258b2002-07-29 20:58:42 +000031 return *CommandLineOptions;
32}
33
Chris Lattnerca6433f2003-05-22 20:06:43 +000034static Option *getOption(const std::string &Str) {
Chris Lattnere8e258b2002-07-29 20:58:42 +000035 if (CommandLineOptions == 0) return 0;
Chris Lattnerca6433f2003-05-22 20:06:43 +000036 std::map<std::string,Option*>::iterator I = CommandLineOptions->find(Str);
Chris Lattnere8e258b2002-07-29 20:58:42 +000037 return I != CommandLineOptions->end() ? I->second : 0;
Chris Lattnerdbab15a2001-07-23 17:17:47 +000038}
39
Chris Lattnerca6433f2003-05-22 20:06:43 +000040static std::vector<Option*> &getPositionalOpts() {
41 static std::vector<Option*> Positional;
Chris Lattner331de232002-07-22 02:07:59 +000042 return Positional;
43}
44
Chris Lattnere8e258b2002-07-29 20:58:42 +000045static void AddArgument(const char *ArgName, Option *Opt) {
46 if (getOption(ArgName)) {
Chris Lattnerca6433f2003-05-22 20:06:43 +000047 std::cerr << "CommandLine Error: Argument '" << ArgName
48 << "' defined more than once!\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +000049 } else {
Chris Lattnerf78032f2001-11-26 18:58:34 +000050 // Add argument to the argument map!
Chris Lattnere8e258b2002-07-29 20:58:42 +000051 getOpts()[ArgName] = Opt;
52 }
53}
54
55// RemoveArgument - It's possible that the argument is no longer in the map if
56// options have already been processed and the map has been deleted!
57//
58static void RemoveArgument(const char *ArgName, Option *Opt) {
59 if (CommandLineOptions == 0) return;
60 assert(getOption(ArgName) == Opt && "Arg not in map!");
61 CommandLineOptions->erase(ArgName);
62 if (CommandLineOptions->empty()) {
63 delete CommandLineOptions;
64 CommandLineOptions = 0;
Chris Lattnerdbab15a2001-07-23 17:17:47 +000065 }
66}
67
68static const char *ProgramName = 0;
69static const char *ProgramOverview = 0;
70
Chris Lattnercaccd762001-10-27 05:54:17 +000071static inline bool ProvideOption(Option *Handler, const char *ArgName,
72 const char *Value, int argc, char **argv,
73 int &i) {
74 // Enforce value requirements
75 switch (Handler->getValueExpectedFlag()) {
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 return Handler->error(" requires a value!");
82 }
83 }
84 break;
85 case ValueDisallowed:
86 if (*Value != 0)
87 return Handler->error(" does not allow a value! '" +
Chris Lattnerca6433f2003-05-22 20:06:43 +000088 std::string(Value) + "' specified.");
Chris Lattnercaccd762001-10-27 05:54:17 +000089 break;
90 case ValueOptional: break;
Chris Lattnerca6433f2003-05-22 20:06:43 +000091 default: std::cerr << "Bad ValueMask flag! CommandLine usage error:"
92 << Handler->getValueExpectedFlag() << "\n"; abort();
Chris Lattnercaccd762001-10-27 05:54:17 +000093 }
94
95 // Run the handler now!
Misha Brukmandd6cb6a2003-07-10 16:49:51 +000096 return Handler->addOccurrence(ArgName, Value);
Chris Lattnercaccd762001-10-27 05:54:17 +000097}
98
Chris Lattner9cf3d472003-07-30 17:34:02 +000099static bool ProvidePositionalOption(Option *Handler, const std::string &Arg) {
Chris Lattner331de232002-07-22 02:07:59 +0000100 int Dummy;
Chris Lattner9cf3d472003-07-30 17:34:02 +0000101 return ProvideOption(Handler, Handler->ArgStr, Arg.c_str(), 0, 0, Dummy);
Chris Lattner331de232002-07-22 02:07:59 +0000102}
Chris Lattnerf78032f2001-11-26 18:58:34 +0000103
Chris Lattner331de232002-07-22 02:07:59 +0000104
105// Option predicates...
106static inline bool isGrouping(const Option *O) {
107 return O->getFormattingFlag() == cl::Grouping;
108}
109static inline bool isPrefixedOrGrouping(const Option *O) {
110 return isGrouping(O) || O->getFormattingFlag() == cl::Prefix;
111}
112
113// getOptionPred - Check to see if there are any options that satisfy the
114// specified predicate with names that are the prefixes in Name. This is
115// checked by progressively stripping characters off of the name, checking to
116// see if there options that satisfy the predicate. If we find one, return it,
117// otherwise return null.
118//
119static Option *getOptionPred(std::string Name, unsigned &Length,
120 bool (*Pred)(const Option*)) {
121
Chris Lattnere8e258b2002-07-29 20:58:42 +0000122 Option *Op = getOption(Name);
123 if (Op && Pred(Op)) {
Chris Lattner331de232002-07-22 02:07:59 +0000124 Length = Name.length();
Chris Lattnere8e258b2002-07-29 20:58:42 +0000125 return Op;
Chris Lattnerf78032f2001-11-26 18:58:34 +0000126 }
127
Chris Lattner331de232002-07-22 02:07:59 +0000128 if (Name.size() == 1) return 0;
129 do {
130 Name.erase(Name.end()-1, Name.end()); // Chop off the last character...
Chris Lattnere8e258b2002-07-29 20:58:42 +0000131 Op = getOption(Name);
Chris Lattner331de232002-07-22 02:07:59 +0000132
133 // Loop while we haven't found an option and Name still has at least two
134 // characters in it (so that the next iteration will not be the empty
135 // string...
Chris Lattnere8e258b2002-07-29 20:58:42 +0000136 } while ((Op == 0 || !Pred(Op)) && Name.size() > 1);
Chris Lattner331de232002-07-22 02:07:59 +0000137
Chris Lattnere8e258b2002-07-29 20:58:42 +0000138 if (Op && Pred(Op)) {
Chris Lattner331de232002-07-22 02:07:59 +0000139 Length = Name.length();
Chris Lattnere8e258b2002-07-29 20:58:42 +0000140 return Op; // Found one!
Chris Lattner331de232002-07-22 02:07:59 +0000141 }
142 return 0; // No option found!
143}
144
145static bool RequiresValue(const Option *O) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000146 return O->getNumOccurrencesFlag() == cl::Required ||
147 O->getNumOccurrencesFlag() == cl::OneOrMore;
Chris Lattner331de232002-07-22 02:07:59 +0000148}
149
150static bool EatsUnboundedNumberOfValues(const Option *O) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000151 return O->getNumOccurrencesFlag() == cl::ZeroOrMore ||
152 O->getNumOccurrencesFlag() == cl::OneOrMore;
Chris Lattnerf78032f2001-11-26 18:58:34 +0000153}
Chris Lattnercaccd762001-10-27 05:54:17 +0000154
Brian Gaeke06b06c52003-08-14 22:00:59 +0000155/// ParseStringVector - Break INPUT up wherever one or more characters
156/// from DELIMS are found, and store the resulting tokens in OUTPUT.
157///
158static void ParseStringVector (std::vector<std::string> &output,
159 std::string &input, const char *delims) {
160 std::string work (input);
161 int pos = work.find_first_not_of (delims);
162 if (pos == -1) return;
163 work = work.substr (pos);
164 pos = work.find_first_of (delims);
165 while (!work.empty() && pos != -1) {
166 if (pos == -1) break;
167 output.push_back (work.substr (0,pos));
168 int nextpos = work.find_first_not_of (delims, pos + 1);
169 if (nextpos != -1) {
170 work = work.substr (work.find_first_not_of (delims, pos + 1));
171 pos = work.find_first_of (delims);
172 } else {
173 work = "";
174 pos = -1;
175 }
176 }
177 if (!work.empty ()) {
178 output.push_back (work);
179 }
180}
181
182/// ParseCStringVector - Same effect as ParseStringVector, but the
183/// resulting output vector contains dynamically-allocated pointers to
184/// char, instead of standard C++ strings.
185///
186static void ParseCStringVector (std::vector<char *> &output,
187 std::string &input, const char *delims) {
188 std::vector<std::string> work;
189 ParseStringVector (work, input, delims);
190 for (std::vector<std::string>::iterator i = work.begin(), e = work.end();
191 i != e; ++i) {
192 output.push_back (strdup (i->c_str ()));
193 }
194}
195
196/// ParseEnvironmentOptions - An alternative entry point to the
197/// CommandLine library, which allows you to read the program's name
198/// from the caller (as PROGNAME) and its command-line arguments from
199/// an environment variable (whose name is given in ENVVAR).
200///
201void cl::ParseEnvironmentOptions (char *progName, char *envvar,
202 const char *Overview) {
203 // Get program's "name", which we wouldn't know without the caller
204 // telling us.
205 assert (progName && "Program name not specified");
206 static std::vector<char *> newargv; // Maybe making it "static" is a hack.
207 int newargc;
208 newargv.push_back (progName);
209
210 // Get the environment variable they want us to parse options out of.
211 assert (envvar && "Environment variable name missing");
212 char *envvalue = getenv (envvar);
213 if (envvalue == NULL) {
214 // Env var not set --> act like there are no more command line
215 // arguments.
216 newargc = newargv.size ();
217 ParseCommandLineOptions (newargc, &newargv[0], Overview);
218 return;
219 }
220 std::string envvaluestr (envvalue);
221
222 // Parse the value of the environment variable into a "command line"
223 // and hand it off to ParseCommandLineOptions().
224 ParseCStringVector (newargv, envvaluestr, " \v\f\t\r\n");
225 newargc = newargv.size ();
226 ParseCommandLineOptions (newargc, &newargv[0], Overview);
227}
228
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000229void cl::ParseCommandLineOptions(int &argc, char **argv,
Chris Lattner0c0edf82002-07-25 06:17:51 +0000230 const char *Overview) {
Chris Lattner331de232002-07-22 02:07:59 +0000231 assert((!getOpts().empty() || !getPositionalOpts().empty()) &&
232 "No options specified, or ParseCommandLineOptions called more"
233 " than once!");
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000234 ProgramName = argv[0]; // Save this away safe and snug
235 ProgramOverview = Overview;
236 bool ErrorParsing = false;
237
Chris Lattnerca6433f2003-05-22 20:06:43 +0000238 std::map<std::string, Option*> &Opts = getOpts();
239 std::vector<Option*> &PositionalOpts = getPositionalOpts();
Chris Lattner331de232002-07-22 02:07:59 +0000240
241 // Check out the positional arguments to collect information about them.
242 unsigned NumPositionalRequired = 0;
243 Option *ConsumeAfterOpt = 0;
244 if (!PositionalOpts.empty()) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000245 if (PositionalOpts[0]->getNumOccurrencesFlag() == cl::ConsumeAfter) {
Chris Lattner331de232002-07-22 02:07:59 +0000246 assert(PositionalOpts.size() > 1 &&
247 "Cannot specify cl::ConsumeAfter without a positional argument!");
248 ConsumeAfterOpt = PositionalOpts[0];
249 }
250
251 // Calculate how many positional values are _required_.
252 bool UnboundedFound = false;
253 for (unsigned i = ConsumeAfterOpt != 0, e = PositionalOpts.size();
254 i != e; ++i) {
255 Option *Opt = PositionalOpts[i];
256 if (RequiresValue(Opt))
257 ++NumPositionalRequired;
258 else if (ConsumeAfterOpt) {
259 // ConsumeAfter cannot be combined with "optional" positional options
Chris Lattner54ec7ae2002-07-22 02:21:57 +0000260 // unless there is only one positional argument...
261 if (PositionalOpts.size() > 2)
262 ErrorParsing |=
263 Opt->error(" error - this positional option will never be matched, "
264 "because it does not Require a value, and a "
265 "cl::ConsumeAfter option is active!");
Chris Lattner9cf3d472003-07-30 17:34:02 +0000266 } else if (UnboundedFound && !Opt->ArgStr[0]) {
267 // This option does not "require" a value... Make sure this option is
268 // not specified after an option that eats all extra arguments, or this
269 // one will never get any!
Chris Lattner331de232002-07-22 02:07:59 +0000270 //
271 ErrorParsing |= Opt->error(" error - option can never match, because "
272 "another positional argument will match an "
273 "unbounded number of values, and this option"
274 " does not require a value!");
275 }
276 UnboundedFound |= EatsUnboundedNumberOfValues(Opt);
277 }
278 }
279
280 // PositionalVals - A vector of "positional" arguments we accumulate into to
281 // processes at the end...
282 //
Chris Lattnerca6433f2003-05-22 20:06:43 +0000283 std::vector<std::string> PositionalVals;
Chris Lattner331de232002-07-22 02:07:59 +0000284
Chris Lattner9cf3d472003-07-30 17:34:02 +0000285 // If the program has named positional arguments, and the name has been run
286 // across, keep track of which positional argument was named. Otherwise put
287 // the positional args into the PositionalVals list...
288 Option *ActivePositionalArg = 0;
289
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000290 // Loop over all of the arguments... processing them.
Chris Lattner331de232002-07-22 02:07:59 +0000291 bool DashDashFound = false; // Have we read '--'?
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000292 for (int i = 1; i < argc; ++i) {
293 Option *Handler = 0;
294 const char *Value = "";
295 const char *ArgName = "";
Chris Lattner331de232002-07-22 02:07:59 +0000296
297 // Check to see if this is a positional argument. This argument is
298 // considered to be positional if it doesn't start with '-', if it is "-"
Misha Brukman1115e042003-07-10 21:38:28 +0000299 // itself, or if we have seen "--" already.
Chris Lattner331de232002-07-22 02:07:59 +0000300 //
301 if (argv[i][0] != '-' || argv[i][1] == 0 || DashDashFound) {
302 // Positional argument!
Chris Lattner9cf3d472003-07-30 17:34:02 +0000303 if (ActivePositionalArg) {
304 ProvidePositionalOption(ActivePositionalArg, argv[i]);
305 continue; // We are done!
306 } else if (!PositionalOpts.empty()) {
Chris Lattner331de232002-07-22 02:07:59 +0000307 PositionalVals.push_back(argv[i]);
308
309 // All of the positional arguments have been fulfulled, give the rest to
310 // the consume after option... if it's specified...
311 //
Chris Lattnerd16714b2002-07-31 16:29:43 +0000312 if (PositionalVals.size() >= NumPositionalRequired &&
Chris Lattner331de232002-07-22 02:07:59 +0000313 ConsumeAfterOpt != 0) {
314 for (++i; i < argc; ++i)
315 PositionalVals.push_back(argv[i]);
316 break; // Handle outside of the argument processing loop...
317 }
318
319 // Delay processing positional arguments until the end...
320 continue;
321 }
322 } else { // We start with a '-', must be an argument...
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000323 ArgName = argv[i]+1;
324 while (*ArgName == '-') ++ArgName; // Eat leading dashes
325
Chris Lattner331de232002-07-22 02:07:59 +0000326 if (*ArgName == 0 && !DashDashFound) { // Is this the mythical "--"?
327 DashDashFound = true; // Yup, take note of that fact...
328 continue; // Don't try to process it as an argument iself.
329 }
330
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000331 const char *ArgNameEnd = ArgName;
Chris Lattnerf78032f2001-11-26 18:58:34 +0000332 while (*ArgNameEnd && *ArgNameEnd != '=')
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000333 ++ArgNameEnd; // Scan till end of argument name...
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000334
335 Value = ArgNameEnd;
336 if (*Value) // If we have an equals sign...
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000337 ++Value; // Advance to value...
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000338
339 if (*ArgName != 0) {
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000340 std::string RealName(ArgName, ArgNameEnd);
341 // Extract arg name part
Chris Lattnerca6433f2003-05-22 20:06:43 +0000342 std::map<std::string, Option*>::iterator I = Opts.find(RealName);
Chris Lattnerf78032f2001-11-26 18:58:34 +0000343
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000344 if (I == Opts.end() && !*Value && RealName.size() > 1) {
Chris Lattner331de232002-07-22 02:07:59 +0000345 // Check to see if this "option" is really a prefixed or grouped
346 // argument...
347 //
348 unsigned Length = 0;
349 Option *PGOpt = getOptionPred(RealName, Length, isPrefixedOrGrouping);
Chris Lattnerf78032f2001-11-26 18:58:34 +0000350
Chris Lattner331de232002-07-22 02:07:59 +0000351 // If the option is a prefixed option, then the value is simply the
352 // rest of the name... so fall through to later processing, by
353 // setting up the argument name flags and value fields.
354 //
355 if (PGOpt && PGOpt->getFormattingFlag() == cl::Prefix) {
356 ArgNameEnd = ArgName+Length;
357 Value = ArgNameEnd;
Chris Lattnerca6433f2003-05-22 20:06:43 +0000358 I = Opts.find(std::string(ArgName, ArgNameEnd));
Chris Lattner331de232002-07-22 02:07:59 +0000359 assert(I->second == PGOpt);
360 } else if (PGOpt) {
361 // This must be a grouped option... handle all of them now...
362 assert(isGrouping(PGOpt) && "Broken getOptionPred!");
363
364 do {
365 // Move current arg name out of RealName into RealArgName...
Chris Lattnerca6433f2003-05-22 20:06:43 +0000366 std::string RealArgName(RealName.begin(),RealName.begin()+Length);
Chris Lattner331de232002-07-22 02:07:59 +0000367 RealName.erase(RealName.begin(), RealName.begin()+Length);
Chris Lattnerf78032f2001-11-26 18:58:34 +0000368
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000369 // Because ValueRequired is an invalid flag for grouped arguments,
370 // we don't need to pass argc/argv in...
371 //
Chris Lattner331de232002-07-22 02:07:59 +0000372 assert(PGOpt->getValueExpectedFlag() != cl::ValueRequired &&
373 "Option can not be cl::Grouping AND cl::ValueRequired!");
374 int Dummy;
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000375 ErrorParsing |= ProvideOption(PGOpt, RealArgName.c_str(), "",
Chris Lattner331de232002-07-22 02:07:59 +0000376 0, 0, Dummy);
377
378 // Get the next grouping option...
379 if (!RealName.empty())
380 PGOpt = getOptionPred(RealName, Length, isGrouping);
381 } while (!RealName.empty() && PGOpt);
382
383 if (RealName.empty()) // Processed all of the options, move on
384 continue; // to the next argv[] value...
385
386 // If RealName is not empty, that means we did not match one of the
387 // options! This is an error.
388 //
389 I = Opts.end();
390 }
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000391 }
Chris Lattnerf78032f2001-11-26 18:58:34 +0000392
Chris Lattner331de232002-07-22 02:07:59 +0000393 Handler = I != Opts.end() ? I->second : 0;
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000394 }
395 }
396
397 if (Handler == 0) {
Chris Lattnerca6433f2003-05-22 20:06:43 +0000398 std::cerr << "Unknown command line argument '" << argv[i] << "'. Try: "
399 << argv[0] << " --help'\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000400 ErrorParsing = true;
401 continue;
402 }
403
Chris Lattner72fb8e52003-05-22 20:26:17 +0000404 // Check to see if this option accepts a comma separated list of values. If
405 // it does, we have to split up the value into multiple values...
406 if (Handler->getMiscFlags() & CommaSeparated) {
407 std::string Val(Value);
408 std::string::size_type Pos = Val.find(',');
409
410 while (Pos != std::string::npos) {
411 // Process the portion before the comma...
412 ErrorParsing |= ProvideOption(Handler, ArgName,
413 std::string(Val.begin(),
414 Val.begin()+Pos).c_str(),
415 argc, argv, i);
416 // Erase the portion before the comma, AND the comma...
417 Val.erase(Val.begin(), Val.begin()+Pos+1);
418 Value += Pos+1; // Increment the original value pointer as well...
419
420 // Check for another comma...
421 Pos = Val.find(',');
422 }
423 }
Chris Lattner9cf3d472003-07-30 17:34:02 +0000424
425 // If this is a named positional argument, just remember that it is the
426 // active one...
427 if (Handler->getFormattingFlag() == cl::Positional)
428 ActivePositionalArg = Handler;
429 else
430 ErrorParsing |= ProvideOption(Handler, ArgName, Value, argc, argv, i);
Chris Lattner331de232002-07-22 02:07:59 +0000431 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000432
Chris Lattner331de232002-07-22 02:07:59 +0000433 // Check and handle positional arguments now...
434 if (NumPositionalRequired > PositionalVals.size()) {
Chris Lattnerca6433f2003-05-22 20:06:43 +0000435 std::cerr << "Not enough positional command line arguments specified!\n"
436 << "Must specify at least " << NumPositionalRequired
437 << " positional arguments: See: " << argv[0] << " --help\n";
Chris Lattner331de232002-07-22 02:07:59 +0000438 ErrorParsing = true;
439
440
441 } else if (ConsumeAfterOpt == 0) {
442 // Positional args have already been handled if ConsumeAfter is specified...
443 unsigned ValNo = 0, NumVals = PositionalVals.size();
444 for (unsigned i = 0, e = PositionalOpts.size(); i != e; ++i) {
445 if (RequiresValue(PositionalOpts[i])) {
446 ProvidePositionalOption(PositionalOpts[i], PositionalVals[ValNo++]);
447 --NumPositionalRequired; // We fulfilled our duty...
448 }
449
450 // If we _can_ give this option more arguments, do so now, as long as we
451 // do not give it values that others need. 'Done' controls whether the
452 // option even _WANTS_ any more.
453 //
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000454 bool Done = PositionalOpts[i]->getNumOccurrencesFlag() == cl::Required;
Chris Lattner331de232002-07-22 02:07:59 +0000455 while (NumVals-ValNo > NumPositionalRequired && !Done) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000456 switch (PositionalOpts[i]->getNumOccurrencesFlag()) {
Chris Lattner331de232002-07-22 02:07:59 +0000457 case cl::Optional:
458 Done = true; // Optional arguments want _at most_ one value
459 // FALL THROUGH
460 case cl::ZeroOrMore: // Zero or more will take all they can get...
461 case cl::OneOrMore: // One or more will take all they can get...
462 ProvidePositionalOption(PositionalOpts[i], PositionalVals[ValNo++]);
463 break;
464 default:
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000465 assert(0 && "Internal error, unexpected NumOccurrences flag in "
Chris Lattner331de232002-07-22 02:07:59 +0000466 "positional argument processing!");
467 }
468 }
Chris Lattnercaccd762001-10-27 05:54:17 +0000469 }
Chris Lattner331de232002-07-22 02:07:59 +0000470 } else {
471 assert(ConsumeAfterOpt && NumPositionalRequired <= PositionalVals.size());
472 unsigned ValNo = 0;
473 for (unsigned j = 1, e = PositionalOpts.size(); j != e; ++j)
474 if (RequiresValue(PositionalOpts[j]))
Chris Lattnerfaba8092002-07-24 20:15:13 +0000475 ErrorParsing |= ProvidePositionalOption(PositionalOpts[j],
476 PositionalVals[ValNo++]);
477
478 // Handle the case where there is just one positional option, and it's
479 // optional. In this case, we want to give JUST THE FIRST option to the
480 // positional option and keep the rest for the consume after. The above
481 // loop would have assigned no values to positional options in this case.
482 //
Chris Lattnerb490c202002-08-02 21:51:29 +0000483 if (PositionalOpts.size() == 2 && ValNo == 0 && !PositionalVals.empty())
Chris Lattnerfaba8092002-07-24 20:15:13 +0000484 ErrorParsing |= ProvidePositionalOption(PositionalOpts[1],
485 PositionalVals[ValNo++]);
Chris Lattner331de232002-07-22 02:07:59 +0000486
487 // Handle over all of the rest of the arguments to the
488 // cl::ConsumeAfter command line option...
489 for (; ValNo != PositionalVals.size(); ++ValNo)
490 ErrorParsing |= ProvidePositionalOption(ConsumeAfterOpt,
491 PositionalVals[ValNo]);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000492 }
493
Chris Lattnerdc4693d2001-07-23 23:02:45 +0000494 // Loop over args and make sure all required args are specified!
Chris Lattnerca6433f2003-05-22 20:06:43 +0000495 for (std::map<std::string, Option*>::iterator I = Opts.begin(),
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000496 E = Opts.end(); I != E; ++I) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000497 switch (I->second->getNumOccurrencesFlag()) {
Chris Lattnerdc4693d2001-07-23 23:02:45 +0000498 case Required:
499 case OneOrMore:
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000500 if (I->second->getNumOccurrences() == 0) {
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000501 I->second->error(" must be specified at least once!");
Chris Lattnerf038acb2001-10-24 06:21:56 +0000502 ErrorParsing = true;
503 }
Chris Lattnerdc4693d2001-07-23 23:02:45 +0000504 // Fall through
505 default:
506 break;
507 }
508 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000509
Chris Lattner331de232002-07-22 02:07:59 +0000510 // Free all of the memory allocated to the map. Command line options may only
511 // be processed once!
Chris Lattnere8e258b2002-07-29 20:58:42 +0000512 delete CommandLineOptions;
513 CommandLineOptions = 0;
Chris Lattner331de232002-07-22 02:07:59 +0000514 PositionalOpts.clear();
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000515
516 // If we had an error processing our arguments, don't let the program execute
517 if (ErrorParsing) exit(1);
518}
519
520//===----------------------------------------------------------------------===//
521// Option Base class implementation
522//
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000523
Chris Lattnerca6433f2003-05-22 20:06:43 +0000524bool Option::error(std::string Message, const char *ArgName) {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000525 if (ArgName == 0) ArgName = ArgStr;
Chris Lattner331de232002-07-22 02:07:59 +0000526 if (ArgName[0] == 0)
Chris Lattnerca6433f2003-05-22 20:06:43 +0000527 std::cerr << HelpStr; // Be nice for positional arguments
Chris Lattner331de232002-07-22 02:07:59 +0000528 else
Chris Lattnerca6433f2003-05-22 20:06:43 +0000529 std::cerr << "-" << ArgName;
530 std::cerr << " option" << Message << "\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000531 return true;
532}
533
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000534bool Option::addOccurrence(const char *ArgName, const std::string &Value) {
535 NumOccurrences++; // Increment the number of times we have been seen
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000536
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000537 switch (getNumOccurrencesFlag()) {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000538 case Optional:
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000539 if (NumOccurrences > 1)
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000540 return error(": may only occur zero or one times!", ArgName);
541 break;
542 case Required:
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000543 if (NumOccurrences > 1)
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000544 return error(": must occur exactly one time!", ArgName);
545 // Fall through
546 case OneOrMore:
Chris Lattnercaccd762001-10-27 05:54:17 +0000547 case ZeroOrMore:
548 case ConsumeAfter: break;
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000549 default: return error(": bad num occurrences flag value!");
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000550 }
551
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000552 return handleOccurrence(ArgName, Value);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000553}
554
Chris Lattner331de232002-07-22 02:07:59 +0000555// addArgument - Tell the system that this Option subclass will handle all
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000556// occurrences of -ArgStr on the command line.
Chris Lattner331de232002-07-22 02:07:59 +0000557//
558void Option::addArgument(const char *ArgStr) {
559 if (ArgStr[0])
560 AddArgument(ArgStr, this);
Chris Lattner9cf3d472003-07-30 17:34:02 +0000561
562 if (getFormattingFlag() == Positional)
Chris Lattner331de232002-07-22 02:07:59 +0000563 getPositionalOpts().push_back(this);
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000564 else if (getNumOccurrencesFlag() == ConsumeAfter) {
Chris Lattner9cf3d472003-07-30 17:34:02 +0000565 if (!getPositionalOpts().empty() &&
566 getPositionalOpts().front()->getNumOccurrencesFlag() == ConsumeAfter)
567 error("Cannot specify more than one option with cl::ConsumeAfter!");
Chris Lattner331de232002-07-22 02:07:59 +0000568 getPositionalOpts().insert(getPositionalOpts().begin(), this);
569 }
570}
571
Chris Lattneraa852bb2002-07-23 17:15:12 +0000572void Option::removeArgument(const char *ArgStr) {
Chris Lattner9cf3d472003-07-30 17:34:02 +0000573 if (ArgStr[0])
Chris Lattnere8e258b2002-07-29 20:58:42 +0000574 RemoveArgument(ArgStr, this);
Chris Lattner9cf3d472003-07-30 17:34:02 +0000575
576 if (getFormattingFlag() == Positional) {
Chris Lattnerca6433f2003-05-22 20:06:43 +0000577 std::vector<Option*>::iterator I =
Chris Lattneraa852bb2002-07-23 17:15:12 +0000578 std::find(getPositionalOpts().begin(), getPositionalOpts().end(), this);
579 assert(I != getPositionalOpts().end() && "Arg not registered!");
580 getPositionalOpts().erase(I);
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000581 } else if (getNumOccurrencesFlag() == ConsumeAfter) {
Chris Lattneraa852bb2002-07-23 17:15:12 +0000582 assert(!getPositionalOpts().empty() && getPositionalOpts()[0] == this &&
583 "Arg not registered correctly!");
584 getPositionalOpts().erase(getPositionalOpts().begin());
585 }
586}
587
Chris Lattner331de232002-07-22 02:07:59 +0000588
589// getValueStr - Get the value description string, using "DefaultMsg" if nothing
590// has been specified yet.
591//
592static const char *getValueStr(const Option &O, const char *DefaultMsg) {
593 if (O.ValueStr[0] == 0) return DefaultMsg;
594 return O.ValueStr;
595}
596
597//===----------------------------------------------------------------------===//
598// cl::alias class implementation
599//
600
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000601// Return the width of the option tag for printing...
Chris Lattner331de232002-07-22 02:07:59 +0000602unsigned alias::getOptionWidth() const {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000603 return std::strlen(ArgStr)+6;
604}
605
Chris Lattner331de232002-07-22 02:07:59 +0000606// Print out the option for the alias...
607void alias::printOptionInfo(unsigned GlobalWidth) const {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000608 unsigned L = std::strlen(ArgStr);
Chris Lattnerca6433f2003-05-22 20:06:43 +0000609 std::cerr << " -" << ArgStr << std::string(GlobalWidth-L-6, ' ') << " - "
610 << HelpStr << "\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000611}
612
613
Chris Lattner331de232002-07-22 02:07:59 +0000614
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000615//===----------------------------------------------------------------------===//
Chris Lattner331de232002-07-22 02:07:59 +0000616// Parser Implementation code...
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000617//
618
Chris Lattner9b14eb52002-08-07 18:36:37 +0000619// basic_parser implementation
620//
621
622// Return the width of the option tag for printing...
623unsigned basic_parser_impl::getOptionWidth(const Option &O) const {
624 unsigned Len = std::strlen(O.ArgStr);
625 if (const char *ValName = getValueName())
626 Len += std::strlen(getValueStr(O, ValName))+3;
627
628 return Len + 6;
629}
630
631// printOptionInfo - Print out information about this option. The
632// to-be-maintained width is specified.
633//
634void basic_parser_impl::printOptionInfo(const Option &O,
635 unsigned GlobalWidth) const {
Chris Lattnerca6433f2003-05-22 20:06:43 +0000636 std::cerr << " -" << O.ArgStr;
Chris Lattner9b14eb52002-08-07 18:36:37 +0000637
638 if (const char *ValName = getValueName())
Chris Lattnerca6433f2003-05-22 20:06:43 +0000639 std::cerr << "=<" << getValueStr(O, ValName) << ">";
Chris Lattner9b14eb52002-08-07 18:36:37 +0000640
Chris Lattnerca6433f2003-05-22 20:06:43 +0000641 std::cerr << std::string(GlobalWidth-getOptionWidth(O), ' ') << " - "
642 << O.HelpStr << "\n";
Chris Lattner9b14eb52002-08-07 18:36:37 +0000643}
644
645
646
647
Chris Lattner331de232002-07-22 02:07:59 +0000648// parser<bool> implementation
649//
Chris Lattner9b14eb52002-08-07 18:36:37 +0000650bool parser<bool>::parse(Option &O, const char *ArgName,
Chris Lattnerca6433f2003-05-22 20:06:43 +0000651 const std::string &Arg, bool &Value) {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000652 if (Arg == "" || Arg == "true" || Arg == "TRUE" || Arg == "True" ||
653 Arg == "1") {
654 Value = true;
655 } else if (Arg == "false" || Arg == "FALSE" || Arg == "False" || Arg == "0") {
656 Value = false;
657 } else {
Chris Lattner331de232002-07-22 02:07:59 +0000658 return O.error(": '" + Arg +
659 "' is invalid value for boolean argument! Try 0 or 1");
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000660 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000661 return false;
662}
663
Chris Lattner331de232002-07-22 02:07:59 +0000664// parser<int> implementation
665//
Chris Lattner9b14eb52002-08-07 18:36:37 +0000666bool parser<int>::parse(Option &O, const char *ArgName,
Chris Lattnerca6433f2003-05-22 20:06:43 +0000667 const std::string &Arg, int &Value) {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000668 char *End;
Chris Lattnerd2a6fc32003-06-28 15:47:20 +0000669 Value = (int)strtol(Arg.c_str(), &End, 0);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000670 if (*End != 0)
Chris Lattner331de232002-07-22 02:07:59 +0000671 return O.error(": '" + Arg + "' value invalid for integer argument!");
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000672 return false;
673}
674
Chris Lattnerd2a6fc32003-06-28 15:47:20 +0000675// parser<unsigned> implementation
676//
677bool parser<unsigned>::parse(Option &O, const char *ArgName,
678 const std::string &Arg, unsigned &Value) {
679 char *End;
680 long long int V = strtoll(Arg.c_str(), &End, 0);
681 Value = (unsigned)V;
682 if (*End != 0 || V < 0 || Value != V)
683 return O.error(": '" + Arg + "' value invalid for uint argument!");
684 return false;
685}
686
Chris Lattner9b14eb52002-08-07 18:36:37 +0000687// parser<double>/parser<float> implementation
Chris Lattnerd215fd12001-10-13 06:53:19 +0000688//
Chris Lattnerca6433f2003-05-22 20:06:43 +0000689static bool parseDouble(Option &O, const std::string &Arg, double &Value) {
Chris Lattner331de232002-07-22 02:07:59 +0000690 const char *ArgStart = Arg.c_str();
691 char *End;
692 Value = strtod(ArgStart, &End);
693 if (*End != 0)
694 return O.error(": '" +Arg+ "' value invalid for floating point argument!");
Chris Lattnerd215fd12001-10-13 06:53:19 +0000695 return false;
696}
697
Chris Lattner9b14eb52002-08-07 18:36:37 +0000698bool parser<double>::parse(Option &O, const char *AN,
699 const std::string &Arg, double &Val) {
700 return parseDouble(O, Arg, Val);
Chris Lattner331de232002-07-22 02:07:59 +0000701}
702
Chris Lattner9b14eb52002-08-07 18:36:37 +0000703bool parser<float>::parse(Option &O, const char *AN,
704 const std::string &Arg, float &Val) {
705 double dVal;
706 if (parseDouble(O, Arg, dVal))
707 return true;
708 Val = (float)dVal;
709 return false;
Chris Lattner331de232002-07-22 02:07:59 +0000710}
711
712
Chris Lattner331de232002-07-22 02:07:59 +0000713
714// generic_parser_base implementation
715//
716
Chris Lattneraa852bb2002-07-23 17:15:12 +0000717// findOption - Return the option number corresponding to the specified
718// argument string. If the option is not found, getNumOptions() is returned.
719//
720unsigned generic_parser_base::findOption(const char *Name) {
721 unsigned i = 0, e = getNumOptions();
Chris Lattnerca6433f2003-05-22 20:06:43 +0000722 std::string N(Name);
Chris Lattneraa852bb2002-07-23 17:15:12 +0000723
724 while (i != e)
725 if (getOption(i) == N)
726 return i;
727 else
728 ++i;
729 return e;
730}
731
732
Chris Lattner331de232002-07-22 02:07:59 +0000733// Return the width of the option tag for printing...
734unsigned generic_parser_base::getOptionWidth(const Option &O) const {
735 if (O.hasArgStr()) {
736 unsigned Size = std::strlen(O.ArgStr)+6;
737 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
738 Size = std::max(Size, (unsigned)std::strlen(getOption(i))+8);
739 return Size;
740 } else {
741 unsigned BaseSize = 0;
742 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
743 BaseSize = std::max(BaseSize, (unsigned)std::strlen(getOption(i))+8);
744 return BaseSize;
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000745 }
746}
747
Chris Lattner331de232002-07-22 02:07:59 +0000748// printOptionInfo - Print out information about this option. The
749// to-be-maintained width is specified.
750//
751void generic_parser_base::printOptionInfo(const Option &O,
752 unsigned GlobalWidth) const {
753 if (O.hasArgStr()) {
754 unsigned L = std::strlen(O.ArgStr);
Chris Lattnerca6433f2003-05-22 20:06:43 +0000755 std::cerr << " -" << O.ArgStr << std::string(GlobalWidth-L-6, ' ')
756 << " - " << O.HelpStr << "\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000757
Chris Lattner331de232002-07-22 02:07:59 +0000758 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
759 unsigned NumSpaces = GlobalWidth-strlen(getOption(i))-8;
Chris Lattnerca6433f2003-05-22 20:06:43 +0000760 std::cerr << " =" << getOption(i) << std::string(NumSpaces, ' ')
761 << " - " << getDescription(i) << "\n";
Chris Lattner9c9be482002-01-31 00:42:56 +0000762 }
Chris Lattner331de232002-07-22 02:07:59 +0000763 } else {
764 if (O.HelpStr[0])
Chris Lattnerca6433f2003-05-22 20:06:43 +0000765 std::cerr << " " << O.HelpStr << "\n";
Chris Lattner331de232002-07-22 02:07:59 +0000766 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
767 unsigned L = std::strlen(getOption(i));
Chris Lattnerca6433f2003-05-22 20:06:43 +0000768 std::cerr << " -" << getOption(i) << std::string(GlobalWidth-L-8, ' ')
769 << " - " << getDescription(i) << "\n";
Chris Lattner331de232002-07-22 02:07:59 +0000770 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000771 }
772}
773
774
775//===----------------------------------------------------------------------===//
Chris Lattner331de232002-07-22 02:07:59 +0000776// --help and --help-hidden option implementation
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000777//
778namespace {
779
Chris Lattner331de232002-07-22 02:07:59 +0000780class HelpPrinter {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000781 unsigned MaxArgLen;
782 const Option *EmptyArg;
783 const bool ShowHidden;
784
Chris Lattner331de232002-07-22 02:07:59 +0000785 // isHidden/isReallyHidden - Predicates to be used to filter down arg lists.
Chris Lattnerca6433f2003-05-22 20:06:43 +0000786 inline static bool isHidden(std::pair<std::string, Option *> &OptPair) {
Chris Lattner331de232002-07-22 02:07:59 +0000787 return OptPair.second->getOptionHiddenFlag() >= Hidden;
788 }
Chris Lattnerca6433f2003-05-22 20:06:43 +0000789 inline static bool isReallyHidden(std::pair<std::string, Option *> &OptPair) {
Chris Lattner331de232002-07-22 02:07:59 +0000790 return OptPair.second->getOptionHiddenFlag() == ReallyHidden;
791 }
792
793public:
794 HelpPrinter(bool showHidden) : ShowHidden(showHidden) {
795 EmptyArg = 0;
796 }
797
798 void operator=(bool Value) {
799 if (Value == false) return;
800
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000801 // Copy Options into a vector so we can sort them as we like...
Chris Lattnerca6433f2003-05-22 20:06:43 +0000802 std::vector<std::pair<std::string, Option*> > Options;
Chris Lattner697954c2002-01-20 22:54:45 +0000803 copy(getOpts().begin(), getOpts().end(), std::back_inserter(Options));
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000804
805 // Eliminate Hidden or ReallyHidden arguments, depending on ShowHidden
Chris Lattner331de232002-07-22 02:07:59 +0000806 Options.erase(std::remove_if(Options.begin(), Options.end(),
807 std::ptr_fun(ShowHidden ? isReallyHidden : isHidden)),
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000808 Options.end());
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000809
810 // Eliminate duplicate entries in table (from enum flags options, f.e.)
Chris Lattner331de232002-07-22 02:07:59 +0000811 { // Give OptionSet a scope
812 std::set<Option*> OptionSet;
813 for (unsigned i = 0; i != Options.size(); ++i)
814 if (OptionSet.count(Options[i].second) == 0)
815 OptionSet.insert(Options[i].second); // Add new entry to set
816 else
817 Options.erase(Options.begin()+i--); // Erase duplicate
818 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000819
820 if (ProgramOverview)
Chris Lattnerca6433f2003-05-22 20:06:43 +0000821 std::cerr << "OVERVIEW:" << ProgramOverview << "\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000822
Chris Lattnerca6433f2003-05-22 20:06:43 +0000823 std::cerr << "USAGE: " << ProgramName << " [options]";
Chris Lattner331de232002-07-22 02:07:59 +0000824
825 // Print out the positional options...
Chris Lattnerca6433f2003-05-22 20:06:43 +0000826 std::vector<Option*> &PosOpts = getPositionalOpts();
Chris Lattner331de232002-07-22 02:07:59 +0000827 Option *CAOpt = 0; // The cl::ConsumeAfter option, if it exists...
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000828 if (!PosOpts.empty() && PosOpts[0]->getNumOccurrencesFlag() == ConsumeAfter)
Chris Lattner331de232002-07-22 02:07:59 +0000829 CAOpt = PosOpts[0];
830
Chris Lattner9cf3d472003-07-30 17:34:02 +0000831 for (unsigned i = CAOpt != 0, e = PosOpts.size(); i != e; ++i) {
832 if (PosOpts[i]->ArgStr[0])
833 std::cerr << " --" << PosOpts[i]->ArgStr;
Chris Lattnerca6433f2003-05-22 20:06:43 +0000834 std::cerr << " " << PosOpts[i]->HelpStr;
Chris Lattner9cf3d472003-07-30 17:34:02 +0000835 }
Chris Lattner331de232002-07-22 02:07:59 +0000836
837 // Print the consume after option info if it exists...
Chris Lattnerca6433f2003-05-22 20:06:43 +0000838 if (CAOpt) std::cerr << " " << CAOpt->HelpStr;
Chris Lattner331de232002-07-22 02:07:59 +0000839
Chris Lattnerca6433f2003-05-22 20:06:43 +0000840 std::cerr << "\n\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000841
842 // Compute the maximum argument length...
843 MaxArgLen = 0;
Chris Lattner331de232002-07-22 02:07:59 +0000844 for (unsigned i = 0, e = Options.size(); i != e; ++i)
845 MaxArgLen = std::max(MaxArgLen, Options[i].second->getOptionWidth());
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000846
Chris Lattnerca6433f2003-05-22 20:06:43 +0000847 std::cerr << "OPTIONS:\n";
Chris Lattner331de232002-07-22 02:07:59 +0000848 for (unsigned i = 0, e = Options.size(); i != e; ++i)
849 Options[i].second->printOptionInfo(MaxArgLen);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000850
Chris Lattner331de232002-07-22 02:07:59 +0000851 // Halt the program if help information is printed
852 exit(1);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000853 }
854};
855
Chris Lattner331de232002-07-22 02:07:59 +0000856
857
858// Define the two HelpPrinter instances that are used to print out help, or
859// help-hidden...
860//
861HelpPrinter NormalPrinter(false);
862HelpPrinter HiddenPrinter(true);
863
864cl::opt<HelpPrinter, true, parser<bool> >
865HOp("help", cl::desc("display available options (--help-hidden for more)"),
Chris Lattner9b14eb52002-08-07 18:36:37 +0000866 cl::location(NormalPrinter), cl::ValueDisallowed);
Chris Lattner331de232002-07-22 02:07:59 +0000867
868cl::opt<HelpPrinter, true, parser<bool> >
869HHOp("help-hidden", cl::desc("display all available options"),
Chris Lattner9b14eb52002-08-07 18:36:37 +0000870 cl::location(HiddenPrinter), cl::Hidden, cl::ValueDisallowed);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000871
872} // End anonymous namespace