blob: cd903126f5bf85ddf0e7b1093dd837ed19640eb9 [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"
Reid Spencer6f4c6072006-08-23 07:10:06 +000022#include "llvm/System/Path.h"
Chris Lattnerdbab15a2001-07-23 17:17:47 +000023#include <algorithm>
Duraid Madina786e3e22005-12-26 04:56:16 +000024#include <functional>
Chris Lattnerdbab15a2001-07-23 17:17:47 +000025#include <map>
26#include <set>
Chris Lattner697954c2002-01-20 22:54:45 +000027#include <iostream>
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
Reid Spencere1cc1502004-09-01 04:41:28 +000060// Globals for name and overview of program
Chris Lattner7422a762006-08-27 12:45:47 +000061static std::string ProgramName = "<premain>";
Reid Spencere1cc1502004-09-01 04:41:28 +000062static const char *ProgramOverview = 0;
63
Chris Lattnerc540ebb2004-11-19 17:08:15 +000064// This collects additional help to be printed.
Chris Lattner90aa8392006-10-04 21:52:35 +000065static ManagedStatic<std::vector<const char*> > MoreHelp;
Chris Lattnerc540ebb2004-11-19 17:08:15 +000066
Chris Lattner90aa8392006-10-04 21:52:35 +000067extrahelp::extrahelp(const char *Help)
Chris Lattnerc540ebb2004-11-19 17:08:15 +000068 : morehelp(Help) {
Chris Lattner90aa8392006-10-04 21:52:35 +000069 MoreHelp->push_back(Help);
Chris Lattnerc540ebb2004-11-19 17:08:15 +000070}
71
Chris Lattner331de232002-07-22 02:07:59 +000072//===----------------------------------------------------------------------===//
Chris Lattner7422a762006-08-27 12:45:47 +000073// Basic, shared command line option processing machinery.
Chris Lattner331de232002-07-22 02:07:59 +000074//
75
Chris Lattner90aa8392006-10-04 21:52:35 +000076static ManagedStatic<std::map<std::string, Option*> > Options;
77static ManagedStatic<std::vector<Option*> > PositionalOptions;
Chris Lattnere8e258b2002-07-29 20:58:42 +000078
Chris Lattnerca6433f2003-05-22 20:06:43 +000079static Option *getOption(const std::string &Str) {
Chris Lattner90aa8392006-10-04 21:52:35 +000080 std::map<std::string,Option*>::iterator I = Options->find(Str);
81 return I != Options->end() ? I->second : 0;
Chris Lattner331de232002-07-22 02:07:59 +000082}
83
Chris Lattnere8e258b2002-07-29 20:58:42 +000084static void AddArgument(const char *ArgName, Option *Opt) {
85 if (getOption(ArgName)) {
Misha Brukmanf976c852005-04-21 22:55:34 +000086 std::cerr << ProgramName << ": CommandLine Error: Argument '"
Reid Spencere1cc1502004-09-01 04:41:28 +000087 << ArgName << "' defined more than once!\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +000088 } else {
Chris Lattnerf78032f2001-11-26 18:58:34 +000089 // Add argument to the argument map!
Chris Lattner90aa8392006-10-04 21:52:35 +000090 (*Options)[ArgName] = Opt;
Chris Lattnere8e258b2002-07-29 20:58:42 +000091 }
92}
93
94// RemoveArgument - It's possible that the argument is no longer in the map if
95// options have already been processed and the map has been deleted!
Misha Brukmanf976c852005-04-21 22:55:34 +000096//
Chris Lattnere8e258b2002-07-29 20:58:42 +000097static void RemoveArgument(const char *ArgName, Option *Opt) {
Chris Lattner90aa8392006-10-04 21:52:35 +000098 if (Options->empty()) return;
Tanya Lattnerc4ae8e92004-11-20 23:35:20 +000099
Chris Lattnerf98cfc72004-07-18 21:56:20 +0000100#ifndef NDEBUG
101 // This disgusting HACK is brought to you courtesy of GCC 3.3.2, which ICE's
102 // If we pass ArgName directly into getOption here.
103 std::string Tmp = ArgName;
104 assert(getOption(Tmp) == Opt && "Arg not in map!");
105#endif
Chris Lattner90aa8392006-10-04 21:52:35 +0000106 Options->erase(ArgName);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000107}
108
Chris Lattnercaccd762001-10-27 05:54:17 +0000109static inline bool ProvideOption(Option *Handler, const char *ArgName,
110 const char *Value, int argc, char **argv,
111 int &i) {
112 // Enforce value requirements
113 switch (Handler->getValueExpectedFlag()) {
114 case ValueRequired:
Chris Lattner6d5857e2005-05-10 23:20:17 +0000115 if (Value == 0) { // No value specified?
Chris Lattnercaccd762001-10-27 05:54:17 +0000116 if (i+1 < argc) { // Steal the next argument, like for '-o filename'
117 Value = argv[++i];
118 } else {
119 return Handler->error(" requires a value!");
120 }
121 }
122 break;
123 case ValueDisallowed:
Chris Lattner6d5857e2005-05-10 23:20:17 +0000124 if (Value)
Misha Brukmanf976c852005-04-21 22:55:34 +0000125 return Handler->error(" does not allow a value! '" +
Chris Lattnerca6433f2003-05-22 20:06:43 +0000126 std::string(Value) + "' specified.");
Chris Lattnercaccd762001-10-27 05:54:17 +0000127 break;
Misha Brukmanf976c852005-04-21 22:55:34 +0000128 case ValueOptional:
Reid Spencere1cc1502004-09-01 04:41:28 +0000129 break;
Misha Brukmanf976c852005-04-21 22:55:34 +0000130 default:
131 std::cerr << ProgramName
132 << ": Bad ValueMask flag! CommandLine usage error:"
133 << Handler->getValueExpectedFlag() << "\n";
Reid Spencere1cc1502004-09-01 04:41:28 +0000134 abort();
135 break;
Chris Lattnercaccd762001-10-27 05:54:17 +0000136 }
137
138 // Run the handler now!
Chris Lattner6d5857e2005-05-10 23:20:17 +0000139 return Handler->addOccurrence(i, ArgName, Value ? Value : "");
Chris Lattnercaccd762001-10-27 05:54:17 +0000140}
141
Misha Brukmanf976c852005-04-21 22:55:34 +0000142static bool ProvidePositionalOption(Option *Handler, const std::string &Arg,
Reid Spencer1e13fd22004-08-13 19:47:30 +0000143 int i) {
144 int Dummy = i;
Chris Lattner9cf3d472003-07-30 17:34:02 +0000145 return ProvideOption(Handler, Handler->ArgStr, Arg.c_str(), 0, 0, Dummy);
Chris Lattner331de232002-07-22 02:07:59 +0000146}
Chris Lattnerf78032f2001-11-26 18:58:34 +0000147
Chris Lattner331de232002-07-22 02:07:59 +0000148
149// Option predicates...
150static inline bool isGrouping(const Option *O) {
151 return O->getFormattingFlag() == cl::Grouping;
152}
153static inline bool isPrefixedOrGrouping(const Option *O) {
154 return isGrouping(O) || O->getFormattingFlag() == cl::Prefix;
155}
156
157// getOptionPred - Check to see if there are any options that satisfy the
158// specified predicate with names that are the prefixes in Name. This is
159// checked by progressively stripping characters off of the name, checking to
160// see if there options that satisfy the predicate. If we find one, return it,
161// otherwise return null.
162//
163static Option *getOptionPred(std::string Name, unsigned &Length,
164 bool (*Pred)(const Option*)) {
Misha Brukmanf976c852005-04-21 22:55:34 +0000165
Chris Lattnere8e258b2002-07-29 20:58:42 +0000166 Option *Op = getOption(Name);
167 if (Op && Pred(Op)) {
Chris Lattner331de232002-07-22 02:07:59 +0000168 Length = Name.length();
Chris Lattnere8e258b2002-07-29 20:58:42 +0000169 return Op;
Chris Lattnerf78032f2001-11-26 18:58:34 +0000170 }
171
Chris Lattner331de232002-07-22 02:07:59 +0000172 if (Name.size() == 1) return 0;
173 do {
174 Name.erase(Name.end()-1, Name.end()); // Chop off the last character...
Chris Lattnere8e258b2002-07-29 20:58:42 +0000175 Op = getOption(Name);
Chris Lattner331de232002-07-22 02:07:59 +0000176
177 // Loop while we haven't found an option and Name still has at least two
178 // characters in it (so that the next iteration will not be the empty
179 // string...
Chris Lattnere8e258b2002-07-29 20:58:42 +0000180 } while ((Op == 0 || !Pred(Op)) && Name.size() > 1);
Chris Lattner331de232002-07-22 02:07:59 +0000181
Chris Lattnere8e258b2002-07-29 20:58:42 +0000182 if (Op && Pred(Op)) {
Chris Lattner331de232002-07-22 02:07:59 +0000183 Length = Name.length();
Chris Lattnere8e258b2002-07-29 20:58:42 +0000184 return Op; // Found one!
Chris Lattner331de232002-07-22 02:07:59 +0000185 }
186 return 0; // No option found!
187}
188
189static bool RequiresValue(const Option *O) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000190 return O->getNumOccurrencesFlag() == cl::Required ||
191 O->getNumOccurrencesFlag() == cl::OneOrMore;
Chris Lattner331de232002-07-22 02:07:59 +0000192}
193
194static bool EatsUnboundedNumberOfValues(const Option *O) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000195 return O->getNumOccurrencesFlag() == cl::ZeroOrMore ||
196 O->getNumOccurrencesFlag() == cl::OneOrMore;
Chris Lattnerf78032f2001-11-26 18:58:34 +0000197}
Chris Lattnercaccd762001-10-27 05:54:17 +0000198
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000199/// ParseCStringVector - Break INPUT up wherever one or more
200/// whitespace characters are found, and store the resulting tokens in
201/// OUTPUT. The tokens stored in OUTPUT are dynamically allocated
202/// using strdup (), so it is the caller's responsibility to free ()
203/// them later.
Brian Gaeke06b06c52003-08-14 22:00:59 +0000204///
Chris Lattner23288582006-08-27 22:10:29 +0000205static void ParseCStringVector(std::vector<char *> &output,
206 const char *input) {
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000207 // Characters which will be treated as token separators:
208 static const char *delims = " \v\f\t\r\n";
209
210 std::string work (input);
211 // Skip past any delims at head of input string.
212 size_t pos = work.find_first_not_of (delims);
213 // If the string consists entirely of delims, then exit early.
214 if (pos == std::string::npos) return;
215 // Otherwise, jump forward to beginning of first word.
216 work = work.substr (pos);
217 // Find position of first delimiter.
218 pos = work.find_first_of (delims);
219
220 while (!work.empty() && pos != std::string::npos) {
221 // Everything from 0 to POS is the next word to copy.
222 output.push_back (strdup (work.substr (0,pos).c_str ()));
223 // Is there another word in the string?
224 size_t nextpos = work.find_first_not_of (delims, pos + 1);
225 if (nextpos != std::string::npos) {
226 // Yes? Then remove delims from beginning ...
227 work = work.substr (work.find_first_not_of (delims, pos + 1));
228 // and find the end of the word.
229 pos = work.find_first_of (delims);
230 } else {
231 // No? (Remainder of string is delims.) End the loop.
232 work = "";
233 pos = std::string::npos;
234 }
235 }
236
237 // If `input' ended with non-delim char, then we'll get here with
238 // the last word of `input' in `work'; copy it now.
239 if (!work.empty ()) {
240 output.push_back (strdup (work.c_str ()));
Brian Gaeke06b06c52003-08-14 22:00:59 +0000241 }
242}
243
244/// ParseEnvironmentOptions - An alternative entry point to the
245/// CommandLine library, which allows you to read the program's name
246/// from the caller (as PROGNAME) and its command-line arguments from
247/// an environment variable (whose name is given in ENVVAR).
248///
Chris Lattnerbf455c22004-05-06 22:04:31 +0000249void cl::ParseEnvironmentOptions(const char *progName, const char *envVar,
250 const char *Overview) {
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000251 // Check args.
Chris Lattnerbf455c22004-05-06 22:04:31 +0000252 assert(progName && "Program name not specified");
253 assert(envVar && "Environment variable name missing");
Misha Brukmanf976c852005-04-21 22:55:34 +0000254
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000255 // Get the environment variable they want us to parse options out of.
Chris Lattner23288582006-08-27 22:10:29 +0000256 const char *envValue = getenv(envVar);
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000257 if (!envValue)
258 return;
259
Brian Gaeke06b06c52003-08-14 22:00:59 +0000260 // Get program's "name", which we wouldn't know without the caller
261 // telling us.
Chris Lattner23288582006-08-27 22:10:29 +0000262 std::vector<char*> newArgv;
263 newArgv.push_back(strdup(progName));
Brian Gaeke06b06c52003-08-14 22:00:59 +0000264
265 // Parse the value of the environment variable into a "command line"
266 // and hand it off to ParseCommandLineOptions().
Chris Lattner23288582006-08-27 22:10:29 +0000267 ParseCStringVector(newArgv, envValue);
268 int newArgc = newArgv.size();
269 ParseCommandLineOptions(newArgc, &newArgv[0], Overview);
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000270
271 // Free all the strdup()ed strings.
Chris Lattner23288582006-08-27 22:10:29 +0000272 for (std::vector<char*>::iterator i = newArgv.begin(), e = newArgv.end();
273 i != e; ++i)
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000274 free (*i);
Brian Gaeke06b06c52003-08-14 22:00:59 +0000275}
276
Chris Lattnerbf455c22004-05-06 22:04:31 +0000277/// LookupOption - Lookup the option specified by the specified option on the
278/// command line. If there is a value specified (after an equal sign) return
279/// that as well.
280static Option *LookupOption(const char *&Arg, const char *&Value) {
281 while (*Arg == '-') ++Arg; // Eat leading dashes
Misha Brukmanf976c852005-04-21 22:55:34 +0000282
Chris Lattnerbf455c22004-05-06 22:04:31 +0000283 const char *ArgEnd = Arg;
284 while (*ArgEnd && *ArgEnd != '=')
Chris Lattner6d5857e2005-05-10 23:20:17 +0000285 ++ArgEnd; // Scan till end of argument name.
Chris Lattnerbf455c22004-05-06 22:04:31 +0000286
Chris Lattner6d5857e2005-05-10 23:20:17 +0000287 if (*ArgEnd == '=') // If we have an equals sign...
288 Value = ArgEnd+1; // Get the value, not the equals
289
Misha Brukmanf976c852005-04-21 22:55:34 +0000290
Chris Lattnerbf455c22004-05-06 22:04:31 +0000291 if (*Arg == 0) return 0;
292
293 // Look up the option.
Chris Lattner90aa8392006-10-04 21:52:35 +0000294 std::map<std::string, Option*> &Opts = *Options;
Chris Lattnerbf455c22004-05-06 22:04:31 +0000295 std::map<std::string, Option*>::iterator I =
296 Opts.find(std::string(Arg, ArgEnd));
297 return (I != Opts.end()) ? I->second : 0;
298}
299
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000300void cl::ParseCommandLineOptions(int &argc, char **argv,
Chris Lattner0c0edf82002-07-25 06:17:51 +0000301 const char *Overview) {
Chris Lattner90aa8392006-10-04 21:52:35 +0000302 assert((!Options->empty() || !PositionalOptions->empty()) &&
Chris Lattner331de232002-07-22 02:07:59 +0000303 "No options specified, or ParseCommandLineOptions called more"
304 " than once!");
Reid Spencer6f4c6072006-08-23 07:10:06 +0000305 sys::Path progname(argv[0]);
306 ProgramName = sys::Path(argv[0]).getLast();
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000307 ProgramOverview = Overview;
308 bool ErrorParsing = false;
309
Chris Lattner90aa8392006-10-04 21:52:35 +0000310 std::map<std::string, Option*> &Opts = *Options;
311 std::vector<Option*> &PositionalOpts = *PositionalOptions;
Chris Lattner331de232002-07-22 02:07:59 +0000312
313 // Check out the positional arguments to collect information about them.
314 unsigned NumPositionalRequired = 0;
Chris Lattnerde013242005-08-08 17:25:38 +0000315
316 // Determine whether or not there are an unlimited number of positionals
317 bool HasUnlimitedPositionals = false;
318
Chris Lattner331de232002-07-22 02:07:59 +0000319 Option *ConsumeAfterOpt = 0;
320 if (!PositionalOpts.empty()) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000321 if (PositionalOpts[0]->getNumOccurrencesFlag() == cl::ConsumeAfter) {
Chris Lattner331de232002-07-22 02:07:59 +0000322 assert(PositionalOpts.size() > 1 &&
323 "Cannot specify cl::ConsumeAfter without a positional argument!");
324 ConsumeAfterOpt = PositionalOpts[0];
325 }
326
327 // Calculate how many positional values are _required_.
328 bool UnboundedFound = false;
329 for (unsigned i = ConsumeAfterOpt != 0, e = PositionalOpts.size();
330 i != e; ++i) {
331 Option *Opt = PositionalOpts[i];
332 if (RequiresValue(Opt))
333 ++NumPositionalRequired;
334 else if (ConsumeAfterOpt) {
335 // ConsumeAfter cannot be combined with "optional" positional options
Chris Lattner54ec7ae2002-07-22 02:21:57 +0000336 // unless there is only one positional argument...
337 if (PositionalOpts.size() > 2)
338 ErrorParsing |=
339 Opt->error(" error - this positional option will never be matched, "
340 "because it does not Require a value, and a "
341 "cl::ConsumeAfter option is active!");
Chris Lattner9cf3d472003-07-30 17:34:02 +0000342 } else if (UnboundedFound && !Opt->ArgStr[0]) {
343 // This option does not "require" a value... Make sure this option is
344 // not specified after an option that eats all extra arguments, or this
345 // one will never get any!
Chris Lattner331de232002-07-22 02:07:59 +0000346 //
347 ErrorParsing |= Opt->error(" error - option can never match, because "
348 "another positional argument will match an "
349 "unbounded number of values, and this option"
350 " does not require a value!");
351 }
352 UnboundedFound |= EatsUnboundedNumberOfValues(Opt);
353 }
Chris Lattner21e1a792005-08-08 21:57:27 +0000354 HasUnlimitedPositionals = UnboundedFound || ConsumeAfterOpt;
Chris Lattner331de232002-07-22 02:07:59 +0000355 }
356
Reid Spencer1e13fd22004-08-13 19:47:30 +0000357 // PositionalVals - A vector of "positional" arguments we accumulate into
358 // the process at the end...
Chris Lattner331de232002-07-22 02:07:59 +0000359 //
Reid Spencer1e13fd22004-08-13 19:47:30 +0000360 std::vector<std::pair<std::string,unsigned> > PositionalVals;
Chris Lattner331de232002-07-22 02:07:59 +0000361
Chris Lattner9cf3d472003-07-30 17:34:02 +0000362 // If the program has named positional arguments, and the name has been run
363 // across, keep track of which positional argument was named. Otherwise put
364 // the positional args into the PositionalVals list...
365 Option *ActivePositionalArg = 0;
366
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000367 // Loop over all of the arguments... processing them.
Chris Lattner331de232002-07-22 02:07:59 +0000368 bool DashDashFound = false; // Have we read '--'?
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000369 for (int i = 1; i < argc; ++i) {
370 Option *Handler = 0;
Chris Lattner6d5857e2005-05-10 23:20:17 +0000371 const char *Value = 0;
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000372 const char *ArgName = "";
Chris Lattner331de232002-07-22 02:07:59 +0000373
374 // Check to see if this is a positional argument. This argument is
375 // considered to be positional if it doesn't start with '-', if it is "-"
Misha Brukman1115e042003-07-10 21:38:28 +0000376 // itself, or if we have seen "--" already.
Chris Lattner331de232002-07-22 02:07:59 +0000377 //
378 if (argv[i][0] != '-' || argv[i][1] == 0 || DashDashFound) {
379 // Positional argument!
Chris Lattner9cf3d472003-07-30 17:34:02 +0000380 if (ActivePositionalArg) {
Reid Spencer1e13fd22004-08-13 19:47:30 +0000381 ProvidePositionalOption(ActivePositionalArg, argv[i], i);
Chris Lattner9cf3d472003-07-30 17:34:02 +0000382 continue; // We are done!
383 } else if (!PositionalOpts.empty()) {
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
386 // All of the positional arguments have been fulfulled, give the rest to
387 // the consume after option... if it's specified...
388 //
Misha Brukmanf976c852005-04-21 22:55:34 +0000389 if (PositionalVals.size() >= NumPositionalRequired &&
Chris Lattner331de232002-07-22 02:07:59 +0000390 ConsumeAfterOpt != 0) {
391 for (++i; i < argc; ++i)
Reid Spencer1e13fd22004-08-13 19:47:30 +0000392 PositionalVals.push_back(std::make_pair(argv[i],i));
Chris Lattner331de232002-07-22 02:07:59 +0000393 break; // Handle outside of the argument processing loop...
394 }
395
396 // Delay processing positional arguments until the end...
397 continue;
398 }
Chris Lattnerbf455c22004-05-06 22:04:31 +0000399 } else if (argv[i][0] == '-' && argv[i][1] == '-' && argv[i][2] == 0 &&
400 !DashDashFound) {
401 DashDashFound = true; // This is the mythical "--"?
402 continue; // Don't try to process it as an argument itself.
403 } else if (ActivePositionalArg &&
404 (ActivePositionalArg->getMiscFlags() & PositionalEatsArgs)) {
405 // If there is a positional argument eating options, check to see if this
406 // option is another positional argument. If so, treat it as an argument,
407 // otherwise feed it to the eating positional.
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000408 ArgName = argv[i]+1;
Chris Lattnerbf455c22004-05-06 22:04:31 +0000409 Handler = LookupOption(ArgName, Value);
410 if (!Handler || Handler->getFormattingFlag() != cl::Positional) {
Reid Spencer1e13fd22004-08-13 19:47:30 +0000411 ProvidePositionalOption(ActivePositionalArg, argv[i], i);
Chris Lattnerbf455c22004-05-06 22:04:31 +0000412 continue; // We are done!
Chris Lattner331de232002-07-22 02:07:59 +0000413 }
414
Chris Lattnerbf455c22004-05-06 22:04:31 +0000415 } else { // We start with a '-', must be an argument...
416 ArgName = argv[i]+1;
417 Handler = LookupOption(ArgName, Value);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000418
Chris Lattnerbf455c22004-05-06 22:04:31 +0000419 // Check to see if this "option" is really a prefixed or grouped argument.
Reid Spencer5f8448f2004-11-24 06:13:42 +0000420 if (Handler == 0) {
Chris Lattnerbf455c22004-05-06 22:04:31 +0000421 std::string RealName(ArgName);
422 if (RealName.size() > 1) {
Chris Lattner331de232002-07-22 02:07:59 +0000423 unsigned Length = 0;
424 Option *PGOpt = getOptionPred(RealName, Length, isPrefixedOrGrouping);
Misha Brukmanf976c852005-04-21 22:55:34 +0000425
Chris Lattner331de232002-07-22 02:07:59 +0000426 // If the option is a prefixed option, then the value is simply the
427 // rest of the name... so fall through to later processing, by
428 // setting up the argument name flags and value fields.
429 //
430 if (PGOpt && PGOpt->getFormattingFlag() == cl::Prefix) {
Chris Lattnerbf455c22004-05-06 22:04:31 +0000431 Value = ArgName+Length;
432 assert(Opts.find(std::string(ArgName, Value)) != Opts.end() &&
433 Opts.find(std::string(ArgName, Value))->second == PGOpt);
434 Handler = PGOpt;
Chris Lattner331de232002-07-22 02:07:59 +0000435 } else if (PGOpt) {
Chris Lattnerbf455c22004-05-06 22:04:31 +0000436 // This must be a grouped option... handle them now.
Chris Lattner331de232002-07-22 02:07:59 +0000437 assert(isGrouping(PGOpt) && "Broken getOptionPred!");
Misha Brukmanf976c852005-04-21 22:55:34 +0000438
Chris Lattner331de232002-07-22 02:07:59 +0000439 do {
440 // Move current arg name out of RealName into RealArgName...
Chris Lattnerbf455c22004-05-06 22:04:31 +0000441 std::string RealArgName(RealName.begin(),
442 RealName.begin() + Length);
443 RealName.erase(RealName.begin(), RealName.begin() + Length);
Misha Brukmanf976c852005-04-21 22:55:34 +0000444
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000445 // Because ValueRequired is an invalid flag for grouped arguments,
446 // we don't need to pass argc/argv in...
447 //
Chris Lattner331de232002-07-22 02:07:59 +0000448 assert(PGOpt->getValueExpectedFlag() != cl::ValueRequired &&
449 "Option can not be cl::Grouping AND cl::ValueRequired!");
450 int Dummy;
Chris Lattnerbf455c22004-05-06 22:04:31 +0000451 ErrorParsing |= ProvideOption(PGOpt, RealArgName.c_str(),
Chris Lattner6d5857e2005-05-10 23:20:17 +0000452 0, 0, 0, Dummy);
Misha Brukmanf976c852005-04-21 22:55:34 +0000453
Chris Lattner331de232002-07-22 02:07:59 +0000454 // Get the next grouping option...
Chris Lattnerbf455c22004-05-06 22:04:31 +0000455 PGOpt = getOptionPred(RealName, Length, isGrouping);
456 } while (PGOpt && Length != RealName.size());
Misha Brukmanf976c852005-04-21 22:55:34 +0000457
Chris Lattnerbf455c22004-05-06 22:04:31 +0000458 Handler = PGOpt; // Ate all of the options.
Chris Lattner331de232002-07-22 02:07:59 +0000459 }
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000460 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000461 }
462 }
463
464 if (Handler == 0) {
Reid Spencer6f4c6072006-08-23 07:10:06 +0000465 std::cerr << ProgramName << ": Unknown command line argument '"
Chris Lattner79959d22006-01-17 00:32:28 +0000466 << argv[i] << "'. Try: '" << argv[0] << " --help'\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000467 ErrorParsing = true;
468 continue;
469 }
470
Chris Lattner72fb8e52003-05-22 20:26:17 +0000471 // Check to see if this option accepts a comma separated list of values. If
472 // it does, we have to split up the value into multiple values...
Chris Lattner6d5857e2005-05-10 23:20:17 +0000473 if (Value && Handler->getMiscFlags() & CommaSeparated) {
Chris Lattner72fb8e52003-05-22 20:26:17 +0000474 std::string Val(Value);
475 std::string::size_type Pos = Val.find(',');
476
477 while (Pos != std::string::npos) {
478 // Process the portion before the comma...
479 ErrorParsing |= ProvideOption(Handler, ArgName,
480 std::string(Val.begin(),
481 Val.begin()+Pos).c_str(),
482 argc, argv, i);
483 // Erase the portion before the comma, AND the comma...
484 Val.erase(Val.begin(), Val.begin()+Pos+1);
485 Value += Pos+1; // Increment the original value pointer as well...
486
487 // Check for another comma...
488 Pos = Val.find(',');
489 }
490 }
Chris Lattner9cf3d472003-07-30 17:34:02 +0000491
492 // If this is a named positional argument, just remember that it is the
493 // active one...
494 if (Handler->getFormattingFlag() == cl::Positional)
495 ActivePositionalArg = Handler;
Misha Brukmanf976c852005-04-21 22:55:34 +0000496 else
Chris Lattner9cf3d472003-07-30 17:34:02 +0000497 ErrorParsing |= ProvideOption(Handler, ArgName, Value, argc, argv, i);
Chris Lattner331de232002-07-22 02:07:59 +0000498 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000499
Chris Lattner331de232002-07-22 02:07:59 +0000500 // Check and handle positional arguments now...
501 if (NumPositionalRequired > PositionalVals.size()) {
Reid Spencer6f4c6072006-08-23 07:10:06 +0000502 std::cerr << ProgramName
503 << ": Not enough positional command line arguments specified!\n"
504 << "Must specify at least " << NumPositionalRequired
505 << " positional arguments: See: " << argv[0] << " --help\n";
Chris Lattner79959d22006-01-17 00:32:28 +0000506
Chris Lattner331de232002-07-22 02:07:59 +0000507 ErrorParsing = true;
Chris Lattnerde013242005-08-08 17:25:38 +0000508 } else if (!HasUnlimitedPositionals
509 && PositionalVals.size() > PositionalOpts.size()) {
Reid Spencer6f4c6072006-08-23 07:10:06 +0000510 std::cerr << ProgramName
511 << ": Too many positional arguments specified!\n"
512 << "Can specify at most " << PositionalOpts.size()
513 << " positional arguments: See: " << argv[0] << " --help\n";
Chris Lattnerde013242005-08-08 17:25:38 +0000514 ErrorParsing = true;
Chris Lattner331de232002-07-22 02:07:59 +0000515
516 } else if (ConsumeAfterOpt == 0) {
517 // Positional args have already been handled if ConsumeAfter is specified...
518 unsigned ValNo = 0, NumVals = PositionalVals.size();
519 for (unsigned i = 0, e = PositionalOpts.size(); i != e; ++i) {
520 if (RequiresValue(PositionalOpts[i])) {
Misha Brukmanf976c852005-04-21 22:55:34 +0000521 ProvidePositionalOption(PositionalOpts[i], PositionalVals[ValNo].first,
Reid Spencer1e13fd22004-08-13 19:47:30 +0000522 PositionalVals[ValNo].second);
523 ValNo++;
Chris Lattner331de232002-07-22 02:07:59 +0000524 --NumPositionalRequired; // We fulfilled our duty...
525 }
526
527 // If we _can_ give this option more arguments, do so now, as long as we
528 // do not give it values that others need. 'Done' controls whether the
529 // option even _WANTS_ any more.
530 //
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000531 bool Done = PositionalOpts[i]->getNumOccurrencesFlag() == cl::Required;
Chris Lattner331de232002-07-22 02:07:59 +0000532 while (NumVals-ValNo > NumPositionalRequired && !Done) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000533 switch (PositionalOpts[i]->getNumOccurrencesFlag()) {
Chris Lattner331de232002-07-22 02:07:59 +0000534 case cl::Optional:
535 Done = true; // Optional arguments want _at most_ one value
536 // FALL THROUGH
537 case cl::ZeroOrMore: // Zero or more will take all they can get...
538 case cl::OneOrMore: // One or more will take all they can get...
Reid Spencer1e13fd22004-08-13 19:47:30 +0000539 ProvidePositionalOption(PositionalOpts[i],
540 PositionalVals[ValNo].first,
541 PositionalVals[ValNo].second);
542 ValNo++;
Chris Lattner331de232002-07-22 02:07:59 +0000543 break;
544 default:
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000545 assert(0 && "Internal error, unexpected NumOccurrences flag in "
Chris Lattner331de232002-07-22 02:07:59 +0000546 "positional argument processing!");
547 }
548 }
Chris Lattnercaccd762001-10-27 05:54:17 +0000549 }
Chris Lattner331de232002-07-22 02:07:59 +0000550 } else {
551 assert(ConsumeAfterOpt && NumPositionalRequired <= PositionalVals.size());
552 unsigned ValNo = 0;
553 for (unsigned j = 1, e = PositionalOpts.size(); j != e; ++j)
Reid Spencer1e13fd22004-08-13 19:47:30 +0000554 if (RequiresValue(PositionalOpts[j])) {
Chris Lattnerfaba8092002-07-24 20:15:13 +0000555 ErrorParsing |= ProvidePositionalOption(PositionalOpts[j],
Reid Spencer1e13fd22004-08-13 19:47:30 +0000556 PositionalVals[ValNo].first,
557 PositionalVals[ValNo].second);
558 ValNo++;
559 }
Chris Lattnerfaba8092002-07-24 20:15:13 +0000560
561 // Handle the case where there is just one positional option, and it's
562 // optional. In this case, we want to give JUST THE FIRST option to the
563 // positional option and keep the rest for the consume after. The above
564 // loop would have assigned no values to positional options in this case.
565 //
Reid Spencer1e13fd22004-08-13 19:47:30 +0000566 if (PositionalOpts.size() == 2 && ValNo == 0 && !PositionalVals.empty()) {
Chris Lattnerfaba8092002-07-24 20:15:13 +0000567 ErrorParsing |= ProvidePositionalOption(PositionalOpts[1],
Reid Spencer1e13fd22004-08-13 19:47:30 +0000568 PositionalVals[ValNo].first,
569 PositionalVals[ValNo].second);
570 ValNo++;
571 }
Misha Brukmanf976c852005-04-21 22:55:34 +0000572
Chris Lattner331de232002-07-22 02:07:59 +0000573 // Handle over all of the rest of the arguments to the
574 // cl::ConsumeAfter command line option...
575 for (; ValNo != PositionalVals.size(); ++ValNo)
576 ErrorParsing |= ProvidePositionalOption(ConsumeAfterOpt,
Reid Spencer1e13fd22004-08-13 19:47:30 +0000577 PositionalVals[ValNo].first,
578 PositionalVals[ValNo].second);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000579 }
580
Chris Lattnerdc4693d2001-07-23 23:02:45 +0000581 // Loop over args and make sure all required args are specified!
Misha Brukmanf976c852005-04-21 22:55:34 +0000582 for (std::map<std::string, Option*>::iterator I = Opts.begin(),
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000583 E = Opts.end(); I != E; ++I) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000584 switch (I->second->getNumOccurrencesFlag()) {
Chris Lattnerdc4693d2001-07-23 23:02:45 +0000585 case Required:
586 case OneOrMore:
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000587 if (I->second->getNumOccurrences() == 0) {
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000588 I->second->error(" must be specified at least once!");
Chris Lattnerf038acb2001-10-24 06:21:56 +0000589 ErrorParsing = true;
590 }
Chris Lattnerdc4693d2001-07-23 23:02:45 +0000591 // Fall through
592 default:
593 break;
594 }
595 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000596
Chris Lattner331de232002-07-22 02:07:59 +0000597 // Free all of the memory allocated to the map. Command line options may only
598 // be processed once!
Chris Lattner90aa8392006-10-04 21:52:35 +0000599 Opts.clear();
Chris Lattner331de232002-07-22 02:07:59 +0000600 PositionalOpts.clear();
Chris Lattner90aa8392006-10-04 21:52:35 +0000601 MoreHelp->clear();
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000602
603 // If we had an error processing our arguments, don't let the program execute
604 if (ErrorParsing) exit(1);
605}
606
607//===----------------------------------------------------------------------===//
608// Option Base class implementation
609//
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000610
Chris Lattnerca6433f2003-05-22 20:06:43 +0000611bool Option::error(std::string Message, const char *ArgName) {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000612 if (ArgName == 0) ArgName = ArgStr;
Chris Lattner331de232002-07-22 02:07:59 +0000613 if (ArgName[0] == 0)
Chris Lattnerca6433f2003-05-22 20:06:43 +0000614 std::cerr << HelpStr; // Be nice for positional arguments
Chris Lattner331de232002-07-22 02:07:59 +0000615 else
Reid Spencer6f4c6072006-08-23 07:10:06 +0000616 std::cerr << ProgramName << ": for the -" << ArgName;
Jim Laskeyabe0e3e2006-08-02 20:15:56 +0000617
Reid Spencere1cc1502004-09-01 04:41:28 +0000618 std::cerr << " option: " << Message << "\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000619 return true;
620}
621
Chris Lattner6d5857e2005-05-10 23:20:17 +0000622bool Option::addOccurrence(unsigned pos, const char *ArgName,
623 const std::string &Value) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000624 NumOccurrences++; // Increment the number of times we have been seen
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000625
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000626 switch (getNumOccurrencesFlag()) {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000627 case Optional:
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000628 if (NumOccurrences > 1)
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000629 return error(": may only occur zero or one times!", ArgName);
630 break;
631 case Required:
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000632 if (NumOccurrences > 1)
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000633 return error(": must occur exactly one time!", ArgName);
634 // Fall through
635 case OneOrMore:
Chris Lattnercaccd762001-10-27 05:54:17 +0000636 case ZeroOrMore:
637 case ConsumeAfter: break;
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000638 default: return error(": bad num occurrences flag value!");
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000639 }
640
Reid Spencer1e13fd22004-08-13 19:47:30 +0000641 return handleOccurrence(pos, ArgName, Value);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000642}
643
Chris Lattner331de232002-07-22 02:07:59 +0000644// addArgument - Tell the system that this Option subclass will handle all
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000645// occurrences of -ArgStr on the command line.
Chris Lattner331de232002-07-22 02:07:59 +0000646//
647void Option::addArgument(const char *ArgStr) {
648 if (ArgStr[0])
649 AddArgument(ArgStr, this);
Chris Lattner9cf3d472003-07-30 17:34:02 +0000650
651 if (getFormattingFlag() == Positional)
Chris Lattner90aa8392006-10-04 21:52:35 +0000652 PositionalOptions->push_back(this);
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000653 else if (getNumOccurrencesFlag() == ConsumeAfter) {
Chris Lattner90aa8392006-10-04 21:52:35 +0000654 if (!PositionalOptions->empty() &&
655 PositionalOptions->front()->getNumOccurrencesFlag() == ConsumeAfter)
Chris Lattner9cf3d472003-07-30 17:34:02 +0000656 error("Cannot specify more than one option with cl::ConsumeAfter!");
Chris Lattner90aa8392006-10-04 21:52:35 +0000657 PositionalOptions->insert(PositionalOptions->begin(), this);
Chris Lattner331de232002-07-22 02:07:59 +0000658 }
659}
660
Chris Lattneraa852bb2002-07-23 17:15:12 +0000661void Option::removeArgument(const char *ArgStr) {
Chris Lattner9cf3d472003-07-30 17:34:02 +0000662 if (ArgStr[0])
Chris Lattnere8e258b2002-07-29 20:58:42 +0000663 RemoveArgument(ArgStr, this);
Chris Lattner9cf3d472003-07-30 17:34:02 +0000664
665 if (getFormattingFlag() == Positional) {
Chris Lattnerca6433f2003-05-22 20:06:43 +0000666 std::vector<Option*>::iterator I =
Chris Lattner90aa8392006-10-04 21:52:35 +0000667 std::find(PositionalOptions->begin(), PositionalOptions->end(), this);
668 assert(I != PositionalOptions->end() && "Arg not registered!");
669 PositionalOptions->erase(I);
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000670 } else if (getNumOccurrencesFlag() == ConsumeAfter) {
Chris Lattner90aa8392006-10-04 21:52:35 +0000671 assert(!PositionalOptions->empty() && (*PositionalOptions)[0] == this &&
Chris Lattneraa852bb2002-07-23 17:15:12 +0000672 "Arg not registered correctly!");
Chris Lattner90aa8392006-10-04 21:52:35 +0000673 PositionalOptions->erase(PositionalOptions->begin());
Chris Lattneraa852bb2002-07-23 17:15:12 +0000674 }
675}
676
Chris Lattner331de232002-07-22 02:07:59 +0000677
678// getValueStr - Get the value description string, using "DefaultMsg" if nothing
679// has been specified yet.
680//
681static const char *getValueStr(const Option &O, const char *DefaultMsg) {
682 if (O.ValueStr[0] == 0) return DefaultMsg;
683 return O.ValueStr;
684}
685
686//===----------------------------------------------------------------------===//
687// cl::alias class implementation
688//
689
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000690// Return the width of the option tag for printing...
Chris Lattner331de232002-07-22 02:07:59 +0000691unsigned alias::getOptionWidth() const {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000692 return std::strlen(ArgStr)+6;
693}
694
Chris Lattnera0de8432006-04-28 05:36:25 +0000695// Print out the option for the alias.
Chris Lattner331de232002-07-22 02:07:59 +0000696void alias::printOptionInfo(unsigned GlobalWidth) const {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000697 unsigned L = std::strlen(ArgStr);
Chris Lattnera0de8432006-04-28 05:36:25 +0000698 std::cout << " -" << ArgStr << std::string(GlobalWidth-L-6, ' ') << " - "
Chris Lattnerca6433f2003-05-22 20:06:43 +0000699 << HelpStr << "\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000700}
701
702
Chris Lattner331de232002-07-22 02:07:59 +0000703
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000704//===----------------------------------------------------------------------===//
Chris Lattner331de232002-07-22 02:07:59 +0000705// Parser Implementation code...
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000706//
707
Chris Lattner9b14eb52002-08-07 18:36:37 +0000708// basic_parser implementation
709//
710
711// Return the width of the option tag for printing...
712unsigned basic_parser_impl::getOptionWidth(const Option &O) const {
713 unsigned Len = std::strlen(O.ArgStr);
714 if (const char *ValName = getValueName())
715 Len += std::strlen(getValueStr(O, ValName))+3;
716
717 return Len + 6;
718}
719
Misha Brukmanf976c852005-04-21 22:55:34 +0000720// printOptionInfo - Print out information about this option. The
Chris Lattner9b14eb52002-08-07 18:36:37 +0000721// to-be-maintained width is specified.
722//
723void basic_parser_impl::printOptionInfo(const Option &O,
724 unsigned GlobalWidth) const {
Chris Lattnera0de8432006-04-28 05:36:25 +0000725 std::cout << " -" << O.ArgStr;
Chris Lattner9b14eb52002-08-07 18:36:37 +0000726
727 if (const char *ValName = getValueName())
Chris Lattnera0de8432006-04-28 05:36:25 +0000728 std::cout << "=<" << getValueStr(O, ValName) << ">";
Chris Lattner9b14eb52002-08-07 18:36:37 +0000729
Chris Lattnera0de8432006-04-28 05:36:25 +0000730 std::cout << std::string(GlobalWidth-getOptionWidth(O), ' ') << " - "
Chris Lattnerca6433f2003-05-22 20:06:43 +0000731 << O.HelpStr << "\n";
Chris Lattner9b14eb52002-08-07 18:36:37 +0000732}
733
734
735
736
Chris Lattner331de232002-07-22 02:07:59 +0000737// parser<bool> implementation
738//
Chris Lattner9b14eb52002-08-07 18:36:37 +0000739bool parser<bool>::parse(Option &O, const char *ArgName,
Chris Lattnerca6433f2003-05-22 20:06:43 +0000740 const std::string &Arg, bool &Value) {
Misha Brukmanf976c852005-04-21 22:55:34 +0000741 if (Arg == "" || Arg == "true" || Arg == "TRUE" || Arg == "True" ||
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000742 Arg == "1") {
743 Value = true;
744 } else if (Arg == "false" || Arg == "FALSE" || Arg == "False" || Arg == "0") {
745 Value = false;
746 } else {
Chris Lattner331de232002-07-22 02:07:59 +0000747 return O.error(": '" + Arg +
748 "' is invalid value for boolean argument! Try 0 or 1");
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000749 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000750 return false;
751}
752
Chris Lattner331de232002-07-22 02:07:59 +0000753// parser<int> implementation
754//
Chris Lattner9b14eb52002-08-07 18:36:37 +0000755bool parser<int>::parse(Option &O, const char *ArgName,
Chris Lattnerca6433f2003-05-22 20:06:43 +0000756 const std::string &Arg, int &Value) {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000757 char *End;
Chris Lattnerd2a6fc32003-06-28 15:47:20 +0000758 Value = (int)strtol(Arg.c_str(), &End, 0);
Misha Brukmanf976c852005-04-21 22:55:34 +0000759 if (*End != 0)
Chris Lattner331de232002-07-22 02:07:59 +0000760 return O.error(": '" + Arg + "' value invalid for integer argument!");
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000761 return false;
762}
763
Chris Lattnerd2a6fc32003-06-28 15:47:20 +0000764// parser<unsigned> implementation
765//
766bool parser<unsigned>::parse(Option &O, const char *ArgName,
767 const std::string &Arg, unsigned &Value) {
768 char *End;
Brian Gaeke2d6a2362003-10-10 17:01:36 +0000769 errno = 0;
770 unsigned long V = strtoul(Arg.c_str(), &End, 0);
Chris Lattnerd2a6fc32003-06-28 15:47:20 +0000771 Value = (unsigned)V;
Brian Gaeke2d6a2362003-10-10 17:01:36 +0000772 if (((V == ULONG_MAX) && (errno == ERANGE))
773 || (*End != 0)
774 || (Value != V))
Chris Lattnerd2a6fc32003-06-28 15:47:20 +0000775 return O.error(": '" + Arg + "' value invalid for uint argument!");
776 return false;
777}
778
Chris Lattner9b14eb52002-08-07 18:36:37 +0000779// parser<double>/parser<float> implementation
Chris Lattnerd215fd12001-10-13 06:53:19 +0000780//
Chris Lattnerca6433f2003-05-22 20:06:43 +0000781static bool parseDouble(Option &O, const std::string &Arg, double &Value) {
Chris Lattner331de232002-07-22 02:07:59 +0000782 const char *ArgStart = Arg.c_str();
783 char *End;
784 Value = strtod(ArgStart, &End);
Misha Brukmanf976c852005-04-21 22:55:34 +0000785 if (*End != 0)
Chris Lattner331de232002-07-22 02:07:59 +0000786 return O.error(": '" +Arg+ "' value invalid for floating point argument!");
Chris Lattnerd215fd12001-10-13 06:53:19 +0000787 return false;
788}
789
Chris Lattner9b14eb52002-08-07 18:36:37 +0000790bool parser<double>::parse(Option &O, const char *AN,
791 const std::string &Arg, double &Val) {
792 return parseDouble(O, Arg, Val);
Chris Lattner331de232002-07-22 02:07:59 +0000793}
794
Chris Lattner9b14eb52002-08-07 18:36:37 +0000795bool parser<float>::parse(Option &O, const char *AN,
796 const std::string &Arg, float &Val) {
797 double dVal;
798 if (parseDouble(O, Arg, dVal))
799 return true;
800 Val = (float)dVal;
801 return false;
Chris Lattner331de232002-07-22 02:07:59 +0000802}
803
804
Chris Lattner331de232002-07-22 02:07:59 +0000805
806// generic_parser_base implementation
807//
808
Chris Lattneraa852bb2002-07-23 17:15:12 +0000809// findOption - Return the option number corresponding to the specified
810// argument string. If the option is not found, getNumOptions() is returned.
811//
812unsigned generic_parser_base::findOption(const char *Name) {
813 unsigned i = 0, e = getNumOptions();
Chris Lattnerca6433f2003-05-22 20:06:43 +0000814 std::string N(Name);
Chris Lattneraa852bb2002-07-23 17:15:12 +0000815
816 while (i != e)
817 if (getOption(i) == N)
818 return i;
819 else
820 ++i;
821 return e;
822}
823
824
Chris Lattner331de232002-07-22 02:07:59 +0000825// Return the width of the option tag for printing...
826unsigned generic_parser_base::getOptionWidth(const Option &O) const {
827 if (O.hasArgStr()) {
828 unsigned Size = std::strlen(O.ArgStr)+6;
829 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
830 Size = std::max(Size, (unsigned)std::strlen(getOption(i))+8);
831 return Size;
832 } else {
833 unsigned BaseSize = 0;
834 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
835 BaseSize = std::max(BaseSize, (unsigned)std::strlen(getOption(i))+8);
836 return BaseSize;
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000837 }
838}
839
Misha Brukmanf976c852005-04-21 22:55:34 +0000840// printOptionInfo - Print out information about this option. The
Chris Lattner331de232002-07-22 02:07:59 +0000841// to-be-maintained width is specified.
842//
843void generic_parser_base::printOptionInfo(const Option &O,
844 unsigned GlobalWidth) const {
845 if (O.hasArgStr()) {
846 unsigned L = std::strlen(O.ArgStr);
Chris Lattnera0de8432006-04-28 05:36:25 +0000847 std::cout << " -" << O.ArgStr << std::string(GlobalWidth-L-6, ' ')
Chris Lattnerca6433f2003-05-22 20:06:43 +0000848 << " - " << O.HelpStr << "\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000849
Chris Lattner331de232002-07-22 02:07:59 +0000850 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
851 unsigned NumSpaces = GlobalWidth-strlen(getOption(i))-8;
Chris Lattnera0de8432006-04-28 05:36:25 +0000852 std::cout << " =" << getOption(i) << std::string(NumSpaces, ' ')
Chris Lattnerca6433f2003-05-22 20:06:43 +0000853 << " - " << getDescription(i) << "\n";
Chris Lattner9c9be482002-01-31 00:42:56 +0000854 }
Chris Lattner331de232002-07-22 02:07:59 +0000855 } else {
856 if (O.HelpStr[0])
Chris Lattnera0de8432006-04-28 05:36:25 +0000857 std::cout << " " << O.HelpStr << "\n";
Chris Lattner331de232002-07-22 02:07:59 +0000858 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
859 unsigned L = std::strlen(getOption(i));
Chris Lattnera0de8432006-04-28 05:36:25 +0000860 std::cout << " -" << getOption(i) << std::string(GlobalWidth-L-8, ' ')
Chris Lattnerca6433f2003-05-22 20:06:43 +0000861 << " - " << getDescription(i) << "\n";
Chris Lattner331de232002-07-22 02:07:59 +0000862 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000863 }
864}
865
866
867//===----------------------------------------------------------------------===//
Chris Lattner331de232002-07-22 02:07:59 +0000868// --help and --help-hidden option implementation
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000869//
Reid Spencerad0846b2004-11-14 22:04:00 +0000870
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000871namespace {
872
Chris Lattner331de232002-07-22 02:07:59 +0000873class HelpPrinter {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000874 unsigned MaxArgLen;
875 const Option *EmptyArg;
876 const bool ShowHidden;
877
Chris Lattner331de232002-07-22 02:07:59 +0000878 // isHidden/isReallyHidden - Predicates to be used to filter down arg lists.
Chris Lattnerca6433f2003-05-22 20:06:43 +0000879 inline static bool isHidden(std::pair<std::string, Option *> &OptPair) {
Chris Lattner331de232002-07-22 02:07:59 +0000880 return OptPair.second->getOptionHiddenFlag() >= Hidden;
881 }
Chris Lattnerca6433f2003-05-22 20:06:43 +0000882 inline static bool isReallyHidden(std::pair<std::string, Option *> &OptPair) {
Chris Lattner331de232002-07-22 02:07:59 +0000883 return OptPair.second->getOptionHiddenFlag() == ReallyHidden;
884 }
885
886public:
887 HelpPrinter(bool showHidden) : ShowHidden(showHidden) {
888 EmptyArg = 0;
889 }
890
891 void operator=(bool Value) {
892 if (Value == false) return;
893
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000894 // Copy Options into a vector so we can sort them as we like...
Chris Lattner90aa8392006-10-04 21:52:35 +0000895 std::vector<std::pair<std::string, Option*> > Opts;
896 copy(Options->begin(), Options->end(), std::back_inserter(Opts));
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000897
898 // Eliminate Hidden or ReallyHidden arguments, depending on ShowHidden
Chris Lattner90aa8392006-10-04 21:52:35 +0000899 Opts.erase(std::remove_if(Opts.begin(), Opts.end(),
900 std::ptr_fun(ShowHidden ? isReallyHidden : isHidden)),
901 Opts.end());
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000902
903 // Eliminate duplicate entries in table (from enum flags options, f.e.)
Chris Lattner331de232002-07-22 02:07:59 +0000904 { // Give OptionSet a scope
905 std::set<Option*> OptionSet;
Chris Lattner90aa8392006-10-04 21:52:35 +0000906 for (unsigned i = 0; i != Opts.size(); ++i)
907 if (OptionSet.count(Opts[i].second) == 0)
908 OptionSet.insert(Opts[i].second); // Add new entry to set
Chris Lattner331de232002-07-22 02:07:59 +0000909 else
Chris Lattner90aa8392006-10-04 21:52:35 +0000910 Opts.erase(Opts.begin()+i--); // Erase duplicate
Chris Lattner331de232002-07-22 02:07:59 +0000911 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000912
913 if (ProgramOverview)
Chris Lattnera0de8432006-04-28 05:36:25 +0000914 std::cout << "OVERVIEW:" << ProgramOverview << "\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000915
Chris Lattnera0de8432006-04-28 05:36:25 +0000916 std::cout << "USAGE: " << ProgramName << " [options]";
Chris Lattner331de232002-07-22 02:07:59 +0000917
Chris Lattner90aa8392006-10-04 21:52:35 +0000918 // Print out the positional options.
919 std::vector<Option*> &PosOpts = *PositionalOptions;
Chris Lattner331de232002-07-22 02:07:59 +0000920 Option *CAOpt = 0; // The cl::ConsumeAfter option, if it exists...
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000921 if (!PosOpts.empty() && PosOpts[0]->getNumOccurrencesFlag() == ConsumeAfter)
Chris Lattner331de232002-07-22 02:07:59 +0000922 CAOpt = PosOpts[0];
923
Chris Lattner9cf3d472003-07-30 17:34:02 +0000924 for (unsigned i = CAOpt != 0, e = PosOpts.size(); i != e; ++i) {
925 if (PosOpts[i]->ArgStr[0])
Chris Lattnera0de8432006-04-28 05:36:25 +0000926 std::cout << " --" << PosOpts[i]->ArgStr;
927 std::cout << " " << PosOpts[i]->HelpStr;
Chris Lattner9cf3d472003-07-30 17:34:02 +0000928 }
Chris Lattner331de232002-07-22 02:07:59 +0000929
930 // Print the consume after option info if it exists...
Chris Lattnera0de8432006-04-28 05:36:25 +0000931 if (CAOpt) std::cout << " " << CAOpt->HelpStr;
Chris Lattner331de232002-07-22 02:07:59 +0000932
Chris Lattnera0de8432006-04-28 05:36:25 +0000933 std::cout << "\n\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000934
935 // Compute the maximum argument length...
936 MaxArgLen = 0;
Chris Lattner90aa8392006-10-04 21:52:35 +0000937 for (unsigned i = 0, e = Opts.size(); i != e; ++i)
938 MaxArgLen = std::max(MaxArgLen, Opts[i].second->getOptionWidth());
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000939
Chris Lattnera0de8432006-04-28 05:36:25 +0000940 std::cout << "OPTIONS:\n";
Chris Lattner90aa8392006-10-04 21:52:35 +0000941 for (unsigned i = 0, e = Opts.size(); i != e; ++i)
942 Opts[i].second->printOptionInfo(MaxArgLen);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000943
Chris Lattnerc540ebb2004-11-19 17:08:15 +0000944 // Print any extra help the user has declared.
Chris Lattner90aa8392006-10-04 21:52:35 +0000945 for (std::vector<const char *>::iterator I = MoreHelp->begin(),
946 E = MoreHelp->end(); I != E; ++I)
Chris Lattnera0de8432006-04-28 05:36:25 +0000947 std::cout << *I;
Chris Lattner90aa8392006-10-04 21:52:35 +0000948 MoreHelp->clear();
Reid Spencerad0846b2004-11-14 22:04:00 +0000949
Reid Spencer9bbba0912004-11-16 06:11:52 +0000950 // Halt the program since help information was printed
Chris Lattner90aa8392006-10-04 21:52:35 +0000951 Options->clear(); // Don't bother making option dtors remove from map.
Chris Lattner331de232002-07-22 02:07:59 +0000952 exit(1);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000953 }
954};
Chris Lattner500d8bf2006-10-12 22:09:17 +0000955} // End anonymous namespace
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000956
Chris Lattner331de232002-07-22 02:07:59 +0000957// Define the two HelpPrinter instances that are used to print out help, or
958// help-hidden...
959//
Chris Lattner500d8bf2006-10-12 22:09:17 +0000960static HelpPrinter NormalPrinter(false);
961static HelpPrinter HiddenPrinter(true);
Chris Lattner331de232002-07-22 02:07:59 +0000962
Chris Lattner500d8bf2006-10-12 22:09:17 +0000963static cl::opt<HelpPrinter, true, parser<bool> >
Chris Lattner4bf7afc2005-05-13 19:49:09 +0000964HOp("help", cl::desc("Display available options (--help-hidden for more)"),
Chris Lattner9b14eb52002-08-07 18:36:37 +0000965 cl::location(NormalPrinter), cl::ValueDisallowed);
Chris Lattner331de232002-07-22 02:07:59 +0000966
Chris Lattner500d8bf2006-10-12 22:09:17 +0000967static cl::opt<HelpPrinter, true, parser<bool> >
Chris Lattner4bf7afc2005-05-13 19:49:09 +0000968HHOp("help-hidden", cl::desc("Display all available options"),
Chris Lattner9b14eb52002-08-07 18:36:37 +0000969 cl::location(HiddenPrinter), cl::Hidden, cl::ValueDisallowed);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000970
Chris Lattner500d8bf2006-10-12 22:09:17 +0000971static void (*OverrideVersionPrinter)() = 0;
Reid Spencer515b5b32006-06-05 16:22:56 +0000972
Chris Lattner500d8bf2006-10-12 22:09:17 +0000973namespace {
Reid Spencer515b5b32006-06-05 16:22:56 +0000974class VersionPrinter {
975public:
976 void operator=(bool OptionWasSpecified) {
977 if (OptionWasSpecified) {
978 if (OverrideVersionPrinter == 0) {
Chris Lattner3fc2f4e2006-07-06 18:33:03 +0000979 std::cout << "Low Level Virtual Machine (http://llvm.org/):\n";
980 std::cout << " " << PACKAGE_NAME << " version " << PACKAGE_VERSION;
981#ifdef LLVM_VERSION_INFO
982 std::cout << LLVM_VERSION_INFO;
Reid Spencer515b5b32006-06-05 16:22:56 +0000983#endif
Chris Lattner3fc2f4e2006-07-06 18:33:03 +0000984 std::cout << "\n ";
985#ifndef __OPTIMIZE__
986 std::cout << "DEBUG build";
987#else
988 std::cout << "Optimized build";
989#endif
990#ifndef NDEBUG
991 std::cout << " with assertions";
992#endif
993 std::cout << ".\n";
Chris Lattner90aa8392006-10-04 21:52:35 +0000994 Options->clear(); // Don't bother making option dtors remove from map.
Reid Spencer515b5b32006-06-05 16:22:56 +0000995 exit(1);
996 } else {
997 (*OverrideVersionPrinter)();
998 exit(1);
999 }
1000 }
1001 }
1002};
Chris Lattner500d8bf2006-10-12 22:09:17 +00001003} // End anonymous namespace
Reid Spencer515b5b32006-06-05 16:22:56 +00001004
1005
Reid Spencer69105f32004-08-04 00:36:06 +00001006// Define the --version option that prints out the LLVM version for the tool
Chris Lattner500d8bf2006-10-12 22:09:17 +00001007static VersionPrinter VersionPrinterInstance;
1008
1009static cl::opt<VersionPrinter, true, parser<bool> >
Chris Lattner4bf7afc2005-05-13 19:49:09 +00001010VersOp("version", cl::desc("Display the version of this program"),
Reid Spencer69105f32004-08-04 00:36:06 +00001011 cl::location(VersionPrinterInstance), cl::ValueDisallowed);
1012
Reid Spencer9bbba0912004-11-16 06:11:52 +00001013// Utility function for printing the help message.
1014void cl::PrintHelpMessage() {
Misha Brukmanf976c852005-04-21 22:55:34 +00001015 // This looks weird, but it actually prints the help message. The
Reid Spencer5cc498f2004-11-16 06:50:36 +00001016 // NormalPrinter variable is a HelpPrinter and the help gets printed when
1017 // its operator= is invoked. That's because the "normal" usages of the
Misha Brukmanf976c852005-04-21 22:55:34 +00001018 // help printer is to be assigned true/false depending on whether the
Reid Spencer5cc498f2004-11-16 06:50:36 +00001019 // --help option was given or not. Since we're circumventing that we have
1020 // to make it look like --help was given, so we assign true.
Reid Spencer9bbba0912004-11-16 06:11:52 +00001021 NormalPrinter = true;
1022}
Reid Spencer515b5b32006-06-05 16:22:56 +00001023
1024void cl::SetVersionPrinter(void (*func)()) {
1025 OverrideVersionPrinter = func;
1026}