blob: 920811a06c6a362a4aa2a911b154af5b5b98261d [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>
27#include <set>
Brian Gaeke2d6a2362003-10-10 17:01:36 +000028#include <cstdlib>
29#include <cerrno>
Chris Lattner51140042004-07-03 01:21:05 +000030#include <cstring>
Chris Lattner2cdd21c2003-12-14 21:35:53 +000031using namespace llvm;
Chris Lattnerdbab15a2001-07-23 17:17:47 +000032using namespace cl;
33
Chris Lattner7422a762006-08-27 12:45:47 +000034//===----------------------------------------------------------------------===//
35// Template instantiations and anchors.
36//
37TEMPLATE_INSTANTIATION(class basic_parser<bool>);
38TEMPLATE_INSTANTIATION(class basic_parser<int>);
39TEMPLATE_INSTANTIATION(class basic_parser<unsigned>);
40TEMPLATE_INSTANTIATION(class basic_parser<double>);
41TEMPLATE_INSTANTIATION(class basic_parser<float>);
42TEMPLATE_INSTANTIATION(class basic_parser<std::string>);
43
44TEMPLATE_INSTANTIATION(class opt<unsigned>);
45TEMPLATE_INSTANTIATION(class opt<int>);
46TEMPLATE_INSTANTIATION(class opt<std::string>);
47TEMPLATE_INSTANTIATION(class opt<bool>);
48
49void Option::anchor() {}
50void basic_parser_impl::anchor() {}
51void parser<bool>::anchor() {}
52void parser<int>::anchor() {}
53void parser<unsigned>::anchor() {}
54void parser<double>::anchor() {}
55void parser<float>::anchor() {}
56void parser<std::string>::anchor() {}
57
58//===----------------------------------------------------------------------===//
59
Chris Lattnerefa3da52006-10-13 00:06:24 +000060// Globals for name and overview of program. Program name is not a string to
61// avoid static ctor/dtor issues.
62static char ProgramName[80] = "<premain>";
Reid Spencere1cc1502004-09-01 04:41:28 +000063static const char *ProgramOverview = 0;
64
Chris Lattnerc540ebb2004-11-19 17:08:15 +000065// This collects additional help to be printed.
Chris Lattner90aa8392006-10-04 21:52:35 +000066static ManagedStatic<std::vector<const char*> > MoreHelp;
Chris Lattnerc540ebb2004-11-19 17:08:15 +000067
Chris Lattner90aa8392006-10-04 21:52:35 +000068extrahelp::extrahelp(const char *Help)
Chris Lattnerc540ebb2004-11-19 17:08:15 +000069 : morehelp(Help) {
Chris Lattner90aa8392006-10-04 21:52:35 +000070 MoreHelp->push_back(Help);
Chris Lattnerc540ebb2004-11-19 17:08:15 +000071}
72
Chris Lattner331de232002-07-22 02:07:59 +000073//===----------------------------------------------------------------------===//
Chris Lattner7422a762006-08-27 12:45:47 +000074// Basic, shared command line option processing machinery.
Chris Lattner331de232002-07-22 02:07:59 +000075//
76
Chris Lattner90aa8392006-10-04 21:52:35 +000077static ManagedStatic<std::map<std::string, Option*> > Options;
78static ManagedStatic<std::vector<Option*> > PositionalOptions;
Chris Lattnere8e258b2002-07-29 20:58:42 +000079
Chris Lattnerca6433f2003-05-22 20:06:43 +000080static Option *getOption(const std::string &Str) {
Chris Lattner90aa8392006-10-04 21:52:35 +000081 std::map<std::string,Option*>::iterator I = Options->find(Str);
82 return I != Options->end() ? I->second : 0;
Chris Lattner331de232002-07-22 02:07:59 +000083}
84
Chris Lattnere8e258b2002-07-29 20:58:42 +000085static void AddArgument(const char *ArgName, Option *Opt) {
86 if (getOption(ArgName)) {
Bill Wendlinge8156192006-12-07 01:30:32 +000087 cerr << ProgramName << ": CommandLine Error: Argument '"
88 << ArgName << "' defined more than once!\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +000089 } else {
Chris Lattnerf78032f2001-11-26 18:58:34 +000090 // Add argument to the argument map!
Chris Lattner90aa8392006-10-04 21:52:35 +000091 (*Options)[ArgName] = Opt;
Chris Lattnere8e258b2002-07-29 20:58:42 +000092 }
93}
94
95// RemoveArgument - It's possible that the argument is no longer in the map if
96// options have already been processed and the map has been deleted!
Misha Brukmanf976c852005-04-21 22:55:34 +000097//
Chris Lattnere8e258b2002-07-29 20:58:42 +000098static void RemoveArgument(const char *ArgName, Option *Opt) {
Chris Lattner90aa8392006-10-04 21:52:35 +000099 if (Options->empty()) return;
Tanya Lattnerc4ae8e92004-11-20 23:35:20 +0000100
Chris Lattnerf98cfc72004-07-18 21:56:20 +0000101#ifndef NDEBUG
102 // This disgusting HACK is brought to you courtesy of GCC 3.3.2, which ICE's
103 // If we pass ArgName directly into getOption here.
104 std::string Tmp = ArgName;
105 assert(getOption(Tmp) == Opt && "Arg not in map!");
106#endif
Chris Lattner90aa8392006-10-04 21:52:35 +0000107 Options->erase(ArgName);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000108}
109
Chris Lattnercaccd762001-10-27 05:54:17 +0000110static inline bool ProvideOption(Option *Handler, const char *ArgName,
111 const char *Value, int argc, char **argv,
112 int &i) {
113 // Enforce value requirements
114 switch (Handler->getValueExpectedFlag()) {
115 case ValueRequired:
Chris Lattner6d5857e2005-05-10 23:20:17 +0000116 if (Value == 0) { // No value specified?
Chris Lattnercaccd762001-10-27 05:54:17 +0000117 if (i+1 < argc) { // Steal the next argument, like for '-o filename'
118 Value = argv[++i];
119 } else {
120 return Handler->error(" requires a value!");
121 }
122 }
123 break;
124 case ValueDisallowed:
Chris Lattner6d5857e2005-05-10 23:20:17 +0000125 if (Value)
Misha Brukmanf976c852005-04-21 22:55:34 +0000126 return Handler->error(" does not allow a value! '" +
Chris Lattnerca6433f2003-05-22 20:06:43 +0000127 std::string(Value) + "' specified.");
Chris Lattnercaccd762001-10-27 05:54:17 +0000128 break;
Misha Brukmanf976c852005-04-21 22:55:34 +0000129 case ValueOptional:
Reid Spencere1cc1502004-09-01 04:41:28 +0000130 break;
Misha Brukmanf976c852005-04-21 22:55:34 +0000131 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000132 cerr << ProgramName
133 << ": Bad ValueMask flag! CommandLine usage error:"
134 << Handler->getValueExpectedFlag() << "\n";
Reid Spencere1cc1502004-09-01 04:41:28 +0000135 abort();
136 break;
Chris Lattnercaccd762001-10-27 05:54:17 +0000137 }
138
139 // Run the handler now!
Chris Lattner6d5857e2005-05-10 23:20:17 +0000140 return Handler->addOccurrence(i, ArgName, Value ? Value : "");
Chris Lattnercaccd762001-10-27 05:54:17 +0000141}
142
Misha Brukmanf976c852005-04-21 22:55:34 +0000143static bool ProvidePositionalOption(Option *Handler, const std::string &Arg,
Reid Spencer1e13fd22004-08-13 19:47:30 +0000144 int i) {
145 int Dummy = i;
Chris Lattner9cf3d472003-07-30 17:34:02 +0000146 return ProvideOption(Handler, Handler->ArgStr, Arg.c_str(), 0, 0, Dummy);
Chris Lattner331de232002-07-22 02:07:59 +0000147}
Chris Lattnerf78032f2001-11-26 18:58:34 +0000148
Chris Lattner331de232002-07-22 02:07:59 +0000149
150// Option predicates...
151static inline bool isGrouping(const Option *O) {
152 return O->getFormattingFlag() == cl::Grouping;
153}
154static inline bool isPrefixedOrGrouping(const Option *O) {
155 return isGrouping(O) || O->getFormattingFlag() == cl::Prefix;
156}
157
158// getOptionPred - Check to see if there are any options that satisfy the
159// specified predicate with names that are the prefixes in Name. This is
160// checked by progressively stripping characters off of the name, checking to
161// see if there options that satisfy the predicate. If we find one, return it,
162// otherwise return null.
163//
164static Option *getOptionPred(std::string Name, unsigned &Length,
165 bool (*Pred)(const Option*)) {
Misha Brukmanf976c852005-04-21 22:55:34 +0000166
Chris Lattnere8e258b2002-07-29 20:58:42 +0000167 Option *Op = getOption(Name);
168 if (Op && Pred(Op)) {
Chris Lattner331de232002-07-22 02:07:59 +0000169 Length = Name.length();
Chris Lattnere8e258b2002-07-29 20:58:42 +0000170 return Op;
Chris Lattnerf78032f2001-11-26 18:58:34 +0000171 }
172
Chris Lattner331de232002-07-22 02:07:59 +0000173 if (Name.size() == 1) return 0;
174 do {
175 Name.erase(Name.end()-1, Name.end()); // Chop off the last character...
Chris Lattnere8e258b2002-07-29 20:58:42 +0000176 Op = getOption(Name);
Chris Lattner331de232002-07-22 02:07:59 +0000177
178 // Loop while we haven't found an option and Name still has at least two
179 // characters in it (so that the next iteration will not be the empty
180 // string...
Chris Lattnere8e258b2002-07-29 20:58:42 +0000181 } while ((Op == 0 || !Pred(Op)) && Name.size() > 1);
Chris Lattner331de232002-07-22 02:07:59 +0000182
Chris Lattnere8e258b2002-07-29 20:58:42 +0000183 if (Op && Pred(Op)) {
Chris Lattner331de232002-07-22 02:07:59 +0000184 Length = Name.length();
Chris Lattnere8e258b2002-07-29 20:58:42 +0000185 return Op; // Found one!
Chris Lattner331de232002-07-22 02:07:59 +0000186 }
187 return 0; // No option found!
188}
189
190static bool RequiresValue(const Option *O) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000191 return O->getNumOccurrencesFlag() == cl::Required ||
192 O->getNumOccurrencesFlag() == cl::OneOrMore;
Chris Lattner331de232002-07-22 02:07:59 +0000193}
194
195static bool EatsUnboundedNumberOfValues(const Option *O) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000196 return O->getNumOccurrencesFlag() == cl::ZeroOrMore ||
197 O->getNumOccurrencesFlag() == cl::OneOrMore;
Chris Lattnerf78032f2001-11-26 18:58:34 +0000198}
Chris Lattnercaccd762001-10-27 05:54:17 +0000199
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000200/// ParseCStringVector - Break INPUT up wherever one or more
201/// whitespace characters are found, and store the resulting tokens in
202/// OUTPUT. The tokens stored in OUTPUT are dynamically allocated
203/// using strdup (), so it is the caller's responsibility to free ()
204/// them later.
Brian Gaeke06b06c52003-08-14 22:00:59 +0000205///
Chris Lattner23288582006-08-27 22:10:29 +0000206static void ParseCStringVector(std::vector<char *> &output,
207 const char *input) {
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000208 // Characters which will be treated as token separators:
209 static const char *delims = " \v\f\t\r\n";
210
211 std::string work (input);
212 // Skip past any delims at head of input string.
213 size_t pos = work.find_first_not_of (delims);
214 // If the string consists entirely of delims, then exit early.
215 if (pos == std::string::npos) return;
216 // Otherwise, jump forward to beginning of first word.
217 work = work.substr (pos);
218 // Find position of first delimiter.
219 pos = work.find_first_of (delims);
220
221 while (!work.empty() && pos != std::string::npos) {
222 // Everything from 0 to POS is the next word to copy.
223 output.push_back (strdup (work.substr (0,pos).c_str ()));
224 // Is there another word in the string?
225 size_t nextpos = work.find_first_not_of (delims, pos + 1);
226 if (nextpos != std::string::npos) {
227 // Yes? Then remove delims from beginning ...
228 work = work.substr (work.find_first_not_of (delims, pos + 1));
229 // and find the end of the word.
230 pos = work.find_first_of (delims);
231 } else {
232 // No? (Remainder of string is delims.) End the loop.
233 work = "";
234 pos = std::string::npos;
235 }
236 }
237
238 // If `input' ended with non-delim char, then we'll get here with
239 // the last word of `input' in `work'; copy it now.
240 if (!work.empty ()) {
241 output.push_back (strdup (work.c_str ()));
Brian Gaeke06b06c52003-08-14 22:00:59 +0000242 }
243}
244
245/// ParseEnvironmentOptions - An alternative entry point to the
246/// CommandLine library, which allows you to read the program's name
247/// from the caller (as PROGNAME) and its command-line arguments from
248/// an environment variable (whose name is given in ENVVAR).
249///
Chris Lattnerbf455c22004-05-06 22:04:31 +0000250void cl::ParseEnvironmentOptions(const char *progName, const char *envVar,
251 const char *Overview) {
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000252 // Check args.
Chris Lattnerbf455c22004-05-06 22:04:31 +0000253 assert(progName && "Program name not specified");
254 assert(envVar && "Environment variable name missing");
Misha Brukmanf976c852005-04-21 22:55:34 +0000255
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000256 // Get the environment variable they want us to parse options out of.
Chris Lattner23288582006-08-27 22:10:29 +0000257 const char *envValue = getenv(envVar);
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000258 if (!envValue)
259 return;
260
Brian Gaeke06b06c52003-08-14 22:00:59 +0000261 // Get program's "name", which we wouldn't know without the caller
262 // telling us.
Chris Lattner23288582006-08-27 22:10:29 +0000263 std::vector<char*> newArgv;
264 newArgv.push_back(strdup(progName));
Brian Gaeke06b06c52003-08-14 22:00:59 +0000265
266 // Parse the value of the environment variable into a "command line"
267 // and hand it off to ParseCommandLineOptions().
Chris Lattner23288582006-08-27 22:10:29 +0000268 ParseCStringVector(newArgv, envValue);
269 int newArgc = newArgv.size();
270 ParseCommandLineOptions(newArgc, &newArgv[0], Overview);
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000271
272 // Free all the strdup()ed strings.
Chris Lattner23288582006-08-27 22:10:29 +0000273 for (std::vector<char*>::iterator i = newArgv.begin(), e = newArgv.end();
274 i != e; ++i)
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000275 free (*i);
Brian Gaeke06b06c52003-08-14 22:00:59 +0000276}
277
Chris Lattnerbf455c22004-05-06 22:04:31 +0000278/// LookupOption - Lookup the option specified by the specified option on the
279/// command line. If there is a value specified (after an equal sign) return
280/// that as well.
281static Option *LookupOption(const char *&Arg, const char *&Value) {
282 while (*Arg == '-') ++Arg; // Eat leading dashes
Misha Brukmanf976c852005-04-21 22:55:34 +0000283
Chris Lattnerbf455c22004-05-06 22:04:31 +0000284 const char *ArgEnd = Arg;
285 while (*ArgEnd && *ArgEnd != '=')
Chris Lattner6d5857e2005-05-10 23:20:17 +0000286 ++ArgEnd; // Scan till end of argument name.
Chris Lattnerbf455c22004-05-06 22:04:31 +0000287
Chris Lattner6d5857e2005-05-10 23:20:17 +0000288 if (*ArgEnd == '=') // If we have an equals sign...
289 Value = ArgEnd+1; // Get the value, not the equals
290
Misha Brukmanf976c852005-04-21 22:55:34 +0000291
Chris Lattnerbf455c22004-05-06 22:04:31 +0000292 if (*Arg == 0) return 0;
293
294 // Look up the option.
Chris Lattner90aa8392006-10-04 21:52:35 +0000295 std::map<std::string, Option*> &Opts = *Options;
Chris Lattnerbf455c22004-05-06 22:04:31 +0000296 std::map<std::string, Option*>::iterator I =
297 Opts.find(std::string(Arg, ArgEnd));
298 return (I != Opts.end()) ? I->second : 0;
299}
300
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000301void cl::ParseCommandLineOptions(int &argc, char **argv,
Chris Lattner0c0edf82002-07-25 06:17:51 +0000302 const char *Overview) {
Chris Lattner90aa8392006-10-04 21:52:35 +0000303 assert((!Options->empty() || !PositionalOptions->empty()) &&
Chris Lattner331de232002-07-22 02:07:59 +0000304 "No options specified, or ParseCommandLineOptions called more"
305 " than once!");
Reid Spencer6f4c6072006-08-23 07:10:06 +0000306 sys::Path progname(argv[0]);
Chris Lattnerefa3da52006-10-13 00:06:24 +0000307
308 // Copy the program name into ProgName, making sure not to overflow it.
309 std::string ProgName = sys::Path(argv[0]).getLast();
310 if (ProgName.size() > 79) ProgName.resize(79);
311 strcpy(ProgramName, ProgName.c_str());
312
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000313 ProgramOverview = Overview;
314 bool ErrorParsing = false;
315
Chris Lattner90aa8392006-10-04 21:52:35 +0000316 std::map<std::string, Option*> &Opts = *Options;
317 std::vector<Option*> &PositionalOpts = *PositionalOptions;
Chris Lattner331de232002-07-22 02:07:59 +0000318
319 // Check out the positional arguments to collect information about them.
320 unsigned NumPositionalRequired = 0;
Chris Lattnerde013242005-08-08 17:25:38 +0000321
322 // Determine whether or not there are an unlimited number of positionals
323 bool HasUnlimitedPositionals = false;
324
Chris Lattner331de232002-07-22 02:07:59 +0000325 Option *ConsumeAfterOpt = 0;
326 if (!PositionalOpts.empty()) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000327 if (PositionalOpts[0]->getNumOccurrencesFlag() == cl::ConsumeAfter) {
Chris Lattner331de232002-07-22 02:07:59 +0000328 assert(PositionalOpts.size() > 1 &&
329 "Cannot specify cl::ConsumeAfter without a positional argument!");
330 ConsumeAfterOpt = PositionalOpts[0];
331 }
332
333 // Calculate how many positional values are _required_.
334 bool UnboundedFound = false;
335 for (unsigned i = ConsumeAfterOpt != 0, e = PositionalOpts.size();
336 i != e; ++i) {
337 Option *Opt = PositionalOpts[i];
338 if (RequiresValue(Opt))
339 ++NumPositionalRequired;
340 else if (ConsumeAfterOpt) {
341 // ConsumeAfter cannot be combined with "optional" positional options
Chris Lattner54ec7ae2002-07-22 02:21:57 +0000342 // unless there is only one positional argument...
343 if (PositionalOpts.size() > 2)
344 ErrorParsing |=
345 Opt->error(" error - this positional option will never be matched, "
346 "because it does not Require a value, and a "
347 "cl::ConsumeAfter option is active!");
Chris Lattner9cf3d472003-07-30 17:34:02 +0000348 } else if (UnboundedFound && !Opt->ArgStr[0]) {
349 // This option does not "require" a value... Make sure this option is
350 // not specified after an option that eats all extra arguments, or this
351 // one will never get any!
Chris Lattner331de232002-07-22 02:07:59 +0000352 //
353 ErrorParsing |= Opt->error(" error - option can never match, because "
354 "another positional argument will match an "
355 "unbounded number of values, and this option"
356 " does not require a value!");
357 }
358 UnboundedFound |= EatsUnboundedNumberOfValues(Opt);
359 }
Chris Lattner21e1a792005-08-08 21:57:27 +0000360 HasUnlimitedPositionals = UnboundedFound || ConsumeAfterOpt;
Chris Lattner331de232002-07-22 02:07:59 +0000361 }
362
Reid Spencer1e13fd22004-08-13 19:47:30 +0000363 // PositionalVals - A vector of "positional" arguments we accumulate into
364 // the process at the end...
Chris Lattner331de232002-07-22 02:07:59 +0000365 //
Reid Spencer1e13fd22004-08-13 19:47:30 +0000366 std::vector<std::pair<std::string,unsigned> > PositionalVals;
Chris Lattner331de232002-07-22 02:07:59 +0000367
Chris Lattner9cf3d472003-07-30 17:34:02 +0000368 // If the program has named positional arguments, and the name has been run
369 // across, keep track of which positional argument was named. Otherwise put
370 // the positional args into the PositionalVals list...
371 Option *ActivePositionalArg = 0;
372
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000373 // Loop over all of the arguments... processing them.
Chris Lattner331de232002-07-22 02:07:59 +0000374 bool DashDashFound = false; // Have we read '--'?
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000375 for (int i = 1; i < argc; ++i) {
376 Option *Handler = 0;
Chris Lattner6d5857e2005-05-10 23:20:17 +0000377 const char *Value = 0;
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000378 const char *ArgName = "";
Chris Lattner331de232002-07-22 02:07:59 +0000379
380 // Check to see if this is a positional argument. This argument is
381 // considered to be positional if it doesn't start with '-', if it is "-"
Misha Brukman1115e042003-07-10 21:38:28 +0000382 // itself, or if we have seen "--" already.
Chris Lattner331de232002-07-22 02:07:59 +0000383 //
384 if (argv[i][0] != '-' || argv[i][1] == 0 || DashDashFound) {
385 // Positional argument!
Chris Lattner9cf3d472003-07-30 17:34:02 +0000386 if (ActivePositionalArg) {
Reid Spencer1e13fd22004-08-13 19:47:30 +0000387 ProvidePositionalOption(ActivePositionalArg, argv[i], i);
Chris Lattner9cf3d472003-07-30 17:34:02 +0000388 continue; // We are done!
389 } else if (!PositionalOpts.empty()) {
Reid Spencer1e13fd22004-08-13 19:47:30 +0000390 PositionalVals.push_back(std::make_pair(argv[i],i));
Chris Lattner331de232002-07-22 02:07:59 +0000391
392 // All of the positional arguments have been fulfulled, give the rest to
393 // the consume after option... if it's specified...
394 //
Misha Brukmanf976c852005-04-21 22:55:34 +0000395 if (PositionalVals.size() >= NumPositionalRequired &&
Chris Lattner331de232002-07-22 02:07:59 +0000396 ConsumeAfterOpt != 0) {
397 for (++i; i < argc; ++i)
Reid Spencer1e13fd22004-08-13 19:47:30 +0000398 PositionalVals.push_back(std::make_pair(argv[i],i));
Chris Lattner331de232002-07-22 02:07:59 +0000399 break; // Handle outside of the argument processing loop...
400 }
401
402 // Delay processing positional arguments until the end...
403 continue;
404 }
Chris Lattnerbf455c22004-05-06 22:04:31 +0000405 } else if (argv[i][0] == '-' && argv[i][1] == '-' && argv[i][2] == 0 &&
406 !DashDashFound) {
407 DashDashFound = true; // This is the mythical "--"?
408 continue; // Don't try to process it as an argument itself.
409 } else if (ActivePositionalArg &&
410 (ActivePositionalArg->getMiscFlags() & PositionalEatsArgs)) {
411 // If there is a positional argument eating options, check to see if this
412 // option is another positional argument. If so, treat it as an argument,
413 // otherwise feed it to the eating positional.
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000414 ArgName = argv[i]+1;
Chris Lattnerbf455c22004-05-06 22:04:31 +0000415 Handler = LookupOption(ArgName, Value);
416 if (!Handler || Handler->getFormattingFlag() != cl::Positional) {
Reid Spencer1e13fd22004-08-13 19:47:30 +0000417 ProvidePositionalOption(ActivePositionalArg, argv[i], i);
Chris Lattnerbf455c22004-05-06 22:04:31 +0000418 continue; // We are done!
Chris Lattner331de232002-07-22 02:07:59 +0000419 }
420
Chris Lattnerbf455c22004-05-06 22:04:31 +0000421 } else { // We start with a '-', must be an argument...
422 ArgName = argv[i]+1;
423 Handler = LookupOption(ArgName, Value);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000424
Chris Lattnerbf455c22004-05-06 22:04:31 +0000425 // Check to see if this "option" is really a prefixed or grouped argument.
Reid Spencer5f8448f2004-11-24 06:13:42 +0000426 if (Handler == 0) {
Chris Lattnerbf455c22004-05-06 22:04:31 +0000427 std::string RealName(ArgName);
428 if (RealName.size() > 1) {
Chris Lattner331de232002-07-22 02:07:59 +0000429 unsigned Length = 0;
430 Option *PGOpt = getOptionPred(RealName, Length, isPrefixedOrGrouping);
Misha Brukmanf976c852005-04-21 22:55:34 +0000431
Chris Lattner331de232002-07-22 02:07:59 +0000432 // If the option is a prefixed option, then the value is simply the
433 // rest of the name... so fall through to later processing, by
434 // setting up the argument name flags and value fields.
435 //
436 if (PGOpt && PGOpt->getFormattingFlag() == cl::Prefix) {
Chris Lattnerbf455c22004-05-06 22:04:31 +0000437 Value = ArgName+Length;
438 assert(Opts.find(std::string(ArgName, Value)) != Opts.end() &&
439 Opts.find(std::string(ArgName, Value))->second == PGOpt);
440 Handler = PGOpt;
Chris Lattner331de232002-07-22 02:07:59 +0000441 } else if (PGOpt) {
Chris Lattnerbf455c22004-05-06 22:04:31 +0000442 // This must be a grouped option... handle them now.
Chris Lattner331de232002-07-22 02:07:59 +0000443 assert(isGrouping(PGOpt) && "Broken getOptionPred!");
Misha Brukmanf976c852005-04-21 22:55:34 +0000444
Chris Lattner331de232002-07-22 02:07:59 +0000445 do {
446 // Move current arg name out of RealName into RealArgName...
Chris Lattnerbf455c22004-05-06 22:04:31 +0000447 std::string RealArgName(RealName.begin(),
448 RealName.begin() + Length);
449 RealName.erase(RealName.begin(), RealName.begin() + Length);
Misha Brukmanf976c852005-04-21 22:55:34 +0000450
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000451 // Because ValueRequired is an invalid flag for grouped arguments,
452 // we don't need to pass argc/argv in...
453 //
Chris Lattner331de232002-07-22 02:07:59 +0000454 assert(PGOpt->getValueExpectedFlag() != cl::ValueRequired &&
455 "Option can not be cl::Grouping AND cl::ValueRequired!");
456 int Dummy;
Chris Lattnerbf455c22004-05-06 22:04:31 +0000457 ErrorParsing |= ProvideOption(PGOpt, RealArgName.c_str(),
Chris Lattner6d5857e2005-05-10 23:20:17 +0000458 0, 0, 0, Dummy);
Misha Brukmanf976c852005-04-21 22:55:34 +0000459
Chris Lattner331de232002-07-22 02:07:59 +0000460 // Get the next grouping option...
Chris Lattnerbf455c22004-05-06 22:04:31 +0000461 PGOpt = getOptionPred(RealName, Length, isGrouping);
462 } while (PGOpt && Length != RealName.size());
Misha Brukmanf976c852005-04-21 22:55:34 +0000463
Chris Lattnerbf455c22004-05-06 22:04:31 +0000464 Handler = PGOpt; // Ate all of the options.
Chris Lattner331de232002-07-22 02:07:59 +0000465 }
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000466 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000467 }
468 }
469
470 if (Handler == 0) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000471 cerr << ProgramName << ": Unknown command line argument '"
472 << argv[i] << "'. Try: '" << argv[0] << " --help'\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000473 ErrorParsing = true;
474 continue;
475 }
476
Chris Lattner72fb8e52003-05-22 20:26:17 +0000477 // Check to see if this option accepts a comma separated list of values. If
478 // it does, we have to split up the value into multiple values...
Chris Lattner6d5857e2005-05-10 23:20:17 +0000479 if (Value && Handler->getMiscFlags() & CommaSeparated) {
Chris Lattner72fb8e52003-05-22 20:26:17 +0000480 std::string Val(Value);
481 std::string::size_type Pos = Val.find(',');
482
483 while (Pos != std::string::npos) {
484 // Process the portion before the comma...
485 ErrorParsing |= ProvideOption(Handler, ArgName,
486 std::string(Val.begin(),
487 Val.begin()+Pos).c_str(),
488 argc, argv, i);
489 // Erase the portion before the comma, AND the comma...
490 Val.erase(Val.begin(), Val.begin()+Pos+1);
491 Value += Pos+1; // Increment the original value pointer as well...
492
493 // Check for another comma...
494 Pos = Val.find(',');
495 }
496 }
Chris Lattner9cf3d472003-07-30 17:34:02 +0000497
498 // If this is a named positional argument, just remember that it is the
499 // active one...
500 if (Handler->getFormattingFlag() == cl::Positional)
501 ActivePositionalArg = Handler;
Misha Brukmanf976c852005-04-21 22:55:34 +0000502 else
Chris Lattner9cf3d472003-07-30 17:34:02 +0000503 ErrorParsing |= ProvideOption(Handler, ArgName, Value, argc, argv, i);
Chris Lattner331de232002-07-22 02:07:59 +0000504 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000505
Chris Lattner331de232002-07-22 02:07:59 +0000506 // Check and handle positional arguments now...
507 if (NumPositionalRequired > PositionalVals.size()) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000508 cerr << ProgramName
509 << ": Not enough positional command line arguments specified!\n"
510 << "Must specify at least " << NumPositionalRequired
511 << " positional arguments: See: " << argv[0] << " --help\n";
Chris Lattner79959d22006-01-17 00:32:28 +0000512
Chris Lattner331de232002-07-22 02:07:59 +0000513 ErrorParsing = true;
Chris Lattnerde013242005-08-08 17:25:38 +0000514 } else if (!HasUnlimitedPositionals
515 && PositionalVals.size() > PositionalOpts.size()) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000516 cerr << ProgramName
517 << ": Too many positional arguments specified!\n"
518 << "Can specify at most " << PositionalOpts.size()
519 << " positional arguments: See: " << argv[0] << " --help\n";
Chris Lattnerde013242005-08-08 17:25:38 +0000520 ErrorParsing = true;
Chris Lattner331de232002-07-22 02:07:59 +0000521
522 } else if (ConsumeAfterOpt == 0) {
523 // Positional args have already been handled if ConsumeAfter is specified...
524 unsigned ValNo = 0, NumVals = PositionalVals.size();
525 for (unsigned i = 0, e = PositionalOpts.size(); i != e; ++i) {
526 if (RequiresValue(PositionalOpts[i])) {
Misha Brukmanf976c852005-04-21 22:55:34 +0000527 ProvidePositionalOption(PositionalOpts[i], PositionalVals[ValNo].first,
Reid Spencer1e13fd22004-08-13 19:47:30 +0000528 PositionalVals[ValNo].second);
529 ValNo++;
Chris Lattner331de232002-07-22 02:07:59 +0000530 --NumPositionalRequired; // We fulfilled our duty...
531 }
532
533 // If we _can_ give this option more arguments, do so now, as long as we
534 // do not give it values that others need. 'Done' controls whether the
535 // option even _WANTS_ any more.
536 //
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000537 bool Done = PositionalOpts[i]->getNumOccurrencesFlag() == cl::Required;
Chris Lattner331de232002-07-22 02:07:59 +0000538 while (NumVals-ValNo > NumPositionalRequired && !Done) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000539 switch (PositionalOpts[i]->getNumOccurrencesFlag()) {
Chris Lattner331de232002-07-22 02:07:59 +0000540 case cl::Optional:
541 Done = true; // Optional arguments want _at most_ one value
542 // FALL THROUGH
543 case cl::ZeroOrMore: // Zero or more will take all they can get...
544 case cl::OneOrMore: // One or more will take all they can get...
Reid Spencer1e13fd22004-08-13 19:47:30 +0000545 ProvidePositionalOption(PositionalOpts[i],
546 PositionalVals[ValNo].first,
547 PositionalVals[ValNo].second);
548 ValNo++;
Chris Lattner331de232002-07-22 02:07:59 +0000549 break;
550 default:
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000551 assert(0 && "Internal error, unexpected NumOccurrences flag in "
Chris Lattner331de232002-07-22 02:07:59 +0000552 "positional argument processing!");
553 }
554 }
Chris Lattnercaccd762001-10-27 05:54:17 +0000555 }
Chris Lattner331de232002-07-22 02:07:59 +0000556 } else {
557 assert(ConsumeAfterOpt && NumPositionalRequired <= PositionalVals.size());
558 unsigned ValNo = 0;
559 for (unsigned j = 1, e = PositionalOpts.size(); j != e; ++j)
Reid Spencer1e13fd22004-08-13 19:47:30 +0000560 if (RequiresValue(PositionalOpts[j])) {
Chris Lattnerfaba8092002-07-24 20:15:13 +0000561 ErrorParsing |= ProvidePositionalOption(PositionalOpts[j],
Reid Spencer1e13fd22004-08-13 19:47:30 +0000562 PositionalVals[ValNo].first,
563 PositionalVals[ValNo].second);
564 ValNo++;
565 }
Chris Lattnerfaba8092002-07-24 20:15:13 +0000566
567 // Handle the case where there is just one positional option, and it's
568 // optional. In this case, we want to give JUST THE FIRST option to the
569 // positional option and keep the rest for the consume after. The above
570 // loop would have assigned no values to positional options in this case.
571 //
Reid Spencer1e13fd22004-08-13 19:47:30 +0000572 if (PositionalOpts.size() == 2 && ValNo == 0 && !PositionalVals.empty()) {
Chris Lattnerfaba8092002-07-24 20:15:13 +0000573 ErrorParsing |= ProvidePositionalOption(PositionalOpts[1],
Reid Spencer1e13fd22004-08-13 19:47:30 +0000574 PositionalVals[ValNo].first,
575 PositionalVals[ValNo].second);
576 ValNo++;
577 }
Misha Brukmanf976c852005-04-21 22:55:34 +0000578
Chris Lattner331de232002-07-22 02:07:59 +0000579 // Handle over all of the rest of the arguments to the
580 // cl::ConsumeAfter command line option...
581 for (; ValNo != PositionalVals.size(); ++ValNo)
582 ErrorParsing |= ProvidePositionalOption(ConsumeAfterOpt,
Reid Spencer1e13fd22004-08-13 19:47:30 +0000583 PositionalVals[ValNo].first,
584 PositionalVals[ValNo].second);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000585 }
586
Chris Lattnerdc4693d2001-07-23 23:02:45 +0000587 // Loop over args and make sure all required args are specified!
Misha Brukmanf976c852005-04-21 22:55:34 +0000588 for (std::map<std::string, Option*>::iterator I = Opts.begin(),
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000589 E = Opts.end(); I != E; ++I) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000590 switch (I->second->getNumOccurrencesFlag()) {
Chris Lattnerdc4693d2001-07-23 23:02:45 +0000591 case Required:
592 case OneOrMore:
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000593 if (I->second->getNumOccurrences() == 0) {
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000594 I->second->error(" must be specified at least once!");
Chris Lattnerf038acb2001-10-24 06:21:56 +0000595 ErrorParsing = true;
596 }
Chris Lattnerdc4693d2001-07-23 23:02:45 +0000597 // Fall through
598 default:
599 break;
600 }
601 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000602
Chris Lattner331de232002-07-22 02:07:59 +0000603 // Free all of the memory allocated to the map. Command line options may only
604 // be processed once!
Chris Lattner90aa8392006-10-04 21:52:35 +0000605 Opts.clear();
Chris Lattner331de232002-07-22 02:07:59 +0000606 PositionalOpts.clear();
Chris Lattner90aa8392006-10-04 21:52:35 +0000607 MoreHelp->clear();
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000608
609 // If we had an error processing our arguments, don't let the program execute
610 if (ErrorParsing) exit(1);
611}
612
613//===----------------------------------------------------------------------===//
614// Option Base class implementation
615//
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000616
Chris Lattnerca6433f2003-05-22 20:06:43 +0000617bool Option::error(std::string Message, const char *ArgName) {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000618 if (ArgName == 0) ArgName = ArgStr;
Chris Lattner331de232002-07-22 02:07:59 +0000619 if (ArgName[0] == 0)
Bill Wendlinge8156192006-12-07 01:30:32 +0000620 cerr << HelpStr; // Be nice for positional arguments
Chris Lattner331de232002-07-22 02:07:59 +0000621 else
Bill Wendlinge8156192006-12-07 01:30:32 +0000622 cerr << ProgramName << ": for the -" << ArgName;
Jim Laskeyabe0e3e2006-08-02 20:15:56 +0000623
Bill Wendlinge8156192006-12-07 01:30:32 +0000624 cerr << " option: " << Message << "\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000625 return true;
626}
627
Chris Lattner6d5857e2005-05-10 23:20:17 +0000628bool Option::addOccurrence(unsigned pos, const char *ArgName,
629 const std::string &Value) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000630 NumOccurrences++; // Increment the number of times we have been seen
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000631
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000632 switch (getNumOccurrencesFlag()) {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000633 case Optional:
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000634 if (NumOccurrences > 1)
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000635 return error(": may only occur zero or one times!", ArgName);
636 break;
637 case Required:
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000638 if (NumOccurrences > 1)
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000639 return error(": must occur exactly one time!", ArgName);
640 // Fall through
641 case OneOrMore:
Chris Lattnercaccd762001-10-27 05:54:17 +0000642 case ZeroOrMore:
643 case ConsumeAfter: break;
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000644 default: return error(": bad num occurrences flag value!");
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000645 }
646
Reid Spencer1e13fd22004-08-13 19:47:30 +0000647 return handleOccurrence(pos, ArgName, Value);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000648}
649
Chris Lattner331de232002-07-22 02:07:59 +0000650// addArgument - Tell the system that this Option subclass will handle all
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000651// occurrences of -ArgStr on the command line.
Chris Lattner331de232002-07-22 02:07:59 +0000652//
653void Option::addArgument(const char *ArgStr) {
654 if (ArgStr[0])
655 AddArgument(ArgStr, this);
Chris Lattner9cf3d472003-07-30 17:34:02 +0000656
657 if (getFormattingFlag() == Positional)
Chris Lattner90aa8392006-10-04 21:52:35 +0000658 PositionalOptions->push_back(this);
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000659 else if (getNumOccurrencesFlag() == ConsumeAfter) {
Chris Lattner90aa8392006-10-04 21:52:35 +0000660 if (!PositionalOptions->empty() &&
661 PositionalOptions->front()->getNumOccurrencesFlag() == ConsumeAfter)
Chris Lattner9cf3d472003-07-30 17:34:02 +0000662 error("Cannot specify more than one option with cl::ConsumeAfter!");
Chris Lattner90aa8392006-10-04 21:52:35 +0000663 PositionalOptions->insert(PositionalOptions->begin(), this);
Chris Lattner331de232002-07-22 02:07:59 +0000664 }
665}
666
Chris Lattneraa852bb2002-07-23 17:15:12 +0000667void Option::removeArgument(const char *ArgStr) {
Chris Lattner9cf3d472003-07-30 17:34:02 +0000668 if (ArgStr[0])
Chris Lattnere8e258b2002-07-29 20:58:42 +0000669 RemoveArgument(ArgStr, this);
Chris Lattner9cf3d472003-07-30 17:34:02 +0000670
671 if (getFormattingFlag() == Positional) {
Chris Lattnerca6433f2003-05-22 20:06:43 +0000672 std::vector<Option*>::iterator I =
Chris Lattner90aa8392006-10-04 21:52:35 +0000673 std::find(PositionalOptions->begin(), PositionalOptions->end(), this);
674 assert(I != PositionalOptions->end() && "Arg not registered!");
675 PositionalOptions->erase(I);
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000676 } else if (getNumOccurrencesFlag() == ConsumeAfter) {
Chris Lattner90aa8392006-10-04 21:52:35 +0000677 assert(!PositionalOptions->empty() && (*PositionalOptions)[0] == this &&
Chris Lattneraa852bb2002-07-23 17:15:12 +0000678 "Arg not registered correctly!");
Chris Lattner90aa8392006-10-04 21:52:35 +0000679 PositionalOptions->erase(PositionalOptions->begin());
Chris Lattneraa852bb2002-07-23 17:15:12 +0000680 }
681}
682
Chris Lattner331de232002-07-22 02:07:59 +0000683
684// getValueStr - Get the value description string, using "DefaultMsg" if nothing
685// has been specified yet.
686//
687static const char *getValueStr(const Option &O, const char *DefaultMsg) {
688 if (O.ValueStr[0] == 0) return DefaultMsg;
689 return O.ValueStr;
690}
691
692//===----------------------------------------------------------------------===//
693// cl::alias class implementation
694//
695
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000696// Return the width of the option tag for printing...
Chris Lattner331de232002-07-22 02:07:59 +0000697unsigned alias::getOptionWidth() const {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000698 return std::strlen(ArgStr)+6;
699}
700
Chris Lattnera0de8432006-04-28 05:36:25 +0000701// Print out the option for the alias.
Chris Lattner331de232002-07-22 02:07:59 +0000702void alias::printOptionInfo(unsigned GlobalWidth) const {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000703 unsigned L = std::strlen(ArgStr);
Bill Wendlinge8156192006-12-07 01:30:32 +0000704 cout << " -" << ArgStr << std::string(GlobalWidth-L-6, ' ') << " - "
705 << HelpStr << "\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000706}
707
708
Chris Lattner331de232002-07-22 02:07:59 +0000709
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000710//===----------------------------------------------------------------------===//
Chris Lattner331de232002-07-22 02:07:59 +0000711// Parser Implementation code...
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000712//
713
Chris Lattner9b14eb52002-08-07 18:36:37 +0000714// basic_parser implementation
715//
716
717// Return the width of the option tag for printing...
718unsigned basic_parser_impl::getOptionWidth(const Option &O) const {
719 unsigned Len = std::strlen(O.ArgStr);
720 if (const char *ValName = getValueName())
721 Len += std::strlen(getValueStr(O, ValName))+3;
722
723 return Len + 6;
724}
725
Misha Brukmanf976c852005-04-21 22:55:34 +0000726// printOptionInfo - Print out information about this option. The
Chris Lattner9b14eb52002-08-07 18:36:37 +0000727// to-be-maintained width is specified.
728//
729void basic_parser_impl::printOptionInfo(const Option &O,
730 unsigned GlobalWidth) const {
Bill Wendlinge8156192006-12-07 01:30:32 +0000731 cout << " -" << O.ArgStr;
Chris Lattner9b14eb52002-08-07 18:36:37 +0000732
733 if (const char *ValName = getValueName())
Bill Wendlinge8156192006-12-07 01:30:32 +0000734 cout << "=<" << getValueStr(O, ValName) << ">";
Chris Lattner9b14eb52002-08-07 18:36:37 +0000735
Bill Wendlinge8156192006-12-07 01:30:32 +0000736 cout << std::string(GlobalWidth-getOptionWidth(O), ' ') << " - "
737 << O.HelpStr << "\n";
Chris Lattner9b14eb52002-08-07 18:36:37 +0000738}
739
740
741
742
Chris Lattner331de232002-07-22 02:07:59 +0000743// parser<bool> implementation
744//
Chris Lattner9b14eb52002-08-07 18:36:37 +0000745bool parser<bool>::parse(Option &O, const char *ArgName,
Chris Lattnerca6433f2003-05-22 20:06:43 +0000746 const std::string &Arg, bool &Value) {
Misha Brukmanf976c852005-04-21 22:55:34 +0000747 if (Arg == "" || Arg == "true" || Arg == "TRUE" || Arg == "True" ||
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000748 Arg == "1") {
749 Value = true;
750 } else if (Arg == "false" || Arg == "FALSE" || Arg == "False" || Arg == "0") {
751 Value = false;
752 } else {
Chris Lattner331de232002-07-22 02:07:59 +0000753 return O.error(": '" + Arg +
754 "' is invalid value for boolean argument! Try 0 or 1");
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000755 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000756 return false;
757}
758
Chris Lattner331de232002-07-22 02:07:59 +0000759// parser<int> implementation
760//
Chris Lattner9b14eb52002-08-07 18:36:37 +0000761bool parser<int>::parse(Option &O, const char *ArgName,
Chris Lattnerca6433f2003-05-22 20:06:43 +0000762 const std::string &Arg, int &Value) {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000763 char *End;
Chris Lattnerd2a6fc32003-06-28 15:47:20 +0000764 Value = (int)strtol(Arg.c_str(), &End, 0);
Misha Brukmanf976c852005-04-21 22:55:34 +0000765 if (*End != 0)
Chris Lattner331de232002-07-22 02:07:59 +0000766 return O.error(": '" + Arg + "' value invalid for integer argument!");
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000767 return false;
768}
769
Chris Lattnerd2a6fc32003-06-28 15:47:20 +0000770// parser<unsigned> implementation
771//
772bool parser<unsigned>::parse(Option &O, const char *ArgName,
773 const std::string &Arg, unsigned &Value) {
774 char *End;
Brian Gaeke2d6a2362003-10-10 17:01:36 +0000775 errno = 0;
776 unsigned long V = strtoul(Arg.c_str(), &End, 0);
Chris Lattnerd2a6fc32003-06-28 15:47:20 +0000777 Value = (unsigned)V;
Brian Gaeke2d6a2362003-10-10 17:01:36 +0000778 if (((V == ULONG_MAX) && (errno == ERANGE))
779 || (*End != 0)
780 || (Value != V))
Chris Lattnerd2a6fc32003-06-28 15:47:20 +0000781 return O.error(": '" + Arg + "' value invalid for uint argument!");
782 return false;
783}
784
Chris Lattner9b14eb52002-08-07 18:36:37 +0000785// parser<double>/parser<float> implementation
Chris Lattnerd215fd12001-10-13 06:53:19 +0000786//
Chris Lattnerca6433f2003-05-22 20:06:43 +0000787static bool parseDouble(Option &O, const std::string &Arg, double &Value) {
Chris Lattner331de232002-07-22 02:07:59 +0000788 const char *ArgStart = Arg.c_str();
789 char *End;
790 Value = strtod(ArgStart, &End);
Misha Brukmanf976c852005-04-21 22:55:34 +0000791 if (*End != 0)
Chris Lattner331de232002-07-22 02:07:59 +0000792 return O.error(": '" +Arg+ "' value invalid for floating point argument!");
Chris Lattnerd215fd12001-10-13 06:53:19 +0000793 return false;
794}
795
Chris Lattner9b14eb52002-08-07 18:36:37 +0000796bool parser<double>::parse(Option &O, const char *AN,
797 const std::string &Arg, double &Val) {
798 return parseDouble(O, Arg, Val);
Chris Lattner331de232002-07-22 02:07:59 +0000799}
800
Chris Lattner9b14eb52002-08-07 18:36:37 +0000801bool parser<float>::parse(Option &O, const char *AN,
802 const std::string &Arg, float &Val) {
803 double dVal;
804 if (parseDouble(O, Arg, dVal))
805 return true;
806 Val = (float)dVal;
807 return false;
Chris Lattner331de232002-07-22 02:07:59 +0000808}
809
810
Chris Lattner331de232002-07-22 02:07:59 +0000811
812// generic_parser_base implementation
813//
814
Chris Lattneraa852bb2002-07-23 17:15:12 +0000815// findOption - Return the option number corresponding to the specified
816// argument string. If the option is not found, getNumOptions() is returned.
817//
818unsigned generic_parser_base::findOption(const char *Name) {
819 unsigned i = 0, e = getNumOptions();
Chris Lattnerca6433f2003-05-22 20:06:43 +0000820 std::string N(Name);
Chris Lattneraa852bb2002-07-23 17:15:12 +0000821
822 while (i != e)
823 if (getOption(i) == N)
824 return i;
825 else
826 ++i;
827 return e;
828}
829
830
Chris Lattner331de232002-07-22 02:07:59 +0000831// Return the width of the option tag for printing...
832unsigned generic_parser_base::getOptionWidth(const Option &O) const {
833 if (O.hasArgStr()) {
834 unsigned Size = std::strlen(O.ArgStr)+6;
835 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
836 Size = std::max(Size, (unsigned)std::strlen(getOption(i))+8);
837 return Size;
838 } else {
839 unsigned BaseSize = 0;
840 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
841 BaseSize = std::max(BaseSize, (unsigned)std::strlen(getOption(i))+8);
842 return BaseSize;
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000843 }
844}
845
Misha Brukmanf976c852005-04-21 22:55:34 +0000846// printOptionInfo - Print out information about this option. The
Chris Lattner331de232002-07-22 02:07:59 +0000847// to-be-maintained width is specified.
848//
849void generic_parser_base::printOptionInfo(const Option &O,
850 unsigned GlobalWidth) const {
851 if (O.hasArgStr()) {
852 unsigned L = std::strlen(O.ArgStr);
Bill Wendlinge8156192006-12-07 01:30:32 +0000853 cout << " -" << O.ArgStr << std::string(GlobalWidth-L-6, ' ')
854 << " - " << O.HelpStr << "\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000855
Chris Lattner331de232002-07-22 02:07:59 +0000856 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
857 unsigned NumSpaces = GlobalWidth-strlen(getOption(i))-8;
Bill Wendlinge8156192006-12-07 01:30:32 +0000858 cout << " =" << getOption(i) << std::string(NumSpaces, ' ')
859 << " - " << getDescription(i) << "\n";
Chris Lattner9c9be482002-01-31 00:42:56 +0000860 }
Chris Lattner331de232002-07-22 02:07:59 +0000861 } else {
862 if (O.HelpStr[0])
Bill Wendlinge8156192006-12-07 01:30:32 +0000863 cout << " " << O.HelpStr << "\n";
Chris Lattner331de232002-07-22 02:07:59 +0000864 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
865 unsigned L = std::strlen(getOption(i));
Bill Wendlinge8156192006-12-07 01:30:32 +0000866 cout << " -" << getOption(i) << std::string(GlobalWidth-L-8, ' ')
867 << " - " << getDescription(i) << "\n";
Chris Lattner331de232002-07-22 02:07:59 +0000868 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000869 }
870}
871
872
873//===----------------------------------------------------------------------===//
Chris Lattner331de232002-07-22 02:07:59 +0000874// --help and --help-hidden option implementation
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000875//
Reid Spencerad0846b2004-11-14 22:04:00 +0000876
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000877namespace {
878
Chris Lattner331de232002-07-22 02:07:59 +0000879class HelpPrinter {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000880 unsigned MaxArgLen;
881 const Option *EmptyArg;
882 const bool ShowHidden;
883
Chris Lattner331de232002-07-22 02:07:59 +0000884 // isHidden/isReallyHidden - Predicates to be used to filter down arg lists.
Chris Lattnerca6433f2003-05-22 20:06:43 +0000885 inline static bool isHidden(std::pair<std::string, Option *> &OptPair) {
Chris Lattner331de232002-07-22 02:07:59 +0000886 return OptPair.second->getOptionHiddenFlag() >= Hidden;
887 }
Chris Lattnerca6433f2003-05-22 20:06:43 +0000888 inline static bool isReallyHidden(std::pair<std::string, Option *> &OptPair) {
Chris Lattner331de232002-07-22 02:07:59 +0000889 return OptPair.second->getOptionHiddenFlag() == ReallyHidden;
890 }
891
892public:
893 HelpPrinter(bool showHidden) : ShowHidden(showHidden) {
894 EmptyArg = 0;
895 }
896
897 void operator=(bool Value) {
898 if (Value == false) return;
899
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000900 // Copy Options into a vector so we can sort them as we like...
Chris Lattner90aa8392006-10-04 21:52:35 +0000901 std::vector<std::pair<std::string, Option*> > Opts;
902 copy(Options->begin(), Options->end(), std::back_inserter(Opts));
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000903
904 // Eliminate Hidden or ReallyHidden arguments, depending on ShowHidden
Chris Lattner90aa8392006-10-04 21:52:35 +0000905 Opts.erase(std::remove_if(Opts.begin(), Opts.end(),
906 std::ptr_fun(ShowHidden ? isReallyHidden : isHidden)),
907 Opts.end());
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000908
909 // Eliminate duplicate entries in table (from enum flags options, f.e.)
Chris Lattner331de232002-07-22 02:07:59 +0000910 { // Give OptionSet a scope
911 std::set<Option*> OptionSet;
Chris Lattner90aa8392006-10-04 21:52:35 +0000912 for (unsigned i = 0; i != Opts.size(); ++i)
913 if (OptionSet.count(Opts[i].second) == 0)
914 OptionSet.insert(Opts[i].second); // Add new entry to set
Chris Lattner331de232002-07-22 02:07:59 +0000915 else
Chris Lattner90aa8392006-10-04 21:52:35 +0000916 Opts.erase(Opts.begin()+i--); // Erase duplicate
Chris Lattner331de232002-07-22 02:07:59 +0000917 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000918
919 if (ProgramOverview)
Bill Wendlinge8156192006-12-07 01:30:32 +0000920 cout << "OVERVIEW:" << ProgramOverview << "\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000921
Bill Wendlinge8156192006-12-07 01:30:32 +0000922 cout << "USAGE: " << ProgramName << " [options]";
Chris Lattner331de232002-07-22 02:07:59 +0000923
Chris Lattner90aa8392006-10-04 21:52:35 +0000924 // Print out the positional options.
925 std::vector<Option*> &PosOpts = *PositionalOptions;
Chris Lattner331de232002-07-22 02:07:59 +0000926 Option *CAOpt = 0; // The cl::ConsumeAfter option, if it exists...
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000927 if (!PosOpts.empty() && PosOpts[0]->getNumOccurrencesFlag() == ConsumeAfter)
Chris Lattner331de232002-07-22 02:07:59 +0000928 CAOpt = PosOpts[0];
929
Chris Lattner9cf3d472003-07-30 17:34:02 +0000930 for (unsigned i = CAOpt != 0, e = PosOpts.size(); i != e; ++i) {
931 if (PosOpts[i]->ArgStr[0])
Bill Wendlinge8156192006-12-07 01:30:32 +0000932 cout << " --" << PosOpts[i]->ArgStr;
933 cout << " " << PosOpts[i]->HelpStr;
Chris Lattner9cf3d472003-07-30 17:34:02 +0000934 }
Chris Lattner331de232002-07-22 02:07:59 +0000935
936 // Print the consume after option info if it exists...
Bill Wendlinge8156192006-12-07 01:30:32 +0000937 if (CAOpt) cout << " " << CAOpt->HelpStr;
Chris Lattner331de232002-07-22 02:07:59 +0000938
Bill Wendlinge8156192006-12-07 01:30:32 +0000939 cout << "\n\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000940
941 // Compute the maximum argument length...
942 MaxArgLen = 0;
Chris Lattner90aa8392006-10-04 21:52:35 +0000943 for (unsigned i = 0, e = Opts.size(); i != e; ++i)
944 MaxArgLen = std::max(MaxArgLen, Opts[i].second->getOptionWidth());
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000945
Bill Wendlinge8156192006-12-07 01:30:32 +0000946 cout << "OPTIONS:\n";
Chris Lattner90aa8392006-10-04 21:52:35 +0000947 for (unsigned i = 0, e = Opts.size(); i != e; ++i)
948 Opts[i].second->printOptionInfo(MaxArgLen);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000949
Chris Lattnerc540ebb2004-11-19 17:08:15 +0000950 // Print any extra help the user has declared.
Chris Lattner90aa8392006-10-04 21:52:35 +0000951 for (std::vector<const char *>::iterator I = MoreHelp->begin(),
952 E = MoreHelp->end(); I != E; ++I)
Bill Wendlinge8156192006-12-07 01:30:32 +0000953 cout << *I;
Chris Lattner90aa8392006-10-04 21:52:35 +0000954 MoreHelp->clear();
Reid Spencerad0846b2004-11-14 22:04:00 +0000955
Reid Spencer9bbba0912004-11-16 06:11:52 +0000956 // Halt the program since help information was printed
Chris Lattner90aa8392006-10-04 21:52:35 +0000957 Options->clear(); // Don't bother making option dtors remove from map.
Chris Lattner331de232002-07-22 02:07:59 +0000958 exit(1);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000959 }
960};
Chris Lattner500d8bf2006-10-12 22:09:17 +0000961} // End anonymous namespace
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000962
Chris Lattner331de232002-07-22 02:07:59 +0000963// Define the two HelpPrinter instances that are used to print out help, or
964// help-hidden...
965//
Chris Lattner500d8bf2006-10-12 22:09:17 +0000966static HelpPrinter NormalPrinter(false);
967static HelpPrinter HiddenPrinter(true);
Chris Lattner331de232002-07-22 02:07:59 +0000968
Chris Lattner500d8bf2006-10-12 22:09:17 +0000969static cl::opt<HelpPrinter, true, parser<bool> >
Chris Lattner4bf7afc2005-05-13 19:49:09 +0000970HOp("help", cl::desc("Display available options (--help-hidden for more)"),
Chris Lattner9b14eb52002-08-07 18:36:37 +0000971 cl::location(NormalPrinter), cl::ValueDisallowed);
Chris Lattner331de232002-07-22 02:07:59 +0000972
Chris Lattner500d8bf2006-10-12 22:09:17 +0000973static cl::opt<HelpPrinter, true, parser<bool> >
Chris Lattner4bf7afc2005-05-13 19:49:09 +0000974HHOp("help-hidden", cl::desc("Display all available options"),
Chris Lattner9b14eb52002-08-07 18:36:37 +0000975 cl::location(HiddenPrinter), cl::Hidden, cl::ValueDisallowed);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000976
Chris Lattner500d8bf2006-10-12 22:09:17 +0000977static void (*OverrideVersionPrinter)() = 0;
Reid Spencer515b5b32006-06-05 16:22:56 +0000978
Chris Lattner500d8bf2006-10-12 22:09:17 +0000979namespace {
Reid Spencer515b5b32006-06-05 16:22:56 +0000980class VersionPrinter {
981public:
982 void operator=(bool OptionWasSpecified) {
983 if (OptionWasSpecified) {
984 if (OverrideVersionPrinter == 0) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000985 cout << "Low Level Virtual Machine (http://llvm.org/):\n";
986 cout << " " << PACKAGE_NAME << " version " << PACKAGE_VERSION;
Chris Lattner3fc2f4e2006-07-06 18:33:03 +0000987#ifdef LLVM_VERSION_INFO
Bill Wendlinge8156192006-12-07 01:30:32 +0000988 cout << LLVM_VERSION_INFO;
Reid Spencer515b5b32006-06-05 16:22:56 +0000989#endif
Bill Wendlinge8156192006-12-07 01:30:32 +0000990 cout << "\n ";
Chris Lattner3fc2f4e2006-07-06 18:33:03 +0000991#ifndef __OPTIMIZE__
Bill Wendlinge8156192006-12-07 01:30:32 +0000992 cout << "DEBUG build";
Chris Lattner3fc2f4e2006-07-06 18:33:03 +0000993#else
Bill Wendlinge8156192006-12-07 01:30:32 +0000994 cout << "Optimized build";
Chris Lattner3fc2f4e2006-07-06 18:33:03 +0000995#endif
996#ifndef NDEBUG
Bill Wendlinge8156192006-12-07 01:30:32 +0000997 cout << " with assertions";
Chris Lattner3fc2f4e2006-07-06 18:33:03 +0000998#endif
Bill Wendlinge8156192006-12-07 01:30:32 +0000999 cout << ".\n";
Chris Lattner90aa8392006-10-04 21:52:35 +00001000 Options->clear(); // Don't bother making option dtors remove from map.
Reid Spencer515b5b32006-06-05 16:22:56 +00001001 exit(1);
1002 } else {
1003 (*OverrideVersionPrinter)();
1004 exit(1);
1005 }
1006 }
1007 }
1008};
Chris Lattner500d8bf2006-10-12 22:09:17 +00001009} // End anonymous namespace
Reid Spencer515b5b32006-06-05 16:22:56 +00001010
1011
Reid Spencer69105f32004-08-04 00:36:06 +00001012// Define the --version option that prints out the LLVM version for the tool
Chris Lattner500d8bf2006-10-12 22:09:17 +00001013static VersionPrinter VersionPrinterInstance;
1014
1015static cl::opt<VersionPrinter, true, parser<bool> >
Chris Lattner4bf7afc2005-05-13 19:49:09 +00001016VersOp("version", cl::desc("Display the version of this program"),
Reid Spencer69105f32004-08-04 00:36:06 +00001017 cl::location(VersionPrinterInstance), cl::ValueDisallowed);
1018
Reid Spencer9bbba0912004-11-16 06:11:52 +00001019// Utility function for printing the help message.
1020void cl::PrintHelpMessage() {
Misha Brukmanf976c852005-04-21 22:55:34 +00001021 // This looks weird, but it actually prints the help message. The
Reid Spencer5cc498f2004-11-16 06:50:36 +00001022 // NormalPrinter variable is a HelpPrinter and the help gets printed when
1023 // its operator= is invoked. That's because the "normal" usages of the
Misha Brukmanf976c852005-04-21 22:55:34 +00001024 // help printer is to be assigned true/false depending on whether the
Reid Spencer5cc498f2004-11-16 06:50:36 +00001025 // --help option was given or not. Since we're circumventing that we have
1026 // to make it look like --help was given, so we assign true.
Reid Spencer9bbba0912004-11-16 06:11:52 +00001027 NormalPrinter = true;
1028}
Reid Spencer515b5b32006-06-05 16:22:56 +00001029
1030void cl::SetVersionPrinter(void (*func)()) {
1031 OverrideVersionPrinter = func;
1032}