blob: 09038d3a5f3826e2dd0844c44550ffd3a8cbd8cc [file] [log] [blame]
Chris Lattnerdbab15a2001-07-23 17:17:47 +00001//===-- CommandLine.cpp - Command line parser implementation --------------===//
Misha Brukmanf976c852005-04-21 22:55:34 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// 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.
Misha Brukmanf976c852005-04-21 22:55:34 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
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 Spencer551ccae2004-09-01 22:55:40 +000019#include "llvm/Config/config.h"
20#include "llvm/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
Reid Spencere1cc1502004-09-01 04:41:28 +000032// Globals for name and overview of program
33static const char *ProgramName = "<unknown>";
34static const char *ProgramOverview = 0;
35
Chris Lattnerc540ebb2004-11-19 17:08:15 +000036// This collects additional help to be printed.
37static std::vector<const char*> &MoreHelp() {
38 static std::vector<const char*> moreHelp;
39 return moreHelp;
40}
41
42extrahelp::extrahelp(const char* Help)
43 : morehelp(Help) {
44 MoreHelp().push_back(Help);
45}
46
Chris Lattner331de232002-07-22 02:07:59 +000047//===----------------------------------------------------------------------===//
48// Basic, shared command line option processing machinery...
49//
50
Chris Lattnerdbab15a2001-07-23 17:17:47 +000051// Return the global command line option vector. Making it a function scoped
Chris Lattnerf78032f2001-11-26 18:58:34 +000052// static ensures that it will be initialized correctly before its first use.
Chris Lattnerdbab15a2001-07-23 17:17:47 +000053//
Chris Lattnerca6433f2003-05-22 20:06:43 +000054static std::map<std::string, Option*> &getOpts() {
Chris Lattnerc540ebb2004-11-19 17:08:15 +000055 static std::map<std::string, Option*> CommandLineOptions;
56 return CommandLineOptions;
Chris Lattnere8e258b2002-07-29 20:58:42 +000057}
58
Chris Lattnerca6433f2003-05-22 20:06:43 +000059static Option *getOption(const std::string &Str) {
Chris Lattnerc540ebb2004-11-19 17:08:15 +000060 std::map<std::string,Option*>::iterator I = getOpts().find(Str);
61 return I != getOpts().end() ? I->second : 0;
Chris Lattnerdbab15a2001-07-23 17:17:47 +000062}
63
Chris Lattnerca6433f2003-05-22 20:06:43 +000064static std::vector<Option*> &getPositionalOpts() {
Chris Lattnerc540ebb2004-11-19 17:08:15 +000065 static std::vector<Option*> Positional;
66 return Positional;
Chris Lattner331de232002-07-22 02:07:59 +000067}
68
Chris Lattnere8e258b2002-07-29 20:58:42 +000069static void AddArgument(const char *ArgName, Option *Opt) {
70 if (getOption(ArgName)) {
Misha Brukmanf976c852005-04-21 22:55:34 +000071 std::cerr << ProgramName << ": CommandLine Error: Argument '"
Reid Spencere1cc1502004-09-01 04:41:28 +000072 << ArgName << "' defined more than once!\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +000073 } else {
Chris Lattnerf78032f2001-11-26 18:58:34 +000074 // Add argument to the argument map!
Chris Lattnere8e258b2002-07-29 20:58:42 +000075 getOpts()[ArgName] = Opt;
76 }
77}
78
79// RemoveArgument - It's possible that the argument is no longer in the map if
80// options have already been processed and the map has been deleted!
Misha Brukmanf976c852005-04-21 22:55:34 +000081//
Chris Lattnere8e258b2002-07-29 20:58:42 +000082static void RemoveArgument(const char *ArgName, Option *Opt) {
Tanya Lattnerc4ae8e92004-11-20 23:35:20 +000083 if(getOpts().empty()) return;
84
Chris Lattnerf98cfc72004-07-18 21:56:20 +000085#ifndef NDEBUG
86 // This disgusting HACK is brought to you courtesy of GCC 3.3.2, which ICE's
87 // If we pass ArgName directly into getOption here.
88 std::string Tmp = ArgName;
89 assert(getOption(Tmp) == Opt && "Arg not in map!");
90#endif
Chris Lattnerc540ebb2004-11-19 17:08:15 +000091 getOpts().erase(ArgName);
Chris Lattnerdbab15a2001-07-23 17:17:47 +000092}
93
Chris Lattnercaccd762001-10-27 05:54:17 +000094static inline bool ProvideOption(Option *Handler, const char *ArgName,
95 const char *Value, int argc, char **argv,
96 int &i) {
97 // Enforce value requirements
98 switch (Handler->getValueExpectedFlag()) {
99 case ValueRequired:
Chris Lattner6d5857e2005-05-10 23:20:17 +0000100 if (Value == 0) { // No value specified?
Chris Lattnercaccd762001-10-27 05:54:17 +0000101 if (i+1 < argc) { // Steal the next argument, like for '-o filename'
102 Value = argv[++i];
103 } else {
104 return Handler->error(" requires a value!");
105 }
106 }
107 break;
108 case ValueDisallowed:
Chris Lattner6d5857e2005-05-10 23:20:17 +0000109 if (Value)
Misha Brukmanf976c852005-04-21 22:55:34 +0000110 return Handler->error(" does not allow a value! '" +
Chris Lattnerca6433f2003-05-22 20:06:43 +0000111 std::string(Value) + "' specified.");
Chris Lattnercaccd762001-10-27 05:54:17 +0000112 break;
Misha Brukmanf976c852005-04-21 22:55:34 +0000113 case ValueOptional:
Reid Spencere1cc1502004-09-01 04:41:28 +0000114 break;
Misha Brukmanf976c852005-04-21 22:55:34 +0000115 default:
116 std::cerr << ProgramName
117 << ": Bad ValueMask flag! CommandLine usage error:"
118 << Handler->getValueExpectedFlag() << "\n";
Reid Spencere1cc1502004-09-01 04:41:28 +0000119 abort();
120 break;
Chris Lattnercaccd762001-10-27 05:54:17 +0000121 }
122
123 // Run the handler now!
Chris Lattner6d5857e2005-05-10 23:20:17 +0000124 return Handler->addOccurrence(i, ArgName, Value ? Value : "");
Chris Lattnercaccd762001-10-27 05:54:17 +0000125}
126
Misha Brukmanf976c852005-04-21 22:55:34 +0000127static bool ProvidePositionalOption(Option *Handler, const std::string &Arg,
Reid Spencer1e13fd22004-08-13 19:47:30 +0000128 int i) {
129 int Dummy = i;
Chris Lattner9cf3d472003-07-30 17:34:02 +0000130 return ProvideOption(Handler, Handler->ArgStr, Arg.c_str(), 0, 0, Dummy);
Chris Lattner331de232002-07-22 02:07:59 +0000131}
Chris Lattnerf78032f2001-11-26 18:58:34 +0000132
Chris Lattner331de232002-07-22 02:07:59 +0000133
134// Option predicates...
135static inline bool isGrouping(const Option *O) {
136 return O->getFormattingFlag() == cl::Grouping;
137}
138static inline bool isPrefixedOrGrouping(const Option *O) {
139 return isGrouping(O) || O->getFormattingFlag() == cl::Prefix;
140}
141
142// getOptionPred - Check to see if there are any options that satisfy the
143// specified predicate with names that are the prefixes in Name. This is
144// checked by progressively stripping characters off of the name, checking to
145// see if there options that satisfy the predicate. If we find one, return it,
146// otherwise return null.
147//
148static Option *getOptionPred(std::string Name, unsigned &Length,
149 bool (*Pred)(const Option*)) {
Misha Brukmanf976c852005-04-21 22:55:34 +0000150
Chris Lattnere8e258b2002-07-29 20:58:42 +0000151 Option *Op = getOption(Name);
152 if (Op && Pred(Op)) {
Chris Lattner331de232002-07-22 02:07:59 +0000153 Length = Name.length();
Chris Lattnere8e258b2002-07-29 20:58:42 +0000154 return Op;
Chris Lattnerf78032f2001-11-26 18:58:34 +0000155 }
156
Chris Lattner331de232002-07-22 02:07:59 +0000157 if (Name.size() == 1) return 0;
158 do {
159 Name.erase(Name.end()-1, Name.end()); // Chop off the last character...
Chris Lattnere8e258b2002-07-29 20:58:42 +0000160 Op = getOption(Name);
Chris Lattner331de232002-07-22 02:07:59 +0000161
162 // Loop while we haven't found an option and Name still has at least two
163 // characters in it (so that the next iteration will not be the empty
164 // string...
Chris Lattnere8e258b2002-07-29 20:58:42 +0000165 } while ((Op == 0 || !Pred(Op)) && Name.size() > 1);
Chris Lattner331de232002-07-22 02:07:59 +0000166
Chris Lattnere8e258b2002-07-29 20:58:42 +0000167 if (Op && Pred(Op)) {
Chris Lattner331de232002-07-22 02:07:59 +0000168 Length = Name.length();
Chris Lattnere8e258b2002-07-29 20:58:42 +0000169 return Op; // Found one!
Chris Lattner331de232002-07-22 02:07:59 +0000170 }
171 return 0; // No option found!
172}
173
174static bool RequiresValue(const Option *O) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000175 return O->getNumOccurrencesFlag() == cl::Required ||
176 O->getNumOccurrencesFlag() == cl::OneOrMore;
Chris Lattner331de232002-07-22 02:07:59 +0000177}
178
179static bool EatsUnboundedNumberOfValues(const Option *O) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000180 return O->getNumOccurrencesFlag() == cl::ZeroOrMore ||
181 O->getNumOccurrencesFlag() == cl::OneOrMore;
Chris Lattnerf78032f2001-11-26 18:58:34 +0000182}
Chris Lattnercaccd762001-10-27 05:54:17 +0000183
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000184/// ParseCStringVector - Break INPUT up wherever one or more
185/// whitespace characters are found, and store the resulting tokens in
186/// OUTPUT. The tokens stored in OUTPUT are dynamically allocated
187/// using strdup (), so it is the caller's responsibility to free ()
188/// them later.
Brian Gaeke06b06c52003-08-14 22:00:59 +0000189///
190static void ParseCStringVector (std::vector<char *> &output,
Reid Spencer69105f32004-08-04 00:36:06 +0000191 const char *input) {
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000192 // Characters which will be treated as token separators:
193 static const char *delims = " \v\f\t\r\n";
194
195 std::string work (input);
196 // Skip past any delims at head of input string.
197 size_t pos = work.find_first_not_of (delims);
198 // If the string consists entirely of delims, then exit early.
199 if (pos == std::string::npos) return;
200 // Otherwise, jump forward to beginning of first word.
201 work = work.substr (pos);
202 // Find position of first delimiter.
203 pos = work.find_first_of (delims);
204
205 while (!work.empty() && pos != std::string::npos) {
206 // Everything from 0 to POS is the next word to copy.
207 output.push_back (strdup (work.substr (0,pos).c_str ()));
208 // Is there another word in the string?
209 size_t nextpos = work.find_first_not_of (delims, pos + 1);
210 if (nextpos != std::string::npos) {
211 // Yes? Then remove delims from beginning ...
212 work = work.substr (work.find_first_not_of (delims, pos + 1));
213 // and find the end of the word.
214 pos = work.find_first_of (delims);
215 } else {
216 // No? (Remainder of string is delims.) End the loop.
217 work = "";
218 pos = std::string::npos;
219 }
220 }
221
222 // If `input' ended with non-delim char, then we'll get here with
223 // the last word of `input' in `work'; copy it now.
224 if (!work.empty ()) {
225 output.push_back (strdup (work.c_str ()));
Brian Gaeke06b06c52003-08-14 22:00:59 +0000226 }
227}
228
229/// ParseEnvironmentOptions - An alternative entry point to the
230/// CommandLine library, which allows you to read the program's name
231/// from the caller (as PROGNAME) and its command-line arguments from
232/// an environment variable (whose name is given in ENVVAR).
233///
Chris Lattnerbf455c22004-05-06 22:04:31 +0000234void cl::ParseEnvironmentOptions(const char *progName, const char *envVar,
235 const char *Overview) {
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000236 // Check args.
Chris Lattnerbf455c22004-05-06 22:04:31 +0000237 assert(progName && "Program name not specified");
238 assert(envVar && "Environment variable name missing");
Misha Brukmanf976c852005-04-21 22:55:34 +0000239
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000240 // Get the environment variable they want us to parse options out of.
241 const char *envValue = getenv (envVar);
242 if (!envValue)
243 return;
244
Brian Gaeke06b06c52003-08-14 22:00:59 +0000245 // Get program's "name", which we wouldn't know without the caller
246 // telling us.
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000247 std::vector<char *> newArgv;
248 newArgv.push_back (strdup (progName));
Brian Gaeke06b06c52003-08-14 22:00:59 +0000249
250 // Parse the value of the environment variable into a "command line"
251 // and hand it off to ParseCommandLineOptions().
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000252 ParseCStringVector (newArgv, envValue);
253 int newArgc = newArgv.size ();
254 ParseCommandLineOptions (newArgc, &newArgv[0], Overview);
255
256 // Free all the strdup()ed strings.
257 for (std::vector<char *>::iterator i = newArgv.begin (), e = newArgv.end ();
258 i != e; ++i) {
259 free (*i);
260 }
Brian Gaeke06b06c52003-08-14 22:00:59 +0000261}
262
Chris Lattnerbf455c22004-05-06 22:04:31 +0000263/// LookupOption - Lookup the option specified by the specified option on the
264/// command line. If there is a value specified (after an equal sign) return
265/// that as well.
266static Option *LookupOption(const char *&Arg, const char *&Value) {
267 while (*Arg == '-') ++Arg; // Eat leading dashes
Misha Brukmanf976c852005-04-21 22:55:34 +0000268
Chris Lattnerbf455c22004-05-06 22:04:31 +0000269 const char *ArgEnd = Arg;
270 while (*ArgEnd && *ArgEnd != '=')
Chris Lattner6d5857e2005-05-10 23:20:17 +0000271 ++ArgEnd; // Scan till end of argument name.
Chris Lattnerbf455c22004-05-06 22:04:31 +0000272
Chris Lattner6d5857e2005-05-10 23:20:17 +0000273 if (*ArgEnd == '=') // If we have an equals sign...
274 Value = ArgEnd+1; // Get the value, not the equals
275
Misha Brukmanf976c852005-04-21 22:55:34 +0000276
Chris Lattnerbf455c22004-05-06 22:04:31 +0000277 if (*Arg == 0) return 0;
278
279 // Look up the option.
280 std::map<std::string, Option*> &Opts = getOpts();
281 std::map<std::string, Option*>::iterator I =
282 Opts.find(std::string(Arg, ArgEnd));
283 return (I != Opts.end()) ? I->second : 0;
284}
285
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000286void cl::ParseCommandLineOptions(int &argc, char **argv,
Chris Lattner0c0edf82002-07-25 06:17:51 +0000287 const char *Overview) {
Chris Lattner331de232002-07-22 02:07:59 +0000288 assert((!getOpts().empty() || !getPositionalOpts().empty()) &&
289 "No options specified, or ParseCommandLineOptions called more"
290 " than once!");
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000291 ProgramName = argv[0]; // Save this away safe and snug
292 ProgramOverview = Overview;
293 bool ErrorParsing = false;
294
Chris Lattnerca6433f2003-05-22 20:06:43 +0000295 std::map<std::string, Option*> &Opts = getOpts();
296 std::vector<Option*> &PositionalOpts = getPositionalOpts();
Chris Lattner331de232002-07-22 02:07:59 +0000297
298 // Check out the positional arguments to collect information about them.
299 unsigned NumPositionalRequired = 0;
Chris Lattnerde013242005-08-08 17:25:38 +0000300
301 // Determine whether or not there are an unlimited number of positionals
302 bool HasUnlimitedPositionals = false;
303
Chris Lattner331de232002-07-22 02:07:59 +0000304 Option *ConsumeAfterOpt = 0;
305 if (!PositionalOpts.empty()) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000306 if (PositionalOpts[0]->getNumOccurrencesFlag() == cl::ConsumeAfter) {
Chris Lattner331de232002-07-22 02:07:59 +0000307 assert(PositionalOpts.size() > 1 &&
308 "Cannot specify cl::ConsumeAfter without a positional argument!");
309 ConsumeAfterOpt = PositionalOpts[0];
310 }
311
312 // Calculate how many positional values are _required_.
313 bool UnboundedFound = false;
314 for (unsigned i = ConsumeAfterOpt != 0, e = PositionalOpts.size();
315 i != e; ++i) {
316 Option *Opt = PositionalOpts[i];
317 if (RequiresValue(Opt))
318 ++NumPositionalRequired;
319 else if (ConsumeAfterOpt) {
320 // ConsumeAfter cannot be combined with "optional" positional options
Chris Lattner54ec7ae2002-07-22 02:21:57 +0000321 // unless there is only one positional argument...
322 if (PositionalOpts.size() > 2)
323 ErrorParsing |=
324 Opt->error(" error - this positional option will never be matched, "
325 "because it does not Require a value, and a "
326 "cl::ConsumeAfter option is active!");
Chris Lattner9cf3d472003-07-30 17:34:02 +0000327 } else if (UnboundedFound && !Opt->ArgStr[0]) {
328 // This option does not "require" a value... Make sure this option is
329 // not specified after an option that eats all extra arguments, or this
330 // one will never get any!
Chris Lattner331de232002-07-22 02:07:59 +0000331 //
332 ErrorParsing |= Opt->error(" error - option can never match, because "
333 "another positional argument will match an "
334 "unbounded number of values, and this option"
335 " does not require a value!");
336 }
337 UnboundedFound |= EatsUnboundedNumberOfValues(Opt);
Chris Lattnerde013242005-08-08 17:25:38 +0000338
339 if (Opt->getNumOccurrencesFlag() == cl::ZeroOrMore
340 || Opt->getNumOccurrencesFlag() == cl::OneOrMore)
341 HasUnlimitedPositionals = true;
Chris Lattner331de232002-07-22 02:07:59 +0000342 }
343 }
344
Reid Spencer1e13fd22004-08-13 19:47:30 +0000345 // PositionalVals - A vector of "positional" arguments we accumulate into
346 // the process at the end...
Chris Lattner331de232002-07-22 02:07:59 +0000347 //
Reid Spencer1e13fd22004-08-13 19:47:30 +0000348 std::vector<std::pair<std::string,unsigned> > PositionalVals;
Chris Lattner331de232002-07-22 02:07:59 +0000349
Chris Lattner9cf3d472003-07-30 17:34:02 +0000350 // If the program has named positional arguments, and the name has been run
351 // across, keep track of which positional argument was named. Otherwise put
352 // the positional args into the PositionalVals list...
353 Option *ActivePositionalArg = 0;
354
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000355 // Loop over all of the arguments... processing them.
Chris Lattner331de232002-07-22 02:07:59 +0000356 bool DashDashFound = false; // Have we read '--'?
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000357 for (int i = 1; i < argc; ++i) {
358 Option *Handler = 0;
Chris Lattner6d5857e2005-05-10 23:20:17 +0000359 const char *Value = 0;
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000360 const char *ArgName = "";
Chris Lattner331de232002-07-22 02:07:59 +0000361
362 // Check to see if this is a positional argument. This argument is
363 // considered to be positional if it doesn't start with '-', if it is "-"
Misha Brukman1115e042003-07-10 21:38:28 +0000364 // itself, or if we have seen "--" already.
Chris Lattner331de232002-07-22 02:07:59 +0000365 //
366 if (argv[i][0] != '-' || argv[i][1] == 0 || DashDashFound) {
367 // Positional argument!
Chris Lattner9cf3d472003-07-30 17:34:02 +0000368 if (ActivePositionalArg) {
Reid Spencer1e13fd22004-08-13 19:47:30 +0000369 ProvidePositionalOption(ActivePositionalArg, argv[i], i);
Chris Lattner9cf3d472003-07-30 17:34:02 +0000370 continue; // We are done!
371 } else if (!PositionalOpts.empty()) {
Reid Spencer1e13fd22004-08-13 19:47:30 +0000372 PositionalVals.push_back(std::make_pair(argv[i],i));
Chris Lattner331de232002-07-22 02:07:59 +0000373
374 // All of the positional arguments have been fulfulled, give the rest to
375 // the consume after option... if it's specified...
376 //
Misha Brukmanf976c852005-04-21 22:55:34 +0000377 if (PositionalVals.size() >= NumPositionalRequired &&
Chris Lattner331de232002-07-22 02:07:59 +0000378 ConsumeAfterOpt != 0) {
379 for (++i; i < argc; ++i)
Reid Spencer1e13fd22004-08-13 19:47:30 +0000380 PositionalVals.push_back(std::make_pair(argv[i],i));
Chris Lattner331de232002-07-22 02:07:59 +0000381 break; // Handle outside of the argument processing loop...
382 }
383
384 // Delay processing positional arguments until the end...
385 continue;
386 }
Chris Lattnerbf455c22004-05-06 22:04:31 +0000387 } else if (argv[i][0] == '-' && argv[i][1] == '-' && argv[i][2] == 0 &&
388 !DashDashFound) {
389 DashDashFound = true; // This is the mythical "--"?
390 continue; // Don't try to process it as an argument itself.
391 } else if (ActivePositionalArg &&
392 (ActivePositionalArg->getMiscFlags() & PositionalEatsArgs)) {
393 // If there is a positional argument eating options, check to see if this
394 // option is another positional argument. If so, treat it as an argument,
395 // otherwise feed it to the eating positional.
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000396 ArgName = argv[i]+1;
Chris Lattnerbf455c22004-05-06 22:04:31 +0000397 Handler = LookupOption(ArgName, Value);
398 if (!Handler || Handler->getFormattingFlag() != cl::Positional) {
Reid Spencer1e13fd22004-08-13 19:47:30 +0000399 ProvidePositionalOption(ActivePositionalArg, argv[i], i);
Chris Lattnerbf455c22004-05-06 22:04:31 +0000400 continue; // We are done!
Chris Lattner331de232002-07-22 02:07:59 +0000401 }
402
Chris Lattnerbf455c22004-05-06 22:04:31 +0000403 } else { // We start with a '-', must be an argument...
404 ArgName = argv[i]+1;
405 Handler = LookupOption(ArgName, Value);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000406
Chris Lattnerbf455c22004-05-06 22:04:31 +0000407 // Check to see if this "option" is really a prefixed or grouped argument.
Reid Spencer5f8448f2004-11-24 06:13:42 +0000408 if (Handler == 0) {
Chris Lattnerbf455c22004-05-06 22:04:31 +0000409 std::string RealName(ArgName);
410 if (RealName.size() > 1) {
Chris Lattner331de232002-07-22 02:07:59 +0000411 unsigned Length = 0;
412 Option *PGOpt = getOptionPred(RealName, Length, isPrefixedOrGrouping);
Misha Brukmanf976c852005-04-21 22:55:34 +0000413
Chris Lattner331de232002-07-22 02:07:59 +0000414 // If the option is a prefixed option, then the value is simply the
415 // rest of the name... so fall through to later processing, by
416 // setting up the argument name flags and value fields.
417 //
418 if (PGOpt && PGOpt->getFormattingFlag() == cl::Prefix) {
Chris Lattnerbf455c22004-05-06 22:04:31 +0000419 Value = ArgName+Length;
420 assert(Opts.find(std::string(ArgName, Value)) != Opts.end() &&
421 Opts.find(std::string(ArgName, Value))->second == PGOpt);
422 Handler = PGOpt;
Chris Lattner331de232002-07-22 02:07:59 +0000423 } else if (PGOpt) {
Chris Lattnerbf455c22004-05-06 22:04:31 +0000424 // This must be a grouped option... handle them now.
Chris Lattner331de232002-07-22 02:07:59 +0000425 assert(isGrouping(PGOpt) && "Broken getOptionPred!");
Misha Brukmanf976c852005-04-21 22:55:34 +0000426
Chris Lattner331de232002-07-22 02:07:59 +0000427 do {
428 // Move current arg name out of RealName into RealArgName...
Chris Lattnerbf455c22004-05-06 22:04:31 +0000429 std::string RealArgName(RealName.begin(),
430 RealName.begin() + Length);
431 RealName.erase(RealName.begin(), RealName.begin() + Length);
Misha Brukmanf976c852005-04-21 22:55:34 +0000432
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000433 // Because ValueRequired is an invalid flag for grouped arguments,
434 // we don't need to pass argc/argv in...
435 //
Chris Lattner331de232002-07-22 02:07:59 +0000436 assert(PGOpt->getValueExpectedFlag() != cl::ValueRequired &&
437 "Option can not be cl::Grouping AND cl::ValueRequired!");
438 int Dummy;
Chris Lattnerbf455c22004-05-06 22:04:31 +0000439 ErrorParsing |= ProvideOption(PGOpt, RealArgName.c_str(),
Chris Lattner6d5857e2005-05-10 23:20:17 +0000440 0, 0, 0, Dummy);
Misha Brukmanf976c852005-04-21 22:55:34 +0000441
Chris Lattner331de232002-07-22 02:07:59 +0000442 // Get the next grouping option...
Chris Lattnerbf455c22004-05-06 22:04:31 +0000443 PGOpt = getOptionPred(RealName, Length, isGrouping);
444 } while (PGOpt && Length != RealName.size());
Misha Brukmanf976c852005-04-21 22:55:34 +0000445
Chris Lattnerbf455c22004-05-06 22:04:31 +0000446 Handler = PGOpt; // Ate all of the options.
Chris Lattner331de232002-07-22 02:07:59 +0000447 }
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000448 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000449 }
450 }
451
452 if (Handler == 0) {
Reid Spencere1cc1502004-09-01 04:41:28 +0000453 std::cerr << ProgramName << ": Unknown command line argument '" << argv[i]
454 << "'. Try: '" << argv[0] << " --help'\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000455 ErrorParsing = true;
456 continue;
457 }
458
Chris Lattner72fb8e52003-05-22 20:26:17 +0000459 // Check to see if this option accepts a comma separated list of values. If
460 // it does, we have to split up the value into multiple values...
Chris Lattner6d5857e2005-05-10 23:20:17 +0000461 if (Value && Handler->getMiscFlags() & CommaSeparated) {
Chris Lattner72fb8e52003-05-22 20:26:17 +0000462 std::string Val(Value);
463 std::string::size_type Pos = Val.find(',');
464
465 while (Pos != std::string::npos) {
466 // Process the portion before the comma...
467 ErrorParsing |= ProvideOption(Handler, ArgName,
468 std::string(Val.begin(),
469 Val.begin()+Pos).c_str(),
470 argc, argv, i);
471 // Erase the portion before the comma, AND the comma...
472 Val.erase(Val.begin(), Val.begin()+Pos+1);
473 Value += Pos+1; // Increment the original value pointer as well...
474
475 // Check for another comma...
476 Pos = Val.find(',');
477 }
478 }
Chris Lattner9cf3d472003-07-30 17:34:02 +0000479
480 // If this is a named positional argument, just remember that it is the
481 // active one...
482 if (Handler->getFormattingFlag() == cl::Positional)
483 ActivePositionalArg = Handler;
Misha Brukmanf976c852005-04-21 22:55:34 +0000484 else
Chris Lattner9cf3d472003-07-30 17:34:02 +0000485 ErrorParsing |= ProvideOption(Handler, ArgName, Value, argc, argv, i);
Chris Lattner331de232002-07-22 02:07:59 +0000486 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000487
Chris Lattner331de232002-07-22 02:07:59 +0000488 // Check and handle positional arguments now...
489 if (NumPositionalRequired > PositionalVals.size()) {
Misha Brukmanf976c852005-04-21 22:55:34 +0000490 std::cerr << ProgramName
Reid Spencere1cc1502004-09-01 04:41:28 +0000491 << ": Not enough positional command line arguments specified!\n"
Chris Lattnerca6433f2003-05-22 20:06:43 +0000492 << "Must specify at least " << NumPositionalRequired
493 << " positional arguments: See: " << argv[0] << " --help\n";
Chris Lattner331de232002-07-22 02:07:59 +0000494 ErrorParsing = true;
Chris Lattnerde013242005-08-08 17:25:38 +0000495 } else if (!HasUnlimitedPositionals
496 && PositionalVals.size() > PositionalOpts.size()) {
497 std::cerr << ProgramName
498 << ": Too many positional arguments specified!\n"
499 << "Can specify at most " << PositionalOpts.size()
500 << " positional arguments: See: " << argv[0] << " --help\n";
501 ErrorParsing = true;
Chris Lattner331de232002-07-22 02:07:59 +0000502
503 } else if (ConsumeAfterOpt == 0) {
504 // Positional args have already been handled if ConsumeAfter is specified...
505 unsigned ValNo = 0, NumVals = PositionalVals.size();
506 for (unsigned i = 0, e = PositionalOpts.size(); i != e; ++i) {
507 if (RequiresValue(PositionalOpts[i])) {
Misha Brukmanf976c852005-04-21 22:55:34 +0000508 ProvidePositionalOption(PositionalOpts[i], PositionalVals[ValNo].first,
Reid Spencer1e13fd22004-08-13 19:47:30 +0000509 PositionalVals[ValNo].second);
510 ValNo++;
Chris Lattner331de232002-07-22 02:07:59 +0000511 --NumPositionalRequired; // We fulfilled our duty...
512 }
513
514 // If we _can_ give this option more arguments, do so now, as long as we
515 // do not give it values that others need. 'Done' controls whether the
516 // option even _WANTS_ any more.
517 //
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000518 bool Done = PositionalOpts[i]->getNumOccurrencesFlag() == cl::Required;
Chris Lattner331de232002-07-22 02:07:59 +0000519 while (NumVals-ValNo > NumPositionalRequired && !Done) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000520 switch (PositionalOpts[i]->getNumOccurrencesFlag()) {
Chris Lattner331de232002-07-22 02:07:59 +0000521 case cl::Optional:
522 Done = true; // Optional arguments want _at most_ one value
523 // FALL THROUGH
524 case cl::ZeroOrMore: // Zero or more will take all they can get...
525 case cl::OneOrMore: // One or more will take all they can get...
Reid Spencer1e13fd22004-08-13 19:47:30 +0000526 ProvidePositionalOption(PositionalOpts[i],
527 PositionalVals[ValNo].first,
528 PositionalVals[ValNo].second);
529 ValNo++;
Chris Lattner331de232002-07-22 02:07:59 +0000530 break;
531 default:
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000532 assert(0 && "Internal error, unexpected NumOccurrences flag in "
Chris Lattner331de232002-07-22 02:07:59 +0000533 "positional argument processing!");
534 }
535 }
Chris Lattnercaccd762001-10-27 05:54:17 +0000536 }
Chris Lattner331de232002-07-22 02:07:59 +0000537 } else {
538 assert(ConsumeAfterOpt && NumPositionalRequired <= PositionalVals.size());
539 unsigned ValNo = 0;
540 for (unsigned j = 1, e = PositionalOpts.size(); j != e; ++j)
Reid Spencer1e13fd22004-08-13 19:47:30 +0000541 if (RequiresValue(PositionalOpts[j])) {
Chris Lattnerfaba8092002-07-24 20:15:13 +0000542 ErrorParsing |= ProvidePositionalOption(PositionalOpts[j],
Reid Spencer1e13fd22004-08-13 19:47:30 +0000543 PositionalVals[ValNo].first,
544 PositionalVals[ValNo].second);
545 ValNo++;
546 }
Chris Lattnerfaba8092002-07-24 20:15:13 +0000547
548 // Handle the case where there is just one positional option, and it's
549 // optional. In this case, we want to give JUST THE FIRST option to the
550 // positional option and keep the rest for the consume after. The above
551 // loop would have assigned no values to positional options in this case.
552 //
Reid Spencer1e13fd22004-08-13 19:47:30 +0000553 if (PositionalOpts.size() == 2 && ValNo == 0 && !PositionalVals.empty()) {
Chris Lattnerfaba8092002-07-24 20:15:13 +0000554 ErrorParsing |= ProvidePositionalOption(PositionalOpts[1],
Reid Spencer1e13fd22004-08-13 19:47:30 +0000555 PositionalVals[ValNo].first,
556 PositionalVals[ValNo].second);
557 ValNo++;
558 }
Misha Brukmanf976c852005-04-21 22:55:34 +0000559
Chris Lattner331de232002-07-22 02:07:59 +0000560 // Handle over all of the rest of the arguments to the
561 // cl::ConsumeAfter command line option...
562 for (; ValNo != PositionalVals.size(); ++ValNo)
563 ErrorParsing |= ProvidePositionalOption(ConsumeAfterOpt,
Reid Spencer1e13fd22004-08-13 19:47:30 +0000564 PositionalVals[ValNo].first,
565 PositionalVals[ValNo].second);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000566 }
567
Chris Lattnerdc4693d2001-07-23 23:02:45 +0000568 // Loop over args and make sure all required args are specified!
Misha Brukmanf976c852005-04-21 22:55:34 +0000569 for (std::map<std::string, Option*>::iterator I = Opts.begin(),
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000570 E = Opts.end(); I != E; ++I) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000571 switch (I->second->getNumOccurrencesFlag()) {
Chris Lattnerdc4693d2001-07-23 23:02:45 +0000572 case Required:
573 case OneOrMore:
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000574 if (I->second->getNumOccurrences() == 0) {
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000575 I->second->error(" must be specified at least once!");
Chris Lattnerf038acb2001-10-24 06:21:56 +0000576 ErrorParsing = true;
577 }
Chris Lattnerdc4693d2001-07-23 23:02:45 +0000578 // Fall through
579 default:
580 break;
581 }
582 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000583
Chris Lattner331de232002-07-22 02:07:59 +0000584 // Free all of the memory allocated to the map. Command line options may only
585 // be processed once!
Chris Lattnerc540ebb2004-11-19 17:08:15 +0000586 getOpts().clear();
Chris Lattner331de232002-07-22 02:07:59 +0000587 PositionalOpts.clear();
Chris Lattnerc540ebb2004-11-19 17:08:15 +0000588 MoreHelp().clear();
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000589
590 // If we had an error processing our arguments, don't let the program execute
591 if (ErrorParsing) exit(1);
592}
593
594//===----------------------------------------------------------------------===//
595// Option Base class implementation
596//
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000597
Chris Lattnerca6433f2003-05-22 20:06:43 +0000598bool Option::error(std::string Message, const char *ArgName) {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000599 if (ArgName == 0) ArgName = ArgStr;
Chris Lattner331de232002-07-22 02:07:59 +0000600 if (ArgName[0] == 0)
Chris Lattnerca6433f2003-05-22 20:06:43 +0000601 std::cerr << HelpStr; // Be nice for positional arguments
Chris Lattner331de232002-07-22 02:07:59 +0000602 else
Reid Spencere1cc1502004-09-01 04:41:28 +0000603 std::cerr << ProgramName << ": for the -" << ArgName;
604 std::cerr << " option: " << Message << "\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000605 return true;
606}
607
Chris Lattner6d5857e2005-05-10 23:20:17 +0000608bool Option::addOccurrence(unsigned pos, const char *ArgName,
609 const std::string &Value) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000610 NumOccurrences++; // Increment the number of times we have been seen
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000611
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000612 switch (getNumOccurrencesFlag()) {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000613 case Optional:
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000614 if (NumOccurrences > 1)
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000615 return error(": may only occur zero or one times!", ArgName);
616 break;
617 case Required:
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000618 if (NumOccurrences > 1)
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000619 return error(": must occur exactly one time!", ArgName);
620 // Fall through
621 case OneOrMore:
Chris Lattnercaccd762001-10-27 05:54:17 +0000622 case ZeroOrMore:
623 case ConsumeAfter: break;
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000624 default: return error(": bad num occurrences flag value!");
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000625 }
626
Reid Spencer1e13fd22004-08-13 19:47:30 +0000627 return handleOccurrence(pos, ArgName, Value);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000628}
629
Chris Lattner331de232002-07-22 02:07:59 +0000630// addArgument - Tell the system that this Option subclass will handle all
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000631// occurrences of -ArgStr on the command line.
Chris Lattner331de232002-07-22 02:07:59 +0000632//
633void Option::addArgument(const char *ArgStr) {
634 if (ArgStr[0])
635 AddArgument(ArgStr, this);
Chris Lattner9cf3d472003-07-30 17:34:02 +0000636
637 if (getFormattingFlag() == Positional)
Chris Lattner331de232002-07-22 02:07:59 +0000638 getPositionalOpts().push_back(this);
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000639 else if (getNumOccurrencesFlag() == ConsumeAfter) {
Chris Lattner9cf3d472003-07-30 17:34:02 +0000640 if (!getPositionalOpts().empty() &&
641 getPositionalOpts().front()->getNumOccurrencesFlag() == ConsumeAfter)
642 error("Cannot specify more than one option with cl::ConsumeAfter!");
Chris Lattner331de232002-07-22 02:07:59 +0000643 getPositionalOpts().insert(getPositionalOpts().begin(), this);
644 }
645}
646
Chris Lattneraa852bb2002-07-23 17:15:12 +0000647void Option::removeArgument(const char *ArgStr) {
Chris Lattner9cf3d472003-07-30 17:34:02 +0000648 if (ArgStr[0])
Chris Lattnere8e258b2002-07-29 20:58:42 +0000649 RemoveArgument(ArgStr, this);
Chris Lattner9cf3d472003-07-30 17:34:02 +0000650
651 if (getFormattingFlag() == Positional) {
Chris Lattnerca6433f2003-05-22 20:06:43 +0000652 std::vector<Option*>::iterator I =
Chris Lattneraa852bb2002-07-23 17:15:12 +0000653 std::find(getPositionalOpts().begin(), getPositionalOpts().end(), this);
654 assert(I != getPositionalOpts().end() && "Arg not registered!");
655 getPositionalOpts().erase(I);
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000656 } else if (getNumOccurrencesFlag() == ConsumeAfter) {
Chris Lattneraa852bb2002-07-23 17:15:12 +0000657 assert(!getPositionalOpts().empty() && getPositionalOpts()[0] == this &&
658 "Arg not registered correctly!");
659 getPositionalOpts().erase(getPositionalOpts().begin());
660 }
661}
662
Chris Lattner331de232002-07-22 02:07:59 +0000663
664// getValueStr - Get the value description string, using "DefaultMsg" if nothing
665// has been specified yet.
666//
667static const char *getValueStr(const Option &O, const char *DefaultMsg) {
668 if (O.ValueStr[0] == 0) return DefaultMsg;
669 return O.ValueStr;
670}
671
672//===----------------------------------------------------------------------===//
673// cl::alias class implementation
674//
675
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000676// Return the width of the option tag for printing...
Chris Lattner331de232002-07-22 02:07:59 +0000677unsigned alias::getOptionWidth() const {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000678 return std::strlen(ArgStr)+6;
679}
680
Chris Lattner331de232002-07-22 02:07:59 +0000681// Print out the option for the alias...
682void alias::printOptionInfo(unsigned GlobalWidth) const {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000683 unsigned L = std::strlen(ArgStr);
Chris Lattnerca6433f2003-05-22 20:06:43 +0000684 std::cerr << " -" << ArgStr << std::string(GlobalWidth-L-6, ' ') << " - "
685 << HelpStr << "\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000686}
687
688
Chris Lattner331de232002-07-22 02:07:59 +0000689
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000690//===----------------------------------------------------------------------===//
Chris Lattner331de232002-07-22 02:07:59 +0000691// Parser Implementation code...
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000692//
693
Chris Lattner9b14eb52002-08-07 18:36:37 +0000694// basic_parser implementation
695//
696
697// Return the width of the option tag for printing...
698unsigned basic_parser_impl::getOptionWidth(const Option &O) const {
699 unsigned Len = std::strlen(O.ArgStr);
700 if (const char *ValName = getValueName())
701 Len += std::strlen(getValueStr(O, ValName))+3;
702
703 return Len + 6;
704}
705
Misha Brukmanf976c852005-04-21 22:55:34 +0000706// printOptionInfo - Print out information about this option. The
Chris Lattner9b14eb52002-08-07 18:36:37 +0000707// to-be-maintained width is specified.
708//
709void basic_parser_impl::printOptionInfo(const Option &O,
710 unsigned GlobalWidth) const {
Chris Lattnerca6433f2003-05-22 20:06:43 +0000711 std::cerr << " -" << O.ArgStr;
Chris Lattner9b14eb52002-08-07 18:36:37 +0000712
713 if (const char *ValName = getValueName())
Chris Lattnerca6433f2003-05-22 20:06:43 +0000714 std::cerr << "=<" << getValueStr(O, ValName) << ">";
Chris Lattner9b14eb52002-08-07 18:36:37 +0000715
Chris Lattnerca6433f2003-05-22 20:06:43 +0000716 std::cerr << std::string(GlobalWidth-getOptionWidth(O), ' ') << " - "
717 << O.HelpStr << "\n";
Chris Lattner9b14eb52002-08-07 18:36:37 +0000718}
719
720
721
722
Chris Lattner331de232002-07-22 02:07:59 +0000723// parser<bool> implementation
724//
Chris Lattner9b14eb52002-08-07 18:36:37 +0000725bool parser<bool>::parse(Option &O, const char *ArgName,
Chris Lattnerca6433f2003-05-22 20:06:43 +0000726 const std::string &Arg, bool &Value) {
Misha Brukmanf976c852005-04-21 22:55:34 +0000727 if (Arg == "" || Arg == "true" || Arg == "TRUE" || Arg == "True" ||
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000728 Arg == "1") {
729 Value = true;
730 } else if (Arg == "false" || Arg == "FALSE" || Arg == "False" || Arg == "0") {
731 Value = false;
732 } else {
Chris Lattner331de232002-07-22 02:07:59 +0000733 return O.error(": '" + Arg +
734 "' is invalid value for boolean argument! Try 0 or 1");
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000735 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000736 return false;
737}
738
Chris Lattner331de232002-07-22 02:07:59 +0000739// parser<int> implementation
740//
Chris Lattner9b14eb52002-08-07 18:36:37 +0000741bool parser<int>::parse(Option &O, const char *ArgName,
Chris Lattnerca6433f2003-05-22 20:06:43 +0000742 const std::string &Arg, int &Value) {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000743 char *End;
Chris Lattnerd2a6fc32003-06-28 15:47:20 +0000744 Value = (int)strtol(Arg.c_str(), &End, 0);
Misha Brukmanf976c852005-04-21 22:55:34 +0000745 if (*End != 0)
Chris Lattner331de232002-07-22 02:07:59 +0000746 return O.error(": '" + Arg + "' value invalid for integer argument!");
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000747 return false;
748}
749
Chris Lattnerd2a6fc32003-06-28 15:47:20 +0000750// parser<unsigned> implementation
751//
752bool parser<unsigned>::parse(Option &O, const char *ArgName,
753 const std::string &Arg, unsigned &Value) {
754 char *End;
Brian Gaeke2d6a2362003-10-10 17:01:36 +0000755 errno = 0;
756 unsigned long V = strtoul(Arg.c_str(), &End, 0);
Chris Lattnerd2a6fc32003-06-28 15:47:20 +0000757 Value = (unsigned)V;
Brian Gaeke2d6a2362003-10-10 17:01:36 +0000758 if (((V == ULONG_MAX) && (errno == ERANGE))
759 || (*End != 0)
760 || (Value != V))
Chris Lattnerd2a6fc32003-06-28 15:47:20 +0000761 return O.error(": '" + Arg + "' value invalid for uint argument!");
762 return false;
763}
764
Chris Lattner9b14eb52002-08-07 18:36:37 +0000765// parser<double>/parser<float> implementation
Chris Lattnerd215fd12001-10-13 06:53:19 +0000766//
Chris Lattnerca6433f2003-05-22 20:06:43 +0000767static bool parseDouble(Option &O, const std::string &Arg, double &Value) {
Chris Lattner331de232002-07-22 02:07:59 +0000768 const char *ArgStart = Arg.c_str();
769 char *End;
770 Value = strtod(ArgStart, &End);
Misha Brukmanf976c852005-04-21 22:55:34 +0000771 if (*End != 0)
Chris Lattner331de232002-07-22 02:07:59 +0000772 return O.error(": '" +Arg+ "' value invalid for floating point argument!");
Chris Lattnerd215fd12001-10-13 06:53:19 +0000773 return false;
774}
775
Chris Lattner9b14eb52002-08-07 18:36:37 +0000776bool parser<double>::parse(Option &O, const char *AN,
777 const std::string &Arg, double &Val) {
778 return parseDouble(O, Arg, Val);
Chris Lattner331de232002-07-22 02:07:59 +0000779}
780
Chris Lattner9b14eb52002-08-07 18:36:37 +0000781bool parser<float>::parse(Option &O, const char *AN,
782 const std::string &Arg, float &Val) {
783 double dVal;
784 if (parseDouble(O, Arg, dVal))
785 return true;
786 Val = (float)dVal;
787 return false;
Chris Lattner331de232002-07-22 02:07:59 +0000788}
789
790
Chris Lattner331de232002-07-22 02:07:59 +0000791
792// generic_parser_base implementation
793//
794
Chris Lattneraa852bb2002-07-23 17:15:12 +0000795// findOption - Return the option number corresponding to the specified
796// argument string. If the option is not found, getNumOptions() is returned.
797//
798unsigned generic_parser_base::findOption(const char *Name) {
799 unsigned i = 0, e = getNumOptions();
Chris Lattnerca6433f2003-05-22 20:06:43 +0000800 std::string N(Name);
Chris Lattneraa852bb2002-07-23 17:15:12 +0000801
802 while (i != e)
803 if (getOption(i) == N)
804 return i;
805 else
806 ++i;
807 return e;
808}
809
810
Chris Lattner331de232002-07-22 02:07:59 +0000811// Return the width of the option tag for printing...
812unsigned generic_parser_base::getOptionWidth(const Option &O) const {
813 if (O.hasArgStr()) {
814 unsigned Size = std::strlen(O.ArgStr)+6;
815 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
816 Size = std::max(Size, (unsigned)std::strlen(getOption(i))+8);
817 return Size;
818 } else {
819 unsigned BaseSize = 0;
820 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
821 BaseSize = std::max(BaseSize, (unsigned)std::strlen(getOption(i))+8);
822 return BaseSize;
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000823 }
824}
825
Misha Brukmanf976c852005-04-21 22:55:34 +0000826// printOptionInfo - Print out information about this option. The
Chris Lattner331de232002-07-22 02:07:59 +0000827// to-be-maintained width is specified.
828//
829void generic_parser_base::printOptionInfo(const Option &O,
830 unsigned GlobalWidth) const {
831 if (O.hasArgStr()) {
832 unsigned L = std::strlen(O.ArgStr);
Chris Lattnerca6433f2003-05-22 20:06:43 +0000833 std::cerr << " -" << O.ArgStr << std::string(GlobalWidth-L-6, ' ')
834 << " - " << O.HelpStr << "\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000835
Chris Lattner331de232002-07-22 02:07:59 +0000836 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
837 unsigned NumSpaces = GlobalWidth-strlen(getOption(i))-8;
Chris Lattnerca6433f2003-05-22 20:06:43 +0000838 std::cerr << " =" << getOption(i) << std::string(NumSpaces, ' ')
839 << " - " << getDescription(i) << "\n";
Chris Lattner9c9be482002-01-31 00:42:56 +0000840 }
Chris Lattner331de232002-07-22 02:07:59 +0000841 } else {
842 if (O.HelpStr[0])
Misha Brukmanf976c852005-04-21 22:55:34 +0000843 std::cerr << " " << O.HelpStr << "\n";
Chris Lattner331de232002-07-22 02:07:59 +0000844 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
845 unsigned L = std::strlen(getOption(i));
Chris Lattnerca6433f2003-05-22 20:06:43 +0000846 std::cerr << " -" << getOption(i) << std::string(GlobalWidth-L-8, ' ')
847 << " - " << getDescription(i) << "\n";
Chris Lattner331de232002-07-22 02:07:59 +0000848 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000849 }
850}
851
852
853//===----------------------------------------------------------------------===//
Chris Lattner331de232002-07-22 02:07:59 +0000854// --help and --help-hidden option implementation
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000855//
Reid Spencerad0846b2004-11-14 22:04:00 +0000856
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000857namespace {
858
Chris Lattner331de232002-07-22 02:07:59 +0000859class HelpPrinter {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000860 unsigned MaxArgLen;
861 const Option *EmptyArg;
862 const bool ShowHidden;
863
Chris Lattner331de232002-07-22 02:07:59 +0000864 // isHidden/isReallyHidden - Predicates to be used to filter down arg lists.
Chris Lattnerca6433f2003-05-22 20:06:43 +0000865 inline static bool isHidden(std::pair<std::string, Option *> &OptPair) {
Chris Lattner331de232002-07-22 02:07:59 +0000866 return OptPair.second->getOptionHiddenFlag() >= Hidden;
867 }
Chris Lattnerca6433f2003-05-22 20:06:43 +0000868 inline static bool isReallyHidden(std::pair<std::string, Option *> &OptPair) {
Chris Lattner331de232002-07-22 02:07:59 +0000869 return OptPair.second->getOptionHiddenFlag() == ReallyHidden;
870 }
871
872public:
873 HelpPrinter(bool showHidden) : ShowHidden(showHidden) {
874 EmptyArg = 0;
875 }
876
877 void operator=(bool Value) {
878 if (Value == false) return;
879
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000880 // Copy Options into a vector so we can sort them as we like...
Chris Lattnerca6433f2003-05-22 20:06:43 +0000881 std::vector<std::pair<std::string, Option*> > Options;
Chris Lattner697954c2002-01-20 22:54:45 +0000882 copy(getOpts().begin(), getOpts().end(), std::back_inserter(Options));
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000883
884 // Eliminate Hidden or ReallyHidden arguments, depending on ShowHidden
Misha Brukmanf976c852005-04-21 22:55:34 +0000885 Options.erase(std::remove_if(Options.begin(), Options.end(),
Chris Lattner331de232002-07-22 02:07:59 +0000886 std::ptr_fun(ShowHidden ? isReallyHidden : isHidden)),
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000887 Options.end());
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000888
889 // Eliminate duplicate entries in table (from enum flags options, f.e.)
Chris Lattner331de232002-07-22 02:07:59 +0000890 { // Give OptionSet a scope
891 std::set<Option*> OptionSet;
892 for (unsigned i = 0; i != Options.size(); ++i)
893 if (OptionSet.count(Options[i].second) == 0)
894 OptionSet.insert(Options[i].second); // Add new entry to set
895 else
896 Options.erase(Options.begin()+i--); // Erase duplicate
897 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000898
899 if (ProgramOverview)
Chris Lattnerca6433f2003-05-22 20:06:43 +0000900 std::cerr << "OVERVIEW:" << ProgramOverview << "\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000901
Chris Lattnerca6433f2003-05-22 20:06:43 +0000902 std::cerr << "USAGE: " << ProgramName << " [options]";
Chris Lattner331de232002-07-22 02:07:59 +0000903
904 // Print out the positional options...
Chris Lattnerca6433f2003-05-22 20:06:43 +0000905 std::vector<Option*> &PosOpts = getPositionalOpts();
Chris Lattner331de232002-07-22 02:07:59 +0000906 Option *CAOpt = 0; // The cl::ConsumeAfter option, if it exists...
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000907 if (!PosOpts.empty() && PosOpts[0]->getNumOccurrencesFlag() == ConsumeAfter)
Chris Lattner331de232002-07-22 02:07:59 +0000908 CAOpt = PosOpts[0];
909
Chris Lattner9cf3d472003-07-30 17:34:02 +0000910 for (unsigned i = CAOpt != 0, e = PosOpts.size(); i != e; ++i) {
911 if (PosOpts[i]->ArgStr[0])
912 std::cerr << " --" << PosOpts[i]->ArgStr;
Chris Lattnerca6433f2003-05-22 20:06:43 +0000913 std::cerr << " " << PosOpts[i]->HelpStr;
Chris Lattner9cf3d472003-07-30 17:34:02 +0000914 }
Chris Lattner331de232002-07-22 02:07:59 +0000915
916 // Print the consume after option info if it exists...
Chris Lattnerca6433f2003-05-22 20:06:43 +0000917 if (CAOpt) std::cerr << " " << CAOpt->HelpStr;
Chris Lattner331de232002-07-22 02:07:59 +0000918
Chris Lattnerca6433f2003-05-22 20:06:43 +0000919 std::cerr << "\n\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000920
921 // Compute the maximum argument length...
922 MaxArgLen = 0;
Chris Lattner331de232002-07-22 02:07:59 +0000923 for (unsigned i = 0, e = Options.size(); i != e; ++i)
924 MaxArgLen = std::max(MaxArgLen, Options[i].second->getOptionWidth());
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000925
Chris Lattnerca6433f2003-05-22 20:06:43 +0000926 std::cerr << "OPTIONS:\n";
Chris Lattner331de232002-07-22 02:07:59 +0000927 for (unsigned i = 0, e = Options.size(); i != e; ++i)
928 Options[i].second->printOptionInfo(MaxArgLen);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000929
Chris Lattnerc540ebb2004-11-19 17:08:15 +0000930 // Print any extra help the user has declared.
931 for (std::vector<const char *>::iterator I = MoreHelp().begin(),
932 E = MoreHelp().end(); I != E; ++I)
933 std::cerr << *I;
934 MoreHelp().clear();
Reid Spencerad0846b2004-11-14 22:04:00 +0000935
Reid Spencer9bbba0912004-11-16 06:11:52 +0000936 // Halt the program since help information was printed
Chris Lattnera92d12c2005-02-14 19:17:29 +0000937 getOpts().clear(); // Don't bother making option dtors remove from map.
Chris Lattner331de232002-07-22 02:07:59 +0000938 exit(1);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000939 }
940};
941
Reid Spencer69105f32004-08-04 00:36:06 +0000942class VersionPrinter {
943public:
944 void operator=(bool OptionWasSpecified) {
945 if (OptionWasSpecified) {
Misha Brukmanf976c852005-04-21 22:55:34 +0000946 std::cerr << "Low Level Virtual Machine (" << PACKAGE_NAME << ") "
Misha Brukmanfb4863a2004-11-07 00:58:38 +0000947 << PACKAGE_VERSION << " (see http://llvm.cs.uiuc.edu/)\n";
Chris Lattnera92d12c2005-02-14 19:17:29 +0000948 getOpts().clear(); // Don't bother making option dtors remove from map.
Reid Spencer69105f32004-08-04 00:36:06 +0000949 exit(1);
950 }
951 }
952};
Chris Lattner331de232002-07-22 02:07:59 +0000953
954
955// Define the two HelpPrinter instances that are used to print out help, or
956// help-hidden...
957//
958HelpPrinter NormalPrinter(false);
959HelpPrinter HiddenPrinter(true);
960
Misha Brukmanf976c852005-04-21 22:55:34 +0000961cl::opt<HelpPrinter, true, parser<bool> >
Chris Lattner4bf7afc2005-05-13 19:49:09 +0000962HOp("help", cl::desc("Display available options (--help-hidden for more)"),
Chris Lattner9b14eb52002-08-07 18:36:37 +0000963 cl::location(NormalPrinter), cl::ValueDisallowed);
Chris Lattner331de232002-07-22 02:07:59 +0000964
965cl::opt<HelpPrinter, true, parser<bool> >
Chris Lattner4bf7afc2005-05-13 19:49:09 +0000966HHOp("help-hidden", cl::desc("Display all available options"),
Chris Lattner9b14eb52002-08-07 18:36:37 +0000967 cl::location(HiddenPrinter), cl::Hidden, cl::ValueDisallowed);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000968
Reid Spencer69105f32004-08-04 00:36:06 +0000969// Define the --version option that prints out the LLVM version for the tool
970VersionPrinter VersionPrinterInstance;
971cl::opt<VersionPrinter, true, parser<bool> >
Chris Lattner4bf7afc2005-05-13 19:49:09 +0000972VersOp("version", cl::desc("Display the version of this program"),
Reid Spencer69105f32004-08-04 00:36:06 +0000973 cl::location(VersionPrinterInstance), cl::ValueDisallowed);
974
Reid Spencer9bbba0912004-11-16 06:11:52 +0000975
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000976} // End anonymous namespace
Reid Spencer9bbba0912004-11-16 06:11:52 +0000977
978// Utility function for printing the help message.
979void cl::PrintHelpMessage() {
Misha Brukmanf976c852005-04-21 22:55:34 +0000980 // This looks weird, but it actually prints the help message. The
Reid Spencer5cc498f2004-11-16 06:50:36 +0000981 // NormalPrinter variable is a HelpPrinter and the help gets printed when
982 // its operator= is invoked. That's because the "normal" usages of the
Misha Brukmanf976c852005-04-21 22:55:34 +0000983 // help printer is to be assigned true/false depending on whether the
Reid Spencer5cc498f2004-11-16 06:50:36 +0000984 // --help option was given or not. Since we're circumventing that we have
985 // to make it look like --help was given, so we assign true.
Reid Spencer9bbba0912004-11-16 06:11:52 +0000986 NormalPrinter = true;
987}