blob: 807ff165b09443a8a20d39e1c5bd505bd7072ab2 [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>
Brian Gaeke2d6a2362003-10-10 17:01:36 +000017#include <cstdlib>
18#include <cerrno>
Chris Lattner7f1576f2002-02-24 23:02:12 +000019
Chris Lattnerdbab15a2001-07-23 17:17:47 +000020using namespace cl;
21
Chris Lattner331de232002-07-22 02:07:59 +000022//===----------------------------------------------------------------------===//
23// Basic, shared command line option processing machinery...
24//
25
Chris Lattnerdbab15a2001-07-23 17:17:47 +000026// Return the global command line option vector. Making it a function scoped
Chris Lattnerf78032f2001-11-26 18:58:34 +000027// static ensures that it will be initialized correctly before its first use.
Chris Lattnerdbab15a2001-07-23 17:17:47 +000028//
Chris Lattnerca6433f2003-05-22 20:06:43 +000029static std::map<std::string, Option*> *CommandLineOptions = 0;
30static std::map<std::string, Option*> &getOpts() {
31 if (CommandLineOptions == 0)
32 CommandLineOptions = new std::map<std::string,Option*>();
Chris Lattnere8e258b2002-07-29 20:58:42 +000033 return *CommandLineOptions;
34}
35
Chris Lattnerca6433f2003-05-22 20:06:43 +000036static Option *getOption(const std::string &Str) {
Chris Lattnere8e258b2002-07-29 20:58:42 +000037 if (CommandLineOptions == 0) return 0;
Chris Lattnerca6433f2003-05-22 20:06:43 +000038 std::map<std::string,Option*>::iterator I = CommandLineOptions->find(Str);
Chris Lattnere8e258b2002-07-29 20:58:42 +000039 return I != CommandLineOptions->end() ? I->second : 0;
Chris Lattnerdbab15a2001-07-23 17:17:47 +000040}
41
Chris Lattnerca6433f2003-05-22 20:06:43 +000042static std::vector<Option*> &getPositionalOpts() {
43 static std::vector<Option*> Positional;
Chris Lattner331de232002-07-22 02:07:59 +000044 return Positional;
45}
46
Chris Lattnere8e258b2002-07-29 20:58:42 +000047static void AddArgument(const char *ArgName, Option *Opt) {
48 if (getOption(ArgName)) {
Chris Lattnerca6433f2003-05-22 20:06:43 +000049 std::cerr << "CommandLine Error: Argument '" << ArgName
50 << "' defined more than once!\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +000051 } else {
Chris Lattnerf78032f2001-11-26 18:58:34 +000052 // Add argument to the argument map!
Chris Lattnere8e258b2002-07-29 20:58:42 +000053 getOpts()[ArgName] = Opt;
54 }
55}
56
57// RemoveArgument - It's possible that the argument is no longer in the map if
58// options have already been processed and the map has been deleted!
59//
60static void RemoveArgument(const char *ArgName, Option *Opt) {
61 if (CommandLineOptions == 0) return;
62 assert(getOption(ArgName) == Opt && "Arg not in map!");
63 CommandLineOptions->erase(ArgName);
64 if (CommandLineOptions->empty()) {
65 delete CommandLineOptions;
66 CommandLineOptions = 0;
Chris Lattnerdbab15a2001-07-23 17:17:47 +000067 }
68}
69
70static const char *ProgramName = 0;
71static const char *ProgramOverview = 0;
72
Chris Lattnercaccd762001-10-27 05:54:17 +000073static inline bool ProvideOption(Option *Handler, const char *ArgName,
74 const char *Value, int argc, char **argv,
75 int &i) {
76 // Enforce value requirements
77 switch (Handler->getValueExpectedFlag()) {
78 case ValueRequired:
79 if (Value == 0 || *Value == 0) { // No value specified?
80 if (i+1 < argc) { // Steal the next argument, like for '-o filename'
81 Value = argv[++i];
82 } else {
83 return Handler->error(" requires a value!");
84 }
85 }
86 break;
87 case ValueDisallowed:
88 if (*Value != 0)
89 return Handler->error(" does not allow a value! '" +
Chris Lattnerca6433f2003-05-22 20:06:43 +000090 std::string(Value) + "' specified.");
Chris Lattnercaccd762001-10-27 05:54:17 +000091 break;
92 case ValueOptional: break;
Chris Lattnerca6433f2003-05-22 20:06:43 +000093 default: std::cerr << "Bad ValueMask flag! CommandLine usage error:"
94 << Handler->getValueExpectedFlag() << "\n"; abort();
Chris Lattnercaccd762001-10-27 05:54:17 +000095 }
96
97 // Run the handler now!
Misha Brukmandd6cb6a2003-07-10 16:49:51 +000098 return Handler->addOccurrence(ArgName, Value);
Chris Lattnercaccd762001-10-27 05:54:17 +000099}
100
Chris Lattner9cf3d472003-07-30 17:34:02 +0000101static bool ProvidePositionalOption(Option *Handler, const std::string &Arg) {
Chris Lattner331de232002-07-22 02:07:59 +0000102 int Dummy;
Chris Lattner9cf3d472003-07-30 17:34:02 +0000103 return ProvideOption(Handler, Handler->ArgStr, Arg.c_str(), 0, 0, Dummy);
Chris Lattner331de232002-07-22 02:07:59 +0000104}
Chris Lattnerf78032f2001-11-26 18:58:34 +0000105
Chris Lattner331de232002-07-22 02:07:59 +0000106
107// Option predicates...
108static inline bool isGrouping(const Option *O) {
109 return O->getFormattingFlag() == cl::Grouping;
110}
111static inline bool isPrefixedOrGrouping(const Option *O) {
112 return isGrouping(O) || O->getFormattingFlag() == cl::Prefix;
113}
114
115// getOptionPred - Check to see if there are any options that satisfy the
116// specified predicate with names that are the prefixes in Name. This is
117// checked by progressively stripping characters off of the name, checking to
118// see if there options that satisfy the predicate. If we find one, return it,
119// otherwise return null.
120//
121static Option *getOptionPred(std::string Name, unsigned &Length,
122 bool (*Pred)(const Option*)) {
123
Chris Lattnere8e258b2002-07-29 20:58:42 +0000124 Option *Op = getOption(Name);
125 if (Op && Pred(Op)) {
Chris Lattner331de232002-07-22 02:07:59 +0000126 Length = Name.length();
Chris Lattnere8e258b2002-07-29 20:58:42 +0000127 return Op;
Chris Lattnerf78032f2001-11-26 18:58:34 +0000128 }
129
Chris Lattner331de232002-07-22 02:07:59 +0000130 if (Name.size() == 1) return 0;
131 do {
132 Name.erase(Name.end()-1, Name.end()); // Chop off the last character...
Chris Lattnere8e258b2002-07-29 20:58:42 +0000133 Op = getOption(Name);
Chris Lattner331de232002-07-22 02:07:59 +0000134
135 // Loop while we haven't found an option and Name still has at least two
136 // characters in it (so that the next iteration will not be the empty
137 // string...
Chris Lattnere8e258b2002-07-29 20:58:42 +0000138 } while ((Op == 0 || !Pred(Op)) && Name.size() > 1);
Chris Lattner331de232002-07-22 02:07:59 +0000139
Chris Lattnere8e258b2002-07-29 20:58:42 +0000140 if (Op && Pred(Op)) {
Chris Lattner331de232002-07-22 02:07:59 +0000141 Length = Name.length();
Chris Lattnere8e258b2002-07-29 20:58:42 +0000142 return Op; // Found one!
Chris Lattner331de232002-07-22 02:07:59 +0000143 }
144 return 0; // No option found!
145}
146
147static bool RequiresValue(const Option *O) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000148 return O->getNumOccurrencesFlag() == cl::Required ||
149 O->getNumOccurrencesFlag() == cl::OneOrMore;
Chris Lattner331de232002-07-22 02:07:59 +0000150}
151
152static bool EatsUnboundedNumberOfValues(const Option *O) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000153 return O->getNumOccurrencesFlag() == cl::ZeroOrMore ||
154 O->getNumOccurrencesFlag() == cl::OneOrMore;
Chris Lattnerf78032f2001-11-26 18:58:34 +0000155}
Chris Lattnercaccd762001-10-27 05:54:17 +0000156
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000157/// ParseCStringVector - Break INPUT up wherever one or more
158/// whitespace characters are found, and store the resulting tokens in
159/// OUTPUT. The tokens stored in OUTPUT are dynamically allocated
160/// using strdup (), so it is the caller's responsibility to free ()
161/// them later.
Brian Gaeke06b06c52003-08-14 22:00:59 +0000162///
163static void ParseCStringVector (std::vector<char *> &output,
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000164 const char *input) {
165 // Characters which will be treated as token separators:
166 static const char *delims = " \v\f\t\r\n";
167
168 std::string work (input);
169 // Skip past any delims at head of input string.
170 size_t pos = work.find_first_not_of (delims);
171 // If the string consists entirely of delims, then exit early.
172 if (pos == std::string::npos) return;
173 // Otherwise, jump forward to beginning of first word.
174 work = work.substr (pos);
175 // Find position of first delimiter.
176 pos = work.find_first_of (delims);
177
178 while (!work.empty() && pos != std::string::npos) {
179 // Everything from 0 to POS is the next word to copy.
180 output.push_back (strdup (work.substr (0,pos).c_str ()));
181 // Is there another word in the string?
182 size_t nextpos = work.find_first_not_of (delims, pos + 1);
183 if (nextpos != std::string::npos) {
184 // Yes? Then remove delims from beginning ...
185 work = work.substr (work.find_first_not_of (delims, pos + 1));
186 // and find the end of the word.
187 pos = work.find_first_of (delims);
188 } else {
189 // No? (Remainder of string is delims.) End the loop.
190 work = "";
191 pos = std::string::npos;
192 }
193 }
194
195 // If `input' ended with non-delim char, then we'll get here with
196 // the last word of `input' in `work'; copy it now.
197 if (!work.empty ()) {
198 output.push_back (strdup (work.c_str ()));
Brian Gaeke06b06c52003-08-14 22:00:59 +0000199 }
200}
201
202/// ParseEnvironmentOptions - An alternative entry point to the
203/// CommandLine library, which allows you to read the program's name
204/// from the caller (as PROGNAME) and its command-line arguments from
205/// an environment variable (whose name is given in ENVVAR).
206///
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000207void cl::ParseEnvironmentOptions (const char *progName, const char *envVar,
Brian Gaeke06b06c52003-08-14 22:00:59 +0000208 const char *Overview) {
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000209 // Check args.
210 assert (progName && "Program name not specified");
211 assert (envVar && "Environment variable name missing");
212
213 // Get the environment variable they want us to parse options out of.
214 const char *envValue = getenv (envVar);
215 if (!envValue)
216 return;
217
Brian Gaeke06b06c52003-08-14 22:00:59 +0000218 // Get program's "name", which we wouldn't know without the caller
219 // telling us.
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000220 std::vector<char *> newArgv;
221 newArgv.push_back (strdup (progName));
Brian Gaeke06b06c52003-08-14 22:00:59 +0000222
223 // Parse the value of the environment variable into a "command line"
224 // and hand it off to ParseCommandLineOptions().
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000225 ParseCStringVector (newArgv, envValue);
226 int newArgc = newArgv.size ();
227 ParseCommandLineOptions (newArgc, &newArgv[0], Overview);
228
229 // Free all the strdup()ed strings.
230 for (std::vector<char *>::iterator i = newArgv.begin (), e = newArgv.end ();
231 i != e; ++i) {
232 free (*i);
233 }
Brian Gaeke06b06c52003-08-14 22:00:59 +0000234}
235
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000236void cl::ParseCommandLineOptions(int &argc, char **argv,
Chris Lattner0c0edf82002-07-25 06:17:51 +0000237 const char *Overview) {
Chris Lattner331de232002-07-22 02:07:59 +0000238 assert((!getOpts().empty() || !getPositionalOpts().empty()) &&
239 "No options specified, or ParseCommandLineOptions called more"
240 " than once!");
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000241 ProgramName = argv[0]; // Save this away safe and snug
242 ProgramOverview = Overview;
243 bool ErrorParsing = false;
244
Chris Lattnerca6433f2003-05-22 20:06:43 +0000245 std::map<std::string, Option*> &Opts = getOpts();
246 std::vector<Option*> &PositionalOpts = getPositionalOpts();
Chris Lattner331de232002-07-22 02:07:59 +0000247
248 // Check out the positional arguments to collect information about them.
249 unsigned NumPositionalRequired = 0;
250 Option *ConsumeAfterOpt = 0;
251 if (!PositionalOpts.empty()) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000252 if (PositionalOpts[0]->getNumOccurrencesFlag() == cl::ConsumeAfter) {
Chris Lattner331de232002-07-22 02:07:59 +0000253 assert(PositionalOpts.size() > 1 &&
254 "Cannot specify cl::ConsumeAfter without a positional argument!");
255 ConsumeAfterOpt = PositionalOpts[0];
256 }
257
258 // Calculate how many positional values are _required_.
259 bool UnboundedFound = false;
260 for (unsigned i = ConsumeAfterOpt != 0, e = PositionalOpts.size();
261 i != e; ++i) {
262 Option *Opt = PositionalOpts[i];
263 if (RequiresValue(Opt))
264 ++NumPositionalRequired;
265 else if (ConsumeAfterOpt) {
266 // ConsumeAfter cannot be combined with "optional" positional options
Chris Lattner54ec7ae2002-07-22 02:21:57 +0000267 // unless there is only one positional argument...
268 if (PositionalOpts.size() > 2)
269 ErrorParsing |=
270 Opt->error(" error - this positional option will never be matched, "
271 "because it does not Require a value, and a "
272 "cl::ConsumeAfter option is active!");
Chris Lattner9cf3d472003-07-30 17:34:02 +0000273 } else if (UnboundedFound && !Opt->ArgStr[0]) {
274 // This option does not "require" a value... Make sure this option is
275 // not specified after an option that eats all extra arguments, or this
276 // one will never get any!
Chris Lattner331de232002-07-22 02:07:59 +0000277 //
278 ErrorParsing |= Opt->error(" error - option can never match, because "
279 "another positional argument will match an "
280 "unbounded number of values, and this option"
281 " does not require a value!");
282 }
283 UnboundedFound |= EatsUnboundedNumberOfValues(Opt);
284 }
285 }
286
287 // PositionalVals - A vector of "positional" arguments we accumulate into to
288 // processes at the end...
289 //
Chris Lattnerca6433f2003-05-22 20:06:43 +0000290 std::vector<std::string> PositionalVals;
Chris Lattner331de232002-07-22 02:07:59 +0000291
Chris Lattner9cf3d472003-07-30 17:34:02 +0000292 // If the program has named positional arguments, and the name has been run
293 // across, keep track of which positional argument was named. Otherwise put
294 // the positional args into the PositionalVals list...
295 Option *ActivePositionalArg = 0;
296
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000297 // Loop over all of the arguments... processing them.
Chris Lattner331de232002-07-22 02:07:59 +0000298 bool DashDashFound = false; // Have we read '--'?
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000299 for (int i = 1; i < argc; ++i) {
300 Option *Handler = 0;
301 const char *Value = "";
302 const char *ArgName = "";
Chris Lattner331de232002-07-22 02:07:59 +0000303
304 // Check to see if this is a positional argument. This argument is
305 // considered to be positional if it doesn't start with '-', if it is "-"
Misha Brukman1115e042003-07-10 21:38:28 +0000306 // itself, or if we have seen "--" already.
Chris Lattner331de232002-07-22 02:07:59 +0000307 //
308 if (argv[i][0] != '-' || argv[i][1] == 0 || DashDashFound) {
309 // Positional argument!
Chris Lattner9cf3d472003-07-30 17:34:02 +0000310 if (ActivePositionalArg) {
311 ProvidePositionalOption(ActivePositionalArg, argv[i]);
312 continue; // We are done!
313 } else if (!PositionalOpts.empty()) {
Chris Lattner331de232002-07-22 02:07:59 +0000314 PositionalVals.push_back(argv[i]);
315
316 // All of the positional arguments have been fulfulled, give the rest to
317 // the consume after option... if it's specified...
318 //
Chris Lattnerd16714b2002-07-31 16:29:43 +0000319 if (PositionalVals.size() >= NumPositionalRequired &&
Chris Lattner331de232002-07-22 02:07:59 +0000320 ConsumeAfterOpt != 0) {
321 for (++i; i < argc; ++i)
322 PositionalVals.push_back(argv[i]);
323 break; // Handle outside of the argument processing loop...
324 }
325
326 // Delay processing positional arguments until the end...
327 continue;
328 }
329 } else { // We start with a '-', must be an argument...
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000330 ArgName = argv[i]+1;
331 while (*ArgName == '-') ++ArgName; // Eat leading dashes
332
Chris Lattner331de232002-07-22 02:07:59 +0000333 if (*ArgName == 0 && !DashDashFound) { // Is this the mythical "--"?
334 DashDashFound = true; // Yup, take note of that fact...
Misha Brukman950971d2003-09-16 15:31:46 +0000335 continue; // Don't try to process it as an argument itself.
Chris Lattner331de232002-07-22 02:07:59 +0000336 }
337
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000338 const char *ArgNameEnd = ArgName;
Chris Lattnerf78032f2001-11-26 18:58:34 +0000339 while (*ArgNameEnd && *ArgNameEnd != '=')
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000340 ++ArgNameEnd; // Scan till end of argument name...
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000341
342 Value = ArgNameEnd;
343 if (*Value) // If we have an equals sign...
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000344 ++Value; // Advance to value...
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000345
346 if (*ArgName != 0) {
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000347 std::string RealName(ArgName, ArgNameEnd);
348 // Extract arg name part
Chris Lattnerca6433f2003-05-22 20:06:43 +0000349 std::map<std::string, Option*>::iterator I = Opts.find(RealName);
Chris Lattnerf78032f2001-11-26 18:58:34 +0000350
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000351 if (I == Opts.end() && !*Value && RealName.size() > 1) {
Chris Lattner331de232002-07-22 02:07:59 +0000352 // Check to see if this "option" is really a prefixed or grouped
353 // argument...
354 //
355 unsigned Length = 0;
356 Option *PGOpt = getOptionPred(RealName, Length, isPrefixedOrGrouping);
Chris Lattnerf78032f2001-11-26 18:58:34 +0000357
Chris Lattner331de232002-07-22 02:07:59 +0000358 // If the option is a prefixed option, then the value is simply the
359 // rest of the name... so fall through to later processing, by
360 // setting up the argument name flags and value fields.
361 //
362 if (PGOpt && PGOpt->getFormattingFlag() == cl::Prefix) {
363 ArgNameEnd = ArgName+Length;
364 Value = ArgNameEnd;
Chris Lattnerca6433f2003-05-22 20:06:43 +0000365 I = Opts.find(std::string(ArgName, ArgNameEnd));
Chris Lattner331de232002-07-22 02:07:59 +0000366 assert(I->second == PGOpt);
367 } else if (PGOpt) {
368 // This must be a grouped option... handle all of them now...
369 assert(isGrouping(PGOpt) && "Broken getOptionPred!");
370
371 do {
372 // Move current arg name out of RealName into RealArgName...
Chris Lattnerca6433f2003-05-22 20:06:43 +0000373 std::string RealArgName(RealName.begin(),RealName.begin()+Length);
Chris Lattner331de232002-07-22 02:07:59 +0000374 RealName.erase(RealName.begin(), RealName.begin()+Length);
Chris Lattnerf78032f2001-11-26 18:58:34 +0000375
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000376 // Because ValueRequired is an invalid flag for grouped arguments,
377 // we don't need to pass argc/argv in...
378 //
Chris Lattner331de232002-07-22 02:07:59 +0000379 assert(PGOpt->getValueExpectedFlag() != cl::ValueRequired &&
380 "Option can not be cl::Grouping AND cl::ValueRequired!");
381 int Dummy;
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000382 ErrorParsing |= ProvideOption(PGOpt, RealArgName.c_str(), "",
Chris Lattner331de232002-07-22 02:07:59 +0000383 0, 0, Dummy);
384
385 // Get the next grouping option...
386 if (!RealName.empty())
387 PGOpt = getOptionPred(RealName, Length, isGrouping);
388 } while (!RealName.empty() && PGOpt);
389
390 if (RealName.empty()) // Processed all of the options, move on
391 continue; // to the next argv[] value...
392
393 // If RealName is not empty, that means we did not match one of the
394 // options! This is an error.
395 //
396 I = Opts.end();
397 }
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000398 }
Chris Lattnerf78032f2001-11-26 18:58:34 +0000399
Chris Lattner331de232002-07-22 02:07:59 +0000400 Handler = I != Opts.end() ? I->second : 0;
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000401 }
402 }
403
404 if (Handler == 0) {
Brian Gaekec86e84b2003-09-16 18:00:35 +0000405 std::cerr << "Unknown command line argument '" << argv[i] << "'. Try: '"
Chris Lattnerca6433f2003-05-22 20:06:43 +0000406 << argv[0] << " --help'\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000407 ErrorParsing = true;
408 continue;
409 }
410
Chris Lattner72fb8e52003-05-22 20:26:17 +0000411 // Check to see if this option accepts a comma separated list of values. If
412 // it does, we have to split up the value into multiple values...
413 if (Handler->getMiscFlags() & CommaSeparated) {
414 std::string Val(Value);
415 std::string::size_type Pos = Val.find(',');
416
417 while (Pos != std::string::npos) {
418 // Process the portion before the comma...
419 ErrorParsing |= ProvideOption(Handler, ArgName,
420 std::string(Val.begin(),
421 Val.begin()+Pos).c_str(),
422 argc, argv, i);
423 // Erase the portion before the comma, AND the comma...
424 Val.erase(Val.begin(), Val.begin()+Pos+1);
425 Value += Pos+1; // Increment the original value pointer as well...
426
427 // Check for another comma...
428 Pos = Val.find(',');
429 }
430 }
Chris Lattner9cf3d472003-07-30 17:34:02 +0000431
432 // If this is a named positional argument, just remember that it is the
433 // active one...
434 if (Handler->getFormattingFlag() == cl::Positional)
435 ActivePositionalArg = Handler;
436 else
437 ErrorParsing |= ProvideOption(Handler, ArgName, Value, argc, argv, i);
Chris Lattner331de232002-07-22 02:07:59 +0000438 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000439
Chris Lattner331de232002-07-22 02:07:59 +0000440 // Check and handle positional arguments now...
441 if (NumPositionalRequired > PositionalVals.size()) {
Chris Lattnerca6433f2003-05-22 20:06:43 +0000442 std::cerr << "Not enough positional command line arguments specified!\n"
443 << "Must specify at least " << NumPositionalRequired
444 << " positional arguments: See: " << argv[0] << " --help\n";
Chris Lattner331de232002-07-22 02:07:59 +0000445 ErrorParsing = true;
446
447
448 } else if (ConsumeAfterOpt == 0) {
449 // Positional args have already been handled if ConsumeAfter is specified...
450 unsigned ValNo = 0, NumVals = PositionalVals.size();
451 for (unsigned i = 0, e = PositionalOpts.size(); i != e; ++i) {
452 if (RequiresValue(PositionalOpts[i])) {
453 ProvidePositionalOption(PositionalOpts[i], PositionalVals[ValNo++]);
454 --NumPositionalRequired; // We fulfilled our duty...
455 }
456
457 // If we _can_ give this option more arguments, do so now, as long as we
458 // do not give it values that others need. 'Done' controls whether the
459 // option even _WANTS_ any more.
460 //
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000461 bool Done = PositionalOpts[i]->getNumOccurrencesFlag() == cl::Required;
Chris Lattner331de232002-07-22 02:07:59 +0000462 while (NumVals-ValNo > NumPositionalRequired && !Done) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000463 switch (PositionalOpts[i]->getNumOccurrencesFlag()) {
Chris Lattner331de232002-07-22 02:07:59 +0000464 case cl::Optional:
465 Done = true; // Optional arguments want _at most_ one value
466 // FALL THROUGH
467 case cl::ZeroOrMore: // Zero or more will take all they can get...
468 case cl::OneOrMore: // One or more will take all they can get...
469 ProvidePositionalOption(PositionalOpts[i], PositionalVals[ValNo++]);
470 break;
471 default:
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000472 assert(0 && "Internal error, unexpected NumOccurrences flag in "
Chris Lattner331de232002-07-22 02:07:59 +0000473 "positional argument processing!");
474 }
475 }
Chris Lattnercaccd762001-10-27 05:54:17 +0000476 }
Chris Lattner331de232002-07-22 02:07:59 +0000477 } else {
478 assert(ConsumeAfterOpt && NumPositionalRequired <= PositionalVals.size());
479 unsigned ValNo = 0;
480 for (unsigned j = 1, e = PositionalOpts.size(); j != e; ++j)
481 if (RequiresValue(PositionalOpts[j]))
Chris Lattnerfaba8092002-07-24 20:15:13 +0000482 ErrorParsing |= ProvidePositionalOption(PositionalOpts[j],
483 PositionalVals[ValNo++]);
484
485 // Handle the case where there is just one positional option, and it's
486 // optional. In this case, we want to give JUST THE FIRST option to the
487 // positional option and keep the rest for the consume after. The above
488 // loop would have assigned no values to positional options in this case.
489 //
Chris Lattnerb490c202002-08-02 21:51:29 +0000490 if (PositionalOpts.size() == 2 && ValNo == 0 && !PositionalVals.empty())
Chris Lattnerfaba8092002-07-24 20:15:13 +0000491 ErrorParsing |= ProvidePositionalOption(PositionalOpts[1],
492 PositionalVals[ValNo++]);
Chris Lattner331de232002-07-22 02:07:59 +0000493
494 // Handle over all of the rest of the arguments to the
495 // cl::ConsumeAfter command line option...
496 for (; ValNo != PositionalVals.size(); ++ValNo)
497 ErrorParsing |= ProvidePositionalOption(ConsumeAfterOpt,
498 PositionalVals[ValNo]);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000499 }
500
Chris Lattnerdc4693d2001-07-23 23:02:45 +0000501 // Loop over args and make sure all required args are specified!
Chris Lattnerca6433f2003-05-22 20:06:43 +0000502 for (std::map<std::string, Option*>::iterator I = Opts.begin(),
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000503 E = Opts.end(); I != E; ++I) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000504 switch (I->second->getNumOccurrencesFlag()) {
Chris Lattnerdc4693d2001-07-23 23:02:45 +0000505 case Required:
506 case OneOrMore:
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000507 if (I->second->getNumOccurrences() == 0) {
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000508 I->second->error(" must be specified at least once!");
Chris Lattnerf038acb2001-10-24 06:21:56 +0000509 ErrorParsing = true;
510 }
Chris Lattnerdc4693d2001-07-23 23:02:45 +0000511 // Fall through
512 default:
513 break;
514 }
515 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000516
Chris Lattner331de232002-07-22 02:07:59 +0000517 // Free all of the memory allocated to the map. Command line options may only
518 // be processed once!
Chris Lattnere8e258b2002-07-29 20:58:42 +0000519 delete CommandLineOptions;
520 CommandLineOptions = 0;
Chris Lattner331de232002-07-22 02:07:59 +0000521 PositionalOpts.clear();
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000522
523 // If we had an error processing our arguments, don't let the program execute
524 if (ErrorParsing) exit(1);
525}
526
527//===----------------------------------------------------------------------===//
528// Option Base class implementation
529//
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000530
Chris Lattnerca6433f2003-05-22 20:06:43 +0000531bool Option::error(std::string Message, const char *ArgName) {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000532 if (ArgName == 0) ArgName = ArgStr;
Chris Lattner331de232002-07-22 02:07:59 +0000533 if (ArgName[0] == 0)
Chris Lattnerca6433f2003-05-22 20:06:43 +0000534 std::cerr << HelpStr; // Be nice for positional arguments
Chris Lattner331de232002-07-22 02:07:59 +0000535 else
Chris Lattnerca6433f2003-05-22 20:06:43 +0000536 std::cerr << "-" << ArgName;
537 std::cerr << " option" << Message << "\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000538 return true;
539}
540
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000541bool Option::addOccurrence(const char *ArgName, const std::string &Value) {
542 NumOccurrences++; // Increment the number of times we have been seen
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000543
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000544 switch (getNumOccurrencesFlag()) {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000545 case Optional:
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000546 if (NumOccurrences > 1)
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000547 return error(": may only occur zero or one times!", ArgName);
548 break;
549 case Required:
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000550 if (NumOccurrences > 1)
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000551 return error(": must occur exactly one time!", ArgName);
552 // Fall through
553 case OneOrMore:
Chris Lattnercaccd762001-10-27 05:54:17 +0000554 case ZeroOrMore:
555 case ConsumeAfter: break;
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000556 default: return error(": bad num occurrences flag value!");
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000557 }
558
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000559 return handleOccurrence(ArgName, Value);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000560}
561
Chris Lattner331de232002-07-22 02:07:59 +0000562// addArgument - Tell the system that this Option subclass will handle all
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000563// occurrences of -ArgStr on the command line.
Chris Lattner331de232002-07-22 02:07:59 +0000564//
565void Option::addArgument(const char *ArgStr) {
566 if (ArgStr[0])
567 AddArgument(ArgStr, this);
Chris Lattner9cf3d472003-07-30 17:34:02 +0000568
569 if (getFormattingFlag() == Positional)
Chris Lattner331de232002-07-22 02:07:59 +0000570 getPositionalOpts().push_back(this);
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000571 else if (getNumOccurrencesFlag() == ConsumeAfter) {
Chris Lattner9cf3d472003-07-30 17:34:02 +0000572 if (!getPositionalOpts().empty() &&
573 getPositionalOpts().front()->getNumOccurrencesFlag() == ConsumeAfter)
574 error("Cannot specify more than one option with cl::ConsumeAfter!");
Chris Lattner331de232002-07-22 02:07:59 +0000575 getPositionalOpts().insert(getPositionalOpts().begin(), this);
576 }
577}
578
Chris Lattneraa852bb2002-07-23 17:15:12 +0000579void Option::removeArgument(const char *ArgStr) {
Chris Lattner9cf3d472003-07-30 17:34:02 +0000580 if (ArgStr[0])
Chris Lattnere8e258b2002-07-29 20:58:42 +0000581 RemoveArgument(ArgStr, this);
Chris Lattner9cf3d472003-07-30 17:34:02 +0000582
583 if (getFormattingFlag() == Positional) {
Chris Lattnerca6433f2003-05-22 20:06:43 +0000584 std::vector<Option*>::iterator I =
Chris Lattneraa852bb2002-07-23 17:15:12 +0000585 std::find(getPositionalOpts().begin(), getPositionalOpts().end(), this);
586 assert(I != getPositionalOpts().end() && "Arg not registered!");
587 getPositionalOpts().erase(I);
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000588 } else if (getNumOccurrencesFlag() == ConsumeAfter) {
Chris Lattneraa852bb2002-07-23 17:15:12 +0000589 assert(!getPositionalOpts().empty() && getPositionalOpts()[0] == this &&
590 "Arg not registered correctly!");
591 getPositionalOpts().erase(getPositionalOpts().begin());
592 }
593}
594
Chris Lattner331de232002-07-22 02:07:59 +0000595
596// getValueStr - Get the value description string, using "DefaultMsg" if nothing
597// has been specified yet.
598//
599static const char *getValueStr(const Option &O, const char *DefaultMsg) {
600 if (O.ValueStr[0] == 0) return DefaultMsg;
601 return O.ValueStr;
602}
603
604//===----------------------------------------------------------------------===//
605// cl::alias class implementation
606//
607
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000608// Return the width of the option tag for printing...
Chris Lattner331de232002-07-22 02:07:59 +0000609unsigned alias::getOptionWidth() const {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000610 return std::strlen(ArgStr)+6;
611}
612
Chris Lattner331de232002-07-22 02:07:59 +0000613// Print out the option for the alias...
614void alias::printOptionInfo(unsigned GlobalWidth) const {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000615 unsigned L = std::strlen(ArgStr);
Chris Lattnerca6433f2003-05-22 20:06:43 +0000616 std::cerr << " -" << ArgStr << std::string(GlobalWidth-L-6, ' ') << " - "
617 << HelpStr << "\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000618}
619
620
Chris Lattner331de232002-07-22 02:07:59 +0000621
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000622//===----------------------------------------------------------------------===//
Chris Lattner331de232002-07-22 02:07:59 +0000623// Parser Implementation code...
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000624//
625
Chris Lattner9b14eb52002-08-07 18:36:37 +0000626// basic_parser implementation
627//
628
629// Return the width of the option tag for printing...
630unsigned basic_parser_impl::getOptionWidth(const Option &O) const {
631 unsigned Len = std::strlen(O.ArgStr);
632 if (const char *ValName = getValueName())
633 Len += std::strlen(getValueStr(O, ValName))+3;
634
635 return Len + 6;
636}
637
638// printOptionInfo - Print out information about this option. The
639// to-be-maintained width is specified.
640//
641void basic_parser_impl::printOptionInfo(const Option &O,
642 unsigned GlobalWidth) const {
Chris Lattnerca6433f2003-05-22 20:06:43 +0000643 std::cerr << " -" << O.ArgStr;
Chris Lattner9b14eb52002-08-07 18:36:37 +0000644
645 if (const char *ValName = getValueName())
Chris Lattnerca6433f2003-05-22 20:06:43 +0000646 std::cerr << "=<" << getValueStr(O, ValName) << ">";
Chris Lattner9b14eb52002-08-07 18:36:37 +0000647
Chris Lattnerca6433f2003-05-22 20:06:43 +0000648 std::cerr << std::string(GlobalWidth-getOptionWidth(O), ' ') << " - "
649 << O.HelpStr << "\n";
Chris Lattner9b14eb52002-08-07 18:36:37 +0000650}
651
652
653
654
Chris Lattner331de232002-07-22 02:07:59 +0000655// parser<bool> implementation
656//
Chris Lattner9b14eb52002-08-07 18:36:37 +0000657bool parser<bool>::parse(Option &O, const char *ArgName,
Chris Lattnerca6433f2003-05-22 20:06:43 +0000658 const std::string &Arg, bool &Value) {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000659 if (Arg == "" || Arg == "true" || Arg == "TRUE" || Arg == "True" ||
660 Arg == "1") {
661 Value = true;
662 } else if (Arg == "false" || Arg == "FALSE" || Arg == "False" || Arg == "0") {
663 Value = false;
664 } else {
Chris Lattner331de232002-07-22 02:07:59 +0000665 return O.error(": '" + Arg +
666 "' is invalid value for boolean argument! Try 0 or 1");
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000667 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000668 return false;
669}
670
Chris Lattner331de232002-07-22 02:07:59 +0000671// parser<int> implementation
672//
Chris Lattner9b14eb52002-08-07 18:36:37 +0000673bool parser<int>::parse(Option &O, const char *ArgName,
Chris Lattnerca6433f2003-05-22 20:06:43 +0000674 const std::string &Arg, int &Value) {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000675 char *End;
Chris Lattnerd2a6fc32003-06-28 15:47:20 +0000676 Value = (int)strtol(Arg.c_str(), &End, 0);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000677 if (*End != 0)
Chris Lattner331de232002-07-22 02:07:59 +0000678 return O.error(": '" + Arg + "' value invalid for integer argument!");
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000679 return false;
680}
681
Chris Lattnerd2a6fc32003-06-28 15:47:20 +0000682// parser<unsigned> implementation
683//
684bool parser<unsigned>::parse(Option &O, const char *ArgName,
685 const std::string &Arg, unsigned &Value) {
686 char *End;
Brian Gaeke2d6a2362003-10-10 17:01:36 +0000687 errno = 0;
688 unsigned long V = strtoul(Arg.c_str(), &End, 0);
Chris Lattnerd2a6fc32003-06-28 15:47:20 +0000689 Value = (unsigned)V;
Brian Gaeke2d6a2362003-10-10 17:01:36 +0000690 if (((V == ULONG_MAX) && (errno == ERANGE))
691 || (*End != 0)
692 || (Value != V))
Chris Lattnerd2a6fc32003-06-28 15:47:20 +0000693 return O.error(": '" + Arg + "' value invalid for uint argument!");
694 return false;
695}
696
Chris Lattner9b14eb52002-08-07 18:36:37 +0000697// parser<double>/parser<float> implementation
Chris Lattnerd215fd12001-10-13 06:53:19 +0000698//
Chris Lattnerca6433f2003-05-22 20:06:43 +0000699static bool parseDouble(Option &O, const std::string &Arg, double &Value) {
Chris Lattner331de232002-07-22 02:07:59 +0000700 const char *ArgStart = Arg.c_str();
701 char *End;
702 Value = strtod(ArgStart, &End);
703 if (*End != 0)
704 return O.error(": '" +Arg+ "' value invalid for floating point argument!");
Chris Lattnerd215fd12001-10-13 06:53:19 +0000705 return false;
706}
707
Chris Lattner9b14eb52002-08-07 18:36:37 +0000708bool parser<double>::parse(Option &O, const char *AN,
709 const std::string &Arg, double &Val) {
710 return parseDouble(O, Arg, Val);
Chris Lattner331de232002-07-22 02:07:59 +0000711}
712
Chris Lattner9b14eb52002-08-07 18:36:37 +0000713bool parser<float>::parse(Option &O, const char *AN,
714 const std::string &Arg, float &Val) {
715 double dVal;
716 if (parseDouble(O, Arg, dVal))
717 return true;
718 Val = (float)dVal;
719 return false;
Chris Lattner331de232002-07-22 02:07:59 +0000720}
721
722
Chris Lattner331de232002-07-22 02:07:59 +0000723
724// generic_parser_base implementation
725//
726
Chris Lattneraa852bb2002-07-23 17:15:12 +0000727// findOption - Return the option number corresponding to the specified
728// argument string. If the option is not found, getNumOptions() is returned.
729//
730unsigned generic_parser_base::findOption(const char *Name) {
731 unsigned i = 0, e = getNumOptions();
Chris Lattnerca6433f2003-05-22 20:06:43 +0000732 std::string N(Name);
Chris Lattneraa852bb2002-07-23 17:15:12 +0000733
734 while (i != e)
735 if (getOption(i) == N)
736 return i;
737 else
738 ++i;
739 return e;
740}
741
742
Chris Lattner331de232002-07-22 02:07:59 +0000743// Return the width of the option tag for printing...
744unsigned generic_parser_base::getOptionWidth(const Option &O) const {
745 if (O.hasArgStr()) {
746 unsigned Size = std::strlen(O.ArgStr)+6;
747 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
748 Size = std::max(Size, (unsigned)std::strlen(getOption(i))+8);
749 return Size;
750 } else {
751 unsigned BaseSize = 0;
752 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
753 BaseSize = std::max(BaseSize, (unsigned)std::strlen(getOption(i))+8);
754 return BaseSize;
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000755 }
756}
757
Chris Lattner331de232002-07-22 02:07:59 +0000758// printOptionInfo - Print out information about this option. The
759// to-be-maintained width is specified.
760//
761void generic_parser_base::printOptionInfo(const Option &O,
762 unsigned GlobalWidth) const {
763 if (O.hasArgStr()) {
764 unsigned L = std::strlen(O.ArgStr);
Chris Lattnerca6433f2003-05-22 20:06:43 +0000765 std::cerr << " -" << O.ArgStr << std::string(GlobalWidth-L-6, ' ')
766 << " - " << O.HelpStr << "\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000767
Chris Lattner331de232002-07-22 02:07:59 +0000768 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
769 unsigned NumSpaces = GlobalWidth-strlen(getOption(i))-8;
Chris Lattnerca6433f2003-05-22 20:06:43 +0000770 std::cerr << " =" << getOption(i) << std::string(NumSpaces, ' ')
771 << " - " << getDescription(i) << "\n";
Chris Lattner9c9be482002-01-31 00:42:56 +0000772 }
Chris Lattner331de232002-07-22 02:07:59 +0000773 } else {
774 if (O.HelpStr[0])
Chris Lattnerca6433f2003-05-22 20:06:43 +0000775 std::cerr << " " << O.HelpStr << "\n";
Chris Lattner331de232002-07-22 02:07:59 +0000776 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
777 unsigned L = std::strlen(getOption(i));
Chris Lattnerca6433f2003-05-22 20:06:43 +0000778 std::cerr << " -" << getOption(i) << std::string(GlobalWidth-L-8, ' ')
779 << " - " << getDescription(i) << "\n";
Chris Lattner331de232002-07-22 02:07:59 +0000780 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000781 }
782}
783
784
785//===----------------------------------------------------------------------===//
Chris Lattner331de232002-07-22 02:07:59 +0000786// --help and --help-hidden option implementation
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000787//
788namespace {
789
Chris Lattner331de232002-07-22 02:07:59 +0000790class HelpPrinter {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000791 unsigned MaxArgLen;
792 const Option *EmptyArg;
793 const bool ShowHidden;
794
Chris Lattner331de232002-07-22 02:07:59 +0000795 // isHidden/isReallyHidden - Predicates to be used to filter down arg lists.
Chris Lattnerca6433f2003-05-22 20:06:43 +0000796 inline static bool isHidden(std::pair<std::string, Option *> &OptPair) {
Chris Lattner331de232002-07-22 02:07:59 +0000797 return OptPair.second->getOptionHiddenFlag() >= Hidden;
798 }
Chris Lattnerca6433f2003-05-22 20:06:43 +0000799 inline static bool isReallyHidden(std::pair<std::string, Option *> &OptPair) {
Chris Lattner331de232002-07-22 02:07:59 +0000800 return OptPair.second->getOptionHiddenFlag() == ReallyHidden;
801 }
802
803public:
804 HelpPrinter(bool showHidden) : ShowHidden(showHidden) {
805 EmptyArg = 0;
806 }
807
808 void operator=(bool Value) {
809 if (Value == false) return;
810
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000811 // Copy Options into a vector so we can sort them as we like...
Chris Lattnerca6433f2003-05-22 20:06:43 +0000812 std::vector<std::pair<std::string, Option*> > Options;
Chris Lattner697954c2002-01-20 22:54:45 +0000813 copy(getOpts().begin(), getOpts().end(), std::back_inserter(Options));
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000814
815 // Eliminate Hidden or ReallyHidden arguments, depending on ShowHidden
Chris Lattner331de232002-07-22 02:07:59 +0000816 Options.erase(std::remove_if(Options.begin(), Options.end(),
817 std::ptr_fun(ShowHidden ? isReallyHidden : isHidden)),
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000818 Options.end());
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000819
820 // Eliminate duplicate entries in table (from enum flags options, f.e.)
Chris Lattner331de232002-07-22 02:07:59 +0000821 { // Give OptionSet a scope
822 std::set<Option*> OptionSet;
823 for (unsigned i = 0; i != Options.size(); ++i)
824 if (OptionSet.count(Options[i].second) == 0)
825 OptionSet.insert(Options[i].second); // Add new entry to set
826 else
827 Options.erase(Options.begin()+i--); // Erase duplicate
828 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000829
830 if (ProgramOverview)
Chris Lattnerca6433f2003-05-22 20:06:43 +0000831 std::cerr << "OVERVIEW:" << ProgramOverview << "\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000832
Chris Lattnerca6433f2003-05-22 20:06:43 +0000833 std::cerr << "USAGE: " << ProgramName << " [options]";
Chris Lattner331de232002-07-22 02:07:59 +0000834
835 // Print out the positional options...
Chris Lattnerca6433f2003-05-22 20:06:43 +0000836 std::vector<Option*> &PosOpts = getPositionalOpts();
Chris Lattner331de232002-07-22 02:07:59 +0000837 Option *CAOpt = 0; // The cl::ConsumeAfter option, if it exists...
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000838 if (!PosOpts.empty() && PosOpts[0]->getNumOccurrencesFlag() == ConsumeAfter)
Chris Lattner331de232002-07-22 02:07:59 +0000839 CAOpt = PosOpts[0];
840
Chris Lattner9cf3d472003-07-30 17:34:02 +0000841 for (unsigned i = CAOpt != 0, e = PosOpts.size(); i != e; ++i) {
842 if (PosOpts[i]->ArgStr[0])
843 std::cerr << " --" << PosOpts[i]->ArgStr;
Chris Lattnerca6433f2003-05-22 20:06:43 +0000844 std::cerr << " " << PosOpts[i]->HelpStr;
Chris Lattner9cf3d472003-07-30 17:34:02 +0000845 }
Chris Lattner331de232002-07-22 02:07:59 +0000846
847 // Print the consume after option info if it exists...
Chris Lattnerca6433f2003-05-22 20:06:43 +0000848 if (CAOpt) std::cerr << " " << CAOpt->HelpStr;
Chris Lattner331de232002-07-22 02:07:59 +0000849
Chris Lattnerca6433f2003-05-22 20:06:43 +0000850 std::cerr << "\n\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000851
852 // Compute the maximum argument length...
853 MaxArgLen = 0;
Chris Lattner331de232002-07-22 02:07:59 +0000854 for (unsigned i = 0, e = Options.size(); i != e; ++i)
855 MaxArgLen = std::max(MaxArgLen, Options[i].second->getOptionWidth());
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000856
Chris Lattnerca6433f2003-05-22 20:06:43 +0000857 std::cerr << "OPTIONS:\n";
Chris Lattner331de232002-07-22 02:07:59 +0000858 for (unsigned i = 0, e = Options.size(); i != e; ++i)
859 Options[i].second->printOptionInfo(MaxArgLen);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000860
Chris Lattner331de232002-07-22 02:07:59 +0000861 // Halt the program if help information is printed
862 exit(1);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000863 }
864};
865
Chris Lattner331de232002-07-22 02:07:59 +0000866
867
868// Define the two HelpPrinter instances that are used to print out help, or
869// help-hidden...
870//
871HelpPrinter NormalPrinter(false);
872HelpPrinter HiddenPrinter(true);
873
874cl::opt<HelpPrinter, true, parser<bool> >
875HOp("help", cl::desc("display available options (--help-hidden for more)"),
Chris Lattner9b14eb52002-08-07 18:36:37 +0000876 cl::location(NormalPrinter), cl::ValueDisallowed);
Chris Lattner331de232002-07-22 02:07:59 +0000877
878cl::opt<HelpPrinter, true, parser<bool> >
879HHOp("help-hidden", cl::desc("display all available options"),
Chris Lattner9b14eb52002-08-07 18:36:37 +0000880 cl::location(HiddenPrinter), cl::Hidden, cl::ValueDisallowed);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000881
882} // End anonymous namespace