blob: 3454ffc00fea7115f34ae273d7aaffa800906a4e [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 Lattner2cdd21c2003-12-14 21:35:53 +000026using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000027
Chris Lattnerdbab15a2001-07-23 17:17:47 +000028using namespace cl;
29
Chris Lattner331de232002-07-22 02:07:59 +000030//===----------------------------------------------------------------------===//
31// Basic, shared command line option processing machinery...
32//
33
Chris Lattnerdbab15a2001-07-23 17:17:47 +000034// Return the global command line option vector. Making it a function scoped
Chris Lattnerf78032f2001-11-26 18:58:34 +000035// static ensures that it will be initialized correctly before its first use.
Chris Lattnerdbab15a2001-07-23 17:17:47 +000036//
Chris Lattnerca6433f2003-05-22 20:06:43 +000037static std::map<std::string, Option*> *CommandLineOptions = 0;
38static std::map<std::string, Option*> &getOpts() {
39 if (CommandLineOptions == 0)
40 CommandLineOptions = new std::map<std::string,Option*>();
Chris Lattnere8e258b2002-07-29 20:58:42 +000041 return *CommandLineOptions;
42}
43
Chris Lattnerca6433f2003-05-22 20:06:43 +000044static Option *getOption(const std::string &Str) {
Chris Lattnere8e258b2002-07-29 20:58:42 +000045 if (CommandLineOptions == 0) return 0;
Chris Lattnerca6433f2003-05-22 20:06:43 +000046 std::map<std::string,Option*>::iterator I = CommandLineOptions->find(Str);
Chris Lattnere8e258b2002-07-29 20:58:42 +000047 return I != CommandLineOptions->end() ? I->second : 0;
Chris Lattnerdbab15a2001-07-23 17:17:47 +000048}
49
Chris Lattnerca6433f2003-05-22 20:06:43 +000050static std::vector<Option*> &getPositionalOpts() {
Alkis Evlogimenos5f65add2004-03-04 17:50:44 +000051 static std::vector<Option*> *Positional = 0;
52 if (!Positional) Positional = new std::vector<Option*>();
53 return *Positional;
Chris Lattner331de232002-07-22 02:07:59 +000054}
55
Chris Lattnere8e258b2002-07-29 20:58:42 +000056static void AddArgument(const char *ArgName, Option *Opt) {
57 if (getOption(ArgName)) {
Chris Lattnerca6433f2003-05-22 20:06:43 +000058 std::cerr << "CommandLine Error: Argument '" << ArgName
59 << "' defined more than once!\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +000060 } else {
Chris Lattnerf78032f2001-11-26 18:58:34 +000061 // Add argument to the argument map!
Chris Lattnere8e258b2002-07-29 20:58:42 +000062 getOpts()[ArgName] = Opt;
63 }
64}
65
66// RemoveArgument - It's possible that the argument is no longer in the map if
67// options have already been processed and the map has been deleted!
68//
69static void RemoveArgument(const char *ArgName, Option *Opt) {
70 if (CommandLineOptions == 0) return;
71 assert(getOption(ArgName) == Opt && "Arg not in map!");
72 CommandLineOptions->erase(ArgName);
73 if (CommandLineOptions->empty()) {
74 delete CommandLineOptions;
75 CommandLineOptions = 0;
Chris Lattnerdbab15a2001-07-23 17:17:47 +000076 }
77}
78
79static const char *ProgramName = 0;
80static const char *ProgramOverview = 0;
81
Chris Lattnercaccd762001-10-27 05:54:17 +000082static inline bool ProvideOption(Option *Handler, const char *ArgName,
83 const char *Value, int argc, char **argv,
84 int &i) {
85 // Enforce value requirements
86 switch (Handler->getValueExpectedFlag()) {
87 case ValueRequired:
88 if (Value == 0 || *Value == 0) { // No value specified?
89 if (i+1 < argc) { // Steal the next argument, like for '-o filename'
90 Value = argv[++i];
91 } else {
92 return Handler->error(" requires a value!");
93 }
94 }
95 break;
96 case ValueDisallowed:
97 if (*Value != 0)
98 return Handler->error(" does not allow a value! '" +
Chris Lattnerca6433f2003-05-22 20:06:43 +000099 std::string(Value) + "' specified.");
Chris Lattnercaccd762001-10-27 05:54:17 +0000100 break;
101 case ValueOptional: break;
Chris Lattnerca6433f2003-05-22 20:06:43 +0000102 default: std::cerr << "Bad ValueMask flag! CommandLine usage error:"
103 << Handler->getValueExpectedFlag() << "\n"; abort();
Chris Lattnercaccd762001-10-27 05:54:17 +0000104 }
105
106 // Run the handler now!
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000107 return Handler->addOccurrence(ArgName, Value);
Chris Lattnercaccd762001-10-27 05:54:17 +0000108}
109
Chris Lattner9cf3d472003-07-30 17:34:02 +0000110static bool ProvidePositionalOption(Option *Handler, const std::string &Arg) {
Chris Lattner331de232002-07-22 02:07:59 +0000111 int Dummy;
Chris Lattner9cf3d472003-07-30 17:34:02 +0000112 return ProvideOption(Handler, Handler->ArgStr, Arg.c_str(), 0, 0, Dummy);
Chris Lattner331de232002-07-22 02:07:59 +0000113}
Chris Lattnerf78032f2001-11-26 18:58:34 +0000114
Chris Lattner331de232002-07-22 02:07:59 +0000115
116// Option predicates...
117static inline bool isGrouping(const Option *O) {
118 return O->getFormattingFlag() == cl::Grouping;
119}
120static inline bool isPrefixedOrGrouping(const Option *O) {
121 return isGrouping(O) || O->getFormattingFlag() == cl::Prefix;
122}
123
124// getOptionPred - Check to see if there are any options that satisfy the
125// specified predicate with names that are the prefixes in Name. This is
126// checked by progressively stripping characters off of the name, checking to
127// see if there options that satisfy the predicate. If we find one, return it,
128// otherwise return null.
129//
130static Option *getOptionPred(std::string Name, unsigned &Length,
131 bool (*Pred)(const Option*)) {
132
Chris Lattnere8e258b2002-07-29 20:58:42 +0000133 Option *Op = getOption(Name);
134 if (Op && Pred(Op)) {
Chris Lattner331de232002-07-22 02:07:59 +0000135 Length = Name.length();
Chris Lattnere8e258b2002-07-29 20:58:42 +0000136 return Op;
Chris Lattnerf78032f2001-11-26 18:58:34 +0000137 }
138
Chris Lattner331de232002-07-22 02:07:59 +0000139 if (Name.size() == 1) return 0;
140 do {
141 Name.erase(Name.end()-1, Name.end()); // Chop off the last character...
Chris Lattnere8e258b2002-07-29 20:58:42 +0000142 Op = getOption(Name);
Chris Lattner331de232002-07-22 02:07:59 +0000143
144 // Loop while we haven't found an option and Name still has at least two
145 // characters in it (so that the next iteration will not be the empty
146 // string...
Chris Lattnere8e258b2002-07-29 20:58:42 +0000147 } while ((Op == 0 || !Pred(Op)) && Name.size() > 1);
Chris Lattner331de232002-07-22 02:07:59 +0000148
Chris Lattnere8e258b2002-07-29 20:58:42 +0000149 if (Op && Pred(Op)) {
Chris Lattner331de232002-07-22 02:07:59 +0000150 Length = Name.length();
Chris Lattnere8e258b2002-07-29 20:58:42 +0000151 return Op; // Found one!
Chris Lattner331de232002-07-22 02:07:59 +0000152 }
153 return 0; // No option found!
154}
155
156static bool RequiresValue(const Option *O) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000157 return O->getNumOccurrencesFlag() == cl::Required ||
158 O->getNumOccurrencesFlag() == cl::OneOrMore;
Chris Lattner331de232002-07-22 02:07:59 +0000159}
160
161static bool EatsUnboundedNumberOfValues(const Option *O) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000162 return O->getNumOccurrencesFlag() == cl::ZeroOrMore ||
163 O->getNumOccurrencesFlag() == cl::OneOrMore;
Chris Lattnerf78032f2001-11-26 18:58:34 +0000164}
Chris Lattnercaccd762001-10-27 05:54:17 +0000165
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000166/// ParseCStringVector - Break INPUT up wherever one or more
167/// whitespace characters are found, and store the resulting tokens in
168/// OUTPUT. The tokens stored in OUTPUT are dynamically allocated
169/// using strdup (), so it is the caller's responsibility to free ()
170/// them later.
Brian Gaeke06b06c52003-08-14 22:00:59 +0000171///
172static void ParseCStringVector (std::vector<char *> &output,
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000173 const char *input) {
174 // Characters which will be treated as token separators:
175 static const char *delims = " \v\f\t\r\n";
176
177 std::string work (input);
178 // Skip past any delims at head of input string.
179 size_t pos = work.find_first_not_of (delims);
180 // If the string consists entirely of delims, then exit early.
181 if (pos == std::string::npos) return;
182 // Otherwise, jump forward to beginning of first word.
183 work = work.substr (pos);
184 // Find position of first delimiter.
185 pos = work.find_first_of (delims);
186
187 while (!work.empty() && pos != std::string::npos) {
188 // Everything from 0 to POS is the next word to copy.
189 output.push_back (strdup (work.substr (0,pos).c_str ()));
190 // Is there another word in the string?
191 size_t nextpos = work.find_first_not_of (delims, pos + 1);
192 if (nextpos != std::string::npos) {
193 // Yes? Then remove delims from beginning ...
194 work = work.substr (work.find_first_not_of (delims, pos + 1));
195 // and find the end of the word.
196 pos = work.find_first_of (delims);
197 } else {
198 // No? (Remainder of string is delims.) End the loop.
199 work = "";
200 pos = std::string::npos;
201 }
202 }
203
204 // If `input' ended with non-delim char, then we'll get here with
205 // the last word of `input' in `work'; copy it now.
206 if (!work.empty ()) {
207 output.push_back (strdup (work.c_str ()));
Brian Gaeke06b06c52003-08-14 22:00:59 +0000208 }
209}
210
211/// ParseEnvironmentOptions - An alternative entry point to the
212/// CommandLine library, which allows you to read the program's name
213/// from the caller (as PROGNAME) and its command-line arguments from
214/// an environment variable (whose name is given in ENVVAR).
215///
Chris Lattnerbf455c22004-05-06 22:04:31 +0000216void cl::ParseEnvironmentOptions(const char *progName, const char *envVar,
217 const char *Overview) {
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000218 // Check args.
Chris Lattnerbf455c22004-05-06 22:04:31 +0000219 assert(progName && "Program name not specified");
220 assert(envVar && "Environment variable name missing");
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000221
222 // Get the environment variable they want us to parse options out of.
223 const char *envValue = getenv (envVar);
224 if (!envValue)
225 return;
226
Brian Gaeke06b06c52003-08-14 22:00:59 +0000227 // Get program's "name", which we wouldn't know without the caller
228 // telling us.
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000229 std::vector<char *> newArgv;
230 newArgv.push_back (strdup (progName));
Brian Gaeke06b06c52003-08-14 22:00:59 +0000231
232 // Parse the value of the environment variable into a "command line"
233 // and hand it off to ParseCommandLineOptions().
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000234 ParseCStringVector (newArgv, envValue);
235 int newArgc = newArgv.size ();
236 ParseCommandLineOptions (newArgc, &newArgv[0], Overview);
237
238 // Free all the strdup()ed strings.
239 for (std::vector<char *>::iterator i = newArgv.begin (), e = newArgv.end ();
240 i != e; ++i) {
241 free (*i);
242 }
Brian Gaeke06b06c52003-08-14 22:00:59 +0000243}
244
Chris Lattnerbf455c22004-05-06 22:04:31 +0000245/// LookupOption - Lookup the option specified by the specified option on the
246/// command line. If there is a value specified (after an equal sign) return
247/// that as well.
248static Option *LookupOption(const char *&Arg, const char *&Value) {
249 while (*Arg == '-') ++Arg; // Eat leading dashes
250
251 const char *ArgEnd = Arg;
252 while (*ArgEnd && *ArgEnd != '=')
253 ++ArgEnd; // Scan till end of argument name...
254
255 Value = ArgEnd;
256 if (*Value) // If we have an equals sign...
257 ++Value; // Advance to value...
258
259 if (*Arg == 0) return 0;
260
261 // Look up the option.
262 std::map<std::string, Option*> &Opts = getOpts();
263 std::map<std::string, Option*>::iterator I =
264 Opts.find(std::string(Arg, ArgEnd));
265 return (I != Opts.end()) ? I->second : 0;
266}
267
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000268void cl::ParseCommandLineOptions(int &argc, char **argv,
Chris Lattner0c0edf82002-07-25 06:17:51 +0000269 const char *Overview) {
Chris Lattner331de232002-07-22 02:07:59 +0000270 assert((!getOpts().empty() || !getPositionalOpts().empty()) &&
271 "No options specified, or ParseCommandLineOptions called more"
272 " than once!");
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000273 ProgramName = argv[0]; // Save this away safe and snug
274 ProgramOverview = Overview;
275 bool ErrorParsing = false;
276
Chris Lattnerca6433f2003-05-22 20:06:43 +0000277 std::map<std::string, Option*> &Opts = getOpts();
278 std::vector<Option*> &PositionalOpts = getPositionalOpts();
Chris Lattner331de232002-07-22 02:07:59 +0000279
280 // Check out the positional arguments to collect information about them.
281 unsigned NumPositionalRequired = 0;
282 Option *ConsumeAfterOpt = 0;
283 if (!PositionalOpts.empty()) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000284 if (PositionalOpts[0]->getNumOccurrencesFlag() == cl::ConsumeAfter) {
Chris Lattner331de232002-07-22 02:07:59 +0000285 assert(PositionalOpts.size() > 1 &&
286 "Cannot specify cl::ConsumeAfter without a positional argument!");
287 ConsumeAfterOpt = PositionalOpts[0];
288 }
289
290 // Calculate how many positional values are _required_.
291 bool UnboundedFound = false;
292 for (unsigned i = ConsumeAfterOpt != 0, e = PositionalOpts.size();
293 i != e; ++i) {
294 Option *Opt = PositionalOpts[i];
295 if (RequiresValue(Opt))
296 ++NumPositionalRequired;
297 else if (ConsumeAfterOpt) {
298 // ConsumeAfter cannot be combined with "optional" positional options
Chris Lattner54ec7ae2002-07-22 02:21:57 +0000299 // unless there is only one positional argument...
300 if (PositionalOpts.size() > 2)
301 ErrorParsing |=
302 Opt->error(" error - this positional option will never be matched, "
303 "because it does not Require a value, and a "
304 "cl::ConsumeAfter option is active!");
Chris Lattner9cf3d472003-07-30 17:34:02 +0000305 } else if (UnboundedFound && !Opt->ArgStr[0]) {
306 // This option does not "require" a value... Make sure this option is
307 // not specified after an option that eats all extra arguments, or this
308 // one will never get any!
Chris Lattner331de232002-07-22 02:07:59 +0000309 //
310 ErrorParsing |= Opt->error(" error - option can never match, because "
311 "another positional argument will match an "
312 "unbounded number of values, and this option"
313 " does not require a value!");
314 }
315 UnboundedFound |= EatsUnboundedNumberOfValues(Opt);
316 }
317 }
318
319 // PositionalVals - A vector of "positional" arguments we accumulate into to
320 // processes at the end...
321 //
Chris Lattnerca6433f2003-05-22 20:06:43 +0000322 std::vector<std::string> PositionalVals;
Chris Lattner331de232002-07-22 02:07:59 +0000323
Chris Lattner9cf3d472003-07-30 17:34:02 +0000324 // If the program has named positional arguments, and the name has been run
325 // across, keep track of which positional argument was named. Otherwise put
326 // the positional args into the PositionalVals list...
327 Option *ActivePositionalArg = 0;
328
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000329 // Loop over all of the arguments... processing them.
Chris Lattner331de232002-07-22 02:07:59 +0000330 bool DashDashFound = false; // Have we read '--'?
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000331 for (int i = 1; i < argc; ++i) {
332 Option *Handler = 0;
333 const char *Value = "";
334 const char *ArgName = "";
Chris Lattner331de232002-07-22 02:07:59 +0000335
336 // Check to see if this is a positional argument. This argument is
337 // considered to be positional if it doesn't start with '-', if it is "-"
Misha Brukman1115e042003-07-10 21:38:28 +0000338 // itself, or if we have seen "--" already.
Chris Lattner331de232002-07-22 02:07:59 +0000339 //
340 if (argv[i][0] != '-' || argv[i][1] == 0 || DashDashFound) {
341 // Positional argument!
Chris Lattner9cf3d472003-07-30 17:34:02 +0000342 if (ActivePositionalArg) {
343 ProvidePositionalOption(ActivePositionalArg, argv[i]);
344 continue; // We are done!
345 } else if (!PositionalOpts.empty()) {
Chris Lattner331de232002-07-22 02:07:59 +0000346 PositionalVals.push_back(argv[i]);
347
348 // All of the positional arguments have been fulfulled, give the rest to
349 // the consume after option... if it's specified...
350 //
Chris Lattnerd16714b2002-07-31 16:29:43 +0000351 if (PositionalVals.size() >= NumPositionalRequired &&
Chris Lattner331de232002-07-22 02:07:59 +0000352 ConsumeAfterOpt != 0) {
353 for (++i; i < argc; ++i)
354 PositionalVals.push_back(argv[i]);
355 break; // Handle outside of the argument processing loop...
356 }
357
358 // Delay processing positional arguments until the end...
359 continue;
360 }
Chris Lattnerbf455c22004-05-06 22:04:31 +0000361 } else if (argv[i][0] == '-' && argv[i][1] == '-' && argv[i][2] == 0 &&
362 !DashDashFound) {
363 DashDashFound = true; // This is the mythical "--"?
364 continue; // Don't try to process it as an argument itself.
365 } else if (ActivePositionalArg &&
366 (ActivePositionalArg->getMiscFlags() & PositionalEatsArgs)) {
367 // If there is a positional argument eating options, check to see if this
368 // option is another positional argument. If so, treat it as an argument,
369 // otherwise feed it to the eating positional.
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000370 ArgName = argv[i]+1;
Chris Lattnerbf455c22004-05-06 22:04:31 +0000371 Handler = LookupOption(ArgName, Value);
372 if (!Handler || Handler->getFormattingFlag() != cl::Positional) {
373 ProvidePositionalOption(ActivePositionalArg, argv[i]);
374 continue; // We are done!
Chris Lattner331de232002-07-22 02:07:59 +0000375 }
376
Chris Lattnerbf455c22004-05-06 22:04:31 +0000377 } else { // We start with a '-', must be an argument...
378 ArgName = argv[i]+1;
379 Handler = LookupOption(ArgName, Value);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000380
Chris Lattnerbf455c22004-05-06 22:04:31 +0000381 // Check to see if this "option" is really a prefixed or grouped argument.
382 if (Handler == 0 && *Value == 0) {
383 std::string RealName(ArgName);
384 if (RealName.size() > 1) {
Chris Lattner331de232002-07-22 02:07:59 +0000385 unsigned Length = 0;
386 Option *PGOpt = getOptionPred(RealName, Length, isPrefixedOrGrouping);
Chris Lattnerbf455c22004-05-06 22:04:31 +0000387
Chris Lattner331de232002-07-22 02:07:59 +0000388 // If the option is a prefixed option, then the value is simply the
389 // rest of the name... so fall through to later processing, by
390 // setting up the argument name flags and value fields.
391 //
392 if (PGOpt && PGOpt->getFormattingFlag() == cl::Prefix) {
Chris Lattnerbf455c22004-05-06 22:04:31 +0000393 Value = ArgName+Length;
394 assert(Opts.find(std::string(ArgName, Value)) != Opts.end() &&
395 Opts.find(std::string(ArgName, Value))->second == PGOpt);
396 Handler = PGOpt;
Chris Lattner331de232002-07-22 02:07:59 +0000397 } else if (PGOpt) {
Chris Lattnerbf455c22004-05-06 22:04:31 +0000398 // This must be a grouped option... handle them now.
Chris Lattner331de232002-07-22 02:07:59 +0000399 assert(isGrouping(PGOpt) && "Broken getOptionPred!");
Chris Lattnerbf455c22004-05-06 22:04:31 +0000400
Chris Lattner331de232002-07-22 02:07:59 +0000401 do {
402 // Move current arg name out of RealName into RealArgName...
Chris Lattnerbf455c22004-05-06 22:04:31 +0000403 std::string RealArgName(RealName.begin(),
404 RealName.begin() + Length);
405 RealName.erase(RealName.begin(), RealName.begin() + Length);
406
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000407 // Because ValueRequired is an invalid flag for grouped arguments,
408 // we don't need to pass argc/argv in...
409 //
Chris Lattner331de232002-07-22 02:07:59 +0000410 assert(PGOpt->getValueExpectedFlag() != cl::ValueRequired &&
411 "Option can not be cl::Grouping AND cl::ValueRequired!");
412 int Dummy;
Chris Lattnerbf455c22004-05-06 22:04:31 +0000413 ErrorParsing |= ProvideOption(PGOpt, RealArgName.c_str(),
414 "", 0, 0, Dummy);
415
Chris Lattner331de232002-07-22 02:07:59 +0000416 // Get the next grouping option...
Chris Lattnerbf455c22004-05-06 22:04:31 +0000417 PGOpt = getOptionPred(RealName, Length, isGrouping);
418 } while (PGOpt && Length != RealName.size());
419
420 Handler = PGOpt; // Ate all of the options.
Chris Lattner331de232002-07-22 02:07:59 +0000421 }
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000422 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000423 }
424 }
425
426 if (Handler == 0) {
Brian Gaekec86e84b2003-09-16 18:00:35 +0000427 std::cerr << "Unknown command line argument '" << argv[i] << "'. Try: '"
Chris Lattnerca6433f2003-05-22 20:06:43 +0000428 << argv[0] << " --help'\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000429 ErrorParsing = true;
430 continue;
431 }
432
Chris Lattner72fb8e52003-05-22 20:26:17 +0000433 // Check to see if this option accepts a comma separated list of values. If
434 // it does, we have to split up the value into multiple values...
435 if (Handler->getMiscFlags() & CommaSeparated) {
436 std::string Val(Value);
437 std::string::size_type Pos = Val.find(',');
438
439 while (Pos != std::string::npos) {
440 // Process the portion before the comma...
441 ErrorParsing |= ProvideOption(Handler, ArgName,
442 std::string(Val.begin(),
443 Val.begin()+Pos).c_str(),
444 argc, argv, i);
445 // Erase the portion before the comma, AND the comma...
446 Val.erase(Val.begin(), Val.begin()+Pos+1);
447 Value += Pos+1; // Increment the original value pointer as well...
448
449 // Check for another comma...
450 Pos = Val.find(',');
451 }
452 }
Chris Lattner9cf3d472003-07-30 17:34:02 +0000453
454 // If this is a named positional argument, just remember that it is the
455 // active one...
456 if (Handler->getFormattingFlag() == cl::Positional)
457 ActivePositionalArg = Handler;
458 else
459 ErrorParsing |= ProvideOption(Handler, ArgName, Value, argc, argv, i);
Chris Lattner331de232002-07-22 02:07:59 +0000460 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000461
Chris Lattner331de232002-07-22 02:07:59 +0000462 // Check and handle positional arguments now...
463 if (NumPositionalRequired > PositionalVals.size()) {
Chris Lattnerca6433f2003-05-22 20:06:43 +0000464 std::cerr << "Not enough positional command line arguments specified!\n"
465 << "Must specify at least " << NumPositionalRequired
466 << " positional arguments: See: " << argv[0] << " --help\n";
Chris Lattner331de232002-07-22 02:07:59 +0000467 ErrorParsing = true;
468
469
470 } else if (ConsumeAfterOpt == 0) {
471 // Positional args have already been handled if ConsumeAfter is specified...
472 unsigned ValNo = 0, NumVals = PositionalVals.size();
473 for (unsigned i = 0, e = PositionalOpts.size(); i != e; ++i) {
474 if (RequiresValue(PositionalOpts[i])) {
475 ProvidePositionalOption(PositionalOpts[i], PositionalVals[ValNo++]);
476 --NumPositionalRequired; // We fulfilled our duty...
477 }
478
479 // If we _can_ give this option more arguments, do so now, as long as we
480 // do not give it values that others need. 'Done' controls whether the
481 // option even _WANTS_ any more.
482 //
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000483 bool Done = PositionalOpts[i]->getNumOccurrencesFlag() == cl::Required;
Chris Lattner331de232002-07-22 02:07:59 +0000484 while (NumVals-ValNo > NumPositionalRequired && !Done) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000485 switch (PositionalOpts[i]->getNumOccurrencesFlag()) {
Chris Lattner331de232002-07-22 02:07:59 +0000486 case cl::Optional:
487 Done = true; // Optional arguments want _at most_ one value
488 // FALL THROUGH
489 case cl::ZeroOrMore: // Zero or more will take all they can get...
490 case cl::OneOrMore: // One or more will take all they can get...
491 ProvidePositionalOption(PositionalOpts[i], PositionalVals[ValNo++]);
492 break;
493 default:
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000494 assert(0 && "Internal error, unexpected NumOccurrences flag in "
Chris Lattner331de232002-07-22 02:07:59 +0000495 "positional argument processing!");
496 }
497 }
Chris Lattnercaccd762001-10-27 05:54:17 +0000498 }
Chris Lattner331de232002-07-22 02:07:59 +0000499 } else {
500 assert(ConsumeAfterOpt && NumPositionalRequired <= PositionalVals.size());
501 unsigned ValNo = 0;
502 for (unsigned j = 1, e = PositionalOpts.size(); j != e; ++j)
503 if (RequiresValue(PositionalOpts[j]))
Chris Lattnerfaba8092002-07-24 20:15:13 +0000504 ErrorParsing |= ProvidePositionalOption(PositionalOpts[j],
505 PositionalVals[ValNo++]);
506
507 // Handle the case where there is just one positional option, and it's
508 // optional. In this case, we want to give JUST THE FIRST option to the
509 // positional option and keep the rest for the consume after. The above
510 // loop would have assigned no values to positional options in this case.
511 //
Chris Lattnerb490c202002-08-02 21:51:29 +0000512 if (PositionalOpts.size() == 2 && ValNo == 0 && !PositionalVals.empty())
Chris Lattnerfaba8092002-07-24 20:15:13 +0000513 ErrorParsing |= ProvidePositionalOption(PositionalOpts[1],
514 PositionalVals[ValNo++]);
Chris Lattner331de232002-07-22 02:07:59 +0000515
516 // Handle over all of the rest of the arguments to the
517 // cl::ConsumeAfter command line option...
518 for (; ValNo != PositionalVals.size(); ++ValNo)
519 ErrorParsing |= ProvidePositionalOption(ConsumeAfterOpt,
520 PositionalVals[ValNo]);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000521 }
522
Chris Lattnerdc4693d2001-07-23 23:02:45 +0000523 // Loop over args and make sure all required args are specified!
Chris Lattnerca6433f2003-05-22 20:06:43 +0000524 for (std::map<std::string, Option*>::iterator I = Opts.begin(),
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000525 E = Opts.end(); I != E; ++I) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000526 switch (I->second->getNumOccurrencesFlag()) {
Chris Lattnerdc4693d2001-07-23 23:02:45 +0000527 case Required:
528 case OneOrMore:
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000529 if (I->second->getNumOccurrences() == 0) {
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000530 I->second->error(" must be specified at least once!");
Chris Lattnerf038acb2001-10-24 06:21:56 +0000531 ErrorParsing = true;
532 }
Chris Lattnerdc4693d2001-07-23 23:02:45 +0000533 // Fall through
534 default:
535 break;
536 }
537 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000538
Chris Lattner331de232002-07-22 02:07:59 +0000539 // Free all of the memory allocated to the map. Command line options may only
540 // be processed once!
Chris Lattnere8e258b2002-07-29 20:58:42 +0000541 delete CommandLineOptions;
542 CommandLineOptions = 0;
Chris Lattner331de232002-07-22 02:07:59 +0000543 PositionalOpts.clear();
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000544
545 // If we had an error processing our arguments, don't let the program execute
546 if (ErrorParsing) exit(1);
547}
548
549//===----------------------------------------------------------------------===//
550// Option Base class implementation
551//
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000552
Chris Lattnerca6433f2003-05-22 20:06:43 +0000553bool Option::error(std::string Message, const char *ArgName) {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000554 if (ArgName == 0) ArgName = ArgStr;
Chris Lattner331de232002-07-22 02:07:59 +0000555 if (ArgName[0] == 0)
Chris Lattnerca6433f2003-05-22 20:06:43 +0000556 std::cerr << HelpStr; // Be nice for positional arguments
Chris Lattner331de232002-07-22 02:07:59 +0000557 else
Chris Lattnerca6433f2003-05-22 20:06:43 +0000558 std::cerr << "-" << ArgName;
559 std::cerr << " option" << Message << "\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000560 return true;
561}
562
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000563bool Option::addOccurrence(const char *ArgName, const std::string &Value) {
564 NumOccurrences++; // Increment the number of times we have been seen
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000565
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000566 switch (getNumOccurrencesFlag()) {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000567 case Optional:
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000568 if (NumOccurrences > 1)
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000569 return error(": may only occur zero or one times!", ArgName);
570 break;
571 case Required:
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000572 if (NumOccurrences > 1)
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000573 return error(": must occur exactly one time!", ArgName);
574 // Fall through
575 case OneOrMore:
Chris Lattnercaccd762001-10-27 05:54:17 +0000576 case ZeroOrMore:
577 case ConsumeAfter: break;
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000578 default: return error(": bad num occurrences flag value!");
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000579 }
580
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000581 return handleOccurrence(ArgName, Value);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000582}
583
Chris Lattner331de232002-07-22 02:07:59 +0000584// addArgument - Tell the system that this Option subclass will handle all
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000585// occurrences of -ArgStr on the command line.
Chris Lattner331de232002-07-22 02:07:59 +0000586//
587void Option::addArgument(const char *ArgStr) {
588 if (ArgStr[0])
589 AddArgument(ArgStr, this);
Chris Lattner9cf3d472003-07-30 17:34:02 +0000590
591 if (getFormattingFlag() == Positional)
Chris Lattner331de232002-07-22 02:07:59 +0000592 getPositionalOpts().push_back(this);
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000593 else if (getNumOccurrencesFlag() == ConsumeAfter) {
Chris Lattner9cf3d472003-07-30 17:34:02 +0000594 if (!getPositionalOpts().empty() &&
595 getPositionalOpts().front()->getNumOccurrencesFlag() == ConsumeAfter)
596 error("Cannot specify more than one option with cl::ConsumeAfter!");
Chris Lattner331de232002-07-22 02:07:59 +0000597 getPositionalOpts().insert(getPositionalOpts().begin(), this);
598 }
599}
600
Chris Lattneraa852bb2002-07-23 17:15:12 +0000601void Option::removeArgument(const char *ArgStr) {
Chris Lattner9cf3d472003-07-30 17:34:02 +0000602 if (ArgStr[0])
Chris Lattnere8e258b2002-07-29 20:58:42 +0000603 RemoveArgument(ArgStr, this);
Chris Lattner9cf3d472003-07-30 17:34:02 +0000604
605 if (getFormattingFlag() == Positional) {
Chris Lattnerca6433f2003-05-22 20:06:43 +0000606 std::vector<Option*>::iterator I =
Chris Lattneraa852bb2002-07-23 17:15:12 +0000607 std::find(getPositionalOpts().begin(), getPositionalOpts().end(), this);
608 assert(I != getPositionalOpts().end() && "Arg not registered!");
609 getPositionalOpts().erase(I);
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000610 } else if (getNumOccurrencesFlag() == ConsumeAfter) {
Chris Lattneraa852bb2002-07-23 17:15:12 +0000611 assert(!getPositionalOpts().empty() && getPositionalOpts()[0] == this &&
612 "Arg not registered correctly!");
613 getPositionalOpts().erase(getPositionalOpts().begin());
614 }
615}
616
Chris Lattner331de232002-07-22 02:07:59 +0000617
618// getValueStr - Get the value description string, using "DefaultMsg" if nothing
619// has been specified yet.
620//
621static const char *getValueStr(const Option &O, const char *DefaultMsg) {
622 if (O.ValueStr[0] == 0) return DefaultMsg;
623 return O.ValueStr;
624}
625
626//===----------------------------------------------------------------------===//
627// cl::alias class implementation
628//
629
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000630// Return the width of the option tag for printing...
Chris Lattner331de232002-07-22 02:07:59 +0000631unsigned alias::getOptionWidth() const {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000632 return std::strlen(ArgStr)+6;
633}
634
Chris Lattner331de232002-07-22 02:07:59 +0000635// Print out the option for the alias...
636void alias::printOptionInfo(unsigned GlobalWidth) const {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000637 unsigned L = std::strlen(ArgStr);
Chris Lattnerca6433f2003-05-22 20:06:43 +0000638 std::cerr << " -" << ArgStr << std::string(GlobalWidth-L-6, ' ') << " - "
639 << HelpStr << "\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000640}
641
642
Chris Lattner331de232002-07-22 02:07:59 +0000643
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000644//===----------------------------------------------------------------------===//
Chris Lattner331de232002-07-22 02:07:59 +0000645// Parser Implementation code...
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000646//
647
Chris Lattner9b14eb52002-08-07 18:36:37 +0000648// basic_parser implementation
649//
650
651// Return the width of the option tag for printing...
652unsigned basic_parser_impl::getOptionWidth(const Option &O) const {
653 unsigned Len = std::strlen(O.ArgStr);
654 if (const char *ValName = getValueName())
655 Len += std::strlen(getValueStr(O, ValName))+3;
656
657 return Len + 6;
658}
659
660// printOptionInfo - Print out information about this option. The
661// to-be-maintained width is specified.
662//
663void basic_parser_impl::printOptionInfo(const Option &O,
664 unsigned GlobalWidth) const {
Chris Lattnerca6433f2003-05-22 20:06:43 +0000665 std::cerr << " -" << O.ArgStr;
Chris Lattner9b14eb52002-08-07 18:36:37 +0000666
667 if (const char *ValName = getValueName())
Chris Lattnerca6433f2003-05-22 20:06:43 +0000668 std::cerr << "=<" << getValueStr(O, ValName) << ">";
Chris Lattner9b14eb52002-08-07 18:36:37 +0000669
Chris Lattnerca6433f2003-05-22 20:06:43 +0000670 std::cerr << std::string(GlobalWidth-getOptionWidth(O), ' ') << " - "
671 << O.HelpStr << "\n";
Chris Lattner9b14eb52002-08-07 18:36:37 +0000672}
673
674
675
676
Chris Lattner331de232002-07-22 02:07:59 +0000677// parser<bool> implementation
678//
Chris Lattner9b14eb52002-08-07 18:36:37 +0000679bool parser<bool>::parse(Option &O, const char *ArgName,
Chris Lattnerca6433f2003-05-22 20:06:43 +0000680 const std::string &Arg, bool &Value) {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000681 if (Arg == "" || Arg == "true" || Arg == "TRUE" || Arg == "True" ||
682 Arg == "1") {
683 Value = true;
684 } else if (Arg == "false" || Arg == "FALSE" || Arg == "False" || Arg == "0") {
685 Value = false;
686 } else {
Chris Lattner331de232002-07-22 02:07:59 +0000687 return O.error(": '" + Arg +
688 "' is invalid value for boolean argument! Try 0 or 1");
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000689 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000690 return false;
691}
692
Chris Lattner331de232002-07-22 02:07:59 +0000693// parser<int> implementation
694//
Chris Lattner9b14eb52002-08-07 18:36:37 +0000695bool parser<int>::parse(Option &O, const char *ArgName,
Chris Lattnerca6433f2003-05-22 20:06:43 +0000696 const std::string &Arg, int &Value) {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000697 char *End;
Chris Lattnerd2a6fc32003-06-28 15:47:20 +0000698 Value = (int)strtol(Arg.c_str(), &End, 0);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000699 if (*End != 0)
Chris Lattner331de232002-07-22 02:07:59 +0000700 return O.error(": '" + Arg + "' value invalid for integer argument!");
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000701 return false;
702}
703
Chris Lattnerd2a6fc32003-06-28 15:47:20 +0000704// parser<unsigned> implementation
705//
706bool parser<unsigned>::parse(Option &O, const char *ArgName,
707 const std::string &Arg, unsigned &Value) {
708 char *End;
Brian Gaeke2d6a2362003-10-10 17:01:36 +0000709 errno = 0;
710 unsigned long V = strtoul(Arg.c_str(), &End, 0);
Chris Lattnerd2a6fc32003-06-28 15:47:20 +0000711 Value = (unsigned)V;
Brian Gaeke2d6a2362003-10-10 17:01:36 +0000712 if (((V == ULONG_MAX) && (errno == ERANGE))
713 || (*End != 0)
714 || (Value != V))
Chris Lattnerd2a6fc32003-06-28 15:47:20 +0000715 return O.error(": '" + Arg + "' value invalid for uint argument!");
716 return false;
717}
718
Chris Lattner9b14eb52002-08-07 18:36:37 +0000719// parser<double>/parser<float> implementation
Chris Lattnerd215fd12001-10-13 06:53:19 +0000720//
Chris Lattnerca6433f2003-05-22 20:06:43 +0000721static bool parseDouble(Option &O, const std::string &Arg, double &Value) {
Chris Lattner331de232002-07-22 02:07:59 +0000722 const char *ArgStart = Arg.c_str();
723 char *End;
724 Value = strtod(ArgStart, &End);
725 if (*End != 0)
726 return O.error(": '" +Arg+ "' value invalid for floating point argument!");
Chris Lattnerd215fd12001-10-13 06:53:19 +0000727 return false;
728}
729
Chris Lattner9b14eb52002-08-07 18:36:37 +0000730bool parser<double>::parse(Option &O, const char *AN,
731 const std::string &Arg, double &Val) {
732 return parseDouble(O, Arg, Val);
Chris Lattner331de232002-07-22 02:07:59 +0000733}
734
Chris Lattner9b14eb52002-08-07 18:36:37 +0000735bool parser<float>::parse(Option &O, const char *AN,
736 const std::string &Arg, float &Val) {
737 double dVal;
738 if (parseDouble(O, Arg, dVal))
739 return true;
740 Val = (float)dVal;
741 return false;
Chris Lattner331de232002-07-22 02:07:59 +0000742}
743
744
Chris Lattner331de232002-07-22 02:07:59 +0000745
746// generic_parser_base implementation
747//
748
Chris Lattneraa852bb2002-07-23 17:15:12 +0000749// findOption - Return the option number corresponding to the specified
750// argument string. If the option is not found, getNumOptions() is returned.
751//
752unsigned generic_parser_base::findOption(const char *Name) {
753 unsigned i = 0, e = getNumOptions();
Chris Lattnerca6433f2003-05-22 20:06:43 +0000754 std::string N(Name);
Chris Lattneraa852bb2002-07-23 17:15:12 +0000755
756 while (i != e)
757 if (getOption(i) == N)
758 return i;
759 else
760 ++i;
761 return e;
762}
763
764
Chris Lattner331de232002-07-22 02:07:59 +0000765// Return the width of the option tag for printing...
766unsigned generic_parser_base::getOptionWidth(const Option &O) const {
767 if (O.hasArgStr()) {
768 unsigned Size = std::strlen(O.ArgStr)+6;
769 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
770 Size = std::max(Size, (unsigned)std::strlen(getOption(i))+8);
771 return Size;
772 } else {
773 unsigned BaseSize = 0;
774 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
775 BaseSize = std::max(BaseSize, (unsigned)std::strlen(getOption(i))+8);
776 return BaseSize;
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000777 }
778}
779
Chris Lattner331de232002-07-22 02:07:59 +0000780// printOptionInfo - Print out information about this option. The
781// to-be-maintained width is specified.
782//
783void generic_parser_base::printOptionInfo(const Option &O,
784 unsigned GlobalWidth) const {
785 if (O.hasArgStr()) {
786 unsigned L = std::strlen(O.ArgStr);
Chris Lattnerca6433f2003-05-22 20:06:43 +0000787 std::cerr << " -" << O.ArgStr << std::string(GlobalWidth-L-6, ' ')
788 << " - " << O.HelpStr << "\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000789
Chris Lattner331de232002-07-22 02:07:59 +0000790 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
791 unsigned NumSpaces = GlobalWidth-strlen(getOption(i))-8;
Chris Lattnerca6433f2003-05-22 20:06:43 +0000792 std::cerr << " =" << getOption(i) << std::string(NumSpaces, ' ')
793 << " - " << getDescription(i) << "\n";
Chris Lattner9c9be482002-01-31 00:42:56 +0000794 }
Chris Lattner331de232002-07-22 02:07:59 +0000795 } else {
796 if (O.HelpStr[0])
Chris Lattnerca6433f2003-05-22 20:06:43 +0000797 std::cerr << " " << O.HelpStr << "\n";
Chris Lattner331de232002-07-22 02:07:59 +0000798 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
799 unsigned L = std::strlen(getOption(i));
Chris Lattnerca6433f2003-05-22 20:06:43 +0000800 std::cerr << " -" << getOption(i) << std::string(GlobalWidth-L-8, ' ')
801 << " - " << getDescription(i) << "\n";
Chris Lattner331de232002-07-22 02:07:59 +0000802 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000803 }
804}
805
806
807//===----------------------------------------------------------------------===//
Chris Lattner331de232002-07-22 02:07:59 +0000808// --help and --help-hidden option implementation
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000809//
810namespace {
811
Chris Lattner331de232002-07-22 02:07:59 +0000812class HelpPrinter {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000813 unsigned MaxArgLen;
814 const Option *EmptyArg;
815 const bool ShowHidden;
816
Chris Lattner331de232002-07-22 02:07:59 +0000817 // isHidden/isReallyHidden - Predicates to be used to filter down arg lists.
Chris Lattnerca6433f2003-05-22 20:06:43 +0000818 inline static bool isHidden(std::pair<std::string, Option *> &OptPair) {
Chris Lattner331de232002-07-22 02:07:59 +0000819 return OptPair.second->getOptionHiddenFlag() >= Hidden;
820 }
Chris Lattnerca6433f2003-05-22 20:06:43 +0000821 inline static bool isReallyHidden(std::pair<std::string, Option *> &OptPair) {
Chris Lattner331de232002-07-22 02:07:59 +0000822 return OptPair.second->getOptionHiddenFlag() == ReallyHidden;
823 }
824
825public:
826 HelpPrinter(bool showHidden) : ShowHidden(showHidden) {
827 EmptyArg = 0;
828 }
829
830 void operator=(bool Value) {
831 if (Value == false) return;
832
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000833 // Copy Options into a vector so we can sort them as we like...
Chris Lattnerca6433f2003-05-22 20:06:43 +0000834 std::vector<std::pair<std::string, Option*> > Options;
Chris Lattner697954c2002-01-20 22:54:45 +0000835 copy(getOpts().begin(), getOpts().end(), std::back_inserter(Options));
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000836
837 // Eliminate Hidden or ReallyHidden arguments, depending on ShowHidden
Chris Lattner331de232002-07-22 02:07:59 +0000838 Options.erase(std::remove_if(Options.begin(), Options.end(),
839 std::ptr_fun(ShowHidden ? isReallyHidden : isHidden)),
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000840 Options.end());
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000841
842 // Eliminate duplicate entries in table (from enum flags options, f.e.)
Chris Lattner331de232002-07-22 02:07:59 +0000843 { // Give OptionSet a scope
844 std::set<Option*> OptionSet;
845 for (unsigned i = 0; i != Options.size(); ++i)
846 if (OptionSet.count(Options[i].second) == 0)
847 OptionSet.insert(Options[i].second); // Add new entry to set
848 else
849 Options.erase(Options.begin()+i--); // Erase duplicate
850 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000851
852 if (ProgramOverview)
Chris Lattnerca6433f2003-05-22 20:06:43 +0000853 std::cerr << "OVERVIEW:" << ProgramOverview << "\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000854
Chris Lattnerca6433f2003-05-22 20:06:43 +0000855 std::cerr << "USAGE: " << ProgramName << " [options]";
Chris Lattner331de232002-07-22 02:07:59 +0000856
857 // Print out the positional options...
Chris Lattnerca6433f2003-05-22 20:06:43 +0000858 std::vector<Option*> &PosOpts = getPositionalOpts();
Chris Lattner331de232002-07-22 02:07:59 +0000859 Option *CAOpt = 0; // The cl::ConsumeAfter option, if it exists...
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000860 if (!PosOpts.empty() && PosOpts[0]->getNumOccurrencesFlag() == ConsumeAfter)
Chris Lattner331de232002-07-22 02:07:59 +0000861 CAOpt = PosOpts[0];
862
Chris Lattner9cf3d472003-07-30 17:34:02 +0000863 for (unsigned i = CAOpt != 0, e = PosOpts.size(); i != e; ++i) {
864 if (PosOpts[i]->ArgStr[0])
865 std::cerr << " --" << PosOpts[i]->ArgStr;
Chris Lattnerca6433f2003-05-22 20:06:43 +0000866 std::cerr << " " << PosOpts[i]->HelpStr;
Chris Lattner9cf3d472003-07-30 17:34:02 +0000867 }
Chris Lattner331de232002-07-22 02:07:59 +0000868
869 // Print the consume after option info if it exists...
Chris Lattnerca6433f2003-05-22 20:06:43 +0000870 if (CAOpt) std::cerr << " " << CAOpt->HelpStr;
Chris Lattner331de232002-07-22 02:07:59 +0000871
Chris Lattnerca6433f2003-05-22 20:06:43 +0000872 std::cerr << "\n\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000873
874 // Compute the maximum argument length...
875 MaxArgLen = 0;
Chris Lattner331de232002-07-22 02:07:59 +0000876 for (unsigned i = 0, e = Options.size(); i != e; ++i)
877 MaxArgLen = std::max(MaxArgLen, Options[i].second->getOptionWidth());
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000878
Chris Lattnerca6433f2003-05-22 20:06:43 +0000879 std::cerr << "OPTIONS:\n";
Chris Lattner331de232002-07-22 02:07:59 +0000880 for (unsigned i = 0, e = Options.size(); i != e; ++i)
881 Options[i].second->printOptionInfo(MaxArgLen);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000882
Chris Lattner331de232002-07-22 02:07:59 +0000883 // Halt the program if help information is printed
884 exit(1);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000885 }
886};
887
Chris Lattner331de232002-07-22 02:07:59 +0000888
889
890// Define the two HelpPrinter instances that are used to print out help, or
891// help-hidden...
892//
893HelpPrinter NormalPrinter(false);
894HelpPrinter HiddenPrinter(true);
895
896cl::opt<HelpPrinter, true, parser<bool> >
897HOp("help", cl::desc("display available options (--help-hidden for more)"),
Chris Lattner9b14eb52002-08-07 18:36:37 +0000898 cl::location(NormalPrinter), cl::ValueDisallowed);
Chris Lattner331de232002-07-22 02:07:59 +0000899
900cl::opt<HelpPrinter, true, parser<bool> >
901HHOp("help-hidden", cl::desc("display all available options"),
Chris Lattner9b14eb52002-08-07 18:36:37 +0000902 cl::location(HiddenPrinter), cl::Hidden, cl::ValueDisallowed);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000903
904} // End anonymous namespace