blob: 2d57abdb3cfdb4cfac4b65e83acb203ef035cd6a [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 Gaekec48ef2a2003-08-15 21:05:57 +0000155/// ParseCStringVector - Break INPUT up wherever one or more
156/// whitespace characters are found, and store the resulting tokens in
157/// OUTPUT. The tokens stored in OUTPUT are dynamically allocated
158/// using strdup (), so it is the caller's responsibility to free ()
159/// them later.
Brian Gaeke06b06c52003-08-14 22:00:59 +0000160///
161static void ParseCStringVector (std::vector<char *> &output,
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000162 const char *input) {
163 // Characters which will be treated as token separators:
164 static const char *delims = " \v\f\t\r\n";
165
166 std::string work (input);
167 // Skip past any delims at head of input string.
168 size_t pos = work.find_first_not_of (delims);
169 // If the string consists entirely of delims, then exit early.
170 if (pos == std::string::npos) return;
171 // Otherwise, jump forward to beginning of first word.
172 work = work.substr (pos);
173 // Find position of first delimiter.
174 pos = work.find_first_of (delims);
175
176 while (!work.empty() && pos != std::string::npos) {
177 // Everything from 0 to POS is the next word to copy.
178 output.push_back (strdup (work.substr (0,pos).c_str ()));
179 // Is there another word in the string?
180 size_t nextpos = work.find_first_not_of (delims, pos + 1);
181 if (nextpos != std::string::npos) {
182 // Yes? Then remove delims from beginning ...
183 work = work.substr (work.find_first_not_of (delims, pos + 1));
184 // and find the end of the word.
185 pos = work.find_first_of (delims);
186 } else {
187 // No? (Remainder of string is delims.) End the loop.
188 work = "";
189 pos = std::string::npos;
190 }
191 }
192
193 // If `input' ended with non-delim char, then we'll get here with
194 // the last word of `input' in `work'; copy it now.
195 if (!work.empty ()) {
196 output.push_back (strdup (work.c_str ()));
Brian Gaeke06b06c52003-08-14 22:00:59 +0000197 }
198}
199
200/// ParseEnvironmentOptions - An alternative entry point to the
201/// CommandLine library, which allows you to read the program's name
202/// from the caller (as PROGNAME) and its command-line arguments from
203/// an environment variable (whose name is given in ENVVAR).
204///
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000205void cl::ParseEnvironmentOptions (const char *progName, const char *envVar,
Brian Gaeke06b06c52003-08-14 22:00:59 +0000206 const char *Overview) {
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000207 // Check args.
208 assert (progName && "Program name not specified");
209 assert (envVar && "Environment variable name missing");
210
211 // Get the environment variable they want us to parse options out of.
212 const char *envValue = getenv (envVar);
213 if (!envValue)
214 return;
215
Brian Gaeke06b06c52003-08-14 22:00:59 +0000216 // Get program's "name", which we wouldn't know without the caller
217 // telling us.
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000218 std::vector<char *> newArgv;
219 newArgv.push_back (strdup (progName));
Brian Gaeke06b06c52003-08-14 22:00:59 +0000220
221 // Parse the value of the environment variable into a "command line"
222 // and hand it off to ParseCommandLineOptions().
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000223 ParseCStringVector (newArgv, envValue);
224 int newArgc = newArgv.size ();
225 ParseCommandLineOptions (newArgc, &newArgv[0], Overview);
226
227 // Free all the strdup()ed strings.
228 for (std::vector<char *>::iterator i = newArgv.begin (), e = newArgv.end ();
229 i != e; ++i) {
230 free (*i);
231 }
Brian Gaeke06b06c52003-08-14 22:00:59 +0000232}
233
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000234void cl::ParseCommandLineOptions(int &argc, char **argv,
Chris Lattner0c0edf82002-07-25 06:17:51 +0000235 const char *Overview) {
Chris Lattner331de232002-07-22 02:07:59 +0000236 assert((!getOpts().empty() || !getPositionalOpts().empty()) &&
237 "No options specified, or ParseCommandLineOptions called more"
238 " than once!");
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000239 ProgramName = argv[0]; // Save this away safe and snug
240 ProgramOverview = Overview;
241 bool ErrorParsing = false;
242
Chris Lattnerca6433f2003-05-22 20:06:43 +0000243 std::map<std::string, Option*> &Opts = getOpts();
244 std::vector<Option*> &PositionalOpts = getPositionalOpts();
Chris Lattner331de232002-07-22 02:07:59 +0000245
246 // Check out the positional arguments to collect information about them.
247 unsigned NumPositionalRequired = 0;
248 Option *ConsumeAfterOpt = 0;
249 if (!PositionalOpts.empty()) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000250 if (PositionalOpts[0]->getNumOccurrencesFlag() == cl::ConsumeAfter) {
Chris Lattner331de232002-07-22 02:07:59 +0000251 assert(PositionalOpts.size() > 1 &&
252 "Cannot specify cl::ConsumeAfter without a positional argument!");
253 ConsumeAfterOpt = PositionalOpts[0];
254 }
255
256 // Calculate how many positional values are _required_.
257 bool UnboundedFound = false;
258 for (unsigned i = ConsumeAfterOpt != 0, e = PositionalOpts.size();
259 i != e; ++i) {
260 Option *Opt = PositionalOpts[i];
261 if (RequiresValue(Opt))
262 ++NumPositionalRequired;
263 else if (ConsumeAfterOpt) {
264 // ConsumeAfter cannot be combined with "optional" positional options
Chris Lattner54ec7ae2002-07-22 02:21:57 +0000265 // unless there is only one positional argument...
266 if (PositionalOpts.size() > 2)
267 ErrorParsing |=
268 Opt->error(" error - this positional option will never be matched, "
269 "because it does not Require a value, and a "
270 "cl::ConsumeAfter option is active!");
Chris Lattner9cf3d472003-07-30 17:34:02 +0000271 } else if (UnboundedFound && !Opt->ArgStr[0]) {
272 // This option does not "require" a value... Make sure this option is
273 // not specified after an option that eats all extra arguments, or this
274 // one will never get any!
Chris Lattner331de232002-07-22 02:07:59 +0000275 //
276 ErrorParsing |= Opt->error(" error - option can never match, because "
277 "another positional argument will match an "
278 "unbounded number of values, and this option"
279 " does not require a value!");
280 }
281 UnboundedFound |= EatsUnboundedNumberOfValues(Opt);
282 }
283 }
284
285 // PositionalVals - A vector of "positional" arguments we accumulate into to
286 // processes at the end...
287 //
Chris Lattnerca6433f2003-05-22 20:06:43 +0000288 std::vector<std::string> PositionalVals;
Chris Lattner331de232002-07-22 02:07:59 +0000289
Chris Lattner9cf3d472003-07-30 17:34:02 +0000290 // If the program has named positional arguments, and the name has been run
291 // across, keep track of which positional argument was named. Otherwise put
292 // the positional args into the PositionalVals list...
293 Option *ActivePositionalArg = 0;
294
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000295 // Loop over all of the arguments... processing them.
Chris Lattner331de232002-07-22 02:07:59 +0000296 bool DashDashFound = false; // Have we read '--'?
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000297 for (int i = 1; i < argc; ++i) {
298 Option *Handler = 0;
299 const char *Value = "";
300 const char *ArgName = "";
Chris Lattner331de232002-07-22 02:07:59 +0000301
302 // Check to see if this is a positional argument. This argument is
303 // considered to be positional if it doesn't start with '-', if it is "-"
Misha Brukman1115e042003-07-10 21:38:28 +0000304 // itself, or if we have seen "--" already.
Chris Lattner331de232002-07-22 02:07:59 +0000305 //
306 if (argv[i][0] != '-' || argv[i][1] == 0 || DashDashFound) {
307 // Positional argument!
Chris Lattner9cf3d472003-07-30 17:34:02 +0000308 if (ActivePositionalArg) {
309 ProvidePositionalOption(ActivePositionalArg, argv[i]);
310 continue; // We are done!
311 } else if (!PositionalOpts.empty()) {
Chris Lattner331de232002-07-22 02:07:59 +0000312 PositionalVals.push_back(argv[i]);
313
314 // All of the positional arguments have been fulfulled, give the rest to
315 // the consume after option... if it's specified...
316 //
Chris Lattnerd16714b2002-07-31 16:29:43 +0000317 if (PositionalVals.size() >= NumPositionalRequired &&
Chris Lattner331de232002-07-22 02:07:59 +0000318 ConsumeAfterOpt != 0) {
319 for (++i; i < argc; ++i)
320 PositionalVals.push_back(argv[i]);
321 break; // Handle outside of the argument processing loop...
322 }
323
324 // Delay processing positional arguments until the end...
325 continue;
326 }
327 } else { // We start with a '-', must be an argument...
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000328 ArgName = argv[i]+1;
329 while (*ArgName == '-') ++ArgName; // Eat leading dashes
330
Chris Lattner331de232002-07-22 02:07:59 +0000331 if (*ArgName == 0 && !DashDashFound) { // Is this the mythical "--"?
332 DashDashFound = true; // Yup, take note of that fact...
Misha Brukman950971d2003-09-16 15:31:46 +0000333 continue; // Don't try to process it as an argument itself.
Chris Lattner331de232002-07-22 02:07:59 +0000334 }
335
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000336 const char *ArgNameEnd = ArgName;
Chris Lattnerf78032f2001-11-26 18:58:34 +0000337 while (*ArgNameEnd && *ArgNameEnd != '=')
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000338 ++ArgNameEnd; // Scan till end of argument name...
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000339
340 Value = ArgNameEnd;
341 if (*Value) // If we have an equals sign...
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000342 ++Value; // Advance to value...
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000343
344 if (*ArgName != 0) {
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000345 std::string RealName(ArgName, ArgNameEnd);
346 // Extract arg name part
Chris Lattnerca6433f2003-05-22 20:06:43 +0000347 std::map<std::string, Option*>::iterator I = Opts.find(RealName);
Chris Lattnerf78032f2001-11-26 18:58:34 +0000348
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000349 if (I == Opts.end() && !*Value && RealName.size() > 1) {
Chris Lattner331de232002-07-22 02:07:59 +0000350 // Check to see if this "option" is really a prefixed or grouped
351 // argument...
352 //
353 unsigned Length = 0;
354 Option *PGOpt = getOptionPred(RealName, Length, isPrefixedOrGrouping);
Chris Lattnerf78032f2001-11-26 18:58:34 +0000355
Chris Lattner331de232002-07-22 02:07:59 +0000356 // If the option is a prefixed option, then the value is simply the
357 // rest of the name... so fall through to later processing, by
358 // setting up the argument name flags and value fields.
359 //
360 if (PGOpt && PGOpt->getFormattingFlag() == cl::Prefix) {
361 ArgNameEnd = ArgName+Length;
362 Value = ArgNameEnd;
Chris Lattnerca6433f2003-05-22 20:06:43 +0000363 I = Opts.find(std::string(ArgName, ArgNameEnd));
Chris Lattner331de232002-07-22 02:07:59 +0000364 assert(I->second == PGOpt);
365 } else if (PGOpt) {
366 // This must be a grouped option... handle all of them now...
367 assert(isGrouping(PGOpt) && "Broken getOptionPred!");
368
369 do {
370 // Move current arg name out of RealName into RealArgName...
Chris Lattnerca6433f2003-05-22 20:06:43 +0000371 std::string RealArgName(RealName.begin(),RealName.begin()+Length);
Chris Lattner331de232002-07-22 02:07:59 +0000372 RealName.erase(RealName.begin(), RealName.begin()+Length);
Chris Lattnerf78032f2001-11-26 18:58:34 +0000373
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000374 // Because ValueRequired is an invalid flag for grouped arguments,
375 // we don't need to pass argc/argv in...
376 //
Chris Lattner331de232002-07-22 02:07:59 +0000377 assert(PGOpt->getValueExpectedFlag() != cl::ValueRequired &&
378 "Option can not be cl::Grouping AND cl::ValueRequired!");
379 int Dummy;
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000380 ErrorParsing |= ProvideOption(PGOpt, RealArgName.c_str(), "",
Chris Lattner331de232002-07-22 02:07:59 +0000381 0, 0, Dummy);
382
383 // Get the next grouping option...
384 if (!RealName.empty())
385 PGOpt = getOptionPred(RealName, Length, isGrouping);
386 } while (!RealName.empty() && PGOpt);
387
388 if (RealName.empty()) // Processed all of the options, move on
389 continue; // to the next argv[] value...
390
391 // If RealName is not empty, that means we did not match one of the
392 // options! This is an error.
393 //
394 I = Opts.end();
395 }
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000396 }
Chris Lattnerf78032f2001-11-26 18:58:34 +0000397
Chris Lattner331de232002-07-22 02:07:59 +0000398 Handler = I != Opts.end() ? I->second : 0;
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000399 }
400 }
401
402 if (Handler == 0) {
Chris Lattnerca6433f2003-05-22 20:06:43 +0000403 std::cerr << "Unknown command line argument '" << argv[i] << "'. Try: "
404 << argv[0] << " --help'\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000405 ErrorParsing = true;
406 continue;
407 }
408
Chris Lattner72fb8e52003-05-22 20:26:17 +0000409 // Check to see if this option accepts a comma separated list of values. If
410 // it does, we have to split up the value into multiple values...
411 if (Handler->getMiscFlags() & CommaSeparated) {
412 std::string Val(Value);
413 std::string::size_type Pos = Val.find(',');
414
415 while (Pos != std::string::npos) {
416 // Process the portion before the comma...
417 ErrorParsing |= ProvideOption(Handler, ArgName,
418 std::string(Val.begin(),
419 Val.begin()+Pos).c_str(),
420 argc, argv, i);
421 // Erase the portion before the comma, AND the comma...
422 Val.erase(Val.begin(), Val.begin()+Pos+1);
423 Value += Pos+1; // Increment the original value pointer as well...
424
425 // Check for another comma...
426 Pos = Val.find(',');
427 }
428 }
Chris Lattner9cf3d472003-07-30 17:34:02 +0000429
430 // If this is a named positional argument, just remember that it is the
431 // active one...
432 if (Handler->getFormattingFlag() == cl::Positional)
433 ActivePositionalArg = Handler;
434 else
435 ErrorParsing |= ProvideOption(Handler, ArgName, Value, argc, argv, i);
Chris Lattner331de232002-07-22 02:07:59 +0000436 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000437
Chris Lattner331de232002-07-22 02:07:59 +0000438 // Check and handle positional arguments now...
439 if (NumPositionalRequired > PositionalVals.size()) {
Chris Lattnerca6433f2003-05-22 20:06:43 +0000440 std::cerr << "Not enough positional command line arguments specified!\n"
441 << "Must specify at least " << NumPositionalRequired
442 << " positional arguments: See: " << argv[0] << " --help\n";
Chris Lattner331de232002-07-22 02:07:59 +0000443 ErrorParsing = true;
444
445
446 } else if (ConsumeAfterOpt == 0) {
447 // Positional args have already been handled if ConsumeAfter is specified...
448 unsigned ValNo = 0, NumVals = PositionalVals.size();
449 for (unsigned i = 0, e = PositionalOpts.size(); i != e; ++i) {
450 if (RequiresValue(PositionalOpts[i])) {
451 ProvidePositionalOption(PositionalOpts[i], PositionalVals[ValNo++]);
452 --NumPositionalRequired; // We fulfilled our duty...
453 }
454
455 // If we _can_ give this option more arguments, do so now, as long as we
456 // do not give it values that others need. 'Done' controls whether the
457 // option even _WANTS_ any more.
458 //
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000459 bool Done = PositionalOpts[i]->getNumOccurrencesFlag() == cl::Required;
Chris Lattner331de232002-07-22 02:07:59 +0000460 while (NumVals-ValNo > NumPositionalRequired && !Done) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000461 switch (PositionalOpts[i]->getNumOccurrencesFlag()) {
Chris Lattner331de232002-07-22 02:07:59 +0000462 case cl::Optional:
463 Done = true; // Optional arguments want _at most_ one value
464 // FALL THROUGH
465 case cl::ZeroOrMore: // Zero or more will take all they can get...
466 case cl::OneOrMore: // One or more will take all they can get...
467 ProvidePositionalOption(PositionalOpts[i], PositionalVals[ValNo++]);
468 break;
469 default:
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000470 assert(0 && "Internal error, unexpected NumOccurrences flag in "
Chris Lattner331de232002-07-22 02:07:59 +0000471 "positional argument processing!");
472 }
473 }
Chris Lattnercaccd762001-10-27 05:54:17 +0000474 }
Chris Lattner331de232002-07-22 02:07:59 +0000475 } else {
476 assert(ConsumeAfterOpt && NumPositionalRequired <= PositionalVals.size());
477 unsigned ValNo = 0;
478 for (unsigned j = 1, e = PositionalOpts.size(); j != e; ++j)
479 if (RequiresValue(PositionalOpts[j]))
Chris Lattnerfaba8092002-07-24 20:15:13 +0000480 ErrorParsing |= ProvidePositionalOption(PositionalOpts[j],
481 PositionalVals[ValNo++]);
482
483 // Handle the case where there is just one positional option, and it's
484 // optional. In this case, we want to give JUST THE FIRST option to the
485 // positional option and keep the rest for the consume after. The above
486 // loop would have assigned no values to positional options in this case.
487 //
Chris Lattnerb490c202002-08-02 21:51:29 +0000488 if (PositionalOpts.size() == 2 && ValNo == 0 && !PositionalVals.empty())
Chris Lattnerfaba8092002-07-24 20:15:13 +0000489 ErrorParsing |= ProvidePositionalOption(PositionalOpts[1],
490 PositionalVals[ValNo++]);
Chris Lattner331de232002-07-22 02:07:59 +0000491
492 // Handle over all of the rest of the arguments to the
493 // cl::ConsumeAfter command line option...
494 for (; ValNo != PositionalVals.size(); ++ValNo)
495 ErrorParsing |= ProvidePositionalOption(ConsumeAfterOpt,
496 PositionalVals[ValNo]);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000497 }
498
Chris Lattnerdc4693d2001-07-23 23:02:45 +0000499 // Loop over args and make sure all required args are specified!
Chris Lattnerca6433f2003-05-22 20:06:43 +0000500 for (std::map<std::string, Option*>::iterator I = Opts.begin(),
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000501 E = Opts.end(); I != E; ++I) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000502 switch (I->second->getNumOccurrencesFlag()) {
Chris Lattnerdc4693d2001-07-23 23:02:45 +0000503 case Required:
504 case OneOrMore:
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000505 if (I->second->getNumOccurrences() == 0) {
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000506 I->second->error(" must be specified at least once!");
Chris Lattnerf038acb2001-10-24 06:21:56 +0000507 ErrorParsing = true;
508 }
Chris Lattnerdc4693d2001-07-23 23:02:45 +0000509 // Fall through
510 default:
511 break;
512 }
513 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000514
Chris Lattner331de232002-07-22 02:07:59 +0000515 // Free all of the memory allocated to the map. Command line options may only
516 // be processed once!
Chris Lattnere8e258b2002-07-29 20:58:42 +0000517 delete CommandLineOptions;
518 CommandLineOptions = 0;
Chris Lattner331de232002-07-22 02:07:59 +0000519 PositionalOpts.clear();
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000520
521 // If we had an error processing our arguments, don't let the program execute
522 if (ErrorParsing) exit(1);
523}
524
525//===----------------------------------------------------------------------===//
526// Option Base class implementation
527//
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000528
Chris Lattnerca6433f2003-05-22 20:06:43 +0000529bool Option::error(std::string Message, const char *ArgName) {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000530 if (ArgName == 0) ArgName = ArgStr;
Chris Lattner331de232002-07-22 02:07:59 +0000531 if (ArgName[0] == 0)
Chris Lattnerca6433f2003-05-22 20:06:43 +0000532 std::cerr << HelpStr; // Be nice for positional arguments
Chris Lattner331de232002-07-22 02:07:59 +0000533 else
Chris Lattnerca6433f2003-05-22 20:06:43 +0000534 std::cerr << "-" << ArgName;
535 std::cerr << " option" << Message << "\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000536 return true;
537}
538
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000539bool Option::addOccurrence(const char *ArgName, const std::string &Value) {
540 NumOccurrences++; // Increment the number of times we have been seen
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000541
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000542 switch (getNumOccurrencesFlag()) {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000543 case Optional:
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000544 if (NumOccurrences > 1)
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000545 return error(": may only occur zero or one times!", ArgName);
546 break;
547 case Required:
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000548 if (NumOccurrences > 1)
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000549 return error(": must occur exactly one time!", ArgName);
550 // Fall through
551 case OneOrMore:
Chris Lattnercaccd762001-10-27 05:54:17 +0000552 case ZeroOrMore:
553 case ConsumeAfter: break;
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000554 default: return error(": bad num occurrences flag value!");
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000555 }
556
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000557 return handleOccurrence(ArgName, Value);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000558}
559
Chris Lattner331de232002-07-22 02:07:59 +0000560// addArgument - Tell the system that this Option subclass will handle all
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000561// occurrences of -ArgStr on the command line.
Chris Lattner331de232002-07-22 02:07:59 +0000562//
563void Option::addArgument(const char *ArgStr) {
564 if (ArgStr[0])
565 AddArgument(ArgStr, this);
Chris Lattner9cf3d472003-07-30 17:34:02 +0000566
567 if (getFormattingFlag() == Positional)
Chris Lattner331de232002-07-22 02:07:59 +0000568 getPositionalOpts().push_back(this);
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000569 else if (getNumOccurrencesFlag() == ConsumeAfter) {
Chris Lattner9cf3d472003-07-30 17:34:02 +0000570 if (!getPositionalOpts().empty() &&
571 getPositionalOpts().front()->getNumOccurrencesFlag() == ConsumeAfter)
572 error("Cannot specify more than one option with cl::ConsumeAfter!");
Chris Lattner331de232002-07-22 02:07:59 +0000573 getPositionalOpts().insert(getPositionalOpts().begin(), this);
574 }
575}
576
Chris Lattneraa852bb2002-07-23 17:15:12 +0000577void Option::removeArgument(const char *ArgStr) {
Chris Lattner9cf3d472003-07-30 17:34:02 +0000578 if (ArgStr[0])
Chris Lattnere8e258b2002-07-29 20:58:42 +0000579 RemoveArgument(ArgStr, this);
Chris Lattner9cf3d472003-07-30 17:34:02 +0000580
581 if (getFormattingFlag() == Positional) {
Chris Lattnerca6433f2003-05-22 20:06:43 +0000582 std::vector<Option*>::iterator I =
Chris Lattneraa852bb2002-07-23 17:15:12 +0000583 std::find(getPositionalOpts().begin(), getPositionalOpts().end(), this);
584 assert(I != getPositionalOpts().end() && "Arg not registered!");
585 getPositionalOpts().erase(I);
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000586 } else if (getNumOccurrencesFlag() == ConsumeAfter) {
Chris Lattneraa852bb2002-07-23 17:15:12 +0000587 assert(!getPositionalOpts().empty() && getPositionalOpts()[0] == this &&
588 "Arg not registered correctly!");
589 getPositionalOpts().erase(getPositionalOpts().begin());
590 }
591}
592
Chris Lattner331de232002-07-22 02:07:59 +0000593
594// getValueStr - Get the value description string, using "DefaultMsg" if nothing
595// has been specified yet.
596//
597static const char *getValueStr(const Option &O, const char *DefaultMsg) {
598 if (O.ValueStr[0] == 0) return DefaultMsg;
599 return O.ValueStr;
600}
601
602//===----------------------------------------------------------------------===//
603// cl::alias class implementation
604//
605
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000606// Return the width of the option tag for printing...
Chris Lattner331de232002-07-22 02:07:59 +0000607unsigned alias::getOptionWidth() const {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000608 return std::strlen(ArgStr)+6;
609}
610
Chris Lattner331de232002-07-22 02:07:59 +0000611// Print out the option for the alias...
612void alias::printOptionInfo(unsigned GlobalWidth) const {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000613 unsigned L = std::strlen(ArgStr);
Chris Lattnerca6433f2003-05-22 20:06:43 +0000614 std::cerr << " -" << ArgStr << std::string(GlobalWidth-L-6, ' ') << " - "
615 << HelpStr << "\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000616}
617
618
Chris Lattner331de232002-07-22 02:07:59 +0000619
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000620//===----------------------------------------------------------------------===//
Chris Lattner331de232002-07-22 02:07:59 +0000621// Parser Implementation code...
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000622//
623
Chris Lattner9b14eb52002-08-07 18:36:37 +0000624// basic_parser implementation
625//
626
627// Return the width of the option tag for printing...
628unsigned basic_parser_impl::getOptionWidth(const Option &O) const {
629 unsigned Len = std::strlen(O.ArgStr);
630 if (const char *ValName = getValueName())
631 Len += std::strlen(getValueStr(O, ValName))+3;
632
633 return Len + 6;
634}
635
636// printOptionInfo - Print out information about this option. The
637// to-be-maintained width is specified.
638//
639void basic_parser_impl::printOptionInfo(const Option &O,
640 unsigned GlobalWidth) const {
Chris Lattnerca6433f2003-05-22 20:06:43 +0000641 std::cerr << " -" << O.ArgStr;
Chris Lattner9b14eb52002-08-07 18:36:37 +0000642
643 if (const char *ValName = getValueName())
Chris Lattnerca6433f2003-05-22 20:06:43 +0000644 std::cerr << "=<" << getValueStr(O, ValName) << ">";
Chris Lattner9b14eb52002-08-07 18:36:37 +0000645
Chris Lattnerca6433f2003-05-22 20:06:43 +0000646 std::cerr << std::string(GlobalWidth-getOptionWidth(O), ' ') << " - "
647 << O.HelpStr << "\n";
Chris Lattner9b14eb52002-08-07 18:36:37 +0000648}
649
650
651
652
Chris Lattner331de232002-07-22 02:07:59 +0000653// parser<bool> implementation
654//
Chris Lattner9b14eb52002-08-07 18:36:37 +0000655bool parser<bool>::parse(Option &O, const char *ArgName,
Chris Lattnerca6433f2003-05-22 20:06:43 +0000656 const std::string &Arg, bool &Value) {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000657 if (Arg == "" || Arg == "true" || Arg == "TRUE" || Arg == "True" ||
658 Arg == "1") {
659 Value = true;
660 } else if (Arg == "false" || Arg == "FALSE" || Arg == "False" || Arg == "0") {
661 Value = false;
662 } else {
Chris Lattner331de232002-07-22 02:07:59 +0000663 return O.error(": '" + Arg +
664 "' is invalid value for boolean argument! Try 0 or 1");
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000665 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000666 return false;
667}
668
Chris Lattner331de232002-07-22 02:07:59 +0000669// parser<int> implementation
670//
Chris Lattner9b14eb52002-08-07 18:36:37 +0000671bool parser<int>::parse(Option &O, const char *ArgName,
Chris Lattnerca6433f2003-05-22 20:06:43 +0000672 const std::string &Arg, int &Value) {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000673 char *End;
Chris Lattnerd2a6fc32003-06-28 15:47:20 +0000674 Value = (int)strtol(Arg.c_str(), &End, 0);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000675 if (*End != 0)
Chris Lattner331de232002-07-22 02:07:59 +0000676 return O.error(": '" + Arg + "' value invalid for integer argument!");
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000677 return false;
678}
679
Chris Lattnerd2a6fc32003-06-28 15:47:20 +0000680// parser<unsigned> implementation
681//
682bool parser<unsigned>::parse(Option &O, const char *ArgName,
683 const std::string &Arg, unsigned &Value) {
684 char *End;
685 long long int V = strtoll(Arg.c_str(), &End, 0);
686 Value = (unsigned)V;
687 if (*End != 0 || V < 0 || Value != V)
688 return O.error(": '" + Arg + "' value invalid for uint argument!");
689 return false;
690}
691
Chris Lattner9b14eb52002-08-07 18:36:37 +0000692// parser<double>/parser<float> implementation
Chris Lattnerd215fd12001-10-13 06:53:19 +0000693//
Chris Lattnerca6433f2003-05-22 20:06:43 +0000694static bool parseDouble(Option &O, const std::string &Arg, double &Value) {
Chris Lattner331de232002-07-22 02:07:59 +0000695 const char *ArgStart = Arg.c_str();
696 char *End;
697 Value = strtod(ArgStart, &End);
698 if (*End != 0)
699 return O.error(": '" +Arg+ "' value invalid for floating point argument!");
Chris Lattnerd215fd12001-10-13 06:53:19 +0000700 return false;
701}
702
Chris Lattner9b14eb52002-08-07 18:36:37 +0000703bool parser<double>::parse(Option &O, const char *AN,
704 const std::string &Arg, double &Val) {
705 return parseDouble(O, Arg, Val);
Chris Lattner331de232002-07-22 02:07:59 +0000706}
707
Chris Lattner9b14eb52002-08-07 18:36:37 +0000708bool parser<float>::parse(Option &O, const char *AN,
709 const std::string &Arg, float &Val) {
710 double dVal;
711 if (parseDouble(O, Arg, dVal))
712 return true;
713 Val = (float)dVal;
714 return false;
Chris Lattner331de232002-07-22 02:07:59 +0000715}
716
717
Chris Lattner331de232002-07-22 02:07:59 +0000718
719// generic_parser_base implementation
720//
721
Chris Lattneraa852bb2002-07-23 17:15:12 +0000722// findOption - Return the option number corresponding to the specified
723// argument string. If the option is not found, getNumOptions() is returned.
724//
725unsigned generic_parser_base::findOption(const char *Name) {
726 unsigned i = 0, e = getNumOptions();
Chris Lattnerca6433f2003-05-22 20:06:43 +0000727 std::string N(Name);
Chris Lattneraa852bb2002-07-23 17:15:12 +0000728
729 while (i != e)
730 if (getOption(i) == N)
731 return i;
732 else
733 ++i;
734 return e;
735}
736
737
Chris Lattner331de232002-07-22 02:07:59 +0000738// Return the width of the option tag for printing...
739unsigned generic_parser_base::getOptionWidth(const Option &O) const {
740 if (O.hasArgStr()) {
741 unsigned Size = std::strlen(O.ArgStr)+6;
742 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
743 Size = std::max(Size, (unsigned)std::strlen(getOption(i))+8);
744 return Size;
745 } else {
746 unsigned BaseSize = 0;
747 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
748 BaseSize = std::max(BaseSize, (unsigned)std::strlen(getOption(i))+8);
749 return BaseSize;
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000750 }
751}
752
Chris Lattner331de232002-07-22 02:07:59 +0000753// printOptionInfo - Print out information about this option. The
754// to-be-maintained width is specified.
755//
756void generic_parser_base::printOptionInfo(const Option &O,
757 unsigned GlobalWidth) const {
758 if (O.hasArgStr()) {
759 unsigned L = std::strlen(O.ArgStr);
Chris Lattnerca6433f2003-05-22 20:06:43 +0000760 std::cerr << " -" << O.ArgStr << std::string(GlobalWidth-L-6, ' ')
761 << " - " << O.HelpStr << "\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000762
Chris Lattner331de232002-07-22 02:07:59 +0000763 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
764 unsigned NumSpaces = GlobalWidth-strlen(getOption(i))-8;
Chris Lattnerca6433f2003-05-22 20:06:43 +0000765 std::cerr << " =" << getOption(i) << std::string(NumSpaces, ' ')
766 << " - " << getDescription(i) << "\n";
Chris Lattner9c9be482002-01-31 00:42:56 +0000767 }
Chris Lattner331de232002-07-22 02:07:59 +0000768 } else {
769 if (O.HelpStr[0])
Chris Lattnerca6433f2003-05-22 20:06:43 +0000770 std::cerr << " " << O.HelpStr << "\n";
Chris Lattner331de232002-07-22 02:07:59 +0000771 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
772 unsigned L = std::strlen(getOption(i));
Chris Lattnerca6433f2003-05-22 20:06:43 +0000773 std::cerr << " -" << getOption(i) << std::string(GlobalWidth-L-8, ' ')
774 << " - " << getDescription(i) << "\n";
Chris Lattner331de232002-07-22 02:07:59 +0000775 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000776 }
777}
778
779
780//===----------------------------------------------------------------------===//
Chris Lattner331de232002-07-22 02:07:59 +0000781// --help and --help-hidden option implementation
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000782//
783namespace {
784
Chris Lattner331de232002-07-22 02:07:59 +0000785class HelpPrinter {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000786 unsigned MaxArgLen;
787 const Option *EmptyArg;
788 const bool ShowHidden;
789
Chris Lattner331de232002-07-22 02:07:59 +0000790 // isHidden/isReallyHidden - Predicates to be used to filter down arg lists.
Chris Lattnerca6433f2003-05-22 20:06:43 +0000791 inline static bool isHidden(std::pair<std::string, Option *> &OptPair) {
Chris Lattner331de232002-07-22 02:07:59 +0000792 return OptPair.second->getOptionHiddenFlag() >= Hidden;
793 }
Chris Lattnerca6433f2003-05-22 20:06:43 +0000794 inline static bool isReallyHidden(std::pair<std::string, Option *> &OptPair) {
Chris Lattner331de232002-07-22 02:07:59 +0000795 return OptPair.second->getOptionHiddenFlag() == ReallyHidden;
796 }
797
798public:
799 HelpPrinter(bool showHidden) : ShowHidden(showHidden) {
800 EmptyArg = 0;
801 }
802
803 void operator=(bool Value) {
804 if (Value == false) return;
805
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000806 // Copy Options into a vector so we can sort them as we like...
Chris Lattnerca6433f2003-05-22 20:06:43 +0000807 std::vector<std::pair<std::string, Option*> > Options;
Chris Lattner697954c2002-01-20 22:54:45 +0000808 copy(getOpts().begin(), getOpts().end(), std::back_inserter(Options));
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000809
810 // Eliminate Hidden or ReallyHidden arguments, depending on ShowHidden
Chris Lattner331de232002-07-22 02:07:59 +0000811 Options.erase(std::remove_if(Options.begin(), Options.end(),
812 std::ptr_fun(ShowHidden ? isReallyHidden : isHidden)),
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000813 Options.end());
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000814
815 // Eliminate duplicate entries in table (from enum flags options, f.e.)
Chris Lattner331de232002-07-22 02:07:59 +0000816 { // Give OptionSet a scope
817 std::set<Option*> OptionSet;
818 for (unsigned i = 0; i != Options.size(); ++i)
819 if (OptionSet.count(Options[i].second) == 0)
820 OptionSet.insert(Options[i].second); // Add new entry to set
821 else
822 Options.erase(Options.begin()+i--); // Erase duplicate
823 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000824
825 if (ProgramOverview)
Chris Lattnerca6433f2003-05-22 20:06:43 +0000826 std::cerr << "OVERVIEW:" << ProgramOverview << "\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000827
Chris Lattnerca6433f2003-05-22 20:06:43 +0000828 std::cerr << "USAGE: " << ProgramName << " [options]";
Chris Lattner331de232002-07-22 02:07:59 +0000829
830 // Print out the positional options...
Chris Lattnerca6433f2003-05-22 20:06:43 +0000831 std::vector<Option*> &PosOpts = getPositionalOpts();
Chris Lattner331de232002-07-22 02:07:59 +0000832 Option *CAOpt = 0; // The cl::ConsumeAfter option, if it exists...
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000833 if (!PosOpts.empty() && PosOpts[0]->getNumOccurrencesFlag() == ConsumeAfter)
Chris Lattner331de232002-07-22 02:07:59 +0000834 CAOpt = PosOpts[0];
835
Chris Lattner9cf3d472003-07-30 17:34:02 +0000836 for (unsigned i = CAOpt != 0, e = PosOpts.size(); i != e; ++i) {
837 if (PosOpts[i]->ArgStr[0])
838 std::cerr << " --" << PosOpts[i]->ArgStr;
Chris Lattnerca6433f2003-05-22 20:06:43 +0000839 std::cerr << " " << PosOpts[i]->HelpStr;
Chris Lattner9cf3d472003-07-30 17:34:02 +0000840 }
Chris Lattner331de232002-07-22 02:07:59 +0000841
842 // Print the consume after option info if it exists...
Chris Lattnerca6433f2003-05-22 20:06:43 +0000843 if (CAOpt) std::cerr << " " << CAOpt->HelpStr;
Chris Lattner331de232002-07-22 02:07:59 +0000844
Chris Lattnerca6433f2003-05-22 20:06:43 +0000845 std::cerr << "\n\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000846
847 // Compute the maximum argument length...
848 MaxArgLen = 0;
Chris Lattner331de232002-07-22 02:07:59 +0000849 for (unsigned i = 0, e = Options.size(); i != e; ++i)
850 MaxArgLen = std::max(MaxArgLen, Options[i].second->getOptionWidth());
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000851
Chris Lattnerca6433f2003-05-22 20:06:43 +0000852 std::cerr << "OPTIONS:\n";
Chris Lattner331de232002-07-22 02:07:59 +0000853 for (unsigned i = 0, e = Options.size(); i != e; ++i)
854 Options[i].second->printOptionInfo(MaxArgLen);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000855
Chris Lattner331de232002-07-22 02:07:59 +0000856 // Halt the program if help information is printed
857 exit(1);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000858 }
859};
860
Chris Lattner331de232002-07-22 02:07:59 +0000861
862
863// Define the two HelpPrinter instances that are used to print out help, or
864// help-hidden...
865//
866HelpPrinter NormalPrinter(false);
867HelpPrinter HiddenPrinter(true);
868
869cl::opt<HelpPrinter, true, parser<bool> >
870HOp("help", cl::desc("display available options (--help-hidden for more)"),
Chris Lattner9b14eb52002-08-07 18:36:37 +0000871 cl::location(NormalPrinter), cl::ValueDisallowed);
Chris Lattner331de232002-07-22 02:07:59 +0000872
873cl::opt<HelpPrinter, true, parser<bool> >
874HHOp("help-hidden", cl::desc("display all available options"),
Chris Lattner9b14eb52002-08-07 18:36:37 +0000875 cl::location(HiddenPrinter), cl::Hidden, cl::ValueDisallowed);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000876
877} // End anonymous namespace