blob: 0e0cad96eaa80f43ca9d25b7a84e9d6881307d06 [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;
Chris Lattnerf98cfc72004-07-18 21:56:20 +000072#ifndef NDEBUG
73 // This disgusting HACK is brought to you courtesy of GCC 3.3.2, which ICE's
74 // If we pass ArgName directly into getOption here.
75 std::string Tmp = ArgName;
76 assert(getOption(Tmp) == Opt && "Arg not in map!");
77#endif
Chris Lattnere8e258b2002-07-29 20:58:42 +000078 CommandLineOptions->erase(ArgName);
79 if (CommandLineOptions->empty()) {
80 delete CommandLineOptions;
81 CommandLineOptions = 0;
Chris Lattnerdbab15a2001-07-23 17:17:47 +000082 }
83}
84
85static const char *ProgramName = 0;
86static const char *ProgramOverview = 0;
87
Chris Lattnercaccd762001-10-27 05:54:17 +000088static inline bool ProvideOption(Option *Handler, const char *ArgName,
89 const char *Value, int argc, char **argv,
90 int &i) {
91 // Enforce value requirements
92 switch (Handler->getValueExpectedFlag()) {
93 case ValueRequired:
94 if (Value == 0 || *Value == 0) { // No value specified?
95 if (i+1 < argc) { // Steal the next argument, like for '-o filename'
96 Value = argv[++i];
97 } else {
98 return Handler->error(" requires a value!");
99 }
100 }
101 break;
102 case ValueDisallowed:
103 if (*Value != 0)
104 return Handler->error(" does not allow a value! '" +
Chris Lattnerca6433f2003-05-22 20:06:43 +0000105 std::string(Value) + "' specified.");
Chris Lattnercaccd762001-10-27 05:54:17 +0000106 break;
107 case ValueOptional: break;
Chris Lattnerca6433f2003-05-22 20:06:43 +0000108 default: std::cerr << "Bad ValueMask flag! CommandLine usage error:"
109 << Handler->getValueExpectedFlag() << "\n"; abort();
Chris Lattnercaccd762001-10-27 05:54:17 +0000110 }
111
112 // Run the handler now!
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000113 return Handler->addOccurrence(ArgName, Value);
Chris Lattnercaccd762001-10-27 05:54:17 +0000114}
115
Chris Lattner9cf3d472003-07-30 17:34:02 +0000116static bool ProvidePositionalOption(Option *Handler, const std::string &Arg) {
Chris Lattner331de232002-07-22 02:07:59 +0000117 int Dummy;
Chris Lattner9cf3d472003-07-30 17:34:02 +0000118 return ProvideOption(Handler, Handler->ArgStr, Arg.c_str(), 0, 0, Dummy);
Chris Lattner331de232002-07-22 02:07:59 +0000119}
Chris Lattnerf78032f2001-11-26 18:58:34 +0000120
Chris Lattner331de232002-07-22 02:07:59 +0000121
122// Option predicates...
123static inline bool isGrouping(const Option *O) {
124 return O->getFormattingFlag() == cl::Grouping;
125}
126static inline bool isPrefixedOrGrouping(const Option *O) {
127 return isGrouping(O) || O->getFormattingFlag() == cl::Prefix;
128}
129
130// getOptionPred - Check to see if there are any options that satisfy the
131// specified predicate with names that are the prefixes in Name. This is
132// checked by progressively stripping characters off of the name, checking to
133// see if there options that satisfy the predicate. If we find one, return it,
134// otherwise return null.
135//
136static Option *getOptionPred(std::string Name, unsigned &Length,
137 bool (*Pred)(const Option*)) {
138
Chris Lattnere8e258b2002-07-29 20:58:42 +0000139 Option *Op = getOption(Name);
140 if (Op && Pred(Op)) {
Chris Lattner331de232002-07-22 02:07:59 +0000141 Length = Name.length();
Chris Lattnere8e258b2002-07-29 20:58:42 +0000142 return Op;
Chris Lattnerf78032f2001-11-26 18:58:34 +0000143 }
144
Chris Lattner331de232002-07-22 02:07:59 +0000145 if (Name.size() == 1) return 0;
146 do {
147 Name.erase(Name.end()-1, Name.end()); // Chop off the last character...
Chris Lattnere8e258b2002-07-29 20:58:42 +0000148 Op = getOption(Name);
Chris Lattner331de232002-07-22 02:07:59 +0000149
150 // Loop while we haven't found an option and Name still has at least two
151 // characters in it (so that the next iteration will not be the empty
152 // string...
Chris Lattnere8e258b2002-07-29 20:58:42 +0000153 } while ((Op == 0 || !Pred(Op)) && Name.size() > 1);
Chris Lattner331de232002-07-22 02:07:59 +0000154
Chris Lattnere8e258b2002-07-29 20:58:42 +0000155 if (Op && Pred(Op)) {
Chris Lattner331de232002-07-22 02:07:59 +0000156 Length = Name.length();
Chris Lattnere8e258b2002-07-29 20:58:42 +0000157 return Op; // Found one!
Chris Lattner331de232002-07-22 02:07:59 +0000158 }
159 return 0; // No option found!
160}
161
162static bool RequiresValue(const Option *O) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000163 return O->getNumOccurrencesFlag() == cl::Required ||
164 O->getNumOccurrencesFlag() == cl::OneOrMore;
Chris Lattner331de232002-07-22 02:07:59 +0000165}
166
167static bool EatsUnboundedNumberOfValues(const Option *O) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000168 return O->getNumOccurrencesFlag() == cl::ZeroOrMore ||
169 O->getNumOccurrencesFlag() == cl::OneOrMore;
Chris Lattnerf78032f2001-11-26 18:58:34 +0000170}
Chris Lattnercaccd762001-10-27 05:54:17 +0000171
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000172/// ParseCStringVector - Break INPUT up wherever one or more
173/// whitespace characters are found, and store the resulting tokens in
174/// OUTPUT. The tokens stored in OUTPUT are dynamically allocated
175/// using strdup (), so it is the caller's responsibility to free ()
176/// them later.
Brian Gaeke06b06c52003-08-14 22:00:59 +0000177///
178static void ParseCStringVector (std::vector<char *> &output,
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000179 const char *input) {
180 // Characters which will be treated as token separators:
181 static const char *delims = " \v\f\t\r\n";
182
183 std::string work (input);
184 // Skip past any delims at head of input string.
185 size_t pos = work.find_first_not_of (delims);
186 // If the string consists entirely of delims, then exit early.
187 if (pos == std::string::npos) return;
188 // Otherwise, jump forward to beginning of first word.
189 work = work.substr (pos);
190 // Find position of first delimiter.
191 pos = work.find_first_of (delims);
192
193 while (!work.empty() && pos != std::string::npos) {
194 // Everything from 0 to POS is the next word to copy.
195 output.push_back (strdup (work.substr (0,pos).c_str ()));
196 // Is there another word in the string?
197 size_t nextpos = work.find_first_not_of (delims, pos + 1);
198 if (nextpos != std::string::npos) {
199 // Yes? Then remove delims from beginning ...
200 work = work.substr (work.find_first_not_of (delims, pos + 1));
201 // and find the end of the word.
202 pos = work.find_first_of (delims);
203 } else {
204 // No? (Remainder of string is delims.) End the loop.
205 work = "";
206 pos = std::string::npos;
207 }
208 }
209
210 // If `input' ended with non-delim char, then we'll get here with
211 // the last word of `input' in `work'; copy it now.
212 if (!work.empty ()) {
213 output.push_back (strdup (work.c_str ()));
Brian Gaeke06b06c52003-08-14 22:00:59 +0000214 }
215}
216
217/// ParseEnvironmentOptions - An alternative entry point to the
218/// CommandLine library, which allows you to read the program's name
219/// from the caller (as PROGNAME) and its command-line arguments from
220/// an environment variable (whose name is given in ENVVAR).
221///
Chris Lattnerbf455c22004-05-06 22:04:31 +0000222void cl::ParseEnvironmentOptions(const char *progName, const char *envVar,
223 const char *Overview) {
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000224 // Check args.
Chris Lattnerbf455c22004-05-06 22:04:31 +0000225 assert(progName && "Program name not specified");
226 assert(envVar && "Environment variable name missing");
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000227
228 // Get the environment variable they want us to parse options out of.
229 const char *envValue = getenv (envVar);
230 if (!envValue)
231 return;
232
Brian Gaeke06b06c52003-08-14 22:00:59 +0000233 // Get program's "name", which we wouldn't know without the caller
234 // telling us.
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000235 std::vector<char *> newArgv;
236 newArgv.push_back (strdup (progName));
Brian Gaeke06b06c52003-08-14 22:00:59 +0000237
238 // Parse the value of the environment variable into a "command line"
239 // and hand it off to ParseCommandLineOptions().
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000240 ParseCStringVector (newArgv, envValue);
241 int newArgc = newArgv.size ();
242 ParseCommandLineOptions (newArgc, &newArgv[0], Overview);
243
244 // Free all the strdup()ed strings.
245 for (std::vector<char *>::iterator i = newArgv.begin (), e = newArgv.end ();
246 i != e; ++i) {
247 free (*i);
248 }
Brian Gaeke06b06c52003-08-14 22:00:59 +0000249}
250
Chris Lattnerbf455c22004-05-06 22:04:31 +0000251/// LookupOption - Lookup the option specified by the specified option on the
252/// command line. If there is a value specified (after an equal sign) return
253/// that as well.
254static Option *LookupOption(const char *&Arg, const char *&Value) {
255 while (*Arg == '-') ++Arg; // Eat leading dashes
256
257 const char *ArgEnd = Arg;
258 while (*ArgEnd && *ArgEnd != '=')
259 ++ArgEnd; // Scan till end of argument name...
260
261 Value = ArgEnd;
262 if (*Value) // If we have an equals sign...
263 ++Value; // Advance to value...
264
265 if (*Arg == 0) return 0;
266
267 // Look up the option.
268 std::map<std::string, Option*> &Opts = getOpts();
269 std::map<std::string, Option*>::iterator I =
270 Opts.find(std::string(Arg, ArgEnd));
271 return (I != Opts.end()) ? I->second : 0;
272}
273
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000274void cl::ParseCommandLineOptions(int &argc, char **argv,
Chris Lattner0c0edf82002-07-25 06:17:51 +0000275 const char *Overview) {
Chris Lattner331de232002-07-22 02:07:59 +0000276 assert((!getOpts().empty() || !getPositionalOpts().empty()) &&
277 "No options specified, or ParseCommandLineOptions called more"
278 " than once!");
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000279 ProgramName = argv[0]; // Save this away safe and snug
280 ProgramOverview = Overview;
281 bool ErrorParsing = false;
282
Chris Lattnerca6433f2003-05-22 20:06:43 +0000283 std::map<std::string, Option*> &Opts = getOpts();
284 std::vector<Option*> &PositionalOpts = getPositionalOpts();
Chris Lattner331de232002-07-22 02:07:59 +0000285
286 // Check out the positional arguments to collect information about them.
287 unsigned NumPositionalRequired = 0;
288 Option *ConsumeAfterOpt = 0;
289 if (!PositionalOpts.empty()) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000290 if (PositionalOpts[0]->getNumOccurrencesFlag() == cl::ConsumeAfter) {
Chris Lattner331de232002-07-22 02:07:59 +0000291 assert(PositionalOpts.size() > 1 &&
292 "Cannot specify cl::ConsumeAfter without a positional argument!");
293 ConsumeAfterOpt = PositionalOpts[0];
294 }
295
296 // Calculate how many positional values are _required_.
297 bool UnboundedFound = false;
298 for (unsigned i = ConsumeAfterOpt != 0, e = PositionalOpts.size();
299 i != e; ++i) {
300 Option *Opt = PositionalOpts[i];
301 if (RequiresValue(Opt))
302 ++NumPositionalRequired;
303 else if (ConsumeAfterOpt) {
304 // ConsumeAfter cannot be combined with "optional" positional options
Chris Lattner54ec7ae2002-07-22 02:21:57 +0000305 // unless there is only one positional argument...
306 if (PositionalOpts.size() > 2)
307 ErrorParsing |=
308 Opt->error(" error - this positional option will never be matched, "
309 "because it does not Require a value, and a "
310 "cl::ConsumeAfter option is active!");
Chris Lattner9cf3d472003-07-30 17:34:02 +0000311 } else if (UnboundedFound && !Opt->ArgStr[0]) {
312 // This option does not "require" a value... Make sure this option is
313 // not specified after an option that eats all extra arguments, or this
314 // one will never get any!
Chris Lattner331de232002-07-22 02:07:59 +0000315 //
316 ErrorParsing |= Opt->error(" error - option can never match, because "
317 "another positional argument will match an "
318 "unbounded number of values, and this option"
319 " does not require a value!");
320 }
321 UnboundedFound |= EatsUnboundedNumberOfValues(Opt);
322 }
323 }
324
325 // PositionalVals - A vector of "positional" arguments we accumulate into to
326 // processes at the end...
327 //
Chris Lattnerca6433f2003-05-22 20:06:43 +0000328 std::vector<std::string> PositionalVals;
Chris Lattner331de232002-07-22 02:07:59 +0000329
Chris Lattner9cf3d472003-07-30 17:34:02 +0000330 // If the program has named positional arguments, and the name has been run
331 // across, keep track of which positional argument was named. Otherwise put
332 // the positional args into the PositionalVals list...
333 Option *ActivePositionalArg = 0;
334
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000335 // Loop over all of the arguments... processing them.
Chris Lattner331de232002-07-22 02:07:59 +0000336 bool DashDashFound = false; // Have we read '--'?
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000337 for (int i = 1; i < argc; ++i) {
338 Option *Handler = 0;
339 const char *Value = "";
340 const char *ArgName = "";
Chris Lattner331de232002-07-22 02:07:59 +0000341
342 // Check to see if this is a positional argument. This argument is
343 // considered to be positional if it doesn't start with '-', if it is "-"
Misha Brukman1115e042003-07-10 21:38:28 +0000344 // itself, or if we have seen "--" already.
Chris Lattner331de232002-07-22 02:07:59 +0000345 //
346 if (argv[i][0] != '-' || argv[i][1] == 0 || DashDashFound) {
347 // Positional argument!
Chris Lattner9cf3d472003-07-30 17:34:02 +0000348 if (ActivePositionalArg) {
349 ProvidePositionalOption(ActivePositionalArg, argv[i]);
350 continue; // We are done!
351 } else if (!PositionalOpts.empty()) {
Chris Lattner331de232002-07-22 02:07:59 +0000352 PositionalVals.push_back(argv[i]);
353
354 // All of the positional arguments have been fulfulled, give the rest to
355 // the consume after option... if it's specified...
356 //
Chris Lattnerd16714b2002-07-31 16:29:43 +0000357 if (PositionalVals.size() >= NumPositionalRequired &&
Chris Lattner331de232002-07-22 02:07:59 +0000358 ConsumeAfterOpt != 0) {
359 for (++i; i < argc; ++i)
360 PositionalVals.push_back(argv[i]);
361 break; // Handle outside of the argument processing loop...
362 }
363
364 // Delay processing positional arguments until the end...
365 continue;
366 }
Chris Lattnerbf455c22004-05-06 22:04:31 +0000367 } else if (argv[i][0] == '-' && argv[i][1] == '-' && argv[i][2] == 0 &&
368 !DashDashFound) {
369 DashDashFound = true; // This is the mythical "--"?
370 continue; // Don't try to process it as an argument itself.
371 } else if (ActivePositionalArg &&
372 (ActivePositionalArg->getMiscFlags() & PositionalEatsArgs)) {
373 // If there is a positional argument eating options, check to see if this
374 // option is another positional argument. If so, treat it as an argument,
375 // otherwise feed it to the eating positional.
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000376 ArgName = argv[i]+1;
Chris Lattnerbf455c22004-05-06 22:04:31 +0000377 Handler = LookupOption(ArgName, Value);
378 if (!Handler || Handler->getFormattingFlag() != cl::Positional) {
379 ProvidePositionalOption(ActivePositionalArg, argv[i]);
380 continue; // We are done!
Chris Lattner331de232002-07-22 02:07:59 +0000381 }
382
Chris Lattnerbf455c22004-05-06 22:04:31 +0000383 } else { // We start with a '-', must be an argument...
384 ArgName = argv[i]+1;
385 Handler = LookupOption(ArgName, Value);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000386
Chris Lattnerbf455c22004-05-06 22:04:31 +0000387 // Check to see if this "option" is really a prefixed or grouped argument.
388 if (Handler == 0 && *Value == 0) {
389 std::string RealName(ArgName);
390 if (RealName.size() > 1) {
Chris Lattner331de232002-07-22 02:07:59 +0000391 unsigned Length = 0;
392 Option *PGOpt = getOptionPred(RealName, Length, isPrefixedOrGrouping);
Chris Lattnerbf455c22004-05-06 22:04:31 +0000393
Chris Lattner331de232002-07-22 02:07:59 +0000394 // If the option is a prefixed option, then the value is simply the
395 // rest of the name... so fall through to later processing, by
396 // setting up the argument name flags and value fields.
397 //
398 if (PGOpt && PGOpt->getFormattingFlag() == cl::Prefix) {
Chris Lattnerbf455c22004-05-06 22:04:31 +0000399 Value = ArgName+Length;
400 assert(Opts.find(std::string(ArgName, Value)) != Opts.end() &&
401 Opts.find(std::string(ArgName, Value))->second == PGOpt);
402 Handler = PGOpt;
Chris Lattner331de232002-07-22 02:07:59 +0000403 } else if (PGOpt) {
Chris Lattnerbf455c22004-05-06 22:04:31 +0000404 // This must be a grouped option... handle them now.
Chris Lattner331de232002-07-22 02:07:59 +0000405 assert(isGrouping(PGOpt) && "Broken getOptionPred!");
Chris Lattnerbf455c22004-05-06 22:04:31 +0000406
Chris Lattner331de232002-07-22 02:07:59 +0000407 do {
408 // Move current arg name out of RealName into RealArgName...
Chris Lattnerbf455c22004-05-06 22:04:31 +0000409 std::string RealArgName(RealName.begin(),
410 RealName.begin() + Length);
411 RealName.erase(RealName.begin(), RealName.begin() + Length);
412
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000413 // Because ValueRequired is an invalid flag for grouped arguments,
414 // we don't need to pass argc/argv in...
415 //
Chris Lattner331de232002-07-22 02:07:59 +0000416 assert(PGOpt->getValueExpectedFlag() != cl::ValueRequired &&
417 "Option can not be cl::Grouping AND cl::ValueRequired!");
418 int Dummy;
Chris Lattnerbf455c22004-05-06 22:04:31 +0000419 ErrorParsing |= ProvideOption(PGOpt, RealArgName.c_str(),
420 "", 0, 0, Dummy);
421
Chris Lattner331de232002-07-22 02:07:59 +0000422 // Get the next grouping option...
Chris Lattnerbf455c22004-05-06 22:04:31 +0000423 PGOpt = getOptionPred(RealName, Length, isGrouping);
424 } while (PGOpt && Length != RealName.size());
425
426 Handler = PGOpt; // Ate all of the options.
Chris Lattner331de232002-07-22 02:07:59 +0000427 }
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000428 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000429 }
430 }
431
432 if (Handler == 0) {
Brian Gaekec86e84b2003-09-16 18:00:35 +0000433 std::cerr << "Unknown command line argument '" << argv[i] << "'. Try: '"
Chris Lattnerca6433f2003-05-22 20:06:43 +0000434 << argv[0] << " --help'\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000435 ErrorParsing = true;
436 continue;
437 }
438
Chris Lattner72fb8e52003-05-22 20:26:17 +0000439 // Check to see if this option accepts a comma separated list of values. If
440 // it does, we have to split up the value into multiple values...
441 if (Handler->getMiscFlags() & CommaSeparated) {
442 std::string Val(Value);
443 std::string::size_type Pos = Val.find(',');
444
445 while (Pos != std::string::npos) {
446 // Process the portion before the comma...
447 ErrorParsing |= ProvideOption(Handler, ArgName,
448 std::string(Val.begin(),
449 Val.begin()+Pos).c_str(),
450 argc, argv, i);
451 // Erase the portion before the comma, AND the comma...
452 Val.erase(Val.begin(), Val.begin()+Pos+1);
453 Value += Pos+1; // Increment the original value pointer as well...
454
455 // Check for another comma...
456 Pos = Val.find(',');
457 }
458 }
Chris Lattner9cf3d472003-07-30 17:34:02 +0000459
460 // If this is a named positional argument, just remember that it is the
461 // active one...
462 if (Handler->getFormattingFlag() == cl::Positional)
463 ActivePositionalArg = Handler;
464 else
465 ErrorParsing |= ProvideOption(Handler, ArgName, Value, argc, argv, i);
Chris Lattner331de232002-07-22 02:07:59 +0000466 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000467
Chris Lattner331de232002-07-22 02:07:59 +0000468 // Check and handle positional arguments now...
469 if (NumPositionalRequired > PositionalVals.size()) {
Chris Lattnerca6433f2003-05-22 20:06:43 +0000470 std::cerr << "Not enough positional command line arguments specified!\n"
471 << "Must specify at least " << NumPositionalRequired
472 << " positional arguments: See: " << argv[0] << " --help\n";
Chris Lattner331de232002-07-22 02:07:59 +0000473 ErrorParsing = true;
474
475
476 } else if (ConsumeAfterOpt == 0) {
477 // Positional args have already been handled if ConsumeAfter is specified...
478 unsigned ValNo = 0, NumVals = PositionalVals.size();
479 for (unsigned i = 0, e = PositionalOpts.size(); i != e; ++i) {
480 if (RequiresValue(PositionalOpts[i])) {
481 ProvidePositionalOption(PositionalOpts[i], PositionalVals[ValNo++]);
482 --NumPositionalRequired; // We fulfilled our duty...
483 }
484
485 // If we _can_ give this option more arguments, do so now, as long as we
486 // do not give it values that others need. 'Done' controls whether the
487 // option even _WANTS_ any more.
488 //
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000489 bool Done = PositionalOpts[i]->getNumOccurrencesFlag() == cl::Required;
Chris Lattner331de232002-07-22 02:07:59 +0000490 while (NumVals-ValNo > NumPositionalRequired && !Done) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000491 switch (PositionalOpts[i]->getNumOccurrencesFlag()) {
Chris Lattner331de232002-07-22 02:07:59 +0000492 case cl::Optional:
493 Done = true; // Optional arguments want _at most_ one value
494 // FALL THROUGH
495 case cl::ZeroOrMore: // Zero or more will take all they can get...
496 case cl::OneOrMore: // One or more will take all they can get...
497 ProvidePositionalOption(PositionalOpts[i], PositionalVals[ValNo++]);
498 break;
499 default:
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000500 assert(0 && "Internal error, unexpected NumOccurrences flag in "
Chris Lattner331de232002-07-22 02:07:59 +0000501 "positional argument processing!");
502 }
503 }
Chris Lattnercaccd762001-10-27 05:54:17 +0000504 }
Chris Lattner331de232002-07-22 02:07:59 +0000505 } else {
506 assert(ConsumeAfterOpt && NumPositionalRequired <= PositionalVals.size());
507 unsigned ValNo = 0;
508 for (unsigned j = 1, e = PositionalOpts.size(); j != e; ++j)
509 if (RequiresValue(PositionalOpts[j]))
Chris Lattnerfaba8092002-07-24 20:15:13 +0000510 ErrorParsing |= ProvidePositionalOption(PositionalOpts[j],
511 PositionalVals[ValNo++]);
512
513 // Handle the case where there is just one positional option, and it's
514 // optional. In this case, we want to give JUST THE FIRST option to the
515 // positional option and keep the rest for the consume after. The above
516 // loop would have assigned no values to positional options in this case.
517 //
Chris Lattnerb490c202002-08-02 21:51:29 +0000518 if (PositionalOpts.size() == 2 && ValNo == 0 && !PositionalVals.empty())
Chris Lattnerfaba8092002-07-24 20:15:13 +0000519 ErrorParsing |= ProvidePositionalOption(PositionalOpts[1],
520 PositionalVals[ValNo++]);
Chris Lattner331de232002-07-22 02:07:59 +0000521
522 // Handle over all of the rest of the arguments to the
523 // cl::ConsumeAfter command line option...
524 for (; ValNo != PositionalVals.size(); ++ValNo)
525 ErrorParsing |= ProvidePositionalOption(ConsumeAfterOpt,
526 PositionalVals[ValNo]);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000527 }
528
Chris Lattnerdc4693d2001-07-23 23:02:45 +0000529 // Loop over args and make sure all required args are specified!
Chris Lattnerca6433f2003-05-22 20:06:43 +0000530 for (std::map<std::string, Option*>::iterator I = Opts.begin(),
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000531 E = Opts.end(); I != E; ++I) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000532 switch (I->second->getNumOccurrencesFlag()) {
Chris Lattnerdc4693d2001-07-23 23:02:45 +0000533 case Required:
534 case OneOrMore:
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000535 if (I->second->getNumOccurrences() == 0) {
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000536 I->second->error(" must be specified at least once!");
Chris Lattnerf038acb2001-10-24 06:21:56 +0000537 ErrorParsing = true;
538 }
Chris Lattnerdc4693d2001-07-23 23:02:45 +0000539 // Fall through
540 default:
541 break;
542 }
543 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000544
Chris Lattner331de232002-07-22 02:07:59 +0000545 // Free all of the memory allocated to the map. Command line options may only
546 // be processed once!
Chris Lattnere8e258b2002-07-29 20:58:42 +0000547 delete CommandLineOptions;
548 CommandLineOptions = 0;
Chris Lattner331de232002-07-22 02:07:59 +0000549 PositionalOpts.clear();
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000550
551 // If we had an error processing our arguments, don't let the program execute
552 if (ErrorParsing) exit(1);
553}
554
555//===----------------------------------------------------------------------===//
556// Option Base class implementation
557//
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000558
Chris Lattnerca6433f2003-05-22 20:06:43 +0000559bool Option::error(std::string Message, const char *ArgName) {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000560 if (ArgName == 0) ArgName = ArgStr;
Chris Lattner331de232002-07-22 02:07:59 +0000561 if (ArgName[0] == 0)
Chris Lattnerca6433f2003-05-22 20:06:43 +0000562 std::cerr << HelpStr; // Be nice for positional arguments
Chris Lattner331de232002-07-22 02:07:59 +0000563 else
Chris Lattnerca6433f2003-05-22 20:06:43 +0000564 std::cerr << "-" << ArgName;
565 std::cerr << " option" << Message << "\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000566 return true;
567}
568
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000569bool Option::addOccurrence(const char *ArgName, const std::string &Value) {
570 NumOccurrences++; // Increment the number of times we have been seen
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000571
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000572 switch (getNumOccurrencesFlag()) {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000573 case Optional:
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000574 if (NumOccurrences > 1)
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000575 return error(": may only occur zero or one times!", ArgName);
576 break;
577 case Required:
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000578 if (NumOccurrences > 1)
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000579 return error(": must occur exactly one time!", ArgName);
580 // Fall through
581 case OneOrMore:
Chris Lattnercaccd762001-10-27 05:54:17 +0000582 case ZeroOrMore:
583 case ConsumeAfter: break;
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000584 default: return error(": bad num occurrences flag value!");
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000585 }
586
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000587 return handleOccurrence(ArgName, Value);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000588}
589
Chris Lattner331de232002-07-22 02:07:59 +0000590// addArgument - Tell the system that this Option subclass will handle all
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000591// occurrences of -ArgStr on the command line.
Chris Lattner331de232002-07-22 02:07:59 +0000592//
593void Option::addArgument(const char *ArgStr) {
594 if (ArgStr[0])
595 AddArgument(ArgStr, this);
Chris Lattner9cf3d472003-07-30 17:34:02 +0000596
597 if (getFormattingFlag() == Positional)
Chris Lattner331de232002-07-22 02:07:59 +0000598 getPositionalOpts().push_back(this);
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000599 else if (getNumOccurrencesFlag() == ConsumeAfter) {
Chris Lattner9cf3d472003-07-30 17:34:02 +0000600 if (!getPositionalOpts().empty() &&
601 getPositionalOpts().front()->getNumOccurrencesFlag() == ConsumeAfter)
602 error("Cannot specify more than one option with cl::ConsumeAfter!");
Chris Lattner331de232002-07-22 02:07:59 +0000603 getPositionalOpts().insert(getPositionalOpts().begin(), this);
604 }
605}
606
Chris Lattneraa852bb2002-07-23 17:15:12 +0000607void Option::removeArgument(const char *ArgStr) {
Chris Lattner9cf3d472003-07-30 17:34:02 +0000608 if (ArgStr[0])
Chris Lattnere8e258b2002-07-29 20:58:42 +0000609 RemoveArgument(ArgStr, this);
Chris Lattner9cf3d472003-07-30 17:34:02 +0000610
611 if (getFormattingFlag() == Positional) {
Chris Lattnerca6433f2003-05-22 20:06:43 +0000612 std::vector<Option*>::iterator I =
Chris Lattneraa852bb2002-07-23 17:15:12 +0000613 std::find(getPositionalOpts().begin(), getPositionalOpts().end(), this);
614 assert(I != getPositionalOpts().end() && "Arg not registered!");
615 getPositionalOpts().erase(I);
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000616 } else if (getNumOccurrencesFlag() == ConsumeAfter) {
Chris Lattneraa852bb2002-07-23 17:15:12 +0000617 assert(!getPositionalOpts().empty() && getPositionalOpts()[0] == this &&
618 "Arg not registered correctly!");
619 getPositionalOpts().erase(getPositionalOpts().begin());
620 }
621}
622
Chris Lattner331de232002-07-22 02:07:59 +0000623
624// getValueStr - Get the value description string, using "DefaultMsg" if nothing
625// has been specified yet.
626//
627static const char *getValueStr(const Option &O, const char *DefaultMsg) {
628 if (O.ValueStr[0] == 0) return DefaultMsg;
629 return O.ValueStr;
630}
631
632//===----------------------------------------------------------------------===//
633// cl::alias class implementation
634//
635
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000636// Return the width of the option tag for printing...
Chris Lattner331de232002-07-22 02:07:59 +0000637unsigned alias::getOptionWidth() const {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000638 return std::strlen(ArgStr)+6;
639}
640
Chris Lattner331de232002-07-22 02:07:59 +0000641// Print out the option for the alias...
642void alias::printOptionInfo(unsigned GlobalWidth) const {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000643 unsigned L = std::strlen(ArgStr);
Chris Lattnerca6433f2003-05-22 20:06:43 +0000644 std::cerr << " -" << ArgStr << std::string(GlobalWidth-L-6, ' ') << " - "
645 << HelpStr << "\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000646}
647
648
Chris Lattner331de232002-07-22 02:07:59 +0000649
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000650//===----------------------------------------------------------------------===//
Chris Lattner331de232002-07-22 02:07:59 +0000651// Parser Implementation code...
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000652//
653
Chris Lattner9b14eb52002-08-07 18:36:37 +0000654// basic_parser implementation
655//
656
657// Return the width of the option tag for printing...
658unsigned basic_parser_impl::getOptionWidth(const Option &O) const {
659 unsigned Len = std::strlen(O.ArgStr);
660 if (const char *ValName = getValueName())
661 Len += std::strlen(getValueStr(O, ValName))+3;
662
663 return Len + 6;
664}
665
666// printOptionInfo - Print out information about this option. The
667// to-be-maintained width is specified.
668//
669void basic_parser_impl::printOptionInfo(const Option &O,
670 unsigned GlobalWidth) const {
Chris Lattnerca6433f2003-05-22 20:06:43 +0000671 std::cerr << " -" << O.ArgStr;
Chris Lattner9b14eb52002-08-07 18:36:37 +0000672
673 if (const char *ValName = getValueName())
Chris Lattnerca6433f2003-05-22 20:06:43 +0000674 std::cerr << "=<" << getValueStr(O, ValName) << ">";
Chris Lattner9b14eb52002-08-07 18:36:37 +0000675
Chris Lattnerca6433f2003-05-22 20:06:43 +0000676 std::cerr << std::string(GlobalWidth-getOptionWidth(O), ' ') << " - "
677 << O.HelpStr << "\n";
Chris Lattner9b14eb52002-08-07 18:36:37 +0000678}
679
680
681
682
Chris Lattner331de232002-07-22 02:07:59 +0000683// parser<bool> implementation
684//
Chris Lattner9b14eb52002-08-07 18:36:37 +0000685bool parser<bool>::parse(Option &O, const char *ArgName,
Chris Lattnerca6433f2003-05-22 20:06:43 +0000686 const std::string &Arg, bool &Value) {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000687 if (Arg == "" || Arg == "true" || Arg == "TRUE" || Arg == "True" ||
688 Arg == "1") {
689 Value = true;
690 } else if (Arg == "false" || Arg == "FALSE" || Arg == "False" || Arg == "0") {
691 Value = false;
692 } else {
Chris Lattner331de232002-07-22 02:07:59 +0000693 return O.error(": '" + Arg +
694 "' is invalid value for boolean argument! Try 0 or 1");
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000695 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000696 return false;
697}
698
Chris Lattner331de232002-07-22 02:07:59 +0000699// parser<int> implementation
700//
Chris Lattner9b14eb52002-08-07 18:36:37 +0000701bool parser<int>::parse(Option &O, const char *ArgName,
Chris Lattnerca6433f2003-05-22 20:06:43 +0000702 const std::string &Arg, int &Value) {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000703 char *End;
Chris Lattnerd2a6fc32003-06-28 15:47:20 +0000704 Value = (int)strtol(Arg.c_str(), &End, 0);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000705 if (*End != 0)
Chris Lattner331de232002-07-22 02:07:59 +0000706 return O.error(": '" + Arg + "' value invalid for integer argument!");
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000707 return false;
708}
709
Chris Lattnerd2a6fc32003-06-28 15:47:20 +0000710// parser<unsigned> implementation
711//
712bool parser<unsigned>::parse(Option &O, const char *ArgName,
713 const std::string &Arg, unsigned &Value) {
714 char *End;
Brian Gaeke2d6a2362003-10-10 17:01:36 +0000715 errno = 0;
716 unsigned long V = strtoul(Arg.c_str(), &End, 0);
Chris Lattnerd2a6fc32003-06-28 15:47:20 +0000717 Value = (unsigned)V;
Brian Gaeke2d6a2362003-10-10 17:01:36 +0000718 if (((V == ULONG_MAX) && (errno == ERANGE))
719 || (*End != 0)
720 || (Value != V))
Chris Lattnerd2a6fc32003-06-28 15:47:20 +0000721 return O.error(": '" + Arg + "' value invalid for uint argument!");
722 return false;
723}
724
Chris Lattner9b14eb52002-08-07 18:36:37 +0000725// parser<double>/parser<float> implementation
Chris Lattnerd215fd12001-10-13 06:53:19 +0000726//
Chris Lattnerca6433f2003-05-22 20:06:43 +0000727static bool parseDouble(Option &O, const std::string &Arg, double &Value) {
Chris Lattner331de232002-07-22 02:07:59 +0000728 const char *ArgStart = Arg.c_str();
729 char *End;
730 Value = strtod(ArgStart, &End);
731 if (*End != 0)
732 return O.error(": '" +Arg+ "' value invalid for floating point argument!");
Chris Lattnerd215fd12001-10-13 06:53:19 +0000733 return false;
734}
735
Chris Lattner9b14eb52002-08-07 18:36:37 +0000736bool parser<double>::parse(Option &O, const char *AN,
737 const std::string &Arg, double &Val) {
738 return parseDouble(O, Arg, Val);
Chris Lattner331de232002-07-22 02:07:59 +0000739}
740
Chris Lattner9b14eb52002-08-07 18:36:37 +0000741bool parser<float>::parse(Option &O, const char *AN,
742 const std::string &Arg, float &Val) {
743 double dVal;
744 if (parseDouble(O, Arg, dVal))
745 return true;
746 Val = (float)dVal;
747 return false;
Chris Lattner331de232002-07-22 02:07:59 +0000748}
749
750
Chris Lattner331de232002-07-22 02:07:59 +0000751
752// generic_parser_base implementation
753//
754
Chris Lattneraa852bb2002-07-23 17:15:12 +0000755// findOption - Return the option number corresponding to the specified
756// argument string. If the option is not found, getNumOptions() is returned.
757//
758unsigned generic_parser_base::findOption(const char *Name) {
759 unsigned i = 0, e = getNumOptions();
Chris Lattnerca6433f2003-05-22 20:06:43 +0000760 std::string N(Name);
Chris Lattneraa852bb2002-07-23 17:15:12 +0000761
762 while (i != e)
763 if (getOption(i) == N)
764 return i;
765 else
766 ++i;
767 return e;
768}
769
770
Chris Lattner331de232002-07-22 02:07:59 +0000771// Return the width of the option tag for printing...
772unsigned generic_parser_base::getOptionWidth(const Option &O) const {
773 if (O.hasArgStr()) {
774 unsigned Size = std::strlen(O.ArgStr)+6;
775 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
776 Size = std::max(Size, (unsigned)std::strlen(getOption(i))+8);
777 return Size;
778 } else {
779 unsigned BaseSize = 0;
780 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
781 BaseSize = std::max(BaseSize, (unsigned)std::strlen(getOption(i))+8);
782 return BaseSize;
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000783 }
784}
785
Chris Lattner331de232002-07-22 02:07:59 +0000786// printOptionInfo - Print out information about this option. The
787// to-be-maintained width is specified.
788//
789void generic_parser_base::printOptionInfo(const Option &O,
790 unsigned GlobalWidth) const {
791 if (O.hasArgStr()) {
792 unsigned L = std::strlen(O.ArgStr);
Chris Lattnerca6433f2003-05-22 20:06:43 +0000793 std::cerr << " -" << O.ArgStr << std::string(GlobalWidth-L-6, ' ')
794 << " - " << O.HelpStr << "\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000795
Chris Lattner331de232002-07-22 02:07:59 +0000796 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
797 unsigned NumSpaces = GlobalWidth-strlen(getOption(i))-8;
Chris Lattnerca6433f2003-05-22 20:06:43 +0000798 std::cerr << " =" << getOption(i) << std::string(NumSpaces, ' ')
799 << " - " << getDescription(i) << "\n";
Chris Lattner9c9be482002-01-31 00:42:56 +0000800 }
Chris Lattner331de232002-07-22 02:07:59 +0000801 } else {
802 if (O.HelpStr[0])
Chris Lattnerca6433f2003-05-22 20:06:43 +0000803 std::cerr << " " << O.HelpStr << "\n";
Chris Lattner331de232002-07-22 02:07:59 +0000804 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
805 unsigned L = std::strlen(getOption(i));
Chris Lattnerca6433f2003-05-22 20:06:43 +0000806 std::cerr << " -" << getOption(i) << std::string(GlobalWidth-L-8, ' ')
807 << " - " << getDescription(i) << "\n";
Chris Lattner331de232002-07-22 02:07:59 +0000808 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000809 }
810}
811
812
813//===----------------------------------------------------------------------===//
Chris Lattner331de232002-07-22 02:07:59 +0000814// --help and --help-hidden option implementation
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000815//
816namespace {
817
Chris Lattner331de232002-07-22 02:07:59 +0000818class HelpPrinter {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000819 unsigned MaxArgLen;
820 const Option *EmptyArg;
821 const bool ShowHidden;
822
Chris Lattner331de232002-07-22 02:07:59 +0000823 // isHidden/isReallyHidden - Predicates to be used to filter down arg lists.
Chris Lattnerca6433f2003-05-22 20:06:43 +0000824 inline static bool isHidden(std::pair<std::string, Option *> &OptPair) {
Chris Lattner331de232002-07-22 02:07:59 +0000825 return OptPair.second->getOptionHiddenFlag() >= Hidden;
826 }
Chris Lattnerca6433f2003-05-22 20:06:43 +0000827 inline static bool isReallyHidden(std::pair<std::string, Option *> &OptPair) {
Chris Lattner331de232002-07-22 02:07:59 +0000828 return OptPair.second->getOptionHiddenFlag() == ReallyHidden;
829 }
830
831public:
832 HelpPrinter(bool showHidden) : ShowHidden(showHidden) {
833 EmptyArg = 0;
834 }
835
836 void operator=(bool Value) {
837 if (Value == false) return;
838
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000839 // Copy Options into a vector so we can sort them as we like...
Chris Lattnerca6433f2003-05-22 20:06:43 +0000840 std::vector<std::pair<std::string, Option*> > Options;
Chris Lattner697954c2002-01-20 22:54:45 +0000841 copy(getOpts().begin(), getOpts().end(), std::back_inserter(Options));
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000842
843 // Eliminate Hidden or ReallyHidden arguments, depending on ShowHidden
Chris Lattner331de232002-07-22 02:07:59 +0000844 Options.erase(std::remove_if(Options.begin(), Options.end(),
845 std::ptr_fun(ShowHidden ? isReallyHidden : isHidden)),
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000846 Options.end());
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000847
848 // Eliminate duplicate entries in table (from enum flags options, f.e.)
Chris Lattner331de232002-07-22 02:07:59 +0000849 { // Give OptionSet a scope
850 std::set<Option*> OptionSet;
851 for (unsigned i = 0; i != Options.size(); ++i)
852 if (OptionSet.count(Options[i].second) == 0)
853 OptionSet.insert(Options[i].second); // Add new entry to set
854 else
855 Options.erase(Options.begin()+i--); // Erase duplicate
856 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000857
858 if (ProgramOverview)
Chris Lattnerca6433f2003-05-22 20:06:43 +0000859 std::cerr << "OVERVIEW:" << ProgramOverview << "\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000860
Chris Lattnerca6433f2003-05-22 20:06:43 +0000861 std::cerr << "USAGE: " << ProgramName << " [options]";
Chris Lattner331de232002-07-22 02:07:59 +0000862
863 // Print out the positional options...
Chris Lattnerca6433f2003-05-22 20:06:43 +0000864 std::vector<Option*> &PosOpts = getPositionalOpts();
Chris Lattner331de232002-07-22 02:07:59 +0000865 Option *CAOpt = 0; // The cl::ConsumeAfter option, if it exists...
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000866 if (!PosOpts.empty() && PosOpts[0]->getNumOccurrencesFlag() == ConsumeAfter)
Chris Lattner331de232002-07-22 02:07:59 +0000867 CAOpt = PosOpts[0];
868
Chris Lattner9cf3d472003-07-30 17:34:02 +0000869 for (unsigned i = CAOpt != 0, e = PosOpts.size(); i != e; ++i) {
870 if (PosOpts[i]->ArgStr[0])
871 std::cerr << " --" << PosOpts[i]->ArgStr;
Chris Lattnerca6433f2003-05-22 20:06:43 +0000872 std::cerr << " " << PosOpts[i]->HelpStr;
Chris Lattner9cf3d472003-07-30 17:34:02 +0000873 }
Chris Lattner331de232002-07-22 02:07:59 +0000874
875 // Print the consume after option info if it exists...
Chris Lattnerca6433f2003-05-22 20:06:43 +0000876 if (CAOpt) std::cerr << " " << CAOpt->HelpStr;
Chris Lattner331de232002-07-22 02:07:59 +0000877
Chris Lattnerca6433f2003-05-22 20:06:43 +0000878 std::cerr << "\n\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000879
880 // Compute the maximum argument length...
881 MaxArgLen = 0;
Chris Lattner331de232002-07-22 02:07:59 +0000882 for (unsigned i = 0, e = Options.size(); i != e; ++i)
883 MaxArgLen = std::max(MaxArgLen, Options[i].second->getOptionWidth());
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000884
Chris Lattnerca6433f2003-05-22 20:06:43 +0000885 std::cerr << "OPTIONS:\n";
Chris Lattner331de232002-07-22 02:07:59 +0000886 for (unsigned i = 0, e = Options.size(); i != e; ++i)
887 Options[i].second->printOptionInfo(MaxArgLen);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000888
Chris Lattner331de232002-07-22 02:07:59 +0000889 // Halt the program if help information is printed
890 exit(1);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000891 }
892};
893
Chris Lattner331de232002-07-22 02:07:59 +0000894
895
896// Define the two HelpPrinter instances that are used to print out help, or
897// help-hidden...
898//
899HelpPrinter NormalPrinter(false);
900HelpPrinter HiddenPrinter(true);
901
902cl::opt<HelpPrinter, true, parser<bool> >
903HOp("help", cl::desc("display available options (--help-hidden for more)"),
Chris Lattner9b14eb52002-08-07 18:36:37 +0000904 cl::location(NormalPrinter), cl::ValueDisallowed);
Chris Lattner331de232002-07-22 02:07:59 +0000905
906cl::opt<HelpPrinter, true, parser<bool> >
907HHOp("help-hidden", cl::desc("display all available options"),
Chris Lattner9b14eb52002-08-07 18:36:37 +0000908 cl::location(HiddenPrinter), cl::Hidden, cl::ValueDisallowed);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000909
910} // End anonymous namespace