blob: 2337e2e25581b1f8d1e8beb66f86fc6ac3984efe [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 Lattner51140042004-07-03 01:21:05 +000026#include <cstring>
Chris Lattner2cdd21c2003-12-14 21:35:53 +000027using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000028
Chris Lattnerdbab15a2001-07-23 17:17:47 +000029using namespace cl;
30
Chris Lattner331de232002-07-22 02:07:59 +000031//===----------------------------------------------------------------------===//
32// Basic, shared command line option processing machinery...
33//
34
Chris Lattnerdbab15a2001-07-23 17:17:47 +000035// Return the global command line option vector. Making it a function scoped
Chris Lattnerf78032f2001-11-26 18:58:34 +000036// static ensures that it will be initialized correctly before its first use.
Chris Lattnerdbab15a2001-07-23 17:17:47 +000037//
Chris Lattnerca6433f2003-05-22 20:06:43 +000038static std::map<std::string, Option*> *CommandLineOptions = 0;
39static std::map<std::string, Option*> &getOpts() {
40 if (CommandLineOptions == 0)
41 CommandLineOptions = new std::map<std::string,Option*>();
Chris Lattnere8e258b2002-07-29 20:58:42 +000042 return *CommandLineOptions;
43}
44
Chris Lattnerca6433f2003-05-22 20:06:43 +000045static Option *getOption(const std::string &Str) {
Chris Lattnere8e258b2002-07-29 20:58:42 +000046 if (CommandLineOptions == 0) return 0;
Chris Lattnerca6433f2003-05-22 20:06:43 +000047 std::map<std::string,Option*>::iterator I = CommandLineOptions->find(Str);
Chris Lattnere8e258b2002-07-29 20:58:42 +000048 return I != CommandLineOptions->end() ? I->second : 0;
Chris Lattnerdbab15a2001-07-23 17:17:47 +000049}
50
Chris Lattnerca6433f2003-05-22 20:06:43 +000051static std::vector<Option*> &getPositionalOpts() {
Alkis Evlogimenos5f65add2004-03-04 17:50:44 +000052 static std::vector<Option*> *Positional = 0;
53 if (!Positional) Positional = new std::vector<Option*>();
54 return *Positional;
Chris Lattner331de232002-07-22 02:07:59 +000055}
56
Chris Lattnere8e258b2002-07-29 20:58:42 +000057static void AddArgument(const char *ArgName, Option *Opt) {
58 if (getOption(ArgName)) {
Chris Lattnerca6433f2003-05-22 20:06:43 +000059 std::cerr << "CommandLine Error: Argument '" << ArgName
60 << "' defined more than once!\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +000061 } else {
Chris Lattnerf78032f2001-11-26 18:58:34 +000062 // Add argument to the argument map!
Chris Lattnere8e258b2002-07-29 20:58:42 +000063 getOpts()[ArgName] = Opt;
64 }
65}
66
67// RemoveArgument - It's possible that the argument is no longer in the map if
68// options have already been processed and the map has been deleted!
69//
70static void RemoveArgument(const char *ArgName, Option *Opt) {
71 if (CommandLineOptions == 0) return;
72 assert(getOption(ArgName) == Opt && "Arg not in map!");
73 CommandLineOptions->erase(ArgName);
74 if (CommandLineOptions->empty()) {
75 delete CommandLineOptions;
76 CommandLineOptions = 0;
Chris Lattnerdbab15a2001-07-23 17:17:47 +000077 }
78}
79
80static const char *ProgramName = 0;
81static const char *ProgramOverview = 0;
82
Chris Lattnercaccd762001-10-27 05:54:17 +000083static inline bool ProvideOption(Option *Handler, const char *ArgName,
84 const char *Value, int argc, char **argv,
85 int &i) {
86 // Enforce value requirements
87 switch (Handler->getValueExpectedFlag()) {
88 case ValueRequired:
89 if (Value == 0 || *Value == 0) { // No value specified?
90 if (i+1 < argc) { // Steal the next argument, like for '-o filename'
91 Value = argv[++i];
92 } else {
93 return Handler->error(" requires a value!");
94 }
95 }
96 break;
97 case ValueDisallowed:
98 if (*Value != 0)
99 return Handler->error(" does not allow a value! '" +
Chris Lattnerca6433f2003-05-22 20:06:43 +0000100 std::string(Value) + "' specified.");
Chris Lattnercaccd762001-10-27 05:54:17 +0000101 break;
102 case ValueOptional: break;
Chris Lattnerca6433f2003-05-22 20:06:43 +0000103 default: std::cerr << "Bad ValueMask flag! CommandLine usage error:"
104 << Handler->getValueExpectedFlag() << "\n"; abort();
Chris Lattnercaccd762001-10-27 05:54:17 +0000105 }
106
107 // Run the handler now!
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000108 return Handler->addOccurrence(ArgName, Value);
Chris Lattnercaccd762001-10-27 05:54:17 +0000109}
110
Chris Lattner9cf3d472003-07-30 17:34:02 +0000111static bool ProvidePositionalOption(Option *Handler, const std::string &Arg) {
Chris Lattner331de232002-07-22 02:07:59 +0000112 int Dummy;
Chris Lattner9cf3d472003-07-30 17:34:02 +0000113 return ProvideOption(Handler, Handler->ArgStr, Arg.c_str(), 0, 0, Dummy);
Chris Lattner331de232002-07-22 02:07:59 +0000114}
Chris Lattnerf78032f2001-11-26 18:58:34 +0000115
Chris Lattner331de232002-07-22 02:07:59 +0000116
117// Option predicates...
118static inline bool isGrouping(const Option *O) {
119 return O->getFormattingFlag() == cl::Grouping;
120}
121static inline bool isPrefixedOrGrouping(const Option *O) {
122 return isGrouping(O) || O->getFormattingFlag() == cl::Prefix;
123}
124
125// getOptionPred - Check to see if there are any options that satisfy the
126// specified predicate with names that are the prefixes in Name. This is
127// checked by progressively stripping characters off of the name, checking to
128// see if there options that satisfy the predicate. If we find one, return it,
129// otherwise return null.
130//
131static Option *getOptionPred(std::string Name, unsigned &Length,
132 bool (*Pred)(const Option*)) {
133
Chris Lattnere8e258b2002-07-29 20:58:42 +0000134 Option *Op = getOption(Name);
135 if (Op && Pred(Op)) {
Chris Lattner331de232002-07-22 02:07:59 +0000136 Length = Name.length();
Chris Lattnere8e258b2002-07-29 20:58:42 +0000137 return Op;
Chris Lattnerf78032f2001-11-26 18:58:34 +0000138 }
139
Chris Lattner331de232002-07-22 02:07:59 +0000140 if (Name.size() == 1) return 0;
141 do {
142 Name.erase(Name.end()-1, Name.end()); // Chop off the last character...
Chris Lattnere8e258b2002-07-29 20:58:42 +0000143 Op = getOption(Name);
Chris Lattner331de232002-07-22 02:07:59 +0000144
145 // Loop while we haven't found an option and Name still has at least two
146 // characters in it (so that the next iteration will not be the empty
147 // string...
Chris Lattnere8e258b2002-07-29 20:58:42 +0000148 } while ((Op == 0 || !Pred(Op)) && Name.size() > 1);
Chris Lattner331de232002-07-22 02:07:59 +0000149
Chris Lattnere8e258b2002-07-29 20:58:42 +0000150 if (Op && Pred(Op)) {
Chris Lattner331de232002-07-22 02:07:59 +0000151 Length = Name.length();
Chris Lattnere8e258b2002-07-29 20:58:42 +0000152 return Op; // Found one!
Chris Lattner331de232002-07-22 02:07:59 +0000153 }
154 return 0; // No option found!
155}
156
157static bool RequiresValue(const Option *O) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000158 return O->getNumOccurrencesFlag() == cl::Required ||
159 O->getNumOccurrencesFlag() == cl::OneOrMore;
Chris Lattner331de232002-07-22 02:07:59 +0000160}
161
162static bool EatsUnboundedNumberOfValues(const Option *O) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000163 return O->getNumOccurrencesFlag() == cl::ZeroOrMore ||
164 O->getNumOccurrencesFlag() == cl::OneOrMore;
Chris Lattnerf78032f2001-11-26 18:58:34 +0000165}
Chris Lattnercaccd762001-10-27 05:54:17 +0000166
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000167/// ParseCStringVector - Break INPUT up wherever one or more
168/// whitespace characters are found, and store the resulting tokens in
169/// OUTPUT. The tokens stored in OUTPUT are dynamically allocated
170/// using strdup (), so it is the caller's responsibility to free ()
171/// them later.
Brian Gaeke06b06c52003-08-14 22:00:59 +0000172///
173static void ParseCStringVector (std::vector<char *> &output,
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000174 const char *input) {
175 // Characters which will be treated as token separators:
176 static const char *delims = " \v\f\t\r\n";
177
178 std::string work (input);
179 // Skip past any delims at head of input string.
180 size_t pos = work.find_first_not_of (delims);
181 // If the string consists entirely of delims, then exit early.
182 if (pos == std::string::npos) return;
183 // Otherwise, jump forward to beginning of first word.
184 work = work.substr (pos);
185 // Find position of first delimiter.
186 pos = work.find_first_of (delims);
187
188 while (!work.empty() && pos != std::string::npos) {
189 // Everything from 0 to POS is the next word to copy.
190 output.push_back (strdup (work.substr (0,pos).c_str ()));
191 // Is there another word in the string?
192 size_t nextpos = work.find_first_not_of (delims, pos + 1);
193 if (nextpos != std::string::npos) {
194 // Yes? Then remove delims from beginning ...
195 work = work.substr (work.find_first_not_of (delims, pos + 1));
196 // and find the end of the word.
197 pos = work.find_first_of (delims);
198 } else {
199 // No? (Remainder of string is delims.) End the loop.
200 work = "";
201 pos = std::string::npos;
202 }
203 }
204
205 // If `input' ended with non-delim char, then we'll get here with
206 // the last word of `input' in `work'; copy it now.
207 if (!work.empty ()) {
208 output.push_back (strdup (work.c_str ()));
Brian Gaeke06b06c52003-08-14 22:00:59 +0000209 }
210}
211
212/// ParseEnvironmentOptions - An alternative entry point to the
213/// CommandLine library, which allows you to read the program's name
214/// from the caller (as PROGNAME) and its command-line arguments from
215/// an environment variable (whose name is given in ENVVAR).
216///
Chris Lattnerbf455c22004-05-06 22:04:31 +0000217void cl::ParseEnvironmentOptions(const char *progName, const char *envVar,
218 const char *Overview) {
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000219 // Check args.
Chris Lattnerbf455c22004-05-06 22:04:31 +0000220 assert(progName && "Program name not specified");
221 assert(envVar && "Environment variable name missing");
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000222
223 // Get the environment variable they want us to parse options out of.
224 const char *envValue = getenv (envVar);
225 if (!envValue)
226 return;
227
Brian Gaeke06b06c52003-08-14 22:00:59 +0000228 // Get program's "name", which we wouldn't know without the caller
229 // telling us.
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000230 std::vector<char *> newArgv;
231 newArgv.push_back (strdup (progName));
Brian Gaeke06b06c52003-08-14 22:00:59 +0000232
233 // Parse the value of the environment variable into a "command line"
234 // and hand it off to ParseCommandLineOptions().
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000235 ParseCStringVector (newArgv, envValue);
236 int newArgc = newArgv.size ();
237 ParseCommandLineOptions (newArgc, &newArgv[0], Overview);
238
239 // Free all the strdup()ed strings.
240 for (std::vector<char *>::iterator i = newArgv.begin (), e = newArgv.end ();
241 i != e; ++i) {
242 free (*i);
243 }
Brian Gaeke06b06c52003-08-14 22:00:59 +0000244}
245
Chris Lattnerbf455c22004-05-06 22:04:31 +0000246/// LookupOption - Lookup the option specified by the specified option on the
247/// command line. If there is a value specified (after an equal sign) return
248/// that as well.
249static Option *LookupOption(const char *&Arg, const char *&Value) {
250 while (*Arg == '-') ++Arg; // Eat leading dashes
251
252 const char *ArgEnd = Arg;
253 while (*ArgEnd && *ArgEnd != '=')
254 ++ArgEnd; // Scan till end of argument name...
255
256 Value = ArgEnd;
257 if (*Value) // If we have an equals sign...
258 ++Value; // Advance to value...
259
260 if (*Arg == 0) return 0;
261
262 // Look up the option.
263 std::map<std::string, Option*> &Opts = getOpts();
264 std::map<std::string, Option*>::iterator I =
265 Opts.find(std::string(Arg, ArgEnd));
266 return (I != Opts.end()) ? I->second : 0;
267}
268
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000269void cl::ParseCommandLineOptions(int &argc, char **argv,
Chris Lattner0c0edf82002-07-25 06:17:51 +0000270 const char *Overview) {
Chris Lattner331de232002-07-22 02:07:59 +0000271 assert((!getOpts().empty() || !getPositionalOpts().empty()) &&
272 "No options specified, or ParseCommandLineOptions called more"
273 " than once!");
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000274 ProgramName = argv[0]; // Save this away safe and snug
275 ProgramOverview = Overview;
276 bool ErrorParsing = false;
277
Chris Lattnerca6433f2003-05-22 20:06:43 +0000278 std::map<std::string, Option*> &Opts = getOpts();
279 std::vector<Option*> &PositionalOpts = getPositionalOpts();
Chris Lattner331de232002-07-22 02:07:59 +0000280
281 // Check out the positional arguments to collect information about them.
282 unsigned NumPositionalRequired = 0;
283 Option *ConsumeAfterOpt = 0;
284 if (!PositionalOpts.empty()) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000285 if (PositionalOpts[0]->getNumOccurrencesFlag() == cl::ConsumeAfter) {
Chris Lattner331de232002-07-22 02:07:59 +0000286 assert(PositionalOpts.size() > 1 &&
287 "Cannot specify cl::ConsumeAfter without a positional argument!");
288 ConsumeAfterOpt = PositionalOpts[0];
289 }
290
291 // Calculate how many positional values are _required_.
292 bool UnboundedFound = false;
293 for (unsigned i = ConsumeAfterOpt != 0, e = PositionalOpts.size();
294 i != e; ++i) {
295 Option *Opt = PositionalOpts[i];
296 if (RequiresValue(Opt))
297 ++NumPositionalRequired;
298 else if (ConsumeAfterOpt) {
299 // ConsumeAfter cannot be combined with "optional" positional options
Chris Lattner54ec7ae2002-07-22 02:21:57 +0000300 // unless there is only one positional argument...
301 if (PositionalOpts.size() > 2)
302 ErrorParsing |=
303 Opt->error(" error - this positional option will never be matched, "
304 "because it does not Require a value, and a "
305 "cl::ConsumeAfter option is active!");
Chris Lattner9cf3d472003-07-30 17:34:02 +0000306 } else if (UnboundedFound && !Opt->ArgStr[0]) {
307 // This option does not "require" a value... Make sure this option is
308 // not specified after an option that eats all extra arguments, or this
309 // one will never get any!
Chris Lattner331de232002-07-22 02:07:59 +0000310 //
311 ErrorParsing |= Opt->error(" error - option can never match, because "
312 "another positional argument will match an "
313 "unbounded number of values, and this option"
314 " does not require a value!");
315 }
316 UnboundedFound |= EatsUnboundedNumberOfValues(Opt);
317 }
318 }
319
320 // PositionalVals - A vector of "positional" arguments we accumulate into to
321 // processes at the end...
322 //
Chris Lattnerca6433f2003-05-22 20:06:43 +0000323 std::vector<std::string> PositionalVals;
Chris Lattner331de232002-07-22 02:07:59 +0000324
Chris Lattner9cf3d472003-07-30 17:34:02 +0000325 // If the program has named positional arguments, and the name has been run
326 // across, keep track of which positional argument was named. Otherwise put
327 // the positional args into the PositionalVals list...
328 Option *ActivePositionalArg = 0;
329
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000330 // Loop over all of the arguments... processing them.
Chris Lattner331de232002-07-22 02:07:59 +0000331 bool DashDashFound = false; // Have we read '--'?
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000332 for (int i = 1; i < argc; ++i) {
333 Option *Handler = 0;
334 const char *Value = "";
335 const char *ArgName = "";
Chris Lattner331de232002-07-22 02:07:59 +0000336
337 // Check to see if this is a positional argument. This argument is
338 // considered to be positional if it doesn't start with '-', if it is "-"
Misha Brukman1115e042003-07-10 21:38:28 +0000339 // itself, or if we have seen "--" already.
Chris Lattner331de232002-07-22 02:07:59 +0000340 //
341 if (argv[i][0] != '-' || argv[i][1] == 0 || DashDashFound) {
342 // Positional argument!
Chris Lattner9cf3d472003-07-30 17:34:02 +0000343 if (ActivePositionalArg) {
344 ProvidePositionalOption(ActivePositionalArg, argv[i]);
345 continue; // We are done!
346 } else if (!PositionalOpts.empty()) {
Chris Lattner331de232002-07-22 02:07:59 +0000347 PositionalVals.push_back(argv[i]);
348
349 // All of the positional arguments have been fulfulled, give the rest to
350 // the consume after option... if it's specified...
351 //
Chris Lattnerd16714b2002-07-31 16:29:43 +0000352 if (PositionalVals.size() >= NumPositionalRequired &&
Chris Lattner331de232002-07-22 02:07:59 +0000353 ConsumeAfterOpt != 0) {
354 for (++i; i < argc; ++i)
355 PositionalVals.push_back(argv[i]);
356 break; // Handle outside of the argument processing loop...
357 }
358
359 // Delay processing positional arguments until the end...
360 continue;
361 }
Chris Lattnerbf455c22004-05-06 22:04:31 +0000362 } else if (argv[i][0] == '-' && argv[i][1] == '-' && argv[i][2] == 0 &&
363 !DashDashFound) {
364 DashDashFound = true; // This is the mythical "--"?
365 continue; // Don't try to process it as an argument itself.
366 } else if (ActivePositionalArg &&
367 (ActivePositionalArg->getMiscFlags() & PositionalEatsArgs)) {
368 // If there is a positional argument eating options, check to see if this
369 // option is another positional argument. If so, treat it as an argument,
370 // otherwise feed it to the eating positional.
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000371 ArgName = argv[i]+1;
Chris Lattnerbf455c22004-05-06 22:04:31 +0000372 Handler = LookupOption(ArgName, Value);
373 if (!Handler || Handler->getFormattingFlag() != cl::Positional) {
374 ProvidePositionalOption(ActivePositionalArg, argv[i]);
375 continue; // We are done!
Chris Lattner331de232002-07-22 02:07:59 +0000376 }
377
Chris Lattnerbf455c22004-05-06 22:04:31 +0000378 } else { // We start with a '-', must be an argument...
379 ArgName = argv[i]+1;
380 Handler = LookupOption(ArgName, Value);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000381
Chris Lattnerbf455c22004-05-06 22:04:31 +0000382 // Check to see if this "option" is really a prefixed or grouped argument.
383 if (Handler == 0 && *Value == 0) {
384 std::string RealName(ArgName);
385 if (RealName.size() > 1) {
Chris Lattner331de232002-07-22 02:07:59 +0000386 unsigned Length = 0;
387 Option *PGOpt = getOptionPred(RealName, Length, isPrefixedOrGrouping);
Chris Lattnerbf455c22004-05-06 22:04:31 +0000388
Chris Lattner331de232002-07-22 02:07:59 +0000389 // If the option is a prefixed option, then the value is simply the
390 // rest of the name... so fall through to later processing, by
391 // setting up the argument name flags and value fields.
392 //
393 if (PGOpt && PGOpt->getFormattingFlag() == cl::Prefix) {
Chris Lattnerbf455c22004-05-06 22:04:31 +0000394 Value = ArgName+Length;
395 assert(Opts.find(std::string(ArgName, Value)) != Opts.end() &&
396 Opts.find(std::string(ArgName, Value))->second == PGOpt);
397 Handler = PGOpt;
Chris Lattner331de232002-07-22 02:07:59 +0000398 } else if (PGOpt) {
Chris Lattnerbf455c22004-05-06 22:04:31 +0000399 // This must be a grouped option... handle them now.
Chris Lattner331de232002-07-22 02:07:59 +0000400 assert(isGrouping(PGOpt) && "Broken getOptionPred!");
Chris Lattnerbf455c22004-05-06 22:04:31 +0000401
Chris Lattner331de232002-07-22 02:07:59 +0000402 do {
403 // Move current arg name out of RealName into RealArgName...
Chris Lattnerbf455c22004-05-06 22:04:31 +0000404 std::string RealArgName(RealName.begin(),
405 RealName.begin() + Length);
406 RealName.erase(RealName.begin(), RealName.begin() + Length);
407
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000408 // Because ValueRequired is an invalid flag for grouped arguments,
409 // we don't need to pass argc/argv in...
410 //
Chris Lattner331de232002-07-22 02:07:59 +0000411 assert(PGOpt->getValueExpectedFlag() != cl::ValueRequired &&
412 "Option can not be cl::Grouping AND cl::ValueRequired!");
413 int Dummy;
Chris Lattnerbf455c22004-05-06 22:04:31 +0000414 ErrorParsing |= ProvideOption(PGOpt, RealArgName.c_str(),
415 "", 0, 0, Dummy);
416
Chris Lattner331de232002-07-22 02:07:59 +0000417 // Get the next grouping option...
Chris Lattnerbf455c22004-05-06 22:04:31 +0000418 PGOpt = getOptionPred(RealName, Length, isGrouping);
419 } while (PGOpt && Length != RealName.size());
420
421 Handler = PGOpt; // Ate all of the options.
Chris Lattner331de232002-07-22 02:07:59 +0000422 }
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000423 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000424 }
425 }
426
427 if (Handler == 0) {
Brian Gaekec86e84b2003-09-16 18:00:35 +0000428 std::cerr << "Unknown command line argument '" << argv[i] << "'. Try: '"
Chris Lattnerca6433f2003-05-22 20:06:43 +0000429 << argv[0] << " --help'\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000430 ErrorParsing = true;
431 continue;
432 }
433
Chris Lattner72fb8e52003-05-22 20:26:17 +0000434 // Check to see if this option accepts a comma separated list of values. If
435 // it does, we have to split up the value into multiple values...
436 if (Handler->getMiscFlags() & CommaSeparated) {
437 std::string Val(Value);
438 std::string::size_type Pos = Val.find(',');
439
440 while (Pos != std::string::npos) {
441 // Process the portion before the comma...
442 ErrorParsing |= ProvideOption(Handler, ArgName,
443 std::string(Val.begin(),
444 Val.begin()+Pos).c_str(),
445 argc, argv, i);
446 // Erase the portion before the comma, AND the comma...
447 Val.erase(Val.begin(), Val.begin()+Pos+1);
448 Value += Pos+1; // Increment the original value pointer as well...
449
450 // Check for another comma...
451 Pos = Val.find(',');
452 }
453 }
Chris Lattner9cf3d472003-07-30 17:34:02 +0000454
455 // If this is a named positional argument, just remember that it is the
456 // active one...
457 if (Handler->getFormattingFlag() == cl::Positional)
458 ActivePositionalArg = Handler;
459 else
460 ErrorParsing |= ProvideOption(Handler, ArgName, Value, argc, argv, i);
Chris Lattner331de232002-07-22 02:07:59 +0000461 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000462
Chris Lattner331de232002-07-22 02:07:59 +0000463 // Check and handle positional arguments now...
464 if (NumPositionalRequired > PositionalVals.size()) {
Chris Lattnerca6433f2003-05-22 20:06:43 +0000465 std::cerr << "Not enough positional command line arguments specified!\n"
466 << "Must specify at least " << NumPositionalRequired
467 << " positional arguments: See: " << argv[0] << " --help\n";
Chris Lattner331de232002-07-22 02:07:59 +0000468 ErrorParsing = true;
469
470
471 } else if (ConsumeAfterOpt == 0) {
472 // Positional args have already been handled if ConsumeAfter is specified...
473 unsigned ValNo = 0, NumVals = PositionalVals.size();
474 for (unsigned i = 0, e = PositionalOpts.size(); i != e; ++i) {
475 if (RequiresValue(PositionalOpts[i])) {
476 ProvidePositionalOption(PositionalOpts[i], PositionalVals[ValNo++]);
477 --NumPositionalRequired; // We fulfilled our duty...
478 }
479
480 // If we _can_ give this option more arguments, do so now, as long as we
481 // do not give it values that others need. 'Done' controls whether the
482 // option even _WANTS_ any more.
483 //
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000484 bool Done = PositionalOpts[i]->getNumOccurrencesFlag() == cl::Required;
Chris Lattner331de232002-07-22 02:07:59 +0000485 while (NumVals-ValNo > NumPositionalRequired && !Done) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000486 switch (PositionalOpts[i]->getNumOccurrencesFlag()) {
Chris Lattner331de232002-07-22 02:07:59 +0000487 case cl::Optional:
488 Done = true; // Optional arguments want _at most_ one value
489 // FALL THROUGH
490 case cl::ZeroOrMore: // Zero or more will take all they can get...
491 case cl::OneOrMore: // One or more will take all they can get...
492 ProvidePositionalOption(PositionalOpts[i], PositionalVals[ValNo++]);
493 break;
494 default:
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000495 assert(0 && "Internal error, unexpected NumOccurrences flag in "
Chris Lattner331de232002-07-22 02:07:59 +0000496 "positional argument processing!");
497 }
498 }
Chris Lattnercaccd762001-10-27 05:54:17 +0000499 }
Chris Lattner331de232002-07-22 02:07:59 +0000500 } else {
501 assert(ConsumeAfterOpt && NumPositionalRequired <= PositionalVals.size());
502 unsigned ValNo = 0;
503 for (unsigned j = 1, e = PositionalOpts.size(); j != e; ++j)
504 if (RequiresValue(PositionalOpts[j]))
Chris Lattnerfaba8092002-07-24 20:15:13 +0000505 ErrorParsing |= ProvidePositionalOption(PositionalOpts[j],
506 PositionalVals[ValNo++]);
507
508 // Handle the case where there is just one positional option, and it's
509 // optional. In this case, we want to give JUST THE FIRST option to the
510 // positional option and keep the rest for the consume after. The above
511 // loop would have assigned no values to positional options in this case.
512 //
Chris Lattnerb490c202002-08-02 21:51:29 +0000513 if (PositionalOpts.size() == 2 && ValNo == 0 && !PositionalVals.empty())
Chris Lattnerfaba8092002-07-24 20:15:13 +0000514 ErrorParsing |= ProvidePositionalOption(PositionalOpts[1],
515 PositionalVals[ValNo++]);
Chris Lattner331de232002-07-22 02:07:59 +0000516
517 // Handle over all of the rest of the arguments to the
518 // cl::ConsumeAfter command line option...
519 for (; ValNo != PositionalVals.size(); ++ValNo)
520 ErrorParsing |= ProvidePositionalOption(ConsumeAfterOpt,
521 PositionalVals[ValNo]);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000522 }
523
Chris Lattnerdc4693d2001-07-23 23:02:45 +0000524 // Loop over args and make sure all required args are specified!
Chris Lattnerca6433f2003-05-22 20:06:43 +0000525 for (std::map<std::string, Option*>::iterator I = Opts.begin(),
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000526 E = Opts.end(); I != E; ++I) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000527 switch (I->second->getNumOccurrencesFlag()) {
Chris Lattnerdc4693d2001-07-23 23:02:45 +0000528 case Required:
529 case OneOrMore:
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000530 if (I->second->getNumOccurrences() == 0) {
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000531 I->second->error(" must be specified at least once!");
Chris Lattnerf038acb2001-10-24 06:21:56 +0000532 ErrorParsing = true;
533 }
Chris Lattnerdc4693d2001-07-23 23:02:45 +0000534 // Fall through
535 default:
536 break;
537 }
538 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000539
Chris Lattner331de232002-07-22 02:07:59 +0000540 // Free all of the memory allocated to the map. Command line options may only
541 // be processed once!
Chris Lattnere8e258b2002-07-29 20:58:42 +0000542 delete CommandLineOptions;
543 CommandLineOptions = 0;
Chris Lattner331de232002-07-22 02:07:59 +0000544 PositionalOpts.clear();
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000545
546 // If we had an error processing our arguments, don't let the program execute
547 if (ErrorParsing) exit(1);
548}
549
550//===----------------------------------------------------------------------===//
551// Option Base class implementation
552//
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000553
Chris Lattnerca6433f2003-05-22 20:06:43 +0000554bool Option::error(std::string Message, const char *ArgName) {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000555 if (ArgName == 0) ArgName = ArgStr;
Chris Lattner331de232002-07-22 02:07:59 +0000556 if (ArgName[0] == 0)
Chris Lattnerca6433f2003-05-22 20:06:43 +0000557 std::cerr << HelpStr; // Be nice for positional arguments
Chris Lattner331de232002-07-22 02:07:59 +0000558 else
Chris Lattnerca6433f2003-05-22 20:06:43 +0000559 std::cerr << "-" << ArgName;
560 std::cerr << " option" << Message << "\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000561 return true;
562}
563
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000564bool Option::addOccurrence(const char *ArgName, const std::string &Value) {
565 NumOccurrences++; // Increment the number of times we have been seen
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000566
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000567 switch (getNumOccurrencesFlag()) {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000568 case Optional:
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000569 if (NumOccurrences > 1)
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000570 return error(": may only occur zero or one times!", ArgName);
571 break;
572 case Required:
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000573 if (NumOccurrences > 1)
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000574 return error(": must occur exactly one time!", ArgName);
575 // Fall through
576 case OneOrMore:
Chris Lattnercaccd762001-10-27 05:54:17 +0000577 case ZeroOrMore:
578 case ConsumeAfter: break;
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000579 default: return error(": bad num occurrences flag value!");
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000580 }
581
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000582 return handleOccurrence(ArgName, Value);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000583}
584
Chris Lattner331de232002-07-22 02:07:59 +0000585// addArgument - Tell the system that this Option subclass will handle all
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000586// occurrences of -ArgStr on the command line.
Chris Lattner331de232002-07-22 02:07:59 +0000587//
588void Option::addArgument(const char *ArgStr) {
589 if (ArgStr[0])
590 AddArgument(ArgStr, this);
Chris Lattner9cf3d472003-07-30 17:34:02 +0000591
592 if (getFormattingFlag() == Positional)
Chris Lattner331de232002-07-22 02:07:59 +0000593 getPositionalOpts().push_back(this);
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000594 else if (getNumOccurrencesFlag() == ConsumeAfter) {
Chris Lattner9cf3d472003-07-30 17:34:02 +0000595 if (!getPositionalOpts().empty() &&
596 getPositionalOpts().front()->getNumOccurrencesFlag() == ConsumeAfter)
597 error("Cannot specify more than one option with cl::ConsumeAfter!");
Chris Lattner331de232002-07-22 02:07:59 +0000598 getPositionalOpts().insert(getPositionalOpts().begin(), this);
599 }
600}
601
Chris Lattneraa852bb2002-07-23 17:15:12 +0000602void Option::removeArgument(const char *ArgStr) {
Chris Lattner9cf3d472003-07-30 17:34:02 +0000603 if (ArgStr[0])
Chris Lattnere8e258b2002-07-29 20:58:42 +0000604 RemoveArgument(ArgStr, this);
Chris Lattner9cf3d472003-07-30 17:34:02 +0000605
606 if (getFormattingFlag() == Positional) {
Chris Lattnerca6433f2003-05-22 20:06:43 +0000607 std::vector<Option*>::iterator I =
Chris Lattneraa852bb2002-07-23 17:15:12 +0000608 std::find(getPositionalOpts().begin(), getPositionalOpts().end(), this);
609 assert(I != getPositionalOpts().end() && "Arg not registered!");
610 getPositionalOpts().erase(I);
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000611 } else if (getNumOccurrencesFlag() == ConsumeAfter) {
Chris Lattneraa852bb2002-07-23 17:15:12 +0000612 assert(!getPositionalOpts().empty() && getPositionalOpts()[0] == this &&
613 "Arg not registered correctly!");
614 getPositionalOpts().erase(getPositionalOpts().begin());
615 }
616}
617
Chris Lattner331de232002-07-22 02:07:59 +0000618
619// getValueStr - Get the value description string, using "DefaultMsg" if nothing
620// has been specified yet.
621//
622static const char *getValueStr(const Option &O, const char *DefaultMsg) {
623 if (O.ValueStr[0] == 0) return DefaultMsg;
624 return O.ValueStr;
625}
626
627//===----------------------------------------------------------------------===//
628// cl::alias class implementation
629//
630
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000631// Return the width of the option tag for printing...
Chris Lattner331de232002-07-22 02:07:59 +0000632unsigned alias::getOptionWidth() const {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000633 return std::strlen(ArgStr)+6;
634}
635
Chris Lattner331de232002-07-22 02:07:59 +0000636// Print out the option for the alias...
637void alias::printOptionInfo(unsigned GlobalWidth) const {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000638 unsigned L = std::strlen(ArgStr);
Chris Lattnerca6433f2003-05-22 20:06:43 +0000639 std::cerr << " -" << ArgStr << std::string(GlobalWidth-L-6, ' ') << " - "
640 << HelpStr << "\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000641}
642
643
Chris Lattner331de232002-07-22 02:07:59 +0000644
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000645//===----------------------------------------------------------------------===//
Chris Lattner331de232002-07-22 02:07:59 +0000646// Parser Implementation code...
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000647//
648
Chris Lattner9b14eb52002-08-07 18:36:37 +0000649// basic_parser implementation
650//
651
652// Return the width of the option tag for printing...
653unsigned basic_parser_impl::getOptionWidth(const Option &O) const {
654 unsigned Len = std::strlen(O.ArgStr);
655 if (const char *ValName = getValueName())
656 Len += std::strlen(getValueStr(O, ValName))+3;
657
658 return Len + 6;
659}
660
661// printOptionInfo - Print out information about this option. The
662// to-be-maintained width is specified.
663//
664void basic_parser_impl::printOptionInfo(const Option &O,
665 unsigned GlobalWidth) const {
Chris Lattnerca6433f2003-05-22 20:06:43 +0000666 std::cerr << " -" << O.ArgStr;
Chris Lattner9b14eb52002-08-07 18:36:37 +0000667
668 if (const char *ValName = getValueName())
Chris Lattnerca6433f2003-05-22 20:06:43 +0000669 std::cerr << "=<" << getValueStr(O, ValName) << ">";
Chris Lattner9b14eb52002-08-07 18:36:37 +0000670
Chris Lattnerca6433f2003-05-22 20:06:43 +0000671 std::cerr << std::string(GlobalWidth-getOptionWidth(O), ' ') << " - "
672 << O.HelpStr << "\n";
Chris Lattner9b14eb52002-08-07 18:36:37 +0000673}
674
675
676
677
Chris Lattner331de232002-07-22 02:07:59 +0000678// parser<bool> implementation
679//
Chris Lattner9b14eb52002-08-07 18:36:37 +0000680bool parser<bool>::parse(Option &O, const char *ArgName,
Chris Lattnerca6433f2003-05-22 20:06:43 +0000681 const std::string &Arg, bool &Value) {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000682 if (Arg == "" || Arg == "true" || Arg == "TRUE" || Arg == "True" ||
683 Arg == "1") {
684 Value = true;
685 } else if (Arg == "false" || Arg == "FALSE" || Arg == "False" || Arg == "0") {
686 Value = false;
687 } else {
Chris Lattner331de232002-07-22 02:07:59 +0000688 return O.error(": '" + Arg +
689 "' is invalid value for boolean argument! Try 0 or 1");
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000690 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000691 return false;
692}
693
Chris Lattner331de232002-07-22 02:07:59 +0000694// parser<int> implementation
695//
Chris Lattner9b14eb52002-08-07 18:36:37 +0000696bool parser<int>::parse(Option &O, const char *ArgName,
Chris Lattnerca6433f2003-05-22 20:06:43 +0000697 const std::string &Arg, int &Value) {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000698 char *End;
Chris Lattnerd2a6fc32003-06-28 15:47:20 +0000699 Value = (int)strtol(Arg.c_str(), &End, 0);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000700 if (*End != 0)
Chris Lattner331de232002-07-22 02:07:59 +0000701 return O.error(": '" + Arg + "' value invalid for integer argument!");
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000702 return false;
703}
704
Chris Lattnerd2a6fc32003-06-28 15:47:20 +0000705// parser<unsigned> implementation
706//
707bool parser<unsigned>::parse(Option &O, const char *ArgName,
708 const std::string &Arg, unsigned &Value) {
709 char *End;
Brian Gaeke2d6a2362003-10-10 17:01:36 +0000710 errno = 0;
711 unsigned long V = strtoul(Arg.c_str(), &End, 0);
Chris Lattnerd2a6fc32003-06-28 15:47:20 +0000712 Value = (unsigned)V;
Brian Gaeke2d6a2362003-10-10 17:01:36 +0000713 if (((V == ULONG_MAX) && (errno == ERANGE))
714 || (*End != 0)
715 || (Value != V))
Chris Lattnerd2a6fc32003-06-28 15:47:20 +0000716 return O.error(": '" + Arg + "' value invalid for uint argument!");
717 return false;
718}
719
Chris Lattner9b14eb52002-08-07 18:36:37 +0000720// parser<double>/parser<float> implementation
Chris Lattnerd215fd12001-10-13 06:53:19 +0000721//
Chris Lattnerca6433f2003-05-22 20:06:43 +0000722static bool parseDouble(Option &O, const std::string &Arg, double &Value) {
Chris Lattner331de232002-07-22 02:07:59 +0000723 const char *ArgStart = Arg.c_str();
724 char *End;
725 Value = strtod(ArgStart, &End);
726 if (*End != 0)
727 return O.error(": '" +Arg+ "' value invalid for floating point argument!");
Chris Lattnerd215fd12001-10-13 06:53:19 +0000728 return false;
729}
730
Chris Lattner9b14eb52002-08-07 18:36:37 +0000731bool parser<double>::parse(Option &O, const char *AN,
732 const std::string &Arg, double &Val) {
733 return parseDouble(O, Arg, Val);
Chris Lattner331de232002-07-22 02:07:59 +0000734}
735
Chris Lattner9b14eb52002-08-07 18:36:37 +0000736bool parser<float>::parse(Option &O, const char *AN,
737 const std::string &Arg, float &Val) {
738 double dVal;
739 if (parseDouble(O, Arg, dVal))
740 return true;
741 Val = (float)dVal;
742 return false;
Chris Lattner331de232002-07-22 02:07:59 +0000743}
744
745
Chris Lattner331de232002-07-22 02:07:59 +0000746
747// generic_parser_base implementation
748//
749
Chris Lattneraa852bb2002-07-23 17:15:12 +0000750// findOption - Return the option number corresponding to the specified
751// argument string. If the option is not found, getNumOptions() is returned.
752//
753unsigned generic_parser_base::findOption(const char *Name) {
754 unsigned i = 0, e = getNumOptions();
Chris Lattnerca6433f2003-05-22 20:06:43 +0000755 std::string N(Name);
Chris Lattneraa852bb2002-07-23 17:15:12 +0000756
757 while (i != e)
758 if (getOption(i) == N)
759 return i;
760 else
761 ++i;
762 return e;
763}
764
765
Chris Lattner331de232002-07-22 02:07:59 +0000766// Return the width of the option tag for printing...
767unsigned generic_parser_base::getOptionWidth(const Option &O) const {
768 if (O.hasArgStr()) {
769 unsigned Size = std::strlen(O.ArgStr)+6;
770 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
771 Size = std::max(Size, (unsigned)std::strlen(getOption(i))+8);
772 return Size;
773 } else {
774 unsigned BaseSize = 0;
775 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
776 BaseSize = std::max(BaseSize, (unsigned)std::strlen(getOption(i))+8);
777 return BaseSize;
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000778 }
779}
780
Chris Lattner331de232002-07-22 02:07:59 +0000781// printOptionInfo - Print out information about this option. The
782// to-be-maintained width is specified.
783//
784void generic_parser_base::printOptionInfo(const Option &O,
785 unsigned GlobalWidth) const {
786 if (O.hasArgStr()) {
787 unsigned L = std::strlen(O.ArgStr);
Chris Lattnerca6433f2003-05-22 20:06:43 +0000788 std::cerr << " -" << O.ArgStr << std::string(GlobalWidth-L-6, ' ')
789 << " - " << O.HelpStr << "\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000790
Chris Lattner331de232002-07-22 02:07:59 +0000791 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
792 unsigned NumSpaces = GlobalWidth-strlen(getOption(i))-8;
Chris Lattnerca6433f2003-05-22 20:06:43 +0000793 std::cerr << " =" << getOption(i) << std::string(NumSpaces, ' ')
794 << " - " << getDescription(i) << "\n";
Chris Lattner9c9be482002-01-31 00:42:56 +0000795 }
Chris Lattner331de232002-07-22 02:07:59 +0000796 } else {
797 if (O.HelpStr[0])
Chris Lattnerca6433f2003-05-22 20:06:43 +0000798 std::cerr << " " << O.HelpStr << "\n";
Chris Lattner331de232002-07-22 02:07:59 +0000799 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
800 unsigned L = std::strlen(getOption(i));
Chris Lattnerca6433f2003-05-22 20:06:43 +0000801 std::cerr << " -" << getOption(i) << std::string(GlobalWidth-L-8, ' ')
802 << " - " << getDescription(i) << "\n";
Chris Lattner331de232002-07-22 02:07:59 +0000803 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000804 }
805}
806
807
808//===----------------------------------------------------------------------===//
Chris Lattner331de232002-07-22 02:07:59 +0000809// --help and --help-hidden option implementation
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000810//
811namespace {
812
Chris Lattner331de232002-07-22 02:07:59 +0000813class HelpPrinter {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000814 unsigned MaxArgLen;
815 const Option *EmptyArg;
816 const bool ShowHidden;
817
Chris Lattner331de232002-07-22 02:07:59 +0000818 // isHidden/isReallyHidden - Predicates to be used to filter down arg lists.
Chris Lattnerca6433f2003-05-22 20:06:43 +0000819 inline static bool isHidden(std::pair<std::string, Option *> &OptPair) {
Chris Lattner331de232002-07-22 02:07:59 +0000820 return OptPair.second->getOptionHiddenFlag() >= Hidden;
821 }
Chris Lattnerca6433f2003-05-22 20:06:43 +0000822 inline static bool isReallyHidden(std::pair<std::string, Option *> &OptPair) {
Chris Lattner331de232002-07-22 02:07:59 +0000823 return OptPair.second->getOptionHiddenFlag() == ReallyHidden;
824 }
825
826public:
827 HelpPrinter(bool showHidden) : ShowHidden(showHidden) {
828 EmptyArg = 0;
829 }
830
831 void operator=(bool Value) {
832 if (Value == false) return;
833
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000834 // Copy Options into a vector so we can sort them as we like...
Chris Lattnerca6433f2003-05-22 20:06:43 +0000835 std::vector<std::pair<std::string, Option*> > Options;
Chris Lattner697954c2002-01-20 22:54:45 +0000836 copy(getOpts().begin(), getOpts().end(), std::back_inserter(Options));
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000837
838 // Eliminate Hidden or ReallyHidden arguments, depending on ShowHidden
Chris Lattner331de232002-07-22 02:07:59 +0000839 Options.erase(std::remove_if(Options.begin(), Options.end(),
840 std::ptr_fun(ShowHidden ? isReallyHidden : isHidden)),
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000841 Options.end());
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000842
843 // Eliminate duplicate entries in table (from enum flags options, f.e.)
Chris Lattner331de232002-07-22 02:07:59 +0000844 { // Give OptionSet a scope
845 std::set<Option*> OptionSet;
846 for (unsigned i = 0; i != Options.size(); ++i)
847 if (OptionSet.count(Options[i].second) == 0)
848 OptionSet.insert(Options[i].second); // Add new entry to set
849 else
850 Options.erase(Options.begin()+i--); // Erase duplicate
851 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000852
853 if (ProgramOverview)
Chris Lattnerca6433f2003-05-22 20:06:43 +0000854 std::cerr << "OVERVIEW:" << ProgramOverview << "\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000855
Chris Lattnerca6433f2003-05-22 20:06:43 +0000856 std::cerr << "USAGE: " << ProgramName << " [options]";
Chris Lattner331de232002-07-22 02:07:59 +0000857
858 // Print out the positional options...
Chris Lattnerca6433f2003-05-22 20:06:43 +0000859 std::vector<Option*> &PosOpts = getPositionalOpts();
Chris Lattner331de232002-07-22 02:07:59 +0000860 Option *CAOpt = 0; // The cl::ConsumeAfter option, if it exists...
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000861 if (!PosOpts.empty() && PosOpts[0]->getNumOccurrencesFlag() == ConsumeAfter)
Chris Lattner331de232002-07-22 02:07:59 +0000862 CAOpt = PosOpts[0];
863
Chris Lattner9cf3d472003-07-30 17:34:02 +0000864 for (unsigned i = CAOpt != 0, e = PosOpts.size(); i != e; ++i) {
865 if (PosOpts[i]->ArgStr[0])
866 std::cerr << " --" << PosOpts[i]->ArgStr;
Chris Lattnerca6433f2003-05-22 20:06:43 +0000867 std::cerr << " " << PosOpts[i]->HelpStr;
Chris Lattner9cf3d472003-07-30 17:34:02 +0000868 }
Chris Lattner331de232002-07-22 02:07:59 +0000869
870 // Print the consume after option info if it exists...
Chris Lattnerca6433f2003-05-22 20:06:43 +0000871 if (CAOpt) std::cerr << " " << CAOpt->HelpStr;
Chris Lattner331de232002-07-22 02:07:59 +0000872
Chris Lattnerca6433f2003-05-22 20:06:43 +0000873 std::cerr << "\n\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000874
875 // Compute the maximum argument length...
876 MaxArgLen = 0;
Chris Lattner331de232002-07-22 02:07:59 +0000877 for (unsigned i = 0, e = Options.size(); i != e; ++i)
878 MaxArgLen = std::max(MaxArgLen, Options[i].second->getOptionWidth());
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000879
Chris Lattnerca6433f2003-05-22 20:06:43 +0000880 std::cerr << "OPTIONS:\n";
Chris Lattner331de232002-07-22 02:07:59 +0000881 for (unsigned i = 0, e = Options.size(); i != e; ++i)
882 Options[i].second->printOptionInfo(MaxArgLen);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000883
Chris Lattner331de232002-07-22 02:07:59 +0000884 // Halt the program if help information is printed
885 exit(1);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000886 }
887};
888
Chris Lattner331de232002-07-22 02:07:59 +0000889
890
891// Define the two HelpPrinter instances that are used to print out help, or
892// help-hidden...
893//
894HelpPrinter NormalPrinter(false);
895HelpPrinter HiddenPrinter(true);
896
897cl::opt<HelpPrinter, true, parser<bool> >
898HOp("help", cl::desc("display available options (--help-hidden for more)"),
Chris Lattner9b14eb52002-08-07 18:36:37 +0000899 cl::location(NormalPrinter), cl::ValueDisallowed);
Chris Lattner331de232002-07-22 02:07:59 +0000900
901cl::opt<HelpPrinter, true, parser<bool> >
902HHOp("help-hidden", cl::desc("display all available options"),
Chris Lattner9b14eb52002-08-07 18:36:37 +0000903 cl::location(HiddenPrinter), cl::Hidden, cl::ValueDisallowed);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000904
905} // End anonymous namespace