blob: 5fa65d82ba5b73d4b7810ef18264675240809a81 [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 Lattner90aa8392006-10-04 21:52:35 +000021#include "llvm/Support/ManagedStatic.h"
Bill Wendlingfe6b1462006-11-26 10:52:51 +000022#include "llvm/Support/Streams.h"
Reid Spencer6f4c6072006-08-23 07:10:06 +000023#include "llvm/System/Path.h"
Chris Lattnerdbab15a2001-07-23 17:17:47 +000024#include <algorithm>
Duraid Madina786e3e22005-12-26 04:56:16 +000025#include <functional>
Chris Lattnerdbab15a2001-07-23 17:17:47 +000026#include <map>
Bill Wendling1a097e32006-12-07 23:41:45 +000027#include <ostream>
Chris Lattnerdbab15a2001-07-23 17:17:47 +000028#include <set>
Brian Gaeke2d6a2362003-10-10 17:01:36 +000029#include <cstdlib>
30#include <cerrno>
Chris Lattner51140042004-07-03 01:21:05 +000031#include <cstring>
Chris Lattner2cdd21c2003-12-14 21:35:53 +000032using namespace llvm;
Chris Lattnerdbab15a2001-07-23 17:17:47 +000033using namespace cl;
34
Chris Lattner7422a762006-08-27 12:45:47 +000035//===----------------------------------------------------------------------===//
36// Template instantiations and anchors.
37//
38TEMPLATE_INSTANTIATION(class basic_parser<bool>);
39TEMPLATE_INSTANTIATION(class basic_parser<int>);
40TEMPLATE_INSTANTIATION(class basic_parser<unsigned>);
41TEMPLATE_INSTANTIATION(class basic_parser<double>);
42TEMPLATE_INSTANTIATION(class basic_parser<float>);
43TEMPLATE_INSTANTIATION(class basic_parser<std::string>);
44
45TEMPLATE_INSTANTIATION(class opt<unsigned>);
46TEMPLATE_INSTANTIATION(class opt<int>);
47TEMPLATE_INSTANTIATION(class opt<std::string>);
48TEMPLATE_INSTANTIATION(class opt<bool>);
49
50void Option::anchor() {}
51void basic_parser_impl::anchor() {}
52void parser<bool>::anchor() {}
53void parser<int>::anchor() {}
54void parser<unsigned>::anchor() {}
55void parser<double>::anchor() {}
56void parser<float>::anchor() {}
57void parser<std::string>::anchor() {}
58
59//===----------------------------------------------------------------------===//
60
Chris Lattnerefa3da52006-10-13 00:06:24 +000061// Globals for name and overview of program. Program name is not a string to
62// avoid static ctor/dtor issues.
63static char ProgramName[80] = "<premain>";
Reid Spencere1cc1502004-09-01 04:41:28 +000064static const char *ProgramOverview = 0;
65
Chris Lattnerc540ebb2004-11-19 17:08:15 +000066// This collects additional help to be printed.
Chris Lattner90aa8392006-10-04 21:52:35 +000067static ManagedStatic<std::vector<const char*> > MoreHelp;
Chris Lattnerc540ebb2004-11-19 17:08:15 +000068
Chris Lattner90aa8392006-10-04 21:52:35 +000069extrahelp::extrahelp(const char *Help)
Chris Lattnerc540ebb2004-11-19 17:08:15 +000070 : morehelp(Help) {
Chris Lattner90aa8392006-10-04 21:52:35 +000071 MoreHelp->push_back(Help);
Chris Lattnerc540ebb2004-11-19 17:08:15 +000072}
73
Chris Lattner331de232002-07-22 02:07:59 +000074//===----------------------------------------------------------------------===//
Chris Lattner7422a762006-08-27 12:45:47 +000075// Basic, shared command line option processing machinery.
Chris Lattner331de232002-07-22 02:07:59 +000076//
77
Chris Lattneraf035f32007-04-05 21:58:17 +000078static ManagedStatic<std::map<std::string, Option*> > OptionsMap;
Chris Lattner90aa8392006-10-04 21:52:35 +000079static ManagedStatic<std::vector<Option*> > PositionalOptions;
Chris Lattnere8e258b2002-07-29 20:58:42 +000080
Chris Lattnerca6433f2003-05-22 20:06:43 +000081static Option *getOption(const std::string &Str) {
Chris Lattneraf035f32007-04-05 21:58:17 +000082 std::map<std::string,Option*>::iterator I = OptionsMap->find(Str);
83 return I != OptionsMap->end() ? I->second : 0;
Chris Lattner331de232002-07-22 02:07:59 +000084}
85
Chris Lattnere8e258b2002-07-29 20:58:42 +000086static void AddArgument(const char *ArgName, Option *Opt) {
87 if (getOption(ArgName)) {
Bill Wendlinge8156192006-12-07 01:30:32 +000088 cerr << ProgramName << ": CommandLine Error: Argument '"
89 << ArgName << "' defined more than once!\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +000090 } else {
Chris Lattnerf78032f2001-11-26 18:58:34 +000091 // Add argument to the argument map!
Chris Lattneraf035f32007-04-05 21:58:17 +000092 (*OptionsMap)[ArgName] = Opt;
Chris Lattnere8e258b2002-07-29 20:58:42 +000093 }
94}
95
Chris Lattneraf035f32007-04-05 21:58:17 +000096/// LookupOption - Lookup the option specified by the specified option on the
97/// command line. If there is a value specified (after an equal sign) return
98/// that as well.
99static Option *LookupOption(const char *&Arg, const char *&Value) {
100 while (*Arg == '-') ++Arg; // Eat leading dashes
101
102 const char *ArgEnd = Arg;
103 while (*ArgEnd && *ArgEnd != '=')
104 ++ArgEnd; // Scan till end of argument name.
105
106 if (*ArgEnd == '=') // If we have an equals sign...
107 Value = ArgEnd+1; // Get the value, not the equals
108
109
110 if (*Arg == 0) return 0;
111
112 // Look up the option.
113 std::map<std::string, Option*> &Opts = *OptionsMap;
114 std::map<std::string, Option*>::iterator I =
115 Opts.find(std::string(Arg, ArgEnd));
116 return (I != Opts.end()) ? I->second : 0;
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000117}
118
Chris Lattnercaccd762001-10-27 05:54:17 +0000119static inline bool ProvideOption(Option *Handler, const char *ArgName,
120 const char *Value, int argc, char **argv,
121 int &i) {
122 // Enforce value requirements
123 switch (Handler->getValueExpectedFlag()) {
124 case ValueRequired:
Chris Lattner6d5857e2005-05-10 23:20:17 +0000125 if (Value == 0) { // No value specified?
Chris Lattnercaccd762001-10-27 05:54:17 +0000126 if (i+1 < argc) { // Steal the next argument, like for '-o filename'
127 Value = argv[++i];
128 } else {
129 return Handler->error(" requires a value!");
130 }
131 }
132 break;
133 case ValueDisallowed:
Chris Lattner6d5857e2005-05-10 23:20:17 +0000134 if (Value)
Misha Brukmanf976c852005-04-21 22:55:34 +0000135 return Handler->error(" does not allow a value! '" +
Chris Lattnerca6433f2003-05-22 20:06:43 +0000136 std::string(Value) + "' specified.");
Chris Lattnercaccd762001-10-27 05:54:17 +0000137 break;
Misha Brukmanf976c852005-04-21 22:55:34 +0000138 case ValueOptional:
Reid Spencere1cc1502004-09-01 04:41:28 +0000139 break;
Misha Brukmanf976c852005-04-21 22:55:34 +0000140 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000141 cerr << ProgramName
142 << ": Bad ValueMask flag! CommandLine usage error:"
143 << Handler->getValueExpectedFlag() << "\n";
Reid Spencere1cc1502004-09-01 04:41:28 +0000144 abort();
145 break;
Chris Lattnercaccd762001-10-27 05:54:17 +0000146 }
147
148 // Run the handler now!
Chris Lattner6d5857e2005-05-10 23:20:17 +0000149 return Handler->addOccurrence(i, ArgName, Value ? Value : "");
Chris Lattnercaccd762001-10-27 05:54:17 +0000150}
151
Misha Brukmanf976c852005-04-21 22:55:34 +0000152static bool ProvidePositionalOption(Option *Handler, const std::string &Arg,
Reid Spencer1e13fd22004-08-13 19:47:30 +0000153 int i) {
154 int Dummy = i;
Chris Lattner9cf3d472003-07-30 17:34:02 +0000155 return ProvideOption(Handler, Handler->ArgStr, Arg.c_str(), 0, 0, Dummy);
Chris Lattner331de232002-07-22 02:07:59 +0000156}
Chris Lattnerf78032f2001-11-26 18:58:34 +0000157
Chris Lattner331de232002-07-22 02:07:59 +0000158
159// Option predicates...
160static inline bool isGrouping(const Option *O) {
161 return O->getFormattingFlag() == cl::Grouping;
162}
163static inline bool isPrefixedOrGrouping(const Option *O) {
164 return isGrouping(O) || O->getFormattingFlag() == cl::Prefix;
165}
166
167// getOptionPred - Check to see if there are any options that satisfy the
168// specified predicate with names that are the prefixes in Name. This is
169// checked by progressively stripping characters off of the name, checking to
170// see if there options that satisfy the predicate. If we find one, return it,
171// otherwise return null.
172//
173static Option *getOptionPred(std::string Name, unsigned &Length,
174 bool (*Pred)(const Option*)) {
Misha Brukmanf976c852005-04-21 22:55:34 +0000175
Chris Lattnere8e258b2002-07-29 20:58:42 +0000176 Option *Op = getOption(Name);
177 if (Op && Pred(Op)) {
Chris Lattner331de232002-07-22 02:07:59 +0000178 Length = Name.length();
Chris Lattnere8e258b2002-07-29 20:58:42 +0000179 return Op;
Chris Lattnerf78032f2001-11-26 18:58:34 +0000180 }
181
Chris Lattner331de232002-07-22 02:07:59 +0000182 if (Name.size() == 1) return 0;
183 do {
184 Name.erase(Name.end()-1, Name.end()); // Chop off the last character...
Chris Lattnere8e258b2002-07-29 20:58:42 +0000185 Op = getOption(Name);
Chris Lattner331de232002-07-22 02:07:59 +0000186
187 // Loop while we haven't found an option and Name still has at least two
188 // characters in it (so that the next iteration will not be the empty
189 // string...
Chris Lattnere8e258b2002-07-29 20:58:42 +0000190 } while ((Op == 0 || !Pred(Op)) && Name.size() > 1);
Chris Lattner331de232002-07-22 02:07:59 +0000191
Chris Lattnere8e258b2002-07-29 20:58:42 +0000192 if (Op && Pred(Op)) {
Chris Lattner331de232002-07-22 02:07:59 +0000193 Length = Name.length();
Chris Lattnere8e258b2002-07-29 20:58:42 +0000194 return Op; // Found one!
Chris Lattner331de232002-07-22 02:07:59 +0000195 }
196 return 0; // No option found!
197}
198
199static bool RequiresValue(const Option *O) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000200 return O->getNumOccurrencesFlag() == cl::Required ||
201 O->getNumOccurrencesFlag() == cl::OneOrMore;
Chris Lattner331de232002-07-22 02:07:59 +0000202}
203
204static bool EatsUnboundedNumberOfValues(const Option *O) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000205 return O->getNumOccurrencesFlag() == cl::ZeroOrMore ||
206 O->getNumOccurrencesFlag() == cl::OneOrMore;
Chris Lattnerf78032f2001-11-26 18:58:34 +0000207}
Chris Lattnercaccd762001-10-27 05:54:17 +0000208
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000209/// ParseCStringVector - Break INPUT up wherever one or more
210/// whitespace characters are found, and store the resulting tokens in
211/// OUTPUT. The tokens stored in OUTPUT are dynamically allocated
212/// using strdup (), so it is the caller's responsibility to free ()
213/// them later.
Brian Gaeke06b06c52003-08-14 22:00:59 +0000214///
Chris Lattner23288582006-08-27 22:10:29 +0000215static void ParseCStringVector(std::vector<char *> &output,
216 const char *input) {
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000217 // Characters which will be treated as token separators:
218 static const char *delims = " \v\f\t\r\n";
219
220 std::string work (input);
221 // Skip past any delims at head of input string.
222 size_t pos = work.find_first_not_of (delims);
223 // If the string consists entirely of delims, then exit early.
224 if (pos == std::string::npos) return;
225 // Otherwise, jump forward to beginning of first word.
226 work = work.substr (pos);
227 // Find position of first delimiter.
228 pos = work.find_first_of (delims);
229
230 while (!work.empty() && pos != std::string::npos) {
231 // Everything from 0 to POS is the next word to copy.
232 output.push_back (strdup (work.substr (0,pos).c_str ()));
233 // Is there another word in the string?
234 size_t nextpos = work.find_first_not_of (delims, pos + 1);
235 if (nextpos != std::string::npos) {
236 // Yes? Then remove delims from beginning ...
237 work = work.substr (work.find_first_not_of (delims, pos + 1));
238 // and find the end of the word.
239 pos = work.find_first_of (delims);
240 } else {
241 // No? (Remainder of string is delims.) End the loop.
242 work = "";
243 pos = std::string::npos;
244 }
245 }
246
247 // If `input' ended with non-delim char, then we'll get here with
248 // the last word of `input' in `work'; copy it now.
249 if (!work.empty ()) {
250 output.push_back (strdup (work.c_str ()));
Brian Gaeke06b06c52003-08-14 22:00:59 +0000251 }
252}
253
254/// ParseEnvironmentOptions - An alternative entry point to the
255/// CommandLine library, which allows you to read the program's name
256/// from the caller (as PROGNAME) and its command-line arguments from
257/// an environment variable (whose name is given in ENVVAR).
258///
Chris Lattnerbf455c22004-05-06 22:04:31 +0000259void cl::ParseEnvironmentOptions(const char *progName, const char *envVar,
260 const char *Overview) {
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000261 // Check args.
Chris Lattnerbf455c22004-05-06 22:04:31 +0000262 assert(progName && "Program name not specified");
263 assert(envVar && "Environment variable name missing");
Misha Brukmanf976c852005-04-21 22:55:34 +0000264
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000265 // Get the environment variable they want us to parse options out of.
Chris Lattner23288582006-08-27 22:10:29 +0000266 const char *envValue = getenv(envVar);
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000267 if (!envValue)
268 return;
269
Brian Gaeke06b06c52003-08-14 22:00:59 +0000270 // Get program's "name", which we wouldn't know without the caller
271 // telling us.
Chris Lattner23288582006-08-27 22:10:29 +0000272 std::vector<char*> newArgv;
273 newArgv.push_back(strdup(progName));
Brian Gaeke06b06c52003-08-14 22:00:59 +0000274
275 // Parse the value of the environment variable into a "command line"
276 // and hand it off to ParseCommandLineOptions().
Chris Lattner23288582006-08-27 22:10:29 +0000277 ParseCStringVector(newArgv, envValue);
278 int newArgc = newArgv.size();
279 ParseCommandLineOptions(newArgc, &newArgv[0], Overview);
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000280
281 // Free all the strdup()ed strings.
Chris Lattner23288582006-08-27 22:10:29 +0000282 for (std::vector<char*>::iterator i = newArgv.begin(), e = newArgv.end();
283 i != e; ++i)
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000284 free (*i);
Brian Gaeke06b06c52003-08-14 22:00:59 +0000285}
286
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000287void cl::ParseCommandLineOptions(int &argc, char **argv,
Chris Lattner0c0edf82002-07-25 06:17:51 +0000288 const char *Overview) {
Chris Lattneraf035f32007-04-05 21:58:17 +0000289 assert((!OptionsMap->empty() || !PositionalOptions->empty()) &&
Chris Lattner331de232002-07-22 02:07:59 +0000290 "No options specified, or ParseCommandLineOptions called more"
291 " than once!");
Reid Spencer6f4c6072006-08-23 07:10:06 +0000292 sys::Path progname(argv[0]);
Chris Lattnerefa3da52006-10-13 00:06:24 +0000293
294 // Copy the program name into ProgName, making sure not to overflow it.
295 std::string ProgName = sys::Path(argv[0]).getLast();
296 if (ProgName.size() > 79) ProgName.resize(79);
297 strcpy(ProgramName, ProgName.c_str());
298
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000299 ProgramOverview = Overview;
300 bool ErrorParsing = false;
301
Chris Lattneraf035f32007-04-05 21:58:17 +0000302 std::map<std::string, Option*> &Opts = *OptionsMap;
Chris Lattner90aa8392006-10-04 21:52:35 +0000303 std::vector<Option*> &PositionalOpts = *PositionalOptions;
Chris Lattner331de232002-07-22 02:07:59 +0000304
305 // Check out the positional arguments to collect information about them.
306 unsigned NumPositionalRequired = 0;
Chris Lattnerde013242005-08-08 17:25:38 +0000307
308 // Determine whether or not there are an unlimited number of positionals
309 bool HasUnlimitedPositionals = false;
310
Chris Lattner331de232002-07-22 02:07:59 +0000311 Option *ConsumeAfterOpt = 0;
312 if (!PositionalOpts.empty()) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000313 if (PositionalOpts[0]->getNumOccurrencesFlag() == cl::ConsumeAfter) {
Chris Lattner331de232002-07-22 02:07:59 +0000314 assert(PositionalOpts.size() > 1 &&
315 "Cannot specify cl::ConsumeAfter without a positional argument!");
316 ConsumeAfterOpt = PositionalOpts[0];
317 }
318
319 // Calculate how many positional values are _required_.
320 bool UnboundedFound = false;
321 for (unsigned i = ConsumeAfterOpt != 0, e = PositionalOpts.size();
322 i != e; ++i) {
323 Option *Opt = PositionalOpts[i];
324 if (RequiresValue(Opt))
325 ++NumPositionalRequired;
326 else if (ConsumeAfterOpt) {
327 // ConsumeAfter cannot be combined with "optional" positional options
Chris Lattner54ec7ae2002-07-22 02:21:57 +0000328 // unless there is only one positional argument...
329 if (PositionalOpts.size() > 2)
330 ErrorParsing |=
331 Opt->error(" error - this positional option will never be matched, "
332 "because it does not Require a value, and a "
333 "cl::ConsumeAfter option is active!");
Chris Lattner9cf3d472003-07-30 17:34:02 +0000334 } else if (UnboundedFound && !Opt->ArgStr[0]) {
335 // This option does not "require" a value... Make sure this option is
336 // not specified after an option that eats all extra arguments, or this
337 // one will never get any!
Chris Lattner331de232002-07-22 02:07:59 +0000338 //
339 ErrorParsing |= Opt->error(" error - option can never match, because "
340 "another positional argument will match an "
341 "unbounded number of values, and this option"
342 " does not require a value!");
343 }
344 UnboundedFound |= EatsUnboundedNumberOfValues(Opt);
345 }
Chris Lattner21e1a792005-08-08 21:57:27 +0000346 HasUnlimitedPositionals = UnboundedFound || ConsumeAfterOpt;
Chris Lattner331de232002-07-22 02:07:59 +0000347 }
348
Reid Spencer1e13fd22004-08-13 19:47:30 +0000349 // PositionalVals - A vector of "positional" arguments we accumulate into
350 // the process at the end...
Chris Lattner331de232002-07-22 02:07:59 +0000351 //
Reid Spencer1e13fd22004-08-13 19:47:30 +0000352 std::vector<std::pair<std::string,unsigned> > PositionalVals;
Chris Lattner331de232002-07-22 02:07:59 +0000353
Chris Lattner9cf3d472003-07-30 17:34:02 +0000354 // If the program has named positional arguments, and the name has been run
355 // across, keep track of which positional argument was named. Otherwise put
356 // the positional args into the PositionalVals list...
357 Option *ActivePositionalArg = 0;
358
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000359 // Loop over all of the arguments... processing them.
Chris Lattner331de232002-07-22 02:07:59 +0000360 bool DashDashFound = false; // Have we read '--'?
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000361 for (int i = 1; i < argc; ++i) {
362 Option *Handler = 0;
Chris Lattner6d5857e2005-05-10 23:20:17 +0000363 const char *Value = 0;
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000364 const char *ArgName = "";
Chris Lattner331de232002-07-22 02:07:59 +0000365
366 // Check to see if this is a positional argument. This argument is
367 // considered to be positional if it doesn't start with '-', if it is "-"
Misha Brukman1115e042003-07-10 21:38:28 +0000368 // itself, or if we have seen "--" already.
Chris Lattner331de232002-07-22 02:07:59 +0000369 //
370 if (argv[i][0] != '-' || argv[i][1] == 0 || DashDashFound) {
371 // Positional argument!
Chris Lattner9cf3d472003-07-30 17:34:02 +0000372 if (ActivePositionalArg) {
Reid Spencer1e13fd22004-08-13 19:47:30 +0000373 ProvidePositionalOption(ActivePositionalArg, argv[i], i);
Chris Lattner9cf3d472003-07-30 17:34:02 +0000374 continue; // We are done!
375 } else if (!PositionalOpts.empty()) {
Reid Spencer1e13fd22004-08-13 19:47:30 +0000376 PositionalVals.push_back(std::make_pair(argv[i],i));
Chris Lattner331de232002-07-22 02:07:59 +0000377
378 // All of the positional arguments have been fulfulled, give the rest to
379 // the consume after option... if it's specified...
380 //
Misha Brukmanf976c852005-04-21 22:55:34 +0000381 if (PositionalVals.size() >= NumPositionalRequired &&
Chris Lattner331de232002-07-22 02:07:59 +0000382 ConsumeAfterOpt != 0) {
383 for (++i; i < argc; ++i)
Reid Spencer1e13fd22004-08-13 19:47:30 +0000384 PositionalVals.push_back(std::make_pair(argv[i],i));
Chris Lattner331de232002-07-22 02:07:59 +0000385 break; // Handle outside of the argument processing loop...
386 }
387
388 // Delay processing positional arguments until the end...
389 continue;
390 }
Chris Lattnerbf455c22004-05-06 22:04:31 +0000391 } else if (argv[i][0] == '-' && argv[i][1] == '-' && argv[i][2] == 0 &&
392 !DashDashFound) {
393 DashDashFound = true; // This is the mythical "--"?
394 continue; // Don't try to process it as an argument itself.
395 } else if (ActivePositionalArg &&
396 (ActivePositionalArg->getMiscFlags() & PositionalEatsArgs)) {
397 // If there is a positional argument eating options, check to see if this
398 // option is another positional argument. If so, treat it as an argument,
399 // otherwise feed it to the eating positional.
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000400 ArgName = argv[i]+1;
Chris Lattnerbf455c22004-05-06 22:04:31 +0000401 Handler = LookupOption(ArgName, Value);
402 if (!Handler || Handler->getFormattingFlag() != cl::Positional) {
Reid Spencer1e13fd22004-08-13 19:47:30 +0000403 ProvidePositionalOption(ActivePositionalArg, argv[i], i);
Chris Lattnerbf455c22004-05-06 22:04:31 +0000404 continue; // We are done!
Chris Lattner331de232002-07-22 02:07:59 +0000405 }
406
Chris Lattnerbf455c22004-05-06 22:04:31 +0000407 } else { // We start with a '-', must be an argument...
408 ArgName = argv[i]+1;
409 Handler = LookupOption(ArgName, Value);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000410
Chris Lattnerbf455c22004-05-06 22:04:31 +0000411 // Check to see if this "option" is really a prefixed or grouped argument.
Reid Spencer5f8448f2004-11-24 06:13:42 +0000412 if (Handler == 0) {
Chris Lattnerbf455c22004-05-06 22:04:31 +0000413 std::string RealName(ArgName);
414 if (RealName.size() > 1) {
Chris Lattner331de232002-07-22 02:07:59 +0000415 unsigned Length = 0;
416 Option *PGOpt = getOptionPred(RealName, Length, isPrefixedOrGrouping);
Misha Brukmanf976c852005-04-21 22:55:34 +0000417
Chris Lattner331de232002-07-22 02:07:59 +0000418 // If the option is a prefixed option, then the value is simply the
419 // rest of the name... so fall through to later processing, by
420 // setting up the argument name flags and value fields.
421 //
422 if (PGOpt && PGOpt->getFormattingFlag() == cl::Prefix) {
Chris Lattnerbf455c22004-05-06 22:04:31 +0000423 Value = ArgName+Length;
424 assert(Opts.find(std::string(ArgName, Value)) != Opts.end() &&
425 Opts.find(std::string(ArgName, Value))->second == PGOpt);
426 Handler = PGOpt;
Chris Lattner331de232002-07-22 02:07:59 +0000427 } else if (PGOpt) {
Chris Lattnerbf455c22004-05-06 22:04:31 +0000428 // This must be a grouped option... handle them now.
Chris Lattner331de232002-07-22 02:07:59 +0000429 assert(isGrouping(PGOpt) && "Broken getOptionPred!");
Misha Brukmanf976c852005-04-21 22:55:34 +0000430
Chris Lattner331de232002-07-22 02:07:59 +0000431 do {
432 // Move current arg name out of RealName into RealArgName...
Chris Lattnerbf455c22004-05-06 22:04:31 +0000433 std::string RealArgName(RealName.begin(),
434 RealName.begin() + Length);
435 RealName.erase(RealName.begin(), RealName.begin() + Length);
Misha Brukmanf976c852005-04-21 22:55:34 +0000436
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000437 // Because ValueRequired is an invalid flag for grouped arguments,
438 // we don't need to pass argc/argv in...
439 //
Chris Lattner331de232002-07-22 02:07:59 +0000440 assert(PGOpt->getValueExpectedFlag() != cl::ValueRequired &&
441 "Option can not be cl::Grouping AND cl::ValueRequired!");
442 int Dummy;
Chris Lattnerbf455c22004-05-06 22:04:31 +0000443 ErrorParsing |= ProvideOption(PGOpt, RealArgName.c_str(),
Chris Lattner6d5857e2005-05-10 23:20:17 +0000444 0, 0, 0, Dummy);
Misha Brukmanf976c852005-04-21 22:55:34 +0000445
Chris Lattner331de232002-07-22 02:07:59 +0000446 // Get the next grouping option...
Chris Lattnerbf455c22004-05-06 22:04:31 +0000447 PGOpt = getOptionPred(RealName, Length, isGrouping);
448 } while (PGOpt && Length != RealName.size());
Misha Brukmanf976c852005-04-21 22:55:34 +0000449
Chris Lattnerbf455c22004-05-06 22:04:31 +0000450 Handler = PGOpt; // Ate all of the options.
Chris Lattner331de232002-07-22 02:07:59 +0000451 }
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000452 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000453 }
454 }
455
456 if (Handler == 0) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000457 cerr << ProgramName << ": Unknown command line argument '"
458 << argv[i] << "'. Try: '" << argv[0] << " --help'\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000459 ErrorParsing = true;
460 continue;
461 }
462
Chris Lattner72fb8e52003-05-22 20:26:17 +0000463 // Check to see if this option accepts a comma separated list of values. If
464 // it does, we have to split up the value into multiple values...
Chris Lattner6d5857e2005-05-10 23:20:17 +0000465 if (Value && Handler->getMiscFlags() & CommaSeparated) {
Chris Lattner72fb8e52003-05-22 20:26:17 +0000466 std::string Val(Value);
467 std::string::size_type Pos = Val.find(',');
468
469 while (Pos != std::string::npos) {
470 // Process the portion before the comma...
471 ErrorParsing |= ProvideOption(Handler, ArgName,
472 std::string(Val.begin(),
473 Val.begin()+Pos).c_str(),
474 argc, argv, i);
475 // Erase the portion before the comma, AND the comma...
476 Val.erase(Val.begin(), Val.begin()+Pos+1);
477 Value += Pos+1; // Increment the original value pointer as well...
478
479 // Check for another comma...
480 Pos = Val.find(',');
481 }
482 }
Chris Lattner9cf3d472003-07-30 17:34:02 +0000483
484 // If this is a named positional argument, just remember that it is the
485 // active one...
486 if (Handler->getFormattingFlag() == cl::Positional)
487 ActivePositionalArg = Handler;
Misha Brukmanf976c852005-04-21 22:55:34 +0000488 else
Chris Lattner9cf3d472003-07-30 17:34:02 +0000489 ErrorParsing |= ProvideOption(Handler, ArgName, Value, argc, argv, i);
Chris Lattner331de232002-07-22 02:07:59 +0000490 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000491
Chris Lattner331de232002-07-22 02:07:59 +0000492 // Check and handle positional arguments now...
493 if (NumPositionalRequired > PositionalVals.size()) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000494 cerr << ProgramName
495 << ": Not enough positional command line arguments specified!\n"
496 << "Must specify at least " << NumPositionalRequired
497 << " positional arguments: See: " << argv[0] << " --help\n";
Chris Lattner79959d22006-01-17 00:32:28 +0000498
Chris Lattner331de232002-07-22 02:07:59 +0000499 ErrorParsing = true;
Chris Lattnerde013242005-08-08 17:25:38 +0000500 } else if (!HasUnlimitedPositionals
501 && PositionalVals.size() > PositionalOpts.size()) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000502 cerr << ProgramName
503 << ": Too many positional arguments specified!\n"
504 << "Can specify at most " << PositionalOpts.size()
505 << " positional arguments: See: " << argv[0] << " --help\n";
Chris Lattnerde013242005-08-08 17:25:38 +0000506 ErrorParsing = true;
Chris Lattner331de232002-07-22 02:07:59 +0000507
508 } else if (ConsumeAfterOpt == 0) {
509 // Positional args have already been handled if ConsumeAfter is specified...
510 unsigned ValNo = 0, NumVals = PositionalVals.size();
511 for (unsigned i = 0, e = PositionalOpts.size(); i != e; ++i) {
512 if (RequiresValue(PositionalOpts[i])) {
Misha Brukmanf976c852005-04-21 22:55:34 +0000513 ProvidePositionalOption(PositionalOpts[i], PositionalVals[ValNo].first,
Reid Spencer1e13fd22004-08-13 19:47:30 +0000514 PositionalVals[ValNo].second);
515 ValNo++;
Chris Lattner331de232002-07-22 02:07:59 +0000516 --NumPositionalRequired; // We fulfilled our duty...
517 }
518
519 // If we _can_ give this option more arguments, do so now, as long as we
520 // do not give it values that others need. 'Done' controls whether the
521 // option even _WANTS_ any more.
522 //
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000523 bool Done = PositionalOpts[i]->getNumOccurrencesFlag() == cl::Required;
Chris Lattner331de232002-07-22 02:07:59 +0000524 while (NumVals-ValNo > NumPositionalRequired && !Done) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000525 switch (PositionalOpts[i]->getNumOccurrencesFlag()) {
Chris Lattner331de232002-07-22 02:07:59 +0000526 case cl::Optional:
527 Done = true; // Optional arguments want _at most_ one value
528 // FALL THROUGH
529 case cl::ZeroOrMore: // Zero or more will take all they can get...
530 case cl::OneOrMore: // One or more will take all they can get...
Reid Spencer1e13fd22004-08-13 19:47:30 +0000531 ProvidePositionalOption(PositionalOpts[i],
532 PositionalVals[ValNo].first,
533 PositionalVals[ValNo].second);
534 ValNo++;
Chris Lattner331de232002-07-22 02:07:59 +0000535 break;
536 default:
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000537 assert(0 && "Internal error, unexpected NumOccurrences flag in "
Chris Lattner331de232002-07-22 02:07:59 +0000538 "positional argument processing!");
539 }
540 }
Chris Lattnercaccd762001-10-27 05:54:17 +0000541 }
Chris Lattner331de232002-07-22 02:07:59 +0000542 } else {
543 assert(ConsumeAfterOpt && NumPositionalRequired <= PositionalVals.size());
544 unsigned ValNo = 0;
545 for (unsigned j = 1, e = PositionalOpts.size(); j != e; ++j)
Reid Spencer1e13fd22004-08-13 19:47:30 +0000546 if (RequiresValue(PositionalOpts[j])) {
Chris Lattnerfaba8092002-07-24 20:15:13 +0000547 ErrorParsing |= ProvidePositionalOption(PositionalOpts[j],
Reid Spencer1e13fd22004-08-13 19:47:30 +0000548 PositionalVals[ValNo].first,
549 PositionalVals[ValNo].second);
550 ValNo++;
551 }
Chris Lattnerfaba8092002-07-24 20:15:13 +0000552
553 // Handle the case where there is just one positional option, and it's
554 // optional. In this case, we want to give JUST THE FIRST option to the
555 // positional option and keep the rest for the consume after. The above
556 // loop would have assigned no values to positional options in this case.
557 //
Reid Spencer1e13fd22004-08-13 19:47:30 +0000558 if (PositionalOpts.size() == 2 && ValNo == 0 && !PositionalVals.empty()) {
Chris Lattnerfaba8092002-07-24 20:15:13 +0000559 ErrorParsing |= ProvidePositionalOption(PositionalOpts[1],
Reid Spencer1e13fd22004-08-13 19:47:30 +0000560 PositionalVals[ValNo].first,
561 PositionalVals[ValNo].second);
562 ValNo++;
563 }
Misha Brukmanf976c852005-04-21 22:55:34 +0000564
Chris Lattner331de232002-07-22 02:07:59 +0000565 // Handle over all of the rest of the arguments to the
566 // cl::ConsumeAfter command line option...
567 for (; ValNo != PositionalVals.size(); ++ValNo)
568 ErrorParsing |= ProvidePositionalOption(ConsumeAfterOpt,
Reid Spencer1e13fd22004-08-13 19:47:30 +0000569 PositionalVals[ValNo].first,
570 PositionalVals[ValNo].second);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000571 }
572
Chris Lattnerdc4693d2001-07-23 23:02:45 +0000573 // Loop over args and make sure all required args are specified!
Misha Brukmanf976c852005-04-21 22:55:34 +0000574 for (std::map<std::string, Option*>::iterator I = Opts.begin(),
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000575 E = Opts.end(); I != E; ++I) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000576 switch (I->second->getNumOccurrencesFlag()) {
Chris Lattnerdc4693d2001-07-23 23:02:45 +0000577 case Required:
578 case OneOrMore:
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000579 if (I->second->getNumOccurrences() == 0) {
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000580 I->second->error(" must be specified at least once!");
Chris Lattnerf038acb2001-10-24 06:21:56 +0000581 ErrorParsing = true;
582 }
Chris Lattnerdc4693d2001-07-23 23:02:45 +0000583 // Fall through
584 default:
585 break;
586 }
587 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000588
Chris Lattner331de232002-07-22 02:07:59 +0000589 // Free all of the memory allocated to the map. Command line options may only
590 // be processed once!
Chris Lattner90aa8392006-10-04 21:52:35 +0000591 Opts.clear();
Chris Lattner331de232002-07-22 02:07:59 +0000592 PositionalOpts.clear();
Chris Lattner90aa8392006-10-04 21:52:35 +0000593 MoreHelp->clear();
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000594
595 // If we had an error processing our arguments, don't let the program execute
596 if (ErrorParsing) exit(1);
597}
598
599//===----------------------------------------------------------------------===//
600// Option Base class implementation
601//
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000602
Chris Lattnerca6433f2003-05-22 20:06:43 +0000603bool Option::error(std::string Message, const char *ArgName) {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000604 if (ArgName == 0) ArgName = ArgStr;
Chris Lattner331de232002-07-22 02:07:59 +0000605 if (ArgName[0] == 0)
Bill Wendlinge8156192006-12-07 01:30:32 +0000606 cerr << HelpStr; // Be nice for positional arguments
Chris Lattner331de232002-07-22 02:07:59 +0000607 else
Bill Wendlinge8156192006-12-07 01:30:32 +0000608 cerr << ProgramName << ": for the -" << ArgName;
Jim Laskeyabe0e3e2006-08-02 20:15:56 +0000609
Bill Wendlinge8156192006-12-07 01:30:32 +0000610 cerr << " option: " << Message << "\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000611 return true;
612}
613
Chris Lattner6d5857e2005-05-10 23:20:17 +0000614bool Option::addOccurrence(unsigned pos, const char *ArgName,
615 const std::string &Value) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000616 NumOccurrences++; // Increment the number of times we have been seen
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000617
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000618 switch (getNumOccurrencesFlag()) {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000619 case Optional:
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000620 if (NumOccurrences > 1)
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000621 return error(": may only occur zero or one times!", ArgName);
622 break;
623 case Required:
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000624 if (NumOccurrences > 1)
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000625 return error(": must occur exactly one time!", ArgName);
626 // Fall through
627 case OneOrMore:
Chris Lattnercaccd762001-10-27 05:54:17 +0000628 case ZeroOrMore:
629 case ConsumeAfter: break;
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000630 default: return error(": bad num occurrences flag value!");
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000631 }
632
Reid Spencer1e13fd22004-08-13 19:47:30 +0000633 return handleOccurrence(pos, ArgName, Value);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000634}
635
Chris Lattner331de232002-07-22 02:07:59 +0000636// addArgument - Tell the system that this Option subclass will handle all
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000637// occurrences of -ArgStr on the command line.
Chris Lattner331de232002-07-22 02:07:59 +0000638//
639void Option::addArgument(const char *ArgStr) {
640 if (ArgStr[0])
641 AddArgument(ArgStr, this);
Chris Lattner9cf3d472003-07-30 17:34:02 +0000642
643 if (getFormattingFlag() == Positional)
Chris Lattner90aa8392006-10-04 21:52:35 +0000644 PositionalOptions->push_back(this);
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000645 else if (getNumOccurrencesFlag() == ConsumeAfter) {
Chris Lattner90aa8392006-10-04 21:52:35 +0000646 if (!PositionalOptions->empty() &&
647 PositionalOptions->front()->getNumOccurrencesFlag() == ConsumeAfter)
Chris Lattner9cf3d472003-07-30 17:34:02 +0000648 error("Cannot specify more than one option with cl::ConsumeAfter!");
Chris Lattner90aa8392006-10-04 21:52:35 +0000649 PositionalOptions->insert(PositionalOptions->begin(), this);
Chris Lattner331de232002-07-22 02:07:59 +0000650 }
651}
652
653
654// getValueStr - Get the value description string, using "DefaultMsg" if nothing
655// has been specified yet.
656//
657static const char *getValueStr(const Option &O, const char *DefaultMsg) {
658 if (O.ValueStr[0] == 0) return DefaultMsg;
659 return O.ValueStr;
660}
661
662//===----------------------------------------------------------------------===//
663// cl::alias class implementation
664//
665
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000666// Return the width of the option tag for printing...
Chris Lattner331de232002-07-22 02:07:59 +0000667unsigned alias::getOptionWidth() const {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000668 return std::strlen(ArgStr)+6;
669}
670
Chris Lattnera0de8432006-04-28 05:36:25 +0000671// Print out the option for the alias.
Chris Lattner331de232002-07-22 02:07:59 +0000672void alias::printOptionInfo(unsigned GlobalWidth) const {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000673 unsigned L = std::strlen(ArgStr);
Bill Wendlinge8156192006-12-07 01:30:32 +0000674 cout << " -" << ArgStr << std::string(GlobalWidth-L-6, ' ') << " - "
675 << HelpStr << "\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000676}
677
678
Chris Lattner331de232002-07-22 02:07:59 +0000679
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000680//===----------------------------------------------------------------------===//
Chris Lattner331de232002-07-22 02:07:59 +0000681// Parser Implementation code...
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000682//
683
Chris Lattner9b14eb52002-08-07 18:36:37 +0000684// basic_parser implementation
685//
686
687// Return the width of the option tag for printing...
688unsigned basic_parser_impl::getOptionWidth(const Option &O) const {
689 unsigned Len = std::strlen(O.ArgStr);
690 if (const char *ValName = getValueName())
691 Len += std::strlen(getValueStr(O, ValName))+3;
692
693 return Len + 6;
694}
695
Misha Brukmanf976c852005-04-21 22:55:34 +0000696// printOptionInfo - Print out information about this option. The
Chris Lattner9b14eb52002-08-07 18:36:37 +0000697// to-be-maintained width is specified.
698//
699void basic_parser_impl::printOptionInfo(const Option &O,
700 unsigned GlobalWidth) const {
Bill Wendlinge8156192006-12-07 01:30:32 +0000701 cout << " -" << O.ArgStr;
Chris Lattner9b14eb52002-08-07 18:36:37 +0000702
703 if (const char *ValName = getValueName())
Bill Wendlinge8156192006-12-07 01:30:32 +0000704 cout << "=<" << getValueStr(O, ValName) << ">";
Chris Lattner9b14eb52002-08-07 18:36:37 +0000705
Bill Wendlinge8156192006-12-07 01:30:32 +0000706 cout << std::string(GlobalWidth-getOptionWidth(O), ' ') << " - "
707 << O.HelpStr << "\n";
Chris Lattner9b14eb52002-08-07 18:36:37 +0000708}
709
710
711
712
Chris Lattner331de232002-07-22 02:07:59 +0000713// parser<bool> implementation
714//
Chris Lattner9b14eb52002-08-07 18:36:37 +0000715bool parser<bool>::parse(Option &O, const char *ArgName,
Chris Lattnerca6433f2003-05-22 20:06:43 +0000716 const std::string &Arg, bool &Value) {
Misha Brukmanf976c852005-04-21 22:55:34 +0000717 if (Arg == "" || Arg == "true" || Arg == "TRUE" || Arg == "True" ||
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000718 Arg == "1") {
719 Value = true;
720 } else if (Arg == "false" || Arg == "FALSE" || Arg == "False" || Arg == "0") {
721 Value = false;
722 } else {
Chris Lattner331de232002-07-22 02:07:59 +0000723 return O.error(": '" + Arg +
724 "' is invalid value for boolean argument! Try 0 or 1");
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000725 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000726 return false;
727}
728
Chris Lattner331de232002-07-22 02:07:59 +0000729// parser<int> implementation
730//
Chris Lattner9b14eb52002-08-07 18:36:37 +0000731bool parser<int>::parse(Option &O, const char *ArgName,
Chris Lattnerca6433f2003-05-22 20:06:43 +0000732 const std::string &Arg, int &Value) {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000733 char *End;
Chris Lattnerd2a6fc32003-06-28 15:47:20 +0000734 Value = (int)strtol(Arg.c_str(), &End, 0);
Misha Brukmanf976c852005-04-21 22:55:34 +0000735 if (*End != 0)
Chris Lattner331de232002-07-22 02:07:59 +0000736 return O.error(": '" + Arg + "' value invalid for integer argument!");
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000737 return false;
738}
739
Chris Lattnerd2a6fc32003-06-28 15:47:20 +0000740// parser<unsigned> implementation
741//
742bool parser<unsigned>::parse(Option &O, const char *ArgName,
743 const std::string &Arg, unsigned &Value) {
744 char *End;
Brian Gaeke2d6a2362003-10-10 17:01:36 +0000745 errno = 0;
746 unsigned long V = strtoul(Arg.c_str(), &End, 0);
Chris Lattnerd2a6fc32003-06-28 15:47:20 +0000747 Value = (unsigned)V;
Brian Gaeke2d6a2362003-10-10 17:01:36 +0000748 if (((V == ULONG_MAX) && (errno == ERANGE))
749 || (*End != 0)
750 || (Value != V))
Chris Lattnerd2a6fc32003-06-28 15:47:20 +0000751 return O.error(": '" + Arg + "' value invalid for uint argument!");
752 return false;
753}
754
Chris Lattner9b14eb52002-08-07 18:36:37 +0000755// parser<double>/parser<float> implementation
Chris Lattnerd215fd12001-10-13 06:53:19 +0000756//
Chris Lattnerca6433f2003-05-22 20:06:43 +0000757static bool parseDouble(Option &O, const std::string &Arg, double &Value) {
Chris Lattner331de232002-07-22 02:07:59 +0000758 const char *ArgStart = Arg.c_str();
759 char *End;
760 Value = strtod(ArgStart, &End);
Misha Brukmanf976c852005-04-21 22:55:34 +0000761 if (*End != 0)
Chris Lattner331de232002-07-22 02:07:59 +0000762 return O.error(": '" +Arg+ "' value invalid for floating point argument!");
Chris Lattnerd215fd12001-10-13 06:53:19 +0000763 return false;
764}
765
Chris Lattner9b14eb52002-08-07 18:36:37 +0000766bool parser<double>::parse(Option &O, const char *AN,
767 const std::string &Arg, double &Val) {
768 return parseDouble(O, Arg, Val);
Chris Lattner331de232002-07-22 02:07:59 +0000769}
770
Chris Lattner9b14eb52002-08-07 18:36:37 +0000771bool parser<float>::parse(Option &O, const char *AN,
772 const std::string &Arg, float &Val) {
773 double dVal;
774 if (parseDouble(O, Arg, dVal))
775 return true;
776 Val = (float)dVal;
777 return false;
Chris Lattner331de232002-07-22 02:07:59 +0000778}
779
780
Chris Lattner331de232002-07-22 02:07:59 +0000781
782// generic_parser_base implementation
783//
784
Chris Lattneraa852bb2002-07-23 17:15:12 +0000785// findOption - Return the option number corresponding to the specified
786// argument string. If the option is not found, getNumOptions() is returned.
787//
788unsigned generic_parser_base::findOption(const char *Name) {
789 unsigned i = 0, e = getNumOptions();
Chris Lattnerca6433f2003-05-22 20:06:43 +0000790 std::string N(Name);
Chris Lattneraa852bb2002-07-23 17:15:12 +0000791
792 while (i != e)
793 if (getOption(i) == N)
794 return i;
795 else
796 ++i;
797 return e;
798}
799
800
Chris Lattner331de232002-07-22 02:07:59 +0000801// Return the width of the option tag for printing...
802unsigned generic_parser_base::getOptionWidth(const Option &O) const {
803 if (O.hasArgStr()) {
804 unsigned Size = std::strlen(O.ArgStr)+6;
805 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
806 Size = std::max(Size, (unsigned)std::strlen(getOption(i))+8);
807 return Size;
808 } else {
809 unsigned BaseSize = 0;
810 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
811 BaseSize = std::max(BaseSize, (unsigned)std::strlen(getOption(i))+8);
812 return BaseSize;
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000813 }
814}
815
Misha Brukmanf976c852005-04-21 22:55:34 +0000816// printOptionInfo - Print out information about this option. The
Chris Lattner331de232002-07-22 02:07:59 +0000817// to-be-maintained width is specified.
818//
819void generic_parser_base::printOptionInfo(const Option &O,
820 unsigned GlobalWidth) const {
821 if (O.hasArgStr()) {
822 unsigned L = std::strlen(O.ArgStr);
Bill Wendlinge8156192006-12-07 01:30:32 +0000823 cout << " -" << O.ArgStr << std::string(GlobalWidth-L-6, ' ')
824 << " - " << O.HelpStr << "\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000825
Chris Lattner331de232002-07-22 02:07:59 +0000826 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
827 unsigned NumSpaces = GlobalWidth-strlen(getOption(i))-8;
Bill Wendlinge8156192006-12-07 01:30:32 +0000828 cout << " =" << getOption(i) << std::string(NumSpaces, ' ')
829 << " - " << getDescription(i) << "\n";
Chris Lattner9c9be482002-01-31 00:42:56 +0000830 }
Chris Lattner331de232002-07-22 02:07:59 +0000831 } else {
832 if (O.HelpStr[0])
Bill Wendlinge8156192006-12-07 01:30:32 +0000833 cout << " " << O.HelpStr << "\n";
Chris Lattner331de232002-07-22 02:07:59 +0000834 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
835 unsigned L = std::strlen(getOption(i));
Bill Wendlinge8156192006-12-07 01:30:32 +0000836 cout << " -" << getOption(i) << std::string(GlobalWidth-L-8, ' ')
837 << " - " << getDescription(i) << "\n";
Chris Lattner331de232002-07-22 02:07:59 +0000838 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000839 }
840}
841
842
843//===----------------------------------------------------------------------===//
Chris Lattner331de232002-07-22 02:07:59 +0000844// --help and --help-hidden option implementation
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000845//
Reid Spencerad0846b2004-11-14 22:04:00 +0000846
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000847namespace {
848
Chris Lattner331de232002-07-22 02:07:59 +0000849class HelpPrinter {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000850 unsigned MaxArgLen;
851 const Option *EmptyArg;
852 const bool ShowHidden;
853
Chris Lattner331de232002-07-22 02:07:59 +0000854 // isHidden/isReallyHidden - Predicates to be used to filter down arg lists.
Chris Lattnerca6433f2003-05-22 20:06:43 +0000855 inline static bool isHidden(std::pair<std::string, Option *> &OptPair) {
Chris Lattner331de232002-07-22 02:07:59 +0000856 return OptPair.second->getOptionHiddenFlag() >= Hidden;
857 }
Chris Lattnerca6433f2003-05-22 20:06:43 +0000858 inline static bool isReallyHidden(std::pair<std::string, Option *> &OptPair) {
Chris Lattner331de232002-07-22 02:07:59 +0000859 return OptPair.second->getOptionHiddenFlag() == ReallyHidden;
860 }
861
862public:
863 HelpPrinter(bool showHidden) : ShowHidden(showHidden) {
864 EmptyArg = 0;
865 }
866
867 void operator=(bool Value) {
868 if (Value == false) return;
869
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000870 // Copy Options into a vector so we can sort them as we like...
Chris Lattner90aa8392006-10-04 21:52:35 +0000871 std::vector<std::pair<std::string, Option*> > Opts;
Chris Lattneraf035f32007-04-05 21:58:17 +0000872 copy(OptionsMap->begin(), OptionsMap->end(), std::back_inserter(Opts));
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000873
874 // Eliminate Hidden or ReallyHidden arguments, depending on ShowHidden
Chris Lattner90aa8392006-10-04 21:52:35 +0000875 Opts.erase(std::remove_if(Opts.begin(), Opts.end(),
876 std::ptr_fun(ShowHidden ? isReallyHidden : isHidden)),
877 Opts.end());
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000878
879 // Eliminate duplicate entries in table (from enum flags options, f.e.)
Chris Lattner331de232002-07-22 02:07:59 +0000880 { // Give OptionSet a scope
881 std::set<Option*> OptionSet;
Chris Lattner90aa8392006-10-04 21:52:35 +0000882 for (unsigned i = 0; i != Opts.size(); ++i)
883 if (OptionSet.count(Opts[i].second) == 0)
884 OptionSet.insert(Opts[i].second); // Add new entry to set
Chris Lattner331de232002-07-22 02:07:59 +0000885 else
Chris Lattner90aa8392006-10-04 21:52:35 +0000886 Opts.erase(Opts.begin()+i--); // Erase duplicate
Chris Lattner331de232002-07-22 02:07:59 +0000887 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000888
889 if (ProgramOverview)
Bill Wendlinge8156192006-12-07 01:30:32 +0000890 cout << "OVERVIEW:" << ProgramOverview << "\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000891
Bill Wendlinge8156192006-12-07 01:30:32 +0000892 cout << "USAGE: " << ProgramName << " [options]";
Chris Lattner331de232002-07-22 02:07:59 +0000893
Chris Lattner90aa8392006-10-04 21:52:35 +0000894 // Print out the positional options.
895 std::vector<Option*> &PosOpts = *PositionalOptions;
Chris Lattner331de232002-07-22 02:07:59 +0000896 Option *CAOpt = 0; // The cl::ConsumeAfter option, if it exists...
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000897 if (!PosOpts.empty() && PosOpts[0]->getNumOccurrencesFlag() == ConsumeAfter)
Chris Lattner331de232002-07-22 02:07:59 +0000898 CAOpt = PosOpts[0];
899
Chris Lattner9cf3d472003-07-30 17:34:02 +0000900 for (unsigned i = CAOpt != 0, e = PosOpts.size(); i != e; ++i) {
901 if (PosOpts[i]->ArgStr[0])
Bill Wendlinge8156192006-12-07 01:30:32 +0000902 cout << " --" << PosOpts[i]->ArgStr;
903 cout << " " << PosOpts[i]->HelpStr;
Chris Lattner9cf3d472003-07-30 17:34:02 +0000904 }
Chris Lattner331de232002-07-22 02:07:59 +0000905
906 // Print the consume after option info if it exists...
Bill Wendlinge8156192006-12-07 01:30:32 +0000907 if (CAOpt) cout << " " << CAOpt->HelpStr;
Chris Lattner331de232002-07-22 02:07:59 +0000908
Bill Wendlinge8156192006-12-07 01:30:32 +0000909 cout << "\n\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000910
911 // Compute the maximum argument length...
912 MaxArgLen = 0;
Chris Lattner90aa8392006-10-04 21:52:35 +0000913 for (unsigned i = 0, e = Opts.size(); i != e; ++i)
914 MaxArgLen = std::max(MaxArgLen, Opts[i].second->getOptionWidth());
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000915
Bill Wendlinge8156192006-12-07 01:30:32 +0000916 cout << "OPTIONS:\n";
Chris Lattner90aa8392006-10-04 21:52:35 +0000917 for (unsigned i = 0, e = Opts.size(); i != e; ++i)
918 Opts[i].second->printOptionInfo(MaxArgLen);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000919
Chris Lattnerc540ebb2004-11-19 17:08:15 +0000920 // Print any extra help the user has declared.
Chris Lattner90aa8392006-10-04 21:52:35 +0000921 for (std::vector<const char *>::iterator I = MoreHelp->begin(),
922 E = MoreHelp->end(); I != E; ++I)
Bill Wendlinge8156192006-12-07 01:30:32 +0000923 cout << *I;
Chris Lattner90aa8392006-10-04 21:52:35 +0000924 MoreHelp->clear();
Reid Spencerad0846b2004-11-14 22:04:00 +0000925
Reid Spencer9bbba0912004-11-16 06:11:52 +0000926 // Halt the program since help information was printed
Chris Lattneraf035f32007-04-05 21:58:17 +0000927 OptionsMap->clear(); // Don't bother making option dtors remove from map.
Chris Lattner331de232002-07-22 02:07:59 +0000928 exit(1);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000929 }
930};
Chris Lattner500d8bf2006-10-12 22:09:17 +0000931} // End anonymous namespace
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000932
Chris Lattner331de232002-07-22 02:07:59 +0000933// Define the two HelpPrinter instances that are used to print out help, or
934// help-hidden...
935//
Chris Lattner500d8bf2006-10-12 22:09:17 +0000936static HelpPrinter NormalPrinter(false);
937static HelpPrinter HiddenPrinter(true);
Chris Lattner331de232002-07-22 02:07:59 +0000938
Chris Lattner500d8bf2006-10-12 22:09:17 +0000939static cl::opt<HelpPrinter, true, parser<bool> >
Chris Lattner4bf7afc2005-05-13 19:49:09 +0000940HOp("help", cl::desc("Display available options (--help-hidden for more)"),
Chris Lattner9b14eb52002-08-07 18:36:37 +0000941 cl::location(NormalPrinter), cl::ValueDisallowed);
Chris Lattner331de232002-07-22 02:07:59 +0000942
Chris Lattner500d8bf2006-10-12 22:09:17 +0000943static cl::opt<HelpPrinter, true, parser<bool> >
Chris Lattner4bf7afc2005-05-13 19:49:09 +0000944HHOp("help-hidden", cl::desc("Display all available options"),
Chris Lattner9b14eb52002-08-07 18:36:37 +0000945 cl::location(HiddenPrinter), cl::Hidden, cl::ValueDisallowed);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000946
Chris Lattner500d8bf2006-10-12 22:09:17 +0000947static void (*OverrideVersionPrinter)() = 0;
Reid Spencer515b5b32006-06-05 16:22:56 +0000948
Chris Lattner500d8bf2006-10-12 22:09:17 +0000949namespace {
Reid Spencer515b5b32006-06-05 16:22:56 +0000950class VersionPrinter {
951public:
Devang Patelaed293d2007-02-01 01:43:37 +0000952 void print() {
Bill Wendlinge8156192006-12-07 01:30:32 +0000953 cout << "Low Level Virtual Machine (http://llvm.org/):\n";
954 cout << " " << PACKAGE_NAME << " version " << PACKAGE_VERSION;
Chris Lattner3fc2f4e2006-07-06 18:33:03 +0000955#ifdef LLVM_VERSION_INFO
Bill Wendlinge8156192006-12-07 01:30:32 +0000956 cout << LLVM_VERSION_INFO;
Reid Spencer515b5b32006-06-05 16:22:56 +0000957#endif
Bill Wendlinge8156192006-12-07 01:30:32 +0000958 cout << "\n ";
Chris Lattner3fc2f4e2006-07-06 18:33:03 +0000959#ifndef __OPTIMIZE__
Bill Wendlinge8156192006-12-07 01:30:32 +0000960 cout << "DEBUG build";
Chris Lattner3fc2f4e2006-07-06 18:33:03 +0000961#else
Bill Wendlinge8156192006-12-07 01:30:32 +0000962 cout << "Optimized build";
Chris Lattner3fc2f4e2006-07-06 18:33:03 +0000963#endif
964#ifndef NDEBUG
Bill Wendlinge8156192006-12-07 01:30:32 +0000965 cout << " with assertions";
Chris Lattner3fc2f4e2006-07-06 18:33:03 +0000966#endif
Bill Wendlinge8156192006-12-07 01:30:32 +0000967 cout << ".\n";
Devang Patelaed293d2007-02-01 01:43:37 +0000968 }
969 void operator=(bool OptionWasSpecified) {
970 if (OptionWasSpecified) {
971 if (OverrideVersionPrinter == 0) {
972 print();
Chris Lattneraf035f32007-04-05 21:58:17 +0000973 OptionsMap->clear();// Don't bother making option dtors remove from map.
Reid Spencer515b5b32006-06-05 16:22:56 +0000974 exit(1);
975 } else {
976 (*OverrideVersionPrinter)();
977 exit(1);
978 }
979 }
980 }
981};
Chris Lattner500d8bf2006-10-12 22:09:17 +0000982} // End anonymous namespace
Reid Spencer515b5b32006-06-05 16:22:56 +0000983
984
Reid Spencer69105f32004-08-04 00:36:06 +0000985// Define the --version option that prints out the LLVM version for the tool
Chris Lattner500d8bf2006-10-12 22:09:17 +0000986static VersionPrinter VersionPrinterInstance;
987
988static cl::opt<VersionPrinter, true, parser<bool> >
Chris Lattner4bf7afc2005-05-13 19:49:09 +0000989VersOp("version", cl::desc("Display the version of this program"),
Reid Spencer69105f32004-08-04 00:36:06 +0000990 cl::location(VersionPrinterInstance), cl::ValueDisallowed);
991
Reid Spencer9bbba0912004-11-16 06:11:52 +0000992// Utility function for printing the help message.
993void cl::PrintHelpMessage() {
Misha Brukmanf976c852005-04-21 22:55:34 +0000994 // This looks weird, but it actually prints the help message. The
Reid Spencer5cc498f2004-11-16 06:50:36 +0000995 // NormalPrinter variable is a HelpPrinter and the help gets printed when
996 // its operator= is invoked. That's because the "normal" usages of the
Misha Brukmanf976c852005-04-21 22:55:34 +0000997 // help printer is to be assigned true/false depending on whether the
Reid Spencer5cc498f2004-11-16 06:50:36 +0000998 // --help option was given or not. Since we're circumventing that we have
999 // to make it look like --help was given, so we assign true.
Reid Spencer9bbba0912004-11-16 06:11:52 +00001000 NormalPrinter = true;
1001}
Reid Spencer515b5b32006-06-05 16:22:56 +00001002
Devang Patelaed293d2007-02-01 01:43:37 +00001003/// Utility function for printing version number.
1004void cl::PrintVersionMessage() {
1005 VersionPrinterInstance.print();
1006}
1007
Reid Spencer515b5b32006-06-05 16:22:56 +00001008void cl::SetVersionPrinter(void (*func)()) {
1009 OverrideVersionPrinter = func;
1010}