blob: de895c9117661a17b2ae3355e85efc2a55f1f0cb [file] [log] [blame]
Chris Lattnerdbab15a2001-07-23 17:17:47 +00001//===-- CommandLine.cpp - Command line parser implementation --------------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattnerdbab15a2001-07-23 17:17:47 +00009//
10// This class implements a command line argument processor that is useful when
11// creating a tool. It provides a simple, minimalistic interface that is easily
12// extensible and supports nonlocal (library) command line options.
13//
Chris Lattner03fe1bd2001-07-23 23:04:07 +000014// Note that rather than trying to figure out what this code does, you could try
15// reading the library documentation located in docs/CommandLine.html
16//
Chris Lattnerdbab15a2001-07-23 17:17:47 +000017//===----------------------------------------------------------------------===//
18
Chris Lattnercee8f9a2001-11-27 00:03:19 +000019#include "Support/CommandLine.h"
Chris Lattnerdbab15a2001-07-23 17:17:47 +000020#include <algorithm>
21#include <map>
22#include <set>
Chris Lattner697954c2002-01-20 22:54:45 +000023#include <iostream>
Brian Gaeke2d6a2362003-10-10 17:01:36 +000024#include <cstdlib>
25#include <cerrno>
Chris Lattner7f1576f2002-02-24 23:02:12 +000026
Chris Lattnerdbab15a2001-07-23 17:17:47 +000027using namespace cl;
28
Chris Lattner331de232002-07-22 02:07:59 +000029//===----------------------------------------------------------------------===//
30// Basic, shared command line option processing machinery...
31//
32
Chris Lattnerdbab15a2001-07-23 17:17:47 +000033// Return the global command line option vector. Making it a function scoped
Chris Lattnerf78032f2001-11-26 18:58:34 +000034// static ensures that it will be initialized correctly before its first use.
Chris Lattnerdbab15a2001-07-23 17:17:47 +000035//
Chris Lattnerca6433f2003-05-22 20:06:43 +000036static std::map<std::string, Option*> *CommandLineOptions = 0;
37static std::map<std::string, Option*> &getOpts() {
38 if (CommandLineOptions == 0)
39 CommandLineOptions = new std::map<std::string,Option*>();
Chris Lattnere8e258b2002-07-29 20:58:42 +000040 return *CommandLineOptions;
41}
42
Chris Lattnerca6433f2003-05-22 20:06:43 +000043static Option *getOption(const std::string &Str) {
Chris Lattnere8e258b2002-07-29 20:58:42 +000044 if (CommandLineOptions == 0) return 0;
Chris Lattnerca6433f2003-05-22 20:06:43 +000045 std::map<std::string,Option*>::iterator I = CommandLineOptions->find(Str);
Chris Lattnere8e258b2002-07-29 20:58:42 +000046 return I != CommandLineOptions->end() ? I->second : 0;
Chris Lattnerdbab15a2001-07-23 17:17:47 +000047}
48
Chris Lattnerca6433f2003-05-22 20:06:43 +000049static std::vector<Option*> &getPositionalOpts() {
50 static std::vector<Option*> Positional;
Chris Lattner331de232002-07-22 02:07:59 +000051 return Positional;
52}
53
Chris Lattnere8e258b2002-07-29 20:58:42 +000054static void AddArgument(const char *ArgName, Option *Opt) {
55 if (getOption(ArgName)) {
Chris Lattnerca6433f2003-05-22 20:06:43 +000056 std::cerr << "CommandLine Error: Argument '" << ArgName
57 << "' defined more than once!\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +000058 } else {
Chris Lattnerf78032f2001-11-26 18:58:34 +000059 // Add argument to the argument map!
Chris Lattnere8e258b2002-07-29 20:58:42 +000060 getOpts()[ArgName] = Opt;
61 }
62}
63
64// RemoveArgument - It's possible that the argument is no longer in the map if
65// options have already been processed and the map has been deleted!
66//
67static void RemoveArgument(const char *ArgName, Option *Opt) {
68 if (CommandLineOptions == 0) return;
69 assert(getOption(ArgName) == Opt && "Arg not in map!");
70 CommandLineOptions->erase(ArgName);
71 if (CommandLineOptions->empty()) {
72 delete CommandLineOptions;
73 CommandLineOptions = 0;
Chris Lattnerdbab15a2001-07-23 17:17:47 +000074 }
75}
76
77static const char *ProgramName = 0;
78static const char *ProgramOverview = 0;
79
Chris Lattnercaccd762001-10-27 05:54:17 +000080static inline bool ProvideOption(Option *Handler, const char *ArgName,
81 const char *Value, int argc, char **argv,
82 int &i) {
83 // Enforce value requirements
84 switch (Handler->getValueExpectedFlag()) {
85 case ValueRequired:
86 if (Value == 0 || *Value == 0) { // No value specified?
87 if (i+1 < argc) { // Steal the next argument, like for '-o filename'
88 Value = argv[++i];
89 } else {
90 return Handler->error(" requires a value!");
91 }
92 }
93 break;
94 case ValueDisallowed:
95 if (*Value != 0)
96 return Handler->error(" does not allow a value! '" +
Chris Lattnerca6433f2003-05-22 20:06:43 +000097 std::string(Value) + "' specified.");
Chris Lattnercaccd762001-10-27 05:54:17 +000098 break;
99 case ValueOptional: break;
Chris Lattnerca6433f2003-05-22 20:06:43 +0000100 default: std::cerr << "Bad ValueMask flag! CommandLine usage error:"
101 << Handler->getValueExpectedFlag() << "\n"; abort();
Chris Lattnercaccd762001-10-27 05:54:17 +0000102 }
103
104 // Run the handler now!
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000105 return Handler->addOccurrence(ArgName, Value);
Chris Lattnercaccd762001-10-27 05:54:17 +0000106}
107
Chris Lattner9cf3d472003-07-30 17:34:02 +0000108static bool ProvidePositionalOption(Option *Handler, const std::string &Arg) {
Chris Lattner331de232002-07-22 02:07:59 +0000109 int Dummy;
Chris Lattner9cf3d472003-07-30 17:34:02 +0000110 return ProvideOption(Handler, Handler->ArgStr, Arg.c_str(), 0, 0, Dummy);
Chris Lattner331de232002-07-22 02:07:59 +0000111}
Chris Lattnerf78032f2001-11-26 18:58:34 +0000112
Chris Lattner331de232002-07-22 02:07:59 +0000113
114// Option predicates...
115static inline bool isGrouping(const Option *O) {
116 return O->getFormattingFlag() == cl::Grouping;
117}
118static inline bool isPrefixedOrGrouping(const Option *O) {
119 return isGrouping(O) || O->getFormattingFlag() == cl::Prefix;
120}
121
122// getOptionPred - Check to see if there are any options that satisfy the
123// specified predicate with names that are the prefixes in Name. This is
124// checked by progressively stripping characters off of the name, checking to
125// see if there options that satisfy the predicate. If we find one, return it,
126// otherwise return null.
127//
128static Option *getOptionPred(std::string Name, unsigned &Length,
129 bool (*Pred)(const Option*)) {
130
Chris Lattnere8e258b2002-07-29 20:58:42 +0000131 Option *Op = getOption(Name);
132 if (Op && Pred(Op)) {
Chris Lattner331de232002-07-22 02:07:59 +0000133 Length = Name.length();
Chris Lattnere8e258b2002-07-29 20:58:42 +0000134 return Op;
Chris Lattnerf78032f2001-11-26 18:58:34 +0000135 }
136
Chris Lattner331de232002-07-22 02:07:59 +0000137 if (Name.size() == 1) return 0;
138 do {
139 Name.erase(Name.end()-1, Name.end()); // Chop off the last character...
Chris Lattnere8e258b2002-07-29 20:58:42 +0000140 Op = getOption(Name);
Chris Lattner331de232002-07-22 02:07:59 +0000141
142 // Loop while we haven't found an option and Name still has at least two
143 // characters in it (so that the next iteration will not be the empty
144 // string...
Chris Lattnere8e258b2002-07-29 20:58:42 +0000145 } while ((Op == 0 || !Pred(Op)) && Name.size() > 1);
Chris Lattner331de232002-07-22 02:07:59 +0000146
Chris Lattnere8e258b2002-07-29 20:58:42 +0000147 if (Op && Pred(Op)) {
Chris Lattner331de232002-07-22 02:07:59 +0000148 Length = Name.length();
Chris Lattnere8e258b2002-07-29 20:58:42 +0000149 return Op; // Found one!
Chris Lattner331de232002-07-22 02:07:59 +0000150 }
151 return 0; // No option found!
152}
153
154static bool RequiresValue(const Option *O) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000155 return O->getNumOccurrencesFlag() == cl::Required ||
156 O->getNumOccurrencesFlag() == cl::OneOrMore;
Chris Lattner331de232002-07-22 02:07:59 +0000157}
158
159static bool EatsUnboundedNumberOfValues(const Option *O) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000160 return O->getNumOccurrencesFlag() == cl::ZeroOrMore ||
161 O->getNumOccurrencesFlag() == cl::OneOrMore;
Chris Lattnerf78032f2001-11-26 18:58:34 +0000162}
Chris Lattnercaccd762001-10-27 05:54:17 +0000163
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000164/// ParseCStringVector - Break INPUT up wherever one or more
165/// whitespace characters are found, and store the resulting tokens in
166/// OUTPUT. The tokens stored in OUTPUT are dynamically allocated
167/// using strdup (), so it is the caller's responsibility to free ()
168/// them later.
Brian Gaeke06b06c52003-08-14 22:00:59 +0000169///
170static void ParseCStringVector (std::vector<char *> &output,
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000171 const char *input) {
172 // Characters which will be treated as token separators:
173 static const char *delims = " \v\f\t\r\n";
174
175 std::string work (input);
176 // Skip past any delims at head of input string.
177 size_t pos = work.find_first_not_of (delims);
178 // If the string consists entirely of delims, then exit early.
179 if (pos == std::string::npos) return;
180 // Otherwise, jump forward to beginning of first word.
181 work = work.substr (pos);
182 // Find position of first delimiter.
183 pos = work.find_first_of (delims);
184
185 while (!work.empty() && pos != std::string::npos) {
186 // Everything from 0 to POS is the next word to copy.
187 output.push_back (strdup (work.substr (0,pos).c_str ()));
188 // Is there another word in the string?
189 size_t nextpos = work.find_first_not_of (delims, pos + 1);
190 if (nextpos != std::string::npos) {
191 // Yes? Then remove delims from beginning ...
192 work = work.substr (work.find_first_not_of (delims, pos + 1));
193 // and find the end of the word.
194 pos = work.find_first_of (delims);
195 } else {
196 // No? (Remainder of string is delims.) End the loop.
197 work = "";
198 pos = std::string::npos;
199 }
200 }
201
202 // If `input' ended with non-delim char, then we'll get here with
203 // the last word of `input' in `work'; copy it now.
204 if (!work.empty ()) {
205 output.push_back (strdup (work.c_str ()));
Brian Gaeke06b06c52003-08-14 22:00:59 +0000206 }
207}
208
209/// ParseEnvironmentOptions - An alternative entry point to the
210/// CommandLine library, which allows you to read the program's name
211/// from the caller (as PROGNAME) and its command-line arguments from
212/// an environment variable (whose name is given in ENVVAR).
213///
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000214void cl::ParseEnvironmentOptions (const char *progName, const char *envVar,
Brian Gaeke06b06c52003-08-14 22:00:59 +0000215 const char *Overview) {
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000216 // Check args.
217 assert (progName && "Program name not specified");
218 assert (envVar && "Environment variable name missing");
219
220 // Get the environment variable they want us to parse options out of.
221 const char *envValue = getenv (envVar);
222 if (!envValue)
223 return;
224
Brian Gaeke06b06c52003-08-14 22:00:59 +0000225 // Get program's "name", which we wouldn't know without the caller
226 // telling us.
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000227 std::vector<char *> newArgv;
228 newArgv.push_back (strdup (progName));
Brian Gaeke06b06c52003-08-14 22:00:59 +0000229
230 // Parse the value of the environment variable into a "command line"
231 // and hand it off to ParseCommandLineOptions().
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000232 ParseCStringVector (newArgv, envValue);
233 int newArgc = newArgv.size ();
234 ParseCommandLineOptions (newArgc, &newArgv[0], Overview);
235
236 // Free all the strdup()ed strings.
237 for (std::vector<char *>::iterator i = newArgv.begin (), e = newArgv.end ();
238 i != e; ++i) {
239 free (*i);
240 }
Brian Gaeke06b06c52003-08-14 22:00:59 +0000241}
242
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000243void cl::ParseCommandLineOptions(int &argc, char **argv,
Chris Lattner0c0edf82002-07-25 06:17:51 +0000244 const char *Overview) {
Chris Lattner331de232002-07-22 02:07:59 +0000245 assert((!getOpts().empty() || !getPositionalOpts().empty()) &&
246 "No options specified, or ParseCommandLineOptions called more"
247 " than once!");
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000248 ProgramName = argv[0]; // Save this away safe and snug
249 ProgramOverview = Overview;
250 bool ErrorParsing = false;
251
Chris Lattnerca6433f2003-05-22 20:06:43 +0000252 std::map<std::string, Option*> &Opts = getOpts();
253 std::vector<Option*> &PositionalOpts = getPositionalOpts();
Chris Lattner331de232002-07-22 02:07:59 +0000254
255 // Check out the positional arguments to collect information about them.
256 unsigned NumPositionalRequired = 0;
257 Option *ConsumeAfterOpt = 0;
258 if (!PositionalOpts.empty()) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000259 if (PositionalOpts[0]->getNumOccurrencesFlag() == cl::ConsumeAfter) {
Chris Lattner331de232002-07-22 02:07:59 +0000260 assert(PositionalOpts.size() > 1 &&
261 "Cannot specify cl::ConsumeAfter without a positional argument!");
262 ConsumeAfterOpt = PositionalOpts[0];
263 }
264
265 // Calculate how many positional values are _required_.
266 bool UnboundedFound = false;
267 for (unsigned i = ConsumeAfterOpt != 0, e = PositionalOpts.size();
268 i != e; ++i) {
269 Option *Opt = PositionalOpts[i];
270 if (RequiresValue(Opt))
271 ++NumPositionalRequired;
272 else if (ConsumeAfterOpt) {
273 // ConsumeAfter cannot be combined with "optional" positional options
Chris Lattner54ec7ae2002-07-22 02:21:57 +0000274 // unless there is only one positional argument...
275 if (PositionalOpts.size() > 2)
276 ErrorParsing |=
277 Opt->error(" error - this positional option will never be matched, "
278 "because it does not Require a value, and a "
279 "cl::ConsumeAfter option is active!");
Chris Lattner9cf3d472003-07-30 17:34:02 +0000280 } else if (UnboundedFound && !Opt->ArgStr[0]) {
281 // This option does not "require" a value... Make sure this option is
282 // not specified after an option that eats all extra arguments, or this
283 // one will never get any!
Chris Lattner331de232002-07-22 02:07:59 +0000284 //
285 ErrorParsing |= Opt->error(" error - option can never match, because "
286 "another positional argument will match an "
287 "unbounded number of values, and this option"
288 " does not require a value!");
289 }
290 UnboundedFound |= EatsUnboundedNumberOfValues(Opt);
291 }
292 }
293
294 // PositionalVals - A vector of "positional" arguments we accumulate into to
295 // processes at the end...
296 //
Chris Lattnerca6433f2003-05-22 20:06:43 +0000297 std::vector<std::string> PositionalVals;
Chris Lattner331de232002-07-22 02:07:59 +0000298
Chris Lattner9cf3d472003-07-30 17:34:02 +0000299 // If the program has named positional arguments, and the name has been run
300 // across, keep track of which positional argument was named. Otherwise put
301 // the positional args into the PositionalVals list...
302 Option *ActivePositionalArg = 0;
303
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000304 // Loop over all of the arguments... processing them.
Chris Lattner331de232002-07-22 02:07:59 +0000305 bool DashDashFound = false; // Have we read '--'?
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000306 for (int i = 1; i < argc; ++i) {
307 Option *Handler = 0;
308 const char *Value = "";
309 const char *ArgName = "";
Chris Lattner331de232002-07-22 02:07:59 +0000310
311 // Check to see if this is a positional argument. This argument is
312 // considered to be positional if it doesn't start with '-', if it is "-"
Misha Brukman1115e042003-07-10 21:38:28 +0000313 // itself, or if we have seen "--" already.
Chris Lattner331de232002-07-22 02:07:59 +0000314 //
315 if (argv[i][0] != '-' || argv[i][1] == 0 || DashDashFound) {
316 // Positional argument!
Chris Lattner9cf3d472003-07-30 17:34:02 +0000317 if (ActivePositionalArg) {
318 ProvidePositionalOption(ActivePositionalArg, argv[i]);
319 continue; // We are done!
320 } else if (!PositionalOpts.empty()) {
Chris Lattner331de232002-07-22 02:07:59 +0000321 PositionalVals.push_back(argv[i]);
322
323 // All of the positional arguments have been fulfulled, give the rest to
324 // the consume after option... if it's specified...
325 //
Chris Lattnerd16714b2002-07-31 16:29:43 +0000326 if (PositionalVals.size() >= NumPositionalRequired &&
Chris Lattner331de232002-07-22 02:07:59 +0000327 ConsumeAfterOpt != 0) {
328 for (++i; i < argc; ++i)
329 PositionalVals.push_back(argv[i]);
330 break; // Handle outside of the argument processing loop...
331 }
332
333 // Delay processing positional arguments until the end...
334 continue;
335 }
336 } else { // We start with a '-', must be an argument...
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000337 ArgName = argv[i]+1;
338 while (*ArgName == '-') ++ArgName; // Eat leading dashes
339
Chris Lattner331de232002-07-22 02:07:59 +0000340 if (*ArgName == 0 && !DashDashFound) { // Is this the mythical "--"?
341 DashDashFound = true; // Yup, take note of that fact...
Misha Brukman950971d2003-09-16 15:31:46 +0000342 continue; // Don't try to process it as an argument itself.
Chris Lattner331de232002-07-22 02:07:59 +0000343 }
344
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000345 const char *ArgNameEnd = ArgName;
Chris Lattnerf78032f2001-11-26 18:58:34 +0000346 while (*ArgNameEnd && *ArgNameEnd != '=')
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000347 ++ArgNameEnd; // Scan till end of argument name...
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000348
349 Value = ArgNameEnd;
350 if (*Value) // If we have an equals sign...
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000351 ++Value; // Advance to value...
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000352
353 if (*ArgName != 0) {
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000354 std::string RealName(ArgName, ArgNameEnd);
355 // Extract arg name part
Chris Lattnerca6433f2003-05-22 20:06:43 +0000356 std::map<std::string, Option*>::iterator I = Opts.find(RealName);
Chris Lattnerf78032f2001-11-26 18:58:34 +0000357
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000358 if (I == Opts.end() && !*Value && RealName.size() > 1) {
Chris Lattner331de232002-07-22 02:07:59 +0000359 // Check to see if this "option" is really a prefixed or grouped
360 // argument...
361 //
362 unsigned Length = 0;
363 Option *PGOpt = getOptionPred(RealName, Length, isPrefixedOrGrouping);
Chris Lattnerf78032f2001-11-26 18:58:34 +0000364
Chris Lattner331de232002-07-22 02:07:59 +0000365 // If the option is a prefixed option, then the value is simply the
366 // rest of the name... so fall through to later processing, by
367 // setting up the argument name flags and value fields.
368 //
369 if (PGOpt && PGOpt->getFormattingFlag() == cl::Prefix) {
370 ArgNameEnd = ArgName+Length;
371 Value = ArgNameEnd;
Chris Lattnerca6433f2003-05-22 20:06:43 +0000372 I = Opts.find(std::string(ArgName, ArgNameEnd));
Chris Lattner331de232002-07-22 02:07:59 +0000373 assert(I->second == PGOpt);
374 } else if (PGOpt) {
375 // This must be a grouped option... handle all of them now...
376 assert(isGrouping(PGOpt) && "Broken getOptionPred!");
377
378 do {
379 // Move current arg name out of RealName into RealArgName...
Chris Lattnerca6433f2003-05-22 20:06:43 +0000380 std::string RealArgName(RealName.begin(),RealName.begin()+Length);
Chris Lattner331de232002-07-22 02:07:59 +0000381 RealName.erase(RealName.begin(), RealName.begin()+Length);
Chris Lattnerf78032f2001-11-26 18:58:34 +0000382
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000383 // Because ValueRequired is an invalid flag for grouped arguments,
384 // we don't need to pass argc/argv in...
385 //
Chris Lattner331de232002-07-22 02:07:59 +0000386 assert(PGOpt->getValueExpectedFlag() != cl::ValueRequired &&
387 "Option can not be cl::Grouping AND cl::ValueRequired!");
388 int Dummy;
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000389 ErrorParsing |= ProvideOption(PGOpt, RealArgName.c_str(), "",
Chris Lattner331de232002-07-22 02:07:59 +0000390 0, 0, Dummy);
391
392 // Get the next grouping option...
393 if (!RealName.empty())
394 PGOpt = getOptionPred(RealName, Length, isGrouping);
395 } while (!RealName.empty() && PGOpt);
396
397 if (RealName.empty()) // Processed all of the options, move on
398 continue; // to the next argv[] value...
399
400 // If RealName is not empty, that means we did not match one of the
401 // options! This is an error.
402 //
403 I = Opts.end();
404 }
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000405 }
Chris Lattnerf78032f2001-11-26 18:58:34 +0000406
Chris Lattner331de232002-07-22 02:07:59 +0000407 Handler = I != Opts.end() ? I->second : 0;
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000408 }
409 }
410
411 if (Handler == 0) {
Brian Gaekec86e84b2003-09-16 18:00:35 +0000412 std::cerr << "Unknown command line argument '" << argv[i] << "'. Try: '"
Chris Lattnerca6433f2003-05-22 20:06:43 +0000413 << argv[0] << " --help'\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000414 ErrorParsing = true;
415 continue;
416 }
417
Chris Lattner72fb8e52003-05-22 20:26:17 +0000418 // Check to see if this option accepts a comma separated list of values. If
419 // it does, we have to split up the value into multiple values...
420 if (Handler->getMiscFlags() & CommaSeparated) {
421 std::string Val(Value);
422 std::string::size_type Pos = Val.find(',');
423
424 while (Pos != std::string::npos) {
425 // Process the portion before the comma...
426 ErrorParsing |= ProvideOption(Handler, ArgName,
427 std::string(Val.begin(),
428 Val.begin()+Pos).c_str(),
429 argc, argv, i);
430 // Erase the portion before the comma, AND the comma...
431 Val.erase(Val.begin(), Val.begin()+Pos+1);
432 Value += Pos+1; // Increment the original value pointer as well...
433
434 // Check for another comma...
435 Pos = Val.find(',');
436 }
437 }
Chris Lattner9cf3d472003-07-30 17:34:02 +0000438
439 // If this is a named positional argument, just remember that it is the
440 // active one...
441 if (Handler->getFormattingFlag() == cl::Positional)
442 ActivePositionalArg = Handler;
443 else
444 ErrorParsing |= ProvideOption(Handler, ArgName, Value, argc, argv, i);
Chris Lattner331de232002-07-22 02:07:59 +0000445 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000446
Chris Lattner331de232002-07-22 02:07:59 +0000447 // Check and handle positional arguments now...
448 if (NumPositionalRequired > PositionalVals.size()) {
Chris Lattnerca6433f2003-05-22 20:06:43 +0000449 std::cerr << "Not enough positional command line arguments specified!\n"
450 << "Must specify at least " << NumPositionalRequired
451 << " positional arguments: See: " << argv[0] << " --help\n";
Chris Lattner331de232002-07-22 02:07:59 +0000452 ErrorParsing = true;
453
454
455 } else if (ConsumeAfterOpt == 0) {
456 // Positional args have already been handled if ConsumeAfter is specified...
457 unsigned ValNo = 0, NumVals = PositionalVals.size();
458 for (unsigned i = 0, e = PositionalOpts.size(); i != e; ++i) {
459 if (RequiresValue(PositionalOpts[i])) {
460 ProvidePositionalOption(PositionalOpts[i], PositionalVals[ValNo++]);
461 --NumPositionalRequired; // We fulfilled our duty...
462 }
463
464 // If we _can_ give this option more arguments, do so now, as long as we
465 // do not give it values that others need. 'Done' controls whether the
466 // option even _WANTS_ any more.
467 //
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000468 bool Done = PositionalOpts[i]->getNumOccurrencesFlag() == cl::Required;
Chris Lattner331de232002-07-22 02:07:59 +0000469 while (NumVals-ValNo > NumPositionalRequired && !Done) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000470 switch (PositionalOpts[i]->getNumOccurrencesFlag()) {
Chris Lattner331de232002-07-22 02:07:59 +0000471 case cl::Optional:
472 Done = true; // Optional arguments want _at most_ one value
473 // FALL THROUGH
474 case cl::ZeroOrMore: // Zero or more will take all they can get...
475 case cl::OneOrMore: // One or more will take all they can get...
476 ProvidePositionalOption(PositionalOpts[i], PositionalVals[ValNo++]);
477 break;
478 default:
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000479 assert(0 && "Internal error, unexpected NumOccurrences flag in "
Chris Lattner331de232002-07-22 02:07:59 +0000480 "positional argument processing!");
481 }
482 }
Chris Lattnercaccd762001-10-27 05:54:17 +0000483 }
Chris Lattner331de232002-07-22 02:07:59 +0000484 } else {
485 assert(ConsumeAfterOpt && NumPositionalRequired <= PositionalVals.size());
486 unsigned ValNo = 0;
487 for (unsigned j = 1, e = PositionalOpts.size(); j != e; ++j)
488 if (RequiresValue(PositionalOpts[j]))
Chris Lattnerfaba8092002-07-24 20:15:13 +0000489 ErrorParsing |= ProvidePositionalOption(PositionalOpts[j],
490 PositionalVals[ValNo++]);
491
492 // Handle the case where there is just one positional option, and it's
493 // optional. In this case, we want to give JUST THE FIRST option to the
494 // positional option and keep the rest for the consume after. The above
495 // loop would have assigned no values to positional options in this case.
496 //
Chris Lattnerb490c202002-08-02 21:51:29 +0000497 if (PositionalOpts.size() == 2 && ValNo == 0 && !PositionalVals.empty())
Chris Lattnerfaba8092002-07-24 20:15:13 +0000498 ErrorParsing |= ProvidePositionalOption(PositionalOpts[1],
499 PositionalVals[ValNo++]);
Chris Lattner331de232002-07-22 02:07:59 +0000500
501 // Handle over all of the rest of the arguments to the
502 // cl::ConsumeAfter command line option...
503 for (; ValNo != PositionalVals.size(); ++ValNo)
504 ErrorParsing |= ProvidePositionalOption(ConsumeAfterOpt,
505 PositionalVals[ValNo]);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000506 }
507
Chris Lattnerdc4693d2001-07-23 23:02:45 +0000508 // Loop over args and make sure all required args are specified!
Chris Lattnerca6433f2003-05-22 20:06:43 +0000509 for (std::map<std::string, Option*>::iterator I = Opts.begin(),
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000510 E = Opts.end(); I != E; ++I) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000511 switch (I->second->getNumOccurrencesFlag()) {
Chris Lattnerdc4693d2001-07-23 23:02:45 +0000512 case Required:
513 case OneOrMore:
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000514 if (I->second->getNumOccurrences() == 0) {
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000515 I->second->error(" must be specified at least once!");
Chris Lattnerf038acb2001-10-24 06:21:56 +0000516 ErrorParsing = true;
517 }
Chris Lattnerdc4693d2001-07-23 23:02:45 +0000518 // Fall through
519 default:
520 break;
521 }
522 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000523
Chris Lattner331de232002-07-22 02:07:59 +0000524 // Free all of the memory allocated to the map. Command line options may only
525 // be processed once!
Chris Lattnere8e258b2002-07-29 20:58:42 +0000526 delete CommandLineOptions;
527 CommandLineOptions = 0;
Chris Lattner331de232002-07-22 02:07:59 +0000528 PositionalOpts.clear();
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000529
530 // If we had an error processing our arguments, don't let the program execute
531 if (ErrorParsing) exit(1);
532}
533
534//===----------------------------------------------------------------------===//
535// Option Base class implementation
536//
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000537
Chris Lattnerca6433f2003-05-22 20:06:43 +0000538bool Option::error(std::string Message, const char *ArgName) {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000539 if (ArgName == 0) ArgName = ArgStr;
Chris Lattner331de232002-07-22 02:07:59 +0000540 if (ArgName[0] == 0)
Chris Lattnerca6433f2003-05-22 20:06:43 +0000541 std::cerr << HelpStr; // Be nice for positional arguments
Chris Lattner331de232002-07-22 02:07:59 +0000542 else
Chris Lattnerca6433f2003-05-22 20:06:43 +0000543 std::cerr << "-" << ArgName;
544 std::cerr << " option" << Message << "\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000545 return true;
546}
547
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000548bool Option::addOccurrence(const char *ArgName, const std::string &Value) {
549 NumOccurrences++; // Increment the number of times we have been seen
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000550
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000551 switch (getNumOccurrencesFlag()) {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000552 case Optional:
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000553 if (NumOccurrences > 1)
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000554 return error(": may only occur zero or one times!", ArgName);
555 break;
556 case Required:
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000557 if (NumOccurrences > 1)
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000558 return error(": must occur exactly one time!", ArgName);
559 // Fall through
560 case OneOrMore:
Chris Lattnercaccd762001-10-27 05:54:17 +0000561 case ZeroOrMore:
562 case ConsumeAfter: break;
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000563 default: return error(": bad num occurrences flag value!");
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000564 }
565
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000566 return handleOccurrence(ArgName, Value);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000567}
568
Chris Lattner331de232002-07-22 02:07:59 +0000569// addArgument - Tell the system that this Option subclass will handle all
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000570// occurrences of -ArgStr on the command line.
Chris Lattner331de232002-07-22 02:07:59 +0000571//
572void Option::addArgument(const char *ArgStr) {
573 if (ArgStr[0])
574 AddArgument(ArgStr, this);
Chris Lattner9cf3d472003-07-30 17:34:02 +0000575
576 if (getFormattingFlag() == Positional)
Chris Lattner331de232002-07-22 02:07:59 +0000577 getPositionalOpts().push_back(this);
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000578 else if (getNumOccurrencesFlag() == ConsumeAfter) {
Chris Lattner9cf3d472003-07-30 17:34:02 +0000579 if (!getPositionalOpts().empty() &&
580 getPositionalOpts().front()->getNumOccurrencesFlag() == ConsumeAfter)
581 error("Cannot specify more than one option with cl::ConsumeAfter!");
Chris Lattner331de232002-07-22 02:07:59 +0000582 getPositionalOpts().insert(getPositionalOpts().begin(), this);
583 }
584}
585
Chris Lattneraa852bb2002-07-23 17:15:12 +0000586void Option::removeArgument(const char *ArgStr) {
Chris Lattner9cf3d472003-07-30 17:34:02 +0000587 if (ArgStr[0])
Chris Lattnere8e258b2002-07-29 20:58:42 +0000588 RemoveArgument(ArgStr, this);
Chris Lattner9cf3d472003-07-30 17:34:02 +0000589
590 if (getFormattingFlag() == Positional) {
Chris Lattnerca6433f2003-05-22 20:06:43 +0000591 std::vector<Option*>::iterator I =
Chris Lattneraa852bb2002-07-23 17:15:12 +0000592 std::find(getPositionalOpts().begin(), getPositionalOpts().end(), this);
593 assert(I != getPositionalOpts().end() && "Arg not registered!");
594 getPositionalOpts().erase(I);
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000595 } else if (getNumOccurrencesFlag() == ConsumeAfter) {
Chris Lattneraa852bb2002-07-23 17:15:12 +0000596 assert(!getPositionalOpts().empty() && getPositionalOpts()[0] == this &&
597 "Arg not registered correctly!");
598 getPositionalOpts().erase(getPositionalOpts().begin());
599 }
600}
601
Chris Lattner331de232002-07-22 02:07:59 +0000602
603// getValueStr - Get the value description string, using "DefaultMsg" if nothing
604// has been specified yet.
605//
606static const char *getValueStr(const Option &O, const char *DefaultMsg) {
607 if (O.ValueStr[0] == 0) return DefaultMsg;
608 return O.ValueStr;
609}
610
611//===----------------------------------------------------------------------===//
612// cl::alias class implementation
613//
614
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000615// Return the width of the option tag for printing...
Chris Lattner331de232002-07-22 02:07:59 +0000616unsigned alias::getOptionWidth() const {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000617 return std::strlen(ArgStr)+6;
618}
619
Chris Lattner331de232002-07-22 02:07:59 +0000620// Print out the option for the alias...
621void alias::printOptionInfo(unsigned GlobalWidth) const {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000622 unsigned L = std::strlen(ArgStr);
Chris Lattnerca6433f2003-05-22 20:06:43 +0000623 std::cerr << " -" << ArgStr << std::string(GlobalWidth-L-6, ' ') << " - "
624 << HelpStr << "\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000625}
626
627
Chris Lattner331de232002-07-22 02:07:59 +0000628
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000629//===----------------------------------------------------------------------===//
Chris Lattner331de232002-07-22 02:07:59 +0000630// Parser Implementation code...
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000631//
632
Chris Lattner9b14eb52002-08-07 18:36:37 +0000633// basic_parser implementation
634//
635
636// Return the width of the option tag for printing...
637unsigned basic_parser_impl::getOptionWidth(const Option &O) const {
638 unsigned Len = std::strlen(O.ArgStr);
639 if (const char *ValName = getValueName())
640 Len += std::strlen(getValueStr(O, ValName))+3;
641
642 return Len + 6;
643}
644
645// printOptionInfo - Print out information about this option. The
646// to-be-maintained width is specified.
647//
648void basic_parser_impl::printOptionInfo(const Option &O,
649 unsigned GlobalWidth) const {
Chris Lattnerca6433f2003-05-22 20:06:43 +0000650 std::cerr << " -" << O.ArgStr;
Chris Lattner9b14eb52002-08-07 18:36:37 +0000651
652 if (const char *ValName = getValueName())
Chris Lattnerca6433f2003-05-22 20:06:43 +0000653 std::cerr << "=<" << getValueStr(O, ValName) << ">";
Chris Lattner9b14eb52002-08-07 18:36:37 +0000654
Chris Lattnerca6433f2003-05-22 20:06:43 +0000655 std::cerr << std::string(GlobalWidth-getOptionWidth(O), ' ') << " - "
656 << O.HelpStr << "\n";
Chris Lattner9b14eb52002-08-07 18:36:37 +0000657}
658
659
660
661
Chris Lattner331de232002-07-22 02:07:59 +0000662// parser<bool> implementation
663//
Chris Lattner9b14eb52002-08-07 18:36:37 +0000664bool parser<bool>::parse(Option &O, const char *ArgName,
Chris Lattnerca6433f2003-05-22 20:06:43 +0000665 const std::string &Arg, bool &Value) {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000666 if (Arg == "" || Arg == "true" || Arg == "TRUE" || Arg == "True" ||
667 Arg == "1") {
668 Value = true;
669 } else if (Arg == "false" || Arg == "FALSE" || Arg == "False" || Arg == "0") {
670 Value = false;
671 } else {
Chris Lattner331de232002-07-22 02:07:59 +0000672 return O.error(": '" + Arg +
673 "' is invalid value for boolean argument! Try 0 or 1");
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000674 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000675 return false;
676}
677
Chris Lattner331de232002-07-22 02:07:59 +0000678// parser<int> implementation
679//
Chris Lattner9b14eb52002-08-07 18:36:37 +0000680bool parser<int>::parse(Option &O, const char *ArgName,
Chris Lattnerca6433f2003-05-22 20:06:43 +0000681 const std::string &Arg, int &Value) {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000682 char *End;
Chris Lattnerd2a6fc32003-06-28 15:47:20 +0000683 Value = (int)strtol(Arg.c_str(), &End, 0);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000684 if (*End != 0)
Chris Lattner331de232002-07-22 02:07:59 +0000685 return O.error(": '" + Arg + "' value invalid for integer argument!");
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000686 return false;
687}
688
Chris Lattnerd2a6fc32003-06-28 15:47:20 +0000689// parser<unsigned> implementation
690//
691bool parser<unsigned>::parse(Option &O, const char *ArgName,
692 const std::string &Arg, unsigned &Value) {
693 char *End;
Brian Gaeke2d6a2362003-10-10 17:01:36 +0000694 errno = 0;
695 unsigned long V = strtoul(Arg.c_str(), &End, 0);
Chris Lattnerd2a6fc32003-06-28 15:47:20 +0000696 Value = (unsigned)V;
Brian Gaeke2d6a2362003-10-10 17:01:36 +0000697 if (((V == ULONG_MAX) && (errno == ERANGE))
698 || (*End != 0)
699 || (Value != V))
Chris Lattnerd2a6fc32003-06-28 15:47:20 +0000700 return O.error(": '" + Arg + "' value invalid for uint argument!");
701 return false;
702}
703
Chris Lattner9b14eb52002-08-07 18:36:37 +0000704// parser<double>/parser<float> implementation
Chris Lattnerd215fd12001-10-13 06:53:19 +0000705//
Chris Lattnerca6433f2003-05-22 20:06:43 +0000706static bool parseDouble(Option &O, const std::string &Arg, double &Value) {
Chris Lattner331de232002-07-22 02:07:59 +0000707 const char *ArgStart = Arg.c_str();
708 char *End;
709 Value = strtod(ArgStart, &End);
710 if (*End != 0)
711 return O.error(": '" +Arg+ "' value invalid for floating point argument!");
Chris Lattnerd215fd12001-10-13 06:53:19 +0000712 return false;
713}
714
Chris Lattner9b14eb52002-08-07 18:36:37 +0000715bool parser<double>::parse(Option &O, const char *AN,
716 const std::string &Arg, double &Val) {
717 return parseDouble(O, Arg, Val);
Chris Lattner331de232002-07-22 02:07:59 +0000718}
719
Chris Lattner9b14eb52002-08-07 18:36:37 +0000720bool parser<float>::parse(Option &O, const char *AN,
721 const std::string &Arg, float &Val) {
722 double dVal;
723 if (parseDouble(O, Arg, dVal))
724 return true;
725 Val = (float)dVal;
726 return false;
Chris Lattner331de232002-07-22 02:07:59 +0000727}
728
729
Chris Lattner331de232002-07-22 02:07:59 +0000730
731// generic_parser_base implementation
732//
733
Chris Lattneraa852bb2002-07-23 17:15:12 +0000734// findOption - Return the option number corresponding to the specified
735// argument string. If the option is not found, getNumOptions() is returned.
736//
737unsigned generic_parser_base::findOption(const char *Name) {
738 unsigned i = 0, e = getNumOptions();
Chris Lattnerca6433f2003-05-22 20:06:43 +0000739 std::string N(Name);
Chris Lattneraa852bb2002-07-23 17:15:12 +0000740
741 while (i != e)
742 if (getOption(i) == N)
743 return i;
744 else
745 ++i;
746 return e;
747}
748
749
Chris Lattner331de232002-07-22 02:07:59 +0000750// Return the width of the option tag for printing...
751unsigned generic_parser_base::getOptionWidth(const Option &O) const {
752 if (O.hasArgStr()) {
753 unsigned Size = std::strlen(O.ArgStr)+6;
754 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
755 Size = std::max(Size, (unsigned)std::strlen(getOption(i))+8);
756 return Size;
757 } else {
758 unsigned BaseSize = 0;
759 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
760 BaseSize = std::max(BaseSize, (unsigned)std::strlen(getOption(i))+8);
761 return BaseSize;
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000762 }
763}
764
Chris Lattner331de232002-07-22 02:07:59 +0000765// printOptionInfo - Print out information about this option. The
766// to-be-maintained width is specified.
767//
768void generic_parser_base::printOptionInfo(const Option &O,
769 unsigned GlobalWidth) const {
770 if (O.hasArgStr()) {
771 unsigned L = std::strlen(O.ArgStr);
Chris Lattnerca6433f2003-05-22 20:06:43 +0000772 std::cerr << " -" << O.ArgStr << std::string(GlobalWidth-L-6, ' ')
773 << " - " << O.HelpStr << "\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000774
Chris Lattner331de232002-07-22 02:07:59 +0000775 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
776 unsigned NumSpaces = GlobalWidth-strlen(getOption(i))-8;
Chris Lattnerca6433f2003-05-22 20:06:43 +0000777 std::cerr << " =" << getOption(i) << std::string(NumSpaces, ' ')
778 << " - " << getDescription(i) << "\n";
Chris Lattner9c9be482002-01-31 00:42:56 +0000779 }
Chris Lattner331de232002-07-22 02:07:59 +0000780 } else {
781 if (O.HelpStr[0])
Chris Lattnerca6433f2003-05-22 20:06:43 +0000782 std::cerr << " " << O.HelpStr << "\n";
Chris Lattner331de232002-07-22 02:07:59 +0000783 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
784 unsigned L = std::strlen(getOption(i));
Chris Lattnerca6433f2003-05-22 20:06:43 +0000785 std::cerr << " -" << getOption(i) << std::string(GlobalWidth-L-8, ' ')
786 << " - " << getDescription(i) << "\n";
Chris Lattner331de232002-07-22 02:07:59 +0000787 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000788 }
789}
790
791
792//===----------------------------------------------------------------------===//
Chris Lattner331de232002-07-22 02:07:59 +0000793// --help and --help-hidden option implementation
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000794//
795namespace {
796
Chris Lattner331de232002-07-22 02:07:59 +0000797class HelpPrinter {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000798 unsigned MaxArgLen;
799 const Option *EmptyArg;
800 const bool ShowHidden;
801
Chris Lattner331de232002-07-22 02:07:59 +0000802 // isHidden/isReallyHidden - Predicates to be used to filter down arg lists.
Chris Lattnerca6433f2003-05-22 20:06:43 +0000803 inline static bool isHidden(std::pair<std::string, Option *> &OptPair) {
Chris Lattner331de232002-07-22 02:07:59 +0000804 return OptPair.second->getOptionHiddenFlag() >= Hidden;
805 }
Chris Lattnerca6433f2003-05-22 20:06:43 +0000806 inline static bool isReallyHidden(std::pair<std::string, Option *> &OptPair) {
Chris Lattner331de232002-07-22 02:07:59 +0000807 return OptPair.second->getOptionHiddenFlag() == ReallyHidden;
808 }
809
810public:
811 HelpPrinter(bool showHidden) : ShowHidden(showHidden) {
812 EmptyArg = 0;
813 }
814
815 void operator=(bool Value) {
816 if (Value == false) return;
817
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000818 // Copy Options into a vector so we can sort them as we like...
Chris Lattnerca6433f2003-05-22 20:06:43 +0000819 std::vector<std::pair<std::string, Option*> > Options;
Chris Lattner697954c2002-01-20 22:54:45 +0000820 copy(getOpts().begin(), getOpts().end(), std::back_inserter(Options));
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000821
822 // Eliminate Hidden or ReallyHidden arguments, depending on ShowHidden
Chris Lattner331de232002-07-22 02:07:59 +0000823 Options.erase(std::remove_if(Options.begin(), Options.end(),
824 std::ptr_fun(ShowHidden ? isReallyHidden : isHidden)),
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000825 Options.end());
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000826
827 // Eliminate duplicate entries in table (from enum flags options, f.e.)
Chris Lattner331de232002-07-22 02:07:59 +0000828 { // Give OptionSet a scope
829 std::set<Option*> OptionSet;
830 for (unsigned i = 0; i != Options.size(); ++i)
831 if (OptionSet.count(Options[i].second) == 0)
832 OptionSet.insert(Options[i].second); // Add new entry to set
833 else
834 Options.erase(Options.begin()+i--); // Erase duplicate
835 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000836
837 if (ProgramOverview)
Chris Lattnerca6433f2003-05-22 20:06:43 +0000838 std::cerr << "OVERVIEW:" << ProgramOverview << "\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000839
Chris Lattnerca6433f2003-05-22 20:06:43 +0000840 std::cerr << "USAGE: " << ProgramName << " [options]";
Chris Lattner331de232002-07-22 02:07:59 +0000841
842 // Print out the positional options...
Chris Lattnerca6433f2003-05-22 20:06:43 +0000843 std::vector<Option*> &PosOpts = getPositionalOpts();
Chris Lattner331de232002-07-22 02:07:59 +0000844 Option *CAOpt = 0; // The cl::ConsumeAfter option, if it exists...
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000845 if (!PosOpts.empty() && PosOpts[0]->getNumOccurrencesFlag() == ConsumeAfter)
Chris Lattner331de232002-07-22 02:07:59 +0000846 CAOpt = PosOpts[0];
847
Chris Lattner9cf3d472003-07-30 17:34:02 +0000848 for (unsigned i = CAOpt != 0, e = PosOpts.size(); i != e; ++i) {
849 if (PosOpts[i]->ArgStr[0])
850 std::cerr << " --" << PosOpts[i]->ArgStr;
Chris Lattnerca6433f2003-05-22 20:06:43 +0000851 std::cerr << " " << PosOpts[i]->HelpStr;
Chris Lattner9cf3d472003-07-30 17:34:02 +0000852 }
Chris Lattner331de232002-07-22 02:07:59 +0000853
854 // Print the consume after option info if it exists...
Chris Lattnerca6433f2003-05-22 20:06:43 +0000855 if (CAOpt) std::cerr << " " << CAOpt->HelpStr;
Chris Lattner331de232002-07-22 02:07:59 +0000856
Chris Lattnerca6433f2003-05-22 20:06:43 +0000857 std::cerr << "\n\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000858
859 // Compute the maximum argument length...
860 MaxArgLen = 0;
Chris Lattner331de232002-07-22 02:07:59 +0000861 for (unsigned i = 0, e = Options.size(); i != e; ++i)
862 MaxArgLen = std::max(MaxArgLen, Options[i].second->getOptionWidth());
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000863
Chris Lattnerca6433f2003-05-22 20:06:43 +0000864 std::cerr << "OPTIONS:\n";
Chris Lattner331de232002-07-22 02:07:59 +0000865 for (unsigned i = 0, e = Options.size(); i != e; ++i)
866 Options[i].second->printOptionInfo(MaxArgLen);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000867
Chris Lattner331de232002-07-22 02:07:59 +0000868 // Halt the program if help information is printed
869 exit(1);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000870 }
871};
872
Chris Lattner331de232002-07-22 02:07:59 +0000873
874
875// Define the two HelpPrinter instances that are used to print out help, or
876// help-hidden...
877//
878HelpPrinter NormalPrinter(false);
879HelpPrinter HiddenPrinter(true);
880
881cl::opt<HelpPrinter, true, parser<bool> >
882HOp("help", cl::desc("display available options (--help-hidden for more)"),
Chris Lattner9b14eb52002-08-07 18:36:37 +0000883 cl::location(NormalPrinter), cl::ValueDisallowed);
Chris Lattner331de232002-07-22 02:07:59 +0000884
885cl::opt<HelpPrinter, true, parser<bool> >
886HHOp("help-hidden", cl::desc("display all available options"),
Chris Lattner9b14eb52002-08-07 18:36:37 +0000887 cl::location(HiddenPrinter), cl::Hidden, cl::ValueDisallowed);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000888
889} // End anonymous namespace