blob: 13de27eb5d518721782b8a4846aaa7062def4982 [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
Reid Spencer69105f32004-08-04 00:36:06 +000019#include "Config/config.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000020#include "Support/CommandLine.h"
Chris Lattnerdbab15a2001-07-23 17:17:47 +000021#include <algorithm>
22#include <map>
23#include <set>
Chris Lattner697954c2002-01-20 22:54:45 +000024#include <iostream>
Brian Gaeke2d6a2362003-10-10 17:01:36 +000025#include <cstdlib>
26#include <cerrno>
Chris Lattner51140042004-07-03 01:21:05 +000027#include <cstring>
Chris Lattner2cdd21c2003-12-14 21:35:53 +000028using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000029
Chris Lattnerdbab15a2001-07-23 17:17:47 +000030using namespace cl;
31
Chris Lattner331de232002-07-22 02:07:59 +000032//===----------------------------------------------------------------------===//
33// Basic, shared command line option processing machinery...
34//
35
Chris Lattnerdbab15a2001-07-23 17:17:47 +000036// Return the global command line option vector. Making it a function scoped
Chris Lattnerf78032f2001-11-26 18:58:34 +000037// static ensures that it will be initialized correctly before its first use.
Chris Lattnerdbab15a2001-07-23 17:17:47 +000038//
Chris Lattnerca6433f2003-05-22 20:06:43 +000039static std::map<std::string, Option*> *CommandLineOptions = 0;
40static std::map<std::string, Option*> &getOpts() {
41 if (CommandLineOptions == 0)
42 CommandLineOptions = new std::map<std::string,Option*>();
Chris Lattnere8e258b2002-07-29 20:58:42 +000043 return *CommandLineOptions;
44}
45
Chris Lattnerca6433f2003-05-22 20:06:43 +000046static Option *getOption(const std::string &Str) {
Chris Lattnere8e258b2002-07-29 20:58:42 +000047 if (CommandLineOptions == 0) return 0;
Chris Lattnerca6433f2003-05-22 20:06:43 +000048 std::map<std::string,Option*>::iterator I = CommandLineOptions->find(Str);
Chris Lattnere8e258b2002-07-29 20:58:42 +000049 return I != CommandLineOptions->end() ? I->second : 0;
Chris Lattnerdbab15a2001-07-23 17:17:47 +000050}
51
Chris Lattnerca6433f2003-05-22 20:06:43 +000052static std::vector<Option*> &getPositionalOpts() {
Alkis Evlogimenos5f65add2004-03-04 17:50:44 +000053 static std::vector<Option*> *Positional = 0;
54 if (!Positional) Positional = new std::vector<Option*>();
55 return *Positional;
Chris Lattner331de232002-07-22 02:07:59 +000056}
57
Chris Lattnere8e258b2002-07-29 20:58:42 +000058static void AddArgument(const char *ArgName, Option *Opt) {
59 if (getOption(ArgName)) {
Chris Lattnerca6433f2003-05-22 20:06:43 +000060 std::cerr << "CommandLine Error: Argument '" << ArgName
61 << "' defined more than once!\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +000062 } else {
Chris Lattnerf78032f2001-11-26 18:58:34 +000063 // Add argument to the argument map!
Chris Lattnere8e258b2002-07-29 20:58:42 +000064 getOpts()[ArgName] = Opt;
65 }
66}
67
68// RemoveArgument - It's possible that the argument is no longer in the map if
69// options have already been processed and the map has been deleted!
70//
71static void RemoveArgument(const char *ArgName, Option *Opt) {
72 if (CommandLineOptions == 0) return;
Chris Lattnerf98cfc72004-07-18 21:56:20 +000073#ifndef NDEBUG
74 // This disgusting HACK is brought to you courtesy of GCC 3.3.2, which ICE's
75 // If we pass ArgName directly into getOption here.
76 std::string Tmp = ArgName;
77 assert(getOption(Tmp) == Opt && "Arg not in map!");
78#endif
Chris Lattnere8e258b2002-07-29 20:58:42 +000079 CommandLineOptions->erase(ArgName);
80 if (CommandLineOptions->empty()) {
81 delete CommandLineOptions;
82 CommandLineOptions = 0;
Chris Lattnerdbab15a2001-07-23 17:17:47 +000083 }
84}
85
86static const char *ProgramName = 0;
87static const char *ProgramOverview = 0;
88
Chris Lattnercaccd762001-10-27 05:54:17 +000089static inline bool ProvideOption(Option *Handler, const char *ArgName,
90 const char *Value, int argc, char **argv,
91 int &i) {
92 // Enforce value requirements
93 switch (Handler->getValueExpectedFlag()) {
94 case ValueRequired:
95 if (Value == 0 || *Value == 0) { // No value specified?
96 if (i+1 < argc) { // Steal the next argument, like for '-o filename'
97 Value = argv[++i];
98 } else {
99 return Handler->error(" requires a value!");
100 }
101 }
102 break;
103 case ValueDisallowed:
104 if (*Value != 0)
105 return Handler->error(" does not allow a value! '" +
Chris Lattnerca6433f2003-05-22 20:06:43 +0000106 std::string(Value) + "' specified.");
Chris Lattnercaccd762001-10-27 05:54:17 +0000107 break;
108 case ValueOptional: break;
Chris Lattnerca6433f2003-05-22 20:06:43 +0000109 default: std::cerr << "Bad ValueMask flag! CommandLine usage error:"
110 << Handler->getValueExpectedFlag() << "\n"; abort();
Chris Lattnercaccd762001-10-27 05:54:17 +0000111 }
112
113 // Run the handler now!
Reid Spencer1e13fd22004-08-13 19:47:30 +0000114 return Handler->addOccurrence(i, ArgName, Value);
Chris Lattnercaccd762001-10-27 05:54:17 +0000115}
116
Reid Spencer1e13fd22004-08-13 19:47:30 +0000117static bool ProvidePositionalOption(Option *Handler, const std::string &Arg,
118 int i) {
119 int Dummy = i;
Chris Lattner9cf3d472003-07-30 17:34:02 +0000120 return ProvideOption(Handler, Handler->ArgStr, Arg.c_str(), 0, 0, Dummy);
Chris Lattner331de232002-07-22 02:07:59 +0000121}
Chris Lattnerf78032f2001-11-26 18:58:34 +0000122
Chris Lattner331de232002-07-22 02:07:59 +0000123
124// Option predicates...
125static inline bool isGrouping(const Option *O) {
126 return O->getFormattingFlag() == cl::Grouping;
127}
128static inline bool isPrefixedOrGrouping(const Option *O) {
129 return isGrouping(O) || O->getFormattingFlag() == cl::Prefix;
130}
131
132// getOptionPred - Check to see if there are any options that satisfy the
133// specified predicate with names that are the prefixes in Name. This is
134// checked by progressively stripping characters off of the name, checking to
135// see if there options that satisfy the predicate. If we find one, return it,
136// otherwise return null.
137//
138static Option *getOptionPred(std::string Name, unsigned &Length,
139 bool (*Pred)(const Option*)) {
140
Chris Lattnere8e258b2002-07-29 20:58:42 +0000141 Option *Op = getOption(Name);
142 if (Op && Pred(Op)) {
Chris Lattner331de232002-07-22 02:07:59 +0000143 Length = Name.length();
Chris Lattnere8e258b2002-07-29 20:58:42 +0000144 return Op;
Chris Lattnerf78032f2001-11-26 18:58:34 +0000145 }
146
Chris Lattner331de232002-07-22 02:07:59 +0000147 if (Name.size() == 1) return 0;
148 do {
149 Name.erase(Name.end()-1, Name.end()); // Chop off the last character...
Chris Lattnere8e258b2002-07-29 20:58:42 +0000150 Op = getOption(Name);
Chris Lattner331de232002-07-22 02:07:59 +0000151
152 // Loop while we haven't found an option and Name still has at least two
153 // characters in it (so that the next iteration will not be the empty
154 // string...
Chris Lattnere8e258b2002-07-29 20:58:42 +0000155 } while ((Op == 0 || !Pred(Op)) && Name.size() > 1);
Chris Lattner331de232002-07-22 02:07:59 +0000156
Chris Lattnere8e258b2002-07-29 20:58:42 +0000157 if (Op && Pred(Op)) {
Chris Lattner331de232002-07-22 02:07:59 +0000158 Length = Name.length();
Chris Lattnere8e258b2002-07-29 20:58:42 +0000159 return Op; // Found one!
Chris Lattner331de232002-07-22 02:07:59 +0000160 }
161 return 0; // No option found!
162}
163
164static bool RequiresValue(const Option *O) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000165 return O->getNumOccurrencesFlag() == cl::Required ||
166 O->getNumOccurrencesFlag() == cl::OneOrMore;
Chris Lattner331de232002-07-22 02:07:59 +0000167}
168
169static bool EatsUnboundedNumberOfValues(const Option *O) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000170 return O->getNumOccurrencesFlag() == cl::ZeroOrMore ||
171 O->getNumOccurrencesFlag() == cl::OneOrMore;
Chris Lattnerf78032f2001-11-26 18:58:34 +0000172}
Chris Lattnercaccd762001-10-27 05:54:17 +0000173
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000174/// ParseCStringVector - Break INPUT up wherever one or more
175/// whitespace characters are found, and store the resulting tokens in
176/// OUTPUT. The tokens stored in OUTPUT are dynamically allocated
177/// using strdup (), so it is the caller's responsibility to free ()
178/// them later.
Brian Gaeke06b06c52003-08-14 22:00:59 +0000179///
180static void ParseCStringVector (std::vector<char *> &output,
Reid Spencer69105f32004-08-04 00:36:06 +0000181 const char *input) {
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000182 // Characters which will be treated as token separators:
183 static const char *delims = " \v\f\t\r\n";
184
185 std::string work (input);
186 // Skip past any delims at head of input string.
187 size_t pos = work.find_first_not_of (delims);
188 // If the string consists entirely of delims, then exit early.
189 if (pos == std::string::npos) return;
190 // Otherwise, jump forward to beginning of first word.
191 work = work.substr (pos);
192 // Find position of first delimiter.
193 pos = work.find_first_of (delims);
194
195 while (!work.empty() && pos != std::string::npos) {
196 // Everything from 0 to POS is the next word to copy.
197 output.push_back (strdup (work.substr (0,pos).c_str ()));
198 // Is there another word in the string?
199 size_t nextpos = work.find_first_not_of (delims, pos + 1);
200 if (nextpos != std::string::npos) {
201 // Yes? Then remove delims from beginning ...
202 work = work.substr (work.find_first_not_of (delims, pos + 1));
203 // and find the end of the word.
204 pos = work.find_first_of (delims);
205 } else {
206 // No? (Remainder of string is delims.) End the loop.
207 work = "";
208 pos = std::string::npos;
209 }
210 }
211
212 // If `input' ended with non-delim char, then we'll get here with
213 // the last word of `input' in `work'; copy it now.
214 if (!work.empty ()) {
215 output.push_back (strdup (work.c_str ()));
Brian Gaeke06b06c52003-08-14 22:00:59 +0000216 }
217}
218
219/// ParseEnvironmentOptions - An alternative entry point to the
220/// CommandLine library, which allows you to read the program's name
221/// from the caller (as PROGNAME) and its command-line arguments from
222/// an environment variable (whose name is given in ENVVAR).
223///
Chris Lattnerbf455c22004-05-06 22:04:31 +0000224void cl::ParseEnvironmentOptions(const char *progName, const char *envVar,
225 const char *Overview) {
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000226 // Check args.
Chris Lattnerbf455c22004-05-06 22:04:31 +0000227 assert(progName && "Program name not specified");
228 assert(envVar && "Environment variable name missing");
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000229
230 // Get the environment variable they want us to parse options out of.
231 const char *envValue = getenv (envVar);
232 if (!envValue)
233 return;
234
Brian Gaeke06b06c52003-08-14 22:00:59 +0000235 // Get program's "name", which we wouldn't know without the caller
236 // telling us.
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000237 std::vector<char *> newArgv;
238 newArgv.push_back (strdup (progName));
Brian Gaeke06b06c52003-08-14 22:00:59 +0000239
240 // Parse the value of the environment variable into a "command line"
241 // and hand it off to ParseCommandLineOptions().
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000242 ParseCStringVector (newArgv, envValue);
243 int newArgc = newArgv.size ();
244 ParseCommandLineOptions (newArgc, &newArgv[0], Overview);
245
246 // Free all the strdup()ed strings.
247 for (std::vector<char *>::iterator i = newArgv.begin (), e = newArgv.end ();
248 i != e; ++i) {
249 free (*i);
250 }
Brian Gaeke06b06c52003-08-14 22:00:59 +0000251}
252
Chris Lattnerbf455c22004-05-06 22:04:31 +0000253/// LookupOption - Lookup the option specified by the specified option on the
254/// command line. If there is a value specified (after an equal sign) return
255/// that as well.
256static Option *LookupOption(const char *&Arg, const char *&Value) {
257 while (*Arg == '-') ++Arg; // Eat leading dashes
258
259 const char *ArgEnd = Arg;
260 while (*ArgEnd && *ArgEnd != '=')
261 ++ArgEnd; // Scan till end of argument name...
262
263 Value = ArgEnd;
264 if (*Value) // If we have an equals sign...
265 ++Value; // Advance to value...
266
267 if (*Arg == 0) return 0;
268
269 // Look up the option.
270 std::map<std::string, Option*> &Opts = getOpts();
271 std::map<std::string, Option*>::iterator I =
272 Opts.find(std::string(Arg, ArgEnd));
273 return (I != Opts.end()) ? I->second : 0;
274}
275
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000276void cl::ParseCommandLineOptions(int &argc, char **argv,
Chris Lattner0c0edf82002-07-25 06:17:51 +0000277 const char *Overview) {
Chris Lattner331de232002-07-22 02:07:59 +0000278 assert((!getOpts().empty() || !getPositionalOpts().empty()) &&
279 "No options specified, or ParseCommandLineOptions called more"
280 " than once!");
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000281 ProgramName = argv[0]; // Save this away safe and snug
282 ProgramOverview = Overview;
283 bool ErrorParsing = false;
284
Chris Lattnerca6433f2003-05-22 20:06:43 +0000285 std::map<std::string, Option*> &Opts = getOpts();
286 std::vector<Option*> &PositionalOpts = getPositionalOpts();
Chris Lattner331de232002-07-22 02:07:59 +0000287
288 // Check out the positional arguments to collect information about them.
289 unsigned NumPositionalRequired = 0;
290 Option *ConsumeAfterOpt = 0;
291 if (!PositionalOpts.empty()) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000292 if (PositionalOpts[0]->getNumOccurrencesFlag() == cl::ConsumeAfter) {
Chris Lattner331de232002-07-22 02:07:59 +0000293 assert(PositionalOpts.size() > 1 &&
294 "Cannot specify cl::ConsumeAfter without a positional argument!");
295 ConsumeAfterOpt = PositionalOpts[0];
296 }
297
298 // Calculate how many positional values are _required_.
299 bool UnboundedFound = false;
300 for (unsigned i = ConsumeAfterOpt != 0, e = PositionalOpts.size();
301 i != e; ++i) {
302 Option *Opt = PositionalOpts[i];
303 if (RequiresValue(Opt))
304 ++NumPositionalRequired;
305 else if (ConsumeAfterOpt) {
306 // ConsumeAfter cannot be combined with "optional" positional options
Chris Lattner54ec7ae2002-07-22 02:21:57 +0000307 // unless there is only one positional argument...
308 if (PositionalOpts.size() > 2)
309 ErrorParsing |=
310 Opt->error(" error - this positional option will never be matched, "
311 "because it does not Require a value, and a "
312 "cl::ConsumeAfter option is active!");
Chris Lattner9cf3d472003-07-30 17:34:02 +0000313 } else if (UnboundedFound && !Opt->ArgStr[0]) {
314 // This option does not "require" a value... Make sure this option is
315 // not specified after an option that eats all extra arguments, or this
316 // one will never get any!
Chris Lattner331de232002-07-22 02:07:59 +0000317 //
318 ErrorParsing |= Opt->error(" error - option can never match, because "
319 "another positional argument will match an "
320 "unbounded number of values, and this option"
321 " does not require a value!");
322 }
323 UnboundedFound |= EatsUnboundedNumberOfValues(Opt);
324 }
325 }
326
Reid Spencer1e13fd22004-08-13 19:47:30 +0000327 // PositionalVals - A vector of "positional" arguments we accumulate into
328 // the process at the end...
Chris Lattner331de232002-07-22 02:07:59 +0000329 //
Reid Spencer1e13fd22004-08-13 19:47:30 +0000330 std::vector<std::pair<std::string,unsigned> > PositionalVals;
Chris Lattner331de232002-07-22 02:07:59 +0000331
Chris Lattner9cf3d472003-07-30 17:34:02 +0000332 // If the program has named positional arguments, and the name has been run
333 // across, keep track of which positional argument was named. Otherwise put
334 // the positional args into the PositionalVals list...
335 Option *ActivePositionalArg = 0;
336
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000337 // Loop over all of the arguments... processing them.
Chris Lattner331de232002-07-22 02:07:59 +0000338 bool DashDashFound = false; // Have we read '--'?
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000339 for (int i = 1; i < argc; ++i) {
340 Option *Handler = 0;
341 const char *Value = "";
342 const char *ArgName = "";
Chris Lattner331de232002-07-22 02:07:59 +0000343
344 // Check to see if this is a positional argument. This argument is
345 // considered to be positional if it doesn't start with '-', if it is "-"
Misha Brukman1115e042003-07-10 21:38:28 +0000346 // itself, or if we have seen "--" already.
Chris Lattner331de232002-07-22 02:07:59 +0000347 //
348 if (argv[i][0] != '-' || argv[i][1] == 0 || DashDashFound) {
349 // Positional argument!
Chris Lattner9cf3d472003-07-30 17:34:02 +0000350 if (ActivePositionalArg) {
Reid Spencer1e13fd22004-08-13 19:47:30 +0000351 ProvidePositionalOption(ActivePositionalArg, argv[i], i);
Chris Lattner9cf3d472003-07-30 17:34:02 +0000352 continue; // We are done!
353 } else if (!PositionalOpts.empty()) {
Reid Spencer1e13fd22004-08-13 19:47:30 +0000354 PositionalVals.push_back(std::make_pair(argv[i],i));
Chris Lattner331de232002-07-22 02:07:59 +0000355
356 // All of the positional arguments have been fulfulled, give the rest to
357 // the consume after option... if it's specified...
358 //
Chris Lattnerd16714b2002-07-31 16:29:43 +0000359 if (PositionalVals.size() >= NumPositionalRequired &&
Chris Lattner331de232002-07-22 02:07:59 +0000360 ConsumeAfterOpt != 0) {
361 for (++i; i < argc; ++i)
Reid Spencer1e13fd22004-08-13 19:47:30 +0000362 PositionalVals.push_back(std::make_pair(argv[i],i));
Chris Lattner331de232002-07-22 02:07:59 +0000363 break; // Handle outside of the argument processing loop...
364 }
365
366 // Delay processing positional arguments until the end...
367 continue;
368 }
Chris Lattnerbf455c22004-05-06 22:04:31 +0000369 } else if (argv[i][0] == '-' && argv[i][1] == '-' && argv[i][2] == 0 &&
370 !DashDashFound) {
371 DashDashFound = true; // This is the mythical "--"?
372 continue; // Don't try to process it as an argument itself.
373 } else if (ActivePositionalArg &&
374 (ActivePositionalArg->getMiscFlags() & PositionalEatsArgs)) {
375 // If there is a positional argument eating options, check to see if this
376 // option is another positional argument. If so, treat it as an argument,
377 // otherwise feed it to the eating positional.
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000378 ArgName = argv[i]+1;
Chris Lattnerbf455c22004-05-06 22:04:31 +0000379 Handler = LookupOption(ArgName, Value);
380 if (!Handler || Handler->getFormattingFlag() != cl::Positional) {
Reid Spencer1e13fd22004-08-13 19:47:30 +0000381 ProvidePositionalOption(ActivePositionalArg, argv[i], i);
Chris Lattnerbf455c22004-05-06 22:04:31 +0000382 continue; // We are done!
Chris Lattner331de232002-07-22 02:07:59 +0000383 }
384
Chris Lattnerbf455c22004-05-06 22:04:31 +0000385 } else { // We start with a '-', must be an argument...
386 ArgName = argv[i]+1;
387 Handler = LookupOption(ArgName, Value);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000388
Chris Lattnerbf455c22004-05-06 22:04:31 +0000389 // Check to see if this "option" is really a prefixed or grouped argument.
390 if (Handler == 0 && *Value == 0) {
391 std::string RealName(ArgName);
392 if (RealName.size() > 1) {
Chris Lattner331de232002-07-22 02:07:59 +0000393 unsigned Length = 0;
394 Option *PGOpt = getOptionPred(RealName, Length, isPrefixedOrGrouping);
Chris Lattnerbf455c22004-05-06 22:04:31 +0000395
Chris Lattner331de232002-07-22 02:07:59 +0000396 // If the option is a prefixed option, then the value is simply the
397 // rest of the name... so fall through to later processing, by
398 // setting up the argument name flags and value fields.
399 //
400 if (PGOpt && PGOpt->getFormattingFlag() == cl::Prefix) {
Chris Lattnerbf455c22004-05-06 22:04:31 +0000401 Value = ArgName+Length;
402 assert(Opts.find(std::string(ArgName, Value)) != Opts.end() &&
403 Opts.find(std::string(ArgName, Value))->second == PGOpt);
404 Handler = PGOpt;
Chris Lattner331de232002-07-22 02:07:59 +0000405 } else if (PGOpt) {
Chris Lattnerbf455c22004-05-06 22:04:31 +0000406 // This must be a grouped option... handle them now.
Chris Lattner331de232002-07-22 02:07:59 +0000407 assert(isGrouping(PGOpt) && "Broken getOptionPred!");
Chris Lattnerbf455c22004-05-06 22:04:31 +0000408
Chris Lattner331de232002-07-22 02:07:59 +0000409 do {
410 // Move current arg name out of RealName into RealArgName...
Chris Lattnerbf455c22004-05-06 22:04:31 +0000411 std::string RealArgName(RealName.begin(),
412 RealName.begin() + Length);
413 RealName.erase(RealName.begin(), RealName.begin() + Length);
414
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000415 // Because ValueRequired is an invalid flag for grouped arguments,
416 // we don't need to pass argc/argv in...
417 //
Chris Lattner331de232002-07-22 02:07:59 +0000418 assert(PGOpt->getValueExpectedFlag() != cl::ValueRequired &&
419 "Option can not be cl::Grouping AND cl::ValueRequired!");
420 int Dummy;
Chris Lattnerbf455c22004-05-06 22:04:31 +0000421 ErrorParsing |= ProvideOption(PGOpt, RealArgName.c_str(),
422 "", 0, 0, Dummy);
423
Chris Lattner331de232002-07-22 02:07:59 +0000424 // Get the next grouping option...
Chris Lattnerbf455c22004-05-06 22:04:31 +0000425 PGOpt = getOptionPred(RealName, Length, isGrouping);
426 } while (PGOpt && Length != RealName.size());
427
428 Handler = PGOpt; // Ate all of the options.
Chris Lattner331de232002-07-22 02:07:59 +0000429 }
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000430 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000431 }
432 }
433
434 if (Handler == 0) {
Brian Gaekec86e84b2003-09-16 18:00:35 +0000435 std::cerr << "Unknown command line argument '" << argv[i] << "'. Try: '"
Chris Lattnerca6433f2003-05-22 20:06:43 +0000436 << argv[0] << " --help'\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000437 ErrorParsing = true;
438 continue;
439 }
440
Chris Lattner72fb8e52003-05-22 20:26:17 +0000441 // Check to see if this option accepts a comma separated list of values. If
442 // it does, we have to split up the value into multiple values...
443 if (Handler->getMiscFlags() & CommaSeparated) {
444 std::string Val(Value);
445 std::string::size_type Pos = Val.find(',');
446
447 while (Pos != std::string::npos) {
448 // Process the portion before the comma...
449 ErrorParsing |= ProvideOption(Handler, ArgName,
450 std::string(Val.begin(),
451 Val.begin()+Pos).c_str(),
452 argc, argv, i);
453 // Erase the portion before the comma, AND the comma...
454 Val.erase(Val.begin(), Val.begin()+Pos+1);
455 Value += Pos+1; // Increment the original value pointer as well...
456
457 // Check for another comma...
458 Pos = Val.find(',');
459 }
460 }
Chris Lattner9cf3d472003-07-30 17:34:02 +0000461
462 // If this is a named positional argument, just remember that it is the
463 // active one...
464 if (Handler->getFormattingFlag() == cl::Positional)
465 ActivePositionalArg = Handler;
466 else
467 ErrorParsing |= ProvideOption(Handler, ArgName, Value, argc, argv, i);
Chris Lattner331de232002-07-22 02:07:59 +0000468 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000469
Chris Lattner331de232002-07-22 02:07:59 +0000470 // Check and handle positional arguments now...
471 if (NumPositionalRequired > PositionalVals.size()) {
Chris Lattnerca6433f2003-05-22 20:06:43 +0000472 std::cerr << "Not enough positional command line arguments specified!\n"
473 << "Must specify at least " << NumPositionalRequired
474 << " positional arguments: See: " << argv[0] << " --help\n";
Chris Lattner331de232002-07-22 02:07:59 +0000475 ErrorParsing = true;
476
477
478 } else if (ConsumeAfterOpt == 0) {
479 // Positional args have already been handled if ConsumeAfter is specified...
480 unsigned ValNo = 0, NumVals = PositionalVals.size();
481 for (unsigned i = 0, e = PositionalOpts.size(); i != e; ++i) {
482 if (RequiresValue(PositionalOpts[i])) {
Reid Spencer1e13fd22004-08-13 19:47:30 +0000483 ProvidePositionalOption(PositionalOpts[i], PositionalVals[ValNo].first,
484 PositionalVals[ValNo].second);
485 ValNo++;
Chris Lattner331de232002-07-22 02:07:59 +0000486 --NumPositionalRequired; // We fulfilled our duty...
487 }
488
489 // If we _can_ give this option more arguments, do so now, as long as we
490 // do not give it values that others need. 'Done' controls whether the
491 // option even _WANTS_ any more.
492 //
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000493 bool Done = PositionalOpts[i]->getNumOccurrencesFlag() == cl::Required;
Chris Lattner331de232002-07-22 02:07:59 +0000494 while (NumVals-ValNo > NumPositionalRequired && !Done) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000495 switch (PositionalOpts[i]->getNumOccurrencesFlag()) {
Chris Lattner331de232002-07-22 02:07:59 +0000496 case cl::Optional:
497 Done = true; // Optional arguments want _at most_ one value
498 // FALL THROUGH
499 case cl::ZeroOrMore: // Zero or more will take all they can get...
500 case cl::OneOrMore: // One or more will take all they can get...
Reid Spencer1e13fd22004-08-13 19:47:30 +0000501 ProvidePositionalOption(PositionalOpts[i],
502 PositionalVals[ValNo].first,
503 PositionalVals[ValNo].second);
504 ValNo++;
Chris Lattner331de232002-07-22 02:07:59 +0000505 break;
506 default:
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000507 assert(0 && "Internal error, unexpected NumOccurrences flag in "
Chris Lattner331de232002-07-22 02:07:59 +0000508 "positional argument processing!");
509 }
510 }
Chris Lattnercaccd762001-10-27 05:54:17 +0000511 }
Chris Lattner331de232002-07-22 02:07:59 +0000512 } else {
513 assert(ConsumeAfterOpt && NumPositionalRequired <= PositionalVals.size());
514 unsigned ValNo = 0;
515 for (unsigned j = 1, e = PositionalOpts.size(); j != e; ++j)
Reid Spencer1e13fd22004-08-13 19:47:30 +0000516 if (RequiresValue(PositionalOpts[j])) {
Chris Lattnerfaba8092002-07-24 20:15:13 +0000517 ErrorParsing |= ProvidePositionalOption(PositionalOpts[j],
Reid Spencer1e13fd22004-08-13 19:47:30 +0000518 PositionalVals[ValNo].first,
519 PositionalVals[ValNo].second);
520 ValNo++;
521 }
Chris Lattnerfaba8092002-07-24 20:15:13 +0000522
523 // Handle the case where there is just one positional option, and it's
524 // optional. In this case, we want to give JUST THE FIRST option to the
525 // positional option and keep the rest for the consume after. The above
526 // loop would have assigned no values to positional options in this case.
527 //
Reid Spencer1e13fd22004-08-13 19:47:30 +0000528 if (PositionalOpts.size() == 2 && ValNo == 0 && !PositionalVals.empty()) {
Chris Lattnerfaba8092002-07-24 20:15:13 +0000529 ErrorParsing |= ProvidePositionalOption(PositionalOpts[1],
Reid Spencer1e13fd22004-08-13 19:47:30 +0000530 PositionalVals[ValNo].first,
531 PositionalVals[ValNo].second);
532 ValNo++;
533 }
Chris Lattner331de232002-07-22 02:07:59 +0000534
535 // Handle over all of the rest of the arguments to the
536 // cl::ConsumeAfter command line option...
537 for (; ValNo != PositionalVals.size(); ++ValNo)
538 ErrorParsing |= ProvidePositionalOption(ConsumeAfterOpt,
Reid Spencer1e13fd22004-08-13 19:47:30 +0000539 PositionalVals[ValNo].first,
540 PositionalVals[ValNo].second);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000541 }
542
Chris Lattnerdc4693d2001-07-23 23:02:45 +0000543 // Loop over args and make sure all required args are specified!
Chris Lattnerca6433f2003-05-22 20:06:43 +0000544 for (std::map<std::string, Option*>::iterator I = Opts.begin(),
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000545 E = Opts.end(); I != E; ++I) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000546 switch (I->second->getNumOccurrencesFlag()) {
Chris Lattnerdc4693d2001-07-23 23:02:45 +0000547 case Required:
548 case OneOrMore:
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000549 if (I->second->getNumOccurrences() == 0) {
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000550 I->second->error(" must be specified at least once!");
Chris Lattnerf038acb2001-10-24 06:21:56 +0000551 ErrorParsing = true;
552 }
Chris Lattnerdc4693d2001-07-23 23:02:45 +0000553 // Fall through
554 default:
555 break;
556 }
557 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000558
Chris Lattner331de232002-07-22 02:07:59 +0000559 // Free all of the memory allocated to the map. Command line options may only
560 // be processed once!
Chris Lattnere8e258b2002-07-29 20:58:42 +0000561 delete CommandLineOptions;
562 CommandLineOptions = 0;
Chris Lattner331de232002-07-22 02:07:59 +0000563 PositionalOpts.clear();
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000564
565 // If we had an error processing our arguments, don't let the program execute
566 if (ErrorParsing) exit(1);
567}
568
569//===----------------------------------------------------------------------===//
570// Option Base class implementation
571//
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000572
Chris Lattnerca6433f2003-05-22 20:06:43 +0000573bool Option::error(std::string Message, const char *ArgName) {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000574 if (ArgName == 0) ArgName = ArgStr;
Chris Lattner331de232002-07-22 02:07:59 +0000575 if (ArgName[0] == 0)
Chris Lattnerca6433f2003-05-22 20:06:43 +0000576 std::cerr << HelpStr; // Be nice for positional arguments
Chris Lattner331de232002-07-22 02:07:59 +0000577 else
Chris Lattnerca6433f2003-05-22 20:06:43 +0000578 std::cerr << "-" << ArgName;
579 std::cerr << " option" << Message << "\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000580 return true;
581}
582
Reid Spencer1e13fd22004-08-13 19:47:30 +0000583bool Option::addOccurrence(unsigned pos, const char *ArgName, const std::string &Value) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000584 NumOccurrences++; // Increment the number of times we have been seen
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000585
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000586 switch (getNumOccurrencesFlag()) {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000587 case Optional:
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000588 if (NumOccurrences > 1)
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000589 return error(": may only occur zero or one times!", ArgName);
590 break;
591 case Required:
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000592 if (NumOccurrences > 1)
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000593 return error(": must occur exactly one time!", ArgName);
594 // Fall through
595 case OneOrMore:
Chris Lattnercaccd762001-10-27 05:54:17 +0000596 case ZeroOrMore:
597 case ConsumeAfter: break;
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000598 default: return error(": bad num occurrences flag value!");
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000599 }
600
Reid Spencer1e13fd22004-08-13 19:47:30 +0000601 return handleOccurrence(pos, ArgName, Value);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000602}
603
Chris Lattner331de232002-07-22 02:07:59 +0000604// addArgument - Tell the system that this Option subclass will handle all
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000605// occurrences of -ArgStr on the command line.
Chris Lattner331de232002-07-22 02:07:59 +0000606//
607void Option::addArgument(const char *ArgStr) {
608 if (ArgStr[0])
609 AddArgument(ArgStr, this);
Chris Lattner9cf3d472003-07-30 17:34:02 +0000610
611 if (getFormattingFlag() == Positional)
Chris Lattner331de232002-07-22 02:07:59 +0000612 getPositionalOpts().push_back(this);
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000613 else if (getNumOccurrencesFlag() == ConsumeAfter) {
Chris Lattner9cf3d472003-07-30 17:34:02 +0000614 if (!getPositionalOpts().empty() &&
615 getPositionalOpts().front()->getNumOccurrencesFlag() == ConsumeAfter)
616 error("Cannot specify more than one option with cl::ConsumeAfter!");
Chris Lattner331de232002-07-22 02:07:59 +0000617 getPositionalOpts().insert(getPositionalOpts().begin(), this);
618 }
619}
620
Chris Lattneraa852bb2002-07-23 17:15:12 +0000621void Option::removeArgument(const char *ArgStr) {
Chris Lattner9cf3d472003-07-30 17:34:02 +0000622 if (ArgStr[0])
Chris Lattnere8e258b2002-07-29 20:58:42 +0000623 RemoveArgument(ArgStr, this);
Chris Lattner9cf3d472003-07-30 17:34:02 +0000624
625 if (getFormattingFlag() == Positional) {
Chris Lattnerca6433f2003-05-22 20:06:43 +0000626 std::vector<Option*>::iterator I =
Chris Lattneraa852bb2002-07-23 17:15:12 +0000627 std::find(getPositionalOpts().begin(), getPositionalOpts().end(), this);
628 assert(I != getPositionalOpts().end() && "Arg not registered!");
629 getPositionalOpts().erase(I);
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000630 } else if (getNumOccurrencesFlag() == ConsumeAfter) {
Chris Lattneraa852bb2002-07-23 17:15:12 +0000631 assert(!getPositionalOpts().empty() && getPositionalOpts()[0] == this &&
632 "Arg not registered correctly!");
633 getPositionalOpts().erase(getPositionalOpts().begin());
634 }
635}
636
Chris Lattner331de232002-07-22 02:07:59 +0000637
638// getValueStr - Get the value description string, using "DefaultMsg" if nothing
639// has been specified yet.
640//
641static const char *getValueStr(const Option &O, const char *DefaultMsg) {
642 if (O.ValueStr[0] == 0) return DefaultMsg;
643 return O.ValueStr;
644}
645
646//===----------------------------------------------------------------------===//
647// cl::alias class implementation
648//
649
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000650// Return the width of the option tag for printing...
Chris Lattner331de232002-07-22 02:07:59 +0000651unsigned alias::getOptionWidth() const {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000652 return std::strlen(ArgStr)+6;
653}
654
Chris Lattner331de232002-07-22 02:07:59 +0000655// Print out the option for the alias...
656void alias::printOptionInfo(unsigned GlobalWidth) const {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000657 unsigned L = std::strlen(ArgStr);
Chris Lattnerca6433f2003-05-22 20:06:43 +0000658 std::cerr << " -" << ArgStr << std::string(GlobalWidth-L-6, ' ') << " - "
659 << HelpStr << "\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000660}
661
662
Chris Lattner331de232002-07-22 02:07:59 +0000663
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000664//===----------------------------------------------------------------------===//
Chris Lattner331de232002-07-22 02:07:59 +0000665// Parser Implementation code...
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000666//
667
Chris Lattner9b14eb52002-08-07 18:36:37 +0000668// basic_parser implementation
669//
670
671// Return the width of the option tag for printing...
672unsigned basic_parser_impl::getOptionWidth(const Option &O) const {
673 unsigned Len = std::strlen(O.ArgStr);
674 if (const char *ValName = getValueName())
675 Len += std::strlen(getValueStr(O, ValName))+3;
676
677 return Len + 6;
678}
679
680// printOptionInfo - Print out information about this option. The
681// to-be-maintained width is specified.
682//
683void basic_parser_impl::printOptionInfo(const Option &O,
684 unsigned GlobalWidth) const {
Chris Lattnerca6433f2003-05-22 20:06:43 +0000685 std::cerr << " -" << O.ArgStr;
Chris Lattner9b14eb52002-08-07 18:36:37 +0000686
687 if (const char *ValName = getValueName())
Chris Lattnerca6433f2003-05-22 20:06:43 +0000688 std::cerr << "=<" << getValueStr(O, ValName) << ">";
Chris Lattner9b14eb52002-08-07 18:36:37 +0000689
Chris Lattnerca6433f2003-05-22 20:06:43 +0000690 std::cerr << std::string(GlobalWidth-getOptionWidth(O), ' ') << " - "
691 << O.HelpStr << "\n";
Chris Lattner9b14eb52002-08-07 18:36:37 +0000692}
693
694
695
696
Chris Lattner331de232002-07-22 02:07:59 +0000697// parser<bool> implementation
698//
Chris Lattner9b14eb52002-08-07 18:36:37 +0000699bool parser<bool>::parse(Option &O, const char *ArgName,
Chris Lattnerca6433f2003-05-22 20:06:43 +0000700 const std::string &Arg, bool &Value) {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000701 if (Arg == "" || Arg == "true" || Arg == "TRUE" || Arg == "True" ||
702 Arg == "1") {
703 Value = true;
704 } else if (Arg == "false" || Arg == "FALSE" || Arg == "False" || Arg == "0") {
705 Value = false;
706 } else {
Chris Lattner331de232002-07-22 02:07:59 +0000707 return O.error(": '" + Arg +
708 "' is invalid value for boolean argument! Try 0 or 1");
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000709 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000710 return false;
711}
712
Chris Lattner331de232002-07-22 02:07:59 +0000713// parser<int> implementation
714//
Chris Lattner9b14eb52002-08-07 18:36:37 +0000715bool parser<int>::parse(Option &O, const char *ArgName,
Chris Lattnerca6433f2003-05-22 20:06:43 +0000716 const std::string &Arg, int &Value) {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000717 char *End;
Chris Lattnerd2a6fc32003-06-28 15:47:20 +0000718 Value = (int)strtol(Arg.c_str(), &End, 0);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000719 if (*End != 0)
Chris Lattner331de232002-07-22 02:07:59 +0000720 return O.error(": '" + Arg + "' value invalid for integer argument!");
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000721 return false;
722}
723
Chris Lattnerd2a6fc32003-06-28 15:47:20 +0000724// parser<unsigned> implementation
725//
726bool parser<unsigned>::parse(Option &O, const char *ArgName,
727 const std::string &Arg, unsigned &Value) {
728 char *End;
Brian Gaeke2d6a2362003-10-10 17:01:36 +0000729 errno = 0;
730 unsigned long V = strtoul(Arg.c_str(), &End, 0);
Chris Lattnerd2a6fc32003-06-28 15:47:20 +0000731 Value = (unsigned)V;
Brian Gaeke2d6a2362003-10-10 17:01:36 +0000732 if (((V == ULONG_MAX) && (errno == ERANGE))
733 || (*End != 0)
734 || (Value != V))
Chris Lattnerd2a6fc32003-06-28 15:47:20 +0000735 return O.error(": '" + Arg + "' value invalid for uint argument!");
736 return false;
737}
738
Chris Lattner9b14eb52002-08-07 18:36:37 +0000739// parser<double>/parser<float> implementation
Chris Lattnerd215fd12001-10-13 06:53:19 +0000740//
Chris Lattnerca6433f2003-05-22 20:06:43 +0000741static bool parseDouble(Option &O, const std::string &Arg, double &Value) {
Chris Lattner331de232002-07-22 02:07:59 +0000742 const char *ArgStart = Arg.c_str();
743 char *End;
744 Value = strtod(ArgStart, &End);
745 if (*End != 0)
746 return O.error(": '" +Arg+ "' value invalid for floating point argument!");
Chris Lattnerd215fd12001-10-13 06:53:19 +0000747 return false;
748}
749
Chris Lattner9b14eb52002-08-07 18:36:37 +0000750bool parser<double>::parse(Option &O, const char *AN,
751 const std::string &Arg, double &Val) {
752 return parseDouble(O, Arg, Val);
Chris Lattner331de232002-07-22 02:07:59 +0000753}
754
Chris Lattner9b14eb52002-08-07 18:36:37 +0000755bool parser<float>::parse(Option &O, const char *AN,
756 const std::string &Arg, float &Val) {
757 double dVal;
758 if (parseDouble(O, Arg, dVal))
759 return true;
760 Val = (float)dVal;
761 return false;
Chris Lattner331de232002-07-22 02:07:59 +0000762}
763
764
Chris Lattner331de232002-07-22 02:07:59 +0000765
766// generic_parser_base implementation
767//
768
Chris Lattneraa852bb2002-07-23 17:15:12 +0000769// findOption - Return the option number corresponding to the specified
770// argument string. If the option is not found, getNumOptions() is returned.
771//
772unsigned generic_parser_base::findOption(const char *Name) {
773 unsigned i = 0, e = getNumOptions();
Chris Lattnerca6433f2003-05-22 20:06:43 +0000774 std::string N(Name);
Chris Lattneraa852bb2002-07-23 17:15:12 +0000775
776 while (i != e)
777 if (getOption(i) == N)
778 return i;
779 else
780 ++i;
781 return e;
782}
783
784
Chris Lattner331de232002-07-22 02:07:59 +0000785// Return the width of the option tag for printing...
786unsigned generic_parser_base::getOptionWidth(const Option &O) const {
787 if (O.hasArgStr()) {
788 unsigned Size = std::strlen(O.ArgStr)+6;
789 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
790 Size = std::max(Size, (unsigned)std::strlen(getOption(i))+8);
791 return Size;
792 } else {
793 unsigned BaseSize = 0;
794 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
795 BaseSize = std::max(BaseSize, (unsigned)std::strlen(getOption(i))+8);
796 return BaseSize;
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000797 }
798}
799
Chris Lattner331de232002-07-22 02:07:59 +0000800// printOptionInfo - Print out information about this option. The
801// to-be-maintained width is specified.
802//
803void generic_parser_base::printOptionInfo(const Option &O,
804 unsigned GlobalWidth) const {
805 if (O.hasArgStr()) {
806 unsigned L = std::strlen(O.ArgStr);
Chris Lattnerca6433f2003-05-22 20:06:43 +0000807 std::cerr << " -" << O.ArgStr << std::string(GlobalWidth-L-6, ' ')
808 << " - " << O.HelpStr << "\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000809
Chris Lattner331de232002-07-22 02:07:59 +0000810 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
811 unsigned NumSpaces = GlobalWidth-strlen(getOption(i))-8;
Chris Lattnerca6433f2003-05-22 20:06:43 +0000812 std::cerr << " =" << getOption(i) << std::string(NumSpaces, ' ')
813 << " - " << getDescription(i) << "\n";
Chris Lattner9c9be482002-01-31 00:42:56 +0000814 }
Chris Lattner331de232002-07-22 02:07:59 +0000815 } else {
816 if (O.HelpStr[0])
Chris Lattnerca6433f2003-05-22 20:06:43 +0000817 std::cerr << " " << O.HelpStr << "\n";
Chris Lattner331de232002-07-22 02:07:59 +0000818 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
819 unsigned L = std::strlen(getOption(i));
Chris Lattnerca6433f2003-05-22 20:06:43 +0000820 std::cerr << " -" << getOption(i) << std::string(GlobalWidth-L-8, ' ')
821 << " - " << getDescription(i) << "\n";
Chris Lattner331de232002-07-22 02:07:59 +0000822 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000823 }
824}
825
826
827//===----------------------------------------------------------------------===//
Chris Lattner331de232002-07-22 02:07:59 +0000828// --help and --help-hidden option implementation
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000829//
830namespace {
831
Chris Lattner331de232002-07-22 02:07:59 +0000832class HelpPrinter {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000833 unsigned MaxArgLen;
834 const Option *EmptyArg;
835 const bool ShowHidden;
836
Chris Lattner331de232002-07-22 02:07:59 +0000837 // isHidden/isReallyHidden - Predicates to be used to filter down arg lists.
Chris Lattnerca6433f2003-05-22 20:06:43 +0000838 inline static bool isHidden(std::pair<std::string, Option *> &OptPair) {
Chris Lattner331de232002-07-22 02:07:59 +0000839 return OptPair.second->getOptionHiddenFlag() >= Hidden;
840 }
Chris Lattnerca6433f2003-05-22 20:06:43 +0000841 inline static bool isReallyHidden(std::pair<std::string, Option *> &OptPair) {
Chris Lattner331de232002-07-22 02:07:59 +0000842 return OptPair.second->getOptionHiddenFlag() == ReallyHidden;
843 }
844
845public:
846 HelpPrinter(bool showHidden) : ShowHidden(showHidden) {
847 EmptyArg = 0;
848 }
849
850 void operator=(bool Value) {
851 if (Value == false) return;
852
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000853 // Copy Options into a vector so we can sort them as we like...
Chris Lattnerca6433f2003-05-22 20:06:43 +0000854 std::vector<std::pair<std::string, Option*> > Options;
Chris Lattner697954c2002-01-20 22:54:45 +0000855 copy(getOpts().begin(), getOpts().end(), std::back_inserter(Options));
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000856
857 // Eliminate Hidden or ReallyHidden arguments, depending on ShowHidden
Chris Lattner331de232002-07-22 02:07:59 +0000858 Options.erase(std::remove_if(Options.begin(), Options.end(),
859 std::ptr_fun(ShowHidden ? isReallyHidden : isHidden)),
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000860 Options.end());
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000861
862 // Eliminate duplicate entries in table (from enum flags options, f.e.)
Chris Lattner331de232002-07-22 02:07:59 +0000863 { // Give OptionSet a scope
864 std::set<Option*> OptionSet;
865 for (unsigned i = 0; i != Options.size(); ++i)
866 if (OptionSet.count(Options[i].second) == 0)
867 OptionSet.insert(Options[i].second); // Add new entry to set
868 else
869 Options.erase(Options.begin()+i--); // Erase duplicate
870 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000871
872 if (ProgramOverview)
Chris Lattnerca6433f2003-05-22 20:06:43 +0000873 std::cerr << "OVERVIEW:" << ProgramOverview << "\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000874
Chris Lattnerca6433f2003-05-22 20:06:43 +0000875 std::cerr << "USAGE: " << ProgramName << " [options]";
Chris Lattner331de232002-07-22 02:07:59 +0000876
877 // Print out the positional options...
Chris Lattnerca6433f2003-05-22 20:06:43 +0000878 std::vector<Option*> &PosOpts = getPositionalOpts();
Chris Lattner331de232002-07-22 02:07:59 +0000879 Option *CAOpt = 0; // The cl::ConsumeAfter option, if it exists...
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000880 if (!PosOpts.empty() && PosOpts[0]->getNumOccurrencesFlag() == ConsumeAfter)
Chris Lattner331de232002-07-22 02:07:59 +0000881 CAOpt = PosOpts[0];
882
Chris Lattner9cf3d472003-07-30 17:34:02 +0000883 for (unsigned i = CAOpt != 0, e = PosOpts.size(); i != e; ++i) {
884 if (PosOpts[i]->ArgStr[0])
885 std::cerr << " --" << PosOpts[i]->ArgStr;
Chris Lattnerca6433f2003-05-22 20:06:43 +0000886 std::cerr << " " << PosOpts[i]->HelpStr;
Chris Lattner9cf3d472003-07-30 17:34:02 +0000887 }
Chris Lattner331de232002-07-22 02:07:59 +0000888
889 // Print the consume after option info if it exists...
Chris Lattnerca6433f2003-05-22 20:06:43 +0000890 if (CAOpt) std::cerr << " " << CAOpt->HelpStr;
Chris Lattner331de232002-07-22 02:07:59 +0000891
Chris Lattnerca6433f2003-05-22 20:06:43 +0000892 std::cerr << "\n\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000893
894 // Compute the maximum argument length...
895 MaxArgLen = 0;
Chris Lattner331de232002-07-22 02:07:59 +0000896 for (unsigned i = 0, e = Options.size(); i != e; ++i)
897 MaxArgLen = std::max(MaxArgLen, Options[i].second->getOptionWidth());
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000898
Chris Lattnerca6433f2003-05-22 20:06:43 +0000899 std::cerr << "OPTIONS:\n";
Chris Lattner331de232002-07-22 02:07:59 +0000900 for (unsigned i = 0, e = Options.size(); i != e; ++i)
901 Options[i].second->printOptionInfo(MaxArgLen);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000902
Chris Lattner331de232002-07-22 02:07:59 +0000903 // Halt the program if help information is printed
904 exit(1);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000905 }
906};
907
Reid Spencer69105f32004-08-04 00:36:06 +0000908class VersionPrinter {
909public:
910 void operator=(bool OptionWasSpecified) {
911 if (OptionWasSpecified) {
912 std::cerr << "Low Level Virtual Machine (" << PACKAGE_NAME << ") "
913 << PACKAGE_VERSION << " (see http://llvm.org/)\n";
914 exit(1);
915 }
916 }
917};
Chris Lattner331de232002-07-22 02:07:59 +0000918
919
920// Define the two HelpPrinter instances that are used to print out help, or
921// help-hidden...
922//
923HelpPrinter NormalPrinter(false);
924HelpPrinter HiddenPrinter(true);
925
926cl::opt<HelpPrinter, true, parser<bool> >
927HOp("help", cl::desc("display available options (--help-hidden for more)"),
Chris Lattner9b14eb52002-08-07 18:36:37 +0000928 cl::location(NormalPrinter), cl::ValueDisallowed);
Chris Lattner331de232002-07-22 02:07:59 +0000929
930cl::opt<HelpPrinter, true, parser<bool> >
931HHOp("help-hidden", cl::desc("display all available options"),
Chris Lattner9b14eb52002-08-07 18:36:37 +0000932 cl::location(HiddenPrinter), cl::Hidden, cl::ValueDisallowed);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000933
Reid Spencer69105f32004-08-04 00:36:06 +0000934// Define the --version option that prints out the LLVM version for the tool
935VersionPrinter VersionPrinterInstance;
936cl::opt<VersionPrinter, true, parser<bool> >
937VersOp("version", cl::desc("display the version"),
938 cl::location(VersionPrinterInstance), cl::ValueDisallowed);
939
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000940} // End anonymous namespace