blob: 8a6bd77e4014508d54a8796213f7f1a3e857134a [file] [log] [blame]
Chris Lattnerdbab15a2001-07-23 17:17:47 +00001//===-- CommandLine.cpp - Command line parser implementation --------------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattnerdbab15a2001-07-23 17:17:47 +00009//
10// This class implements a command line argument processor that is useful when
11// creating a tool. It provides a simple, minimalistic interface that is easily
12// extensible and supports nonlocal (library) command line options.
13//
Chris Lattner03fe1bd2001-07-23 23:04:07 +000014// Note that rather than trying to figure out what this code does, you could try
15// reading the library documentation located in docs/CommandLine.html
16//
Chris Lattnerdbab15a2001-07-23 17:17:47 +000017//===----------------------------------------------------------------------===//
18
Chris Lattnercee8f9a2001-11-27 00:03:19 +000019#include "Support/CommandLine.h"
Chris Lattnerdbab15a2001-07-23 17:17:47 +000020#include <algorithm>
21#include <map>
22#include <set>
Chris Lattner697954c2002-01-20 22:54:45 +000023#include <iostream>
Brian Gaeke2d6a2362003-10-10 17:01:36 +000024#include <cstdlib>
25#include <cerrno>
Chris Lattner2cdd21c2003-12-14 21:35:53 +000026using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000027
Chris Lattnerdbab15a2001-07-23 17:17:47 +000028using namespace cl;
29
Chris Lattner331de232002-07-22 02:07:59 +000030//===----------------------------------------------------------------------===//
31// Basic, shared command line option processing machinery...
32//
33
Chris Lattnerdbab15a2001-07-23 17:17:47 +000034// Return the global command line option vector. Making it a function scoped
Chris Lattnerf78032f2001-11-26 18:58:34 +000035// static ensures that it will be initialized correctly before its first use.
Chris Lattnerdbab15a2001-07-23 17:17:47 +000036//
Chris Lattnerca6433f2003-05-22 20:06:43 +000037static std::map<std::string, Option*> *CommandLineOptions = 0;
38static std::map<std::string, Option*> &getOpts() {
39 if (CommandLineOptions == 0)
40 CommandLineOptions = new std::map<std::string,Option*>();
Chris Lattnere8e258b2002-07-29 20:58:42 +000041 return *CommandLineOptions;
42}
43
Chris Lattnerca6433f2003-05-22 20:06:43 +000044static Option *getOption(const std::string &Str) {
Chris Lattnere8e258b2002-07-29 20:58:42 +000045 if (CommandLineOptions == 0) return 0;
Chris Lattnerca6433f2003-05-22 20:06:43 +000046 std::map<std::string,Option*>::iterator I = CommandLineOptions->find(Str);
Chris Lattnere8e258b2002-07-29 20:58:42 +000047 return I != CommandLineOptions->end() ? I->second : 0;
Chris Lattnerdbab15a2001-07-23 17:17:47 +000048}
49
Chris Lattnerca6433f2003-05-22 20:06:43 +000050static std::vector<Option*> &getPositionalOpts() {
51 static std::vector<Option*> Positional;
Chris Lattner331de232002-07-22 02:07:59 +000052 return Positional;
53}
54
Chris Lattnere8e258b2002-07-29 20:58:42 +000055static void AddArgument(const char *ArgName, Option *Opt) {
56 if (getOption(ArgName)) {
Chris Lattnerca6433f2003-05-22 20:06:43 +000057 std::cerr << "CommandLine Error: Argument '" << ArgName
58 << "' defined more than once!\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +000059 } else {
Chris Lattnerf78032f2001-11-26 18:58:34 +000060 // Add argument to the argument map!
Chris Lattnere8e258b2002-07-29 20:58:42 +000061 getOpts()[ArgName] = Opt;
62 }
63}
64
65// RemoveArgument - It's possible that the argument is no longer in the map if
66// options have already been processed and the map has been deleted!
67//
68static void RemoveArgument(const char *ArgName, Option *Opt) {
69 if (CommandLineOptions == 0) return;
70 assert(getOption(ArgName) == Opt && "Arg not in map!");
71 CommandLineOptions->erase(ArgName);
72 if (CommandLineOptions->empty()) {
73 delete CommandLineOptions;
74 CommandLineOptions = 0;
Chris Lattnerdbab15a2001-07-23 17:17:47 +000075 }
76}
77
78static const char *ProgramName = 0;
79static const char *ProgramOverview = 0;
80
Chris Lattnercaccd762001-10-27 05:54:17 +000081static inline bool ProvideOption(Option *Handler, const char *ArgName,
82 const char *Value, int argc, char **argv,
83 int &i) {
84 // Enforce value requirements
85 switch (Handler->getValueExpectedFlag()) {
86 case ValueRequired:
87 if (Value == 0 || *Value == 0) { // No value specified?
88 if (i+1 < argc) { // Steal the next argument, like for '-o filename'
89 Value = argv[++i];
90 } else {
91 return Handler->error(" requires a value!");
92 }
93 }
94 break;
95 case ValueDisallowed:
96 if (*Value != 0)
97 return Handler->error(" does not allow a value! '" +
Chris Lattnerca6433f2003-05-22 20:06:43 +000098 std::string(Value) + "' specified.");
Chris Lattnercaccd762001-10-27 05:54:17 +000099 break;
100 case ValueOptional: break;
Chris Lattnerca6433f2003-05-22 20:06:43 +0000101 default: std::cerr << "Bad ValueMask flag! CommandLine usage error:"
102 << Handler->getValueExpectedFlag() << "\n"; abort();
Chris Lattnercaccd762001-10-27 05:54:17 +0000103 }
104
105 // Run the handler now!
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000106 return Handler->addOccurrence(ArgName, Value);
Chris Lattnercaccd762001-10-27 05:54:17 +0000107}
108
Chris Lattner9cf3d472003-07-30 17:34:02 +0000109static bool ProvidePositionalOption(Option *Handler, const std::string &Arg) {
Chris Lattner331de232002-07-22 02:07:59 +0000110 int Dummy;
Chris Lattner9cf3d472003-07-30 17:34:02 +0000111 return ProvideOption(Handler, Handler->ArgStr, Arg.c_str(), 0, 0, Dummy);
Chris Lattner331de232002-07-22 02:07:59 +0000112}
Chris Lattnerf78032f2001-11-26 18:58:34 +0000113
Chris Lattner331de232002-07-22 02:07:59 +0000114
115// Option predicates...
116static inline bool isGrouping(const Option *O) {
117 return O->getFormattingFlag() == cl::Grouping;
118}
119static inline bool isPrefixedOrGrouping(const Option *O) {
120 return isGrouping(O) || O->getFormattingFlag() == cl::Prefix;
121}
122
123// getOptionPred - Check to see if there are any options that satisfy the
124// specified predicate with names that are the prefixes in Name. This is
125// checked by progressively stripping characters off of the name, checking to
126// see if there options that satisfy the predicate. If we find one, return it,
127// otherwise return null.
128//
129static Option *getOptionPred(std::string Name, unsigned &Length,
130 bool (*Pred)(const Option*)) {
131
Chris Lattnere8e258b2002-07-29 20:58:42 +0000132 Option *Op = getOption(Name);
133 if (Op && Pred(Op)) {
Chris Lattner331de232002-07-22 02:07:59 +0000134 Length = Name.length();
Chris Lattnere8e258b2002-07-29 20:58:42 +0000135 return Op;
Chris Lattnerf78032f2001-11-26 18:58:34 +0000136 }
137
Chris Lattner331de232002-07-22 02:07:59 +0000138 if (Name.size() == 1) return 0;
139 do {
140 Name.erase(Name.end()-1, Name.end()); // Chop off the last character...
Chris Lattnere8e258b2002-07-29 20:58:42 +0000141 Op = getOption(Name);
Chris Lattner331de232002-07-22 02:07:59 +0000142
143 // Loop while we haven't found an option and Name still has at least two
144 // characters in it (so that the next iteration will not be the empty
145 // string...
Chris Lattnere8e258b2002-07-29 20:58:42 +0000146 } while ((Op == 0 || !Pred(Op)) && Name.size() > 1);
Chris Lattner331de232002-07-22 02:07:59 +0000147
Chris Lattnere8e258b2002-07-29 20:58:42 +0000148 if (Op && Pred(Op)) {
Chris Lattner331de232002-07-22 02:07:59 +0000149 Length = Name.length();
Chris Lattnere8e258b2002-07-29 20:58:42 +0000150 return Op; // Found one!
Chris Lattner331de232002-07-22 02:07:59 +0000151 }
152 return 0; // No option found!
153}
154
155static bool RequiresValue(const Option *O) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000156 return O->getNumOccurrencesFlag() == cl::Required ||
157 O->getNumOccurrencesFlag() == cl::OneOrMore;
Chris Lattner331de232002-07-22 02:07:59 +0000158}
159
160static bool EatsUnboundedNumberOfValues(const Option *O) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000161 return O->getNumOccurrencesFlag() == cl::ZeroOrMore ||
162 O->getNumOccurrencesFlag() == cl::OneOrMore;
Chris Lattnerf78032f2001-11-26 18:58:34 +0000163}
Chris Lattnercaccd762001-10-27 05:54:17 +0000164
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000165/// ParseCStringVector - Break INPUT up wherever one or more
166/// whitespace characters are found, and store the resulting tokens in
167/// OUTPUT. The tokens stored in OUTPUT are dynamically allocated
168/// using strdup (), so it is the caller's responsibility to free ()
169/// them later.
Brian Gaeke06b06c52003-08-14 22:00:59 +0000170///
171static void ParseCStringVector (std::vector<char *> &output,
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000172 const char *input) {
173 // Characters which will be treated as token separators:
174 static const char *delims = " \v\f\t\r\n";
175
176 std::string work (input);
177 // Skip past any delims at head of input string.
178 size_t pos = work.find_first_not_of (delims);
179 // If the string consists entirely of delims, then exit early.
180 if (pos == std::string::npos) return;
181 // Otherwise, jump forward to beginning of first word.
182 work = work.substr (pos);
183 // Find position of first delimiter.
184 pos = work.find_first_of (delims);
185
186 while (!work.empty() && pos != std::string::npos) {
187 // Everything from 0 to POS is the next word to copy.
188 output.push_back (strdup (work.substr (0,pos).c_str ()));
189 // Is there another word in the string?
190 size_t nextpos = work.find_first_not_of (delims, pos + 1);
191 if (nextpos != std::string::npos) {
192 // Yes? Then remove delims from beginning ...
193 work = work.substr (work.find_first_not_of (delims, pos + 1));
194 // and find the end of the word.
195 pos = work.find_first_of (delims);
196 } else {
197 // No? (Remainder of string is delims.) End the loop.
198 work = "";
199 pos = std::string::npos;
200 }
201 }
202
203 // If `input' ended with non-delim char, then we'll get here with
204 // the last word of `input' in `work'; copy it now.
205 if (!work.empty ()) {
206 output.push_back (strdup (work.c_str ()));
Brian Gaeke06b06c52003-08-14 22:00:59 +0000207 }
208}
209
210/// ParseEnvironmentOptions - An alternative entry point to the
211/// CommandLine library, which allows you to read the program's name
212/// from the caller (as PROGNAME) and its command-line arguments from
213/// an environment variable (whose name is given in ENVVAR).
214///
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000215void cl::ParseEnvironmentOptions (const char *progName, const char *envVar,
Brian Gaeke06b06c52003-08-14 22:00:59 +0000216 const char *Overview) {
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000217 // Check args.
218 assert (progName && "Program name not specified");
219 assert (envVar && "Environment variable name missing");
220
221 // Get the environment variable they want us to parse options out of.
222 const char *envValue = getenv (envVar);
223 if (!envValue)
224 return;
225
Brian Gaeke06b06c52003-08-14 22:00:59 +0000226 // Get program's "name", which we wouldn't know without the caller
227 // telling us.
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000228 std::vector<char *> newArgv;
229 newArgv.push_back (strdup (progName));
Brian Gaeke06b06c52003-08-14 22:00:59 +0000230
231 // Parse the value of the environment variable into a "command line"
232 // and hand it off to ParseCommandLineOptions().
Brian Gaekec48ef2a2003-08-15 21:05:57 +0000233 ParseCStringVector (newArgv, envValue);
234 int newArgc = newArgv.size ();
235 ParseCommandLineOptions (newArgc, &newArgv[0], Overview);
236
237 // Free all the strdup()ed strings.
238 for (std::vector<char *>::iterator i = newArgv.begin (), e = newArgv.end ();
239 i != e; ++i) {
240 free (*i);
241 }
Brian Gaeke06b06c52003-08-14 22:00:59 +0000242}
243
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000244void cl::ParseCommandLineOptions(int &argc, char **argv,
Chris Lattner0c0edf82002-07-25 06:17:51 +0000245 const char *Overview) {
Chris Lattner331de232002-07-22 02:07:59 +0000246 assert((!getOpts().empty() || !getPositionalOpts().empty()) &&
247 "No options specified, or ParseCommandLineOptions called more"
248 " than once!");
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000249 ProgramName = argv[0]; // Save this away safe and snug
250 ProgramOverview = Overview;
251 bool ErrorParsing = false;
252
Chris Lattnerca6433f2003-05-22 20:06:43 +0000253 std::map<std::string, Option*> &Opts = getOpts();
254 std::vector<Option*> &PositionalOpts = getPositionalOpts();
Chris Lattner331de232002-07-22 02:07:59 +0000255
256 // Check out the positional arguments to collect information about them.
257 unsigned NumPositionalRequired = 0;
258 Option *ConsumeAfterOpt = 0;
259 if (!PositionalOpts.empty()) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000260 if (PositionalOpts[0]->getNumOccurrencesFlag() == cl::ConsumeAfter) {
Chris Lattner331de232002-07-22 02:07:59 +0000261 assert(PositionalOpts.size() > 1 &&
262 "Cannot specify cl::ConsumeAfter without a positional argument!");
263 ConsumeAfterOpt = PositionalOpts[0];
264 }
265
266 // Calculate how many positional values are _required_.
267 bool UnboundedFound = false;
268 for (unsigned i = ConsumeAfterOpt != 0, e = PositionalOpts.size();
269 i != e; ++i) {
270 Option *Opt = PositionalOpts[i];
271 if (RequiresValue(Opt))
272 ++NumPositionalRequired;
273 else if (ConsumeAfterOpt) {
274 // ConsumeAfter cannot be combined with "optional" positional options
Chris Lattner54ec7ae2002-07-22 02:21:57 +0000275 // unless there is only one positional argument...
276 if (PositionalOpts.size() > 2)
277 ErrorParsing |=
278 Opt->error(" error - this positional option will never be matched, "
279 "because it does not Require a value, and a "
280 "cl::ConsumeAfter option is active!");
Chris Lattner9cf3d472003-07-30 17:34:02 +0000281 } else if (UnboundedFound && !Opt->ArgStr[0]) {
282 // This option does not "require" a value... Make sure this option is
283 // not specified after an option that eats all extra arguments, or this
284 // one will never get any!
Chris Lattner331de232002-07-22 02:07:59 +0000285 //
286 ErrorParsing |= Opt->error(" error - option can never match, because "
287 "another positional argument will match an "
288 "unbounded number of values, and this option"
289 " does not require a value!");
290 }
291 UnboundedFound |= EatsUnboundedNumberOfValues(Opt);
292 }
293 }
294
295 // PositionalVals - A vector of "positional" arguments we accumulate into to
296 // processes at the end...
297 //
Chris Lattnerca6433f2003-05-22 20:06:43 +0000298 std::vector<std::string> PositionalVals;
Chris Lattner331de232002-07-22 02:07:59 +0000299
Chris Lattner9cf3d472003-07-30 17:34:02 +0000300 // If the program has named positional arguments, and the name has been run
301 // across, keep track of which positional argument was named. Otherwise put
302 // the positional args into the PositionalVals list...
303 Option *ActivePositionalArg = 0;
304
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000305 // Loop over all of the arguments... processing them.
Chris Lattner331de232002-07-22 02:07:59 +0000306 bool DashDashFound = false; // Have we read '--'?
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000307 for (int i = 1; i < argc; ++i) {
308 Option *Handler = 0;
309 const char *Value = "";
310 const char *ArgName = "";
Chris Lattner331de232002-07-22 02:07:59 +0000311
312 // Check to see if this is a positional argument. This argument is
313 // considered to be positional if it doesn't start with '-', if it is "-"
Misha Brukman1115e042003-07-10 21:38:28 +0000314 // itself, or if we have seen "--" already.
Chris Lattner331de232002-07-22 02:07:59 +0000315 //
316 if (argv[i][0] != '-' || argv[i][1] == 0 || DashDashFound) {
317 // Positional argument!
Chris Lattner9cf3d472003-07-30 17:34:02 +0000318 if (ActivePositionalArg) {
319 ProvidePositionalOption(ActivePositionalArg, argv[i]);
320 continue; // We are done!
321 } else if (!PositionalOpts.empty()) {
Chris Lattner331de232002-07-22 02:07:59 +0000322 PositionalVals.push_back(argv[i]);
323
324 // All of the positional arguments have been fulfulled, give the rest to
325 // the consume after option... if it's specified...
326 //
Chris Lattnerd16714b2002-07-31 16:29:43 +0000327 if (PositionalVals.size() >= NumPositionalRequired &&
Chris Lattner331de232002-07-22 02:07:59 +0000328 ConsumeAfterOpt != 0) {
329 for (++i; i < argc; ++i)
330 PositionalVals.push_back(argv[i]);
331 break; // Handle outside of the argument processing loop...
332 }
333
334 // Delay processing positional arguments until the end...
335 continue;
336 }
337 } else { // We start with a '-', must be an argument...
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000338 ArgName = argv[i]+1;
339 while (*ArgName == '-') ++ArgName; // Eat leading dashes
340
Chris Lattner331de232002-07-22 02:07:59 +0000341 if (*ArgName == 0 && !DashDashFound) { // Is this the mythical "--"?
342 DashDashFound = true; // Yup, take note of that fact...
Misha Brukman950971d2003-09-16 15:31:46 +0000343 continue; // Don't try to process it as an argument itself.
Chris Lattner331de232002-07-22 02:07:59 +0000344 }
345
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000346 const char *ArgNameEnd = ArgName;
Chris Lattnerf78032f2001-11-26 18:58:34 +0000347 while (*ArgNameEnd && *ArgNameEnd != '=')
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000348 ++ArgNameEnd; // Scan till end of argument name...
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000349
350 Value = ArgNameEnd;
351 if (*Value) // If we have an equals sign...
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000352 ++Value; // Advance to value...
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000353
354 if (*ArgName != 0) {
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000355 std::string RealName(ArgName, ArgNameEnd);
356 // Extract arg name part
Chris Lattnerca6433f2003-05-22 20:06:43 +0000357 std::map<std::string, Option*>::iterator I = Opts.find(RealName);
Chris Lattnerf78032f2001-11-26 18:58:34 +0000358
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000359 if (I == Opts.end() && !*Value && RealName.size() > 1) {
Chris Lattner331de232002-07-22 02:07:59 +0000360 // Check to see if this "option" is really a prefixed or grouped
361 // argument...
362 //
363 unsigned Length = 0;
364 Option *PGOpt = getOptionPred(RealName, Length, isPrefixedOrGrouping);
Chris Lattnerf78032f2001-11-26 18:58:34 +0000365
Chris Lattner331de232002-07-22 02:07:59 +0000366 // If the option is a prefixed option, then the value is simply the
367 // rest of the name... so fall through to later processing, by
368 // setting up the argument name flags and value fields.
369 //
370 if (PGOpt && PGOpt->getFormattingFlag() == cl::Prefix) {
371 ArgNameEnd = ArgName+Length;
372 Value = ArgNameEnd;
Chris Lattnerca6433f2003-05-22 20:06:43 +0000373 I = Opts.find(std::string(ArgName, ArgNameEnd));
Chris Lattner331de232002-07-22 02:07:59 +0000374 assert(I->second == PGOpt);
375 } else if (PGOpt) {
376 // This must be a grouped option... handle all of them now...
377 assert(isGrouping(PGOpt) && "Broken getOptionPred!");
378
379 do {
380 // Move current arg name out of RealName into RealArgName...
Chris Lattnerca6433f2003-05-22 20:06:43 +0000381 std::string RealArgName(RealName.begin(),RealName.begin()+Length);
Chris Lattner331de232002-07-22 02:07:59 +0000382 RealName.erase(RealName.begin(), RealName.begin()+Length);
Chris Lattnerf78032f2001-11-26 18:58:34 +0000383
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000384 // Because ValueRequired is an invalid flag for grouped arguments,
385 // we don't need to pass argc/argv in...
386 //
Chris Lattner331de232002-07-22 02:07:59 +0000387 assert(PGOpt->getValueExpectedFlag() != cl::ValueRequired &&
388 "Option can not be cl::Grouping AND cl::ValueRequired!");
389 int Dummy;
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000390 ErrorParsing |= ProvideOption(PGOpt, RealArgName.c_str(), "",
Chris Lattner331de232002-07-22 02:07:59 +0000391 0, 0, Dummy);
392
393 // Get the next grouping option...
394 if (!RealName.empty())
395 PGOpt = getOptionPred(RealName, Length, isGrouping);
396 } while (!RealName.empty() && PGOpt);
397
398 if (RealName.empty()) // Processed all of the options, move on
399 continue; // to the next argv[] value...
400
401 // If RealName is not empty, that means we did not match one of the
402 // options! This is an error.
403 //
404 I = Opts.end();
405 }
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000406 }
Chris Lattnerf78032f2001-11-26 18:58:34 +0000407
Chris Lattner331de232002-07-22 02:07:59 +0000408 Handler = I != Opts.end() ? I->second : 0;
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000409 }
410 }
411
412 if (Handler == 0) {
Brian Gaekec86e84b2003-09-16 18:00:35 +0000413 std::cerr << "Unknown command line argument '" << argv[i] << "'. Try: '"
Chris Lattnerca6433f2003-05-22 20:06:43 +0000414 << argv[0] << " --help'\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000415 ErrorParsing = true;
416 continue;
417 }
418
Chris Lattner72fb8e52003-05-22 20:26:17 +0000419 // Check to see if this option accepts a comma separated list of values. If
420 // it does, we have to split up the value into multiple values...
421 if (Handler->getMiscFlags() & CommaSeparated) {
422 std::string Val(Value);
423 std::string::size_type Pos = Val.find(',');
424
425 while (Pos != std::string::npos) {
426 // Process the portion before the comma...
427 ErrorParsing |= ProvideOption(Handler, ArgName,
428 std::string(Val.begin(),
429 Val.begin()+Pos).c_str(),
430 argc, argv, i);
431 // Erase the portion before the comma, AND the comma...
432 Val.erase(Val.begin(), Val.begin()+Pos+1);
433 Value += Pos+1; // Increment the original value pointer as well...
434
435 // Check for another comma...
436 Pos = Val.find(',');
437 }
438 }
Chris Lattner9cf3d472003-07-30 17:34:02 +0000439
440 // If this is a named positional argument, just remember that it is the
441 // active one...
442 if (Handler->getFormattingFlag() == cl::Positional)
443 ActivePositionalArg = Handler;
444 else
445 ErrorParsing |= ProvideOption(Handler, ArgName, Value, argc, argv, i);
Chris Lattner331de232002-07-22 02:07:59 +0000446 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000447
Chris Lattner331de232002-07-22 02:07:59 +0000448 // Check and handle positional arguments now...
449 if (NumPositionalRequired > PositionalVals.size()) {
Chris Lattnerca6433f2003-05-22 20:06:43 +0000450 std::cerr << "Not enough positional command line arguments specified!\n"
451 << "Must specify at least " << NumPositionalRequired
452 << " positional arguments: See: " << argv[0] << " --help\n";
Chris Lattner331de232002-07-22 02:07:59 +0000453 ErrorParsing = true;
454
455
456 } else if (ConsumeAfterOpt == 0) {
457 // Positional args have already been handled if ConsumeAfter is specified...
458 unsigned ValNo = 0, NumVals = PositionalVals.size();
459 for (unsigned i = 0, e = PositionalOpts.size(); i != e; ++i) {
460 if (RequiresValue(PositionalOpts[i])) {
461 ProvidePositionalOption(PositionalOpts[i], PositionalVals[ValNo++]);
462 --NumPositionalRequired; // We fulfilled our duty...
463 }
464
465 // If we _can_ give this option more arguments, do so now, as long as we
466 // do not give it values that others need. 'Done' controls whether the
467 // option even _WANTS_ any more.
468 //
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000469 bool Done = PositionalOpts[i]->getNumOccurrencesFlag() == cl::Required;
Chris Lattner331de232002-07-22 02:07:59 +0000470 while (NumVals-ValNo > NumPositionalRequired && !Done) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000471 switch (PositionalOpts[i]->getNumOccurrencesFlag()) {
Chris Lattner331de232002-07-22 02:07:59 +0000472 case cl::Optional:
473 Done = true; // Optional arguments want _at most_ one value
474 // FALL THROUGH
475 case cl::ZeroOrMore: // Zero or more will take all they can get...
476 case cl::OneOrMore: // One or more will take all they can get...
477 ProvidePositionalOption(PositionalOpts[i], PositionalVals[ValNo++]);
478 break;
479 default:
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000480 assert(0 && "Internal error, unexpected NumOccurrences flag in "
Chris Lattner331de232002-07-22 02:07:59 +0000481 "positional argument processing!");
482 }
483 }
Chris Lattnercaccd762001-10-27 05:54:17 +0000484 }
Chris Lattner331de232002-07-22 02:07:59 +0000485 } else {
486 assert(ConsumeAfterOpt && NumPositionalRequired <= PositionalVals.size());
487 unsigned ValNo = 0;
488 for (unsigned j = 1, e = PositionalOpts.size(); j != e; ++j)
489 if (RequiresValue(PositionalOpts[j]))
Chris Lattnerfaba8092002-07-24 20:15:13 +0000490 ErrorParsing |= ProvidePositionalOption(PositionalOpts[j],
491 PositionalVals[ValNo++]);
492
493 // Handle the case where there is just one positional option, and it's
494 // optional. In this case, we want to give JUST THE FIRST option to the
495 // positional option and keep the rest for the consume after. The above
496 // loop would have assigned no values to positional options in this case.
497 //
Chris Lattnerb490c202002-08-02 21:51:29 +0000498 if (PositionalOpts.size() == 2 && ValNo == 0 && !PositionalVals.empty())
Chris Lattnerfaba8092002-07-24 20:15:13 +0000499 ErrorParsing |= ProvidePositionalOption(PositionalOpts[1],
500 PositionalVals[ValNo++]);
Chris Lattner331de232002-07-22 02:07:59 +0000501
502 // Handle over all of the rest of the arguments to the
503 // cl::ConsumeAfter command line option...
504 for (; ValNo != PositionalVals.size(); ++ValNo)
505 ErrorParsing |= ProvidePositionalOption(ConsumeAfterOpt,
506 PositionalVals[ValNo]);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000507 }
508
Chris Lattnerdc4693d2001-07-23 23:02:45 +0000509 // Loop over args and make sure all required args are specified!
Chris Lattnerca6433f2003-05-22 20:06:43 +0000510 for (std::map<std::string, Option*>::iterator I = Opts.begin(),
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000511 E = Opts.end(); I != E; ++I) {
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000512 switch (I->second->getNumOccurrencesFlag()) {
Chris Lattnerdc4693d2001-07-23 23:02:45 +0000513 case Required:
514 case OneOrMore:
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000515 if (I->second->getNumOccurrences() == 0) {
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000516 I->second->error(" must be specified at least once!");
Chris Lattnerf038acb2001-10-24 06:21:56 +0000517 ErrorParsing = true;
518 }
Chris Lattnerdc4693d2001-07-23 23:02:45 +0000519 // Fall through
520 default:
521 break;
522 }
523 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000524
Chris Lattner331de232002-07-22 02:07:59 +0000525 // Free all of the memory allocated to the map. Command line options may only
526 // be processed once!
Chris Lattnere8e258b2002-07-29 20:58:42 +0000527 delete CommandLineOptions;
528 CommandLineOptions = 0;
Chris Lattner331de232002-07-22 02:07:59 +0000529 PositionalOpts.clear();
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000530
531 // If we had an error processing our arguments, don't let the program execute
532 if (ErrorParsing) exit(1);
533}
534
535//===----------------------------------------------------------------------===//
536// Option Base class implementation
537//
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000538
Chris Lattnerca6433f2003-05-22 20:06:43 +0000539bool Option::error(std::string Message, const char *ArgName) {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000540 if (ArgName == 0) ArgName = ArgStr;
Chris Lattner331de232002-07-22 02:07:59 +0000541 if (ArgName[0] == 0)
Chris Lattnerca6433f2003-05-22 20:06:43 +0000542 std::cerr << HelpStr; // Be nice for positional arguments
Chris Lattner331de232002-07-22 02:07:59 +0000543 else
Chris Lattnerca6433f2003-05-22 20:06:43 +0000544 std::cerr << "-" << ArgName;
545 std::cerr << " option" << Message << "\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000546 return true;
547}
548
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000549bool Option::addOccurrence(const char *ArgName, const std::string &Value) {
550 NumOccurrences++; // Increment the number of times we have been seen
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000551
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000552 switch (getNumOccurrencesFlag()) {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000553 case Optional:
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000554 if (NumOccurrences > 1)
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000555 return error(": may only occur zero or one times!", ArgName);
556 break;
557 case Required:
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000558 if (NumOccurrences > 1)
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000559 return error(": must occur exactly one time!", ArgName);
560 // Fall through
561 case OneOrMore:
Chris Lattnercaccd762001-10-27 05:54:17 +0000562 case ZeroOrMore:
563 case ConsumeAfter: break;
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000564 default: return error(": bad num occurrences flag value!");
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000565 }
566
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000567 return handleOccurrence(ArgName, Value);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000568}
569
Chris Lattner331de232002-07-22 02:07:59 +0000570// addArgument - Tell the system that this Option subclass will handle all
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000571// occurrences of -ArgStr on the command line.
Chris Lattner331de232002-07-22 02:07:59 +0000572//
573void Option::addArgument(const char *ArgStr) {
574 if (ArgStr[0])
575 AddArgument(ArgStr, this);
Chris Lattner9cf3d472003-07-30 17:34:02 +0000576
577 if (getFormattingFlag() == Positional)
Chris Lattner331de232002-07-22 02:07:59 +0000578 getPositionalOpts().push_back(this);
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000579 else if (getNumOccurrencesFlag() == ConsumeAfter) {
Chris Lattner9cf3d472003-07-30 17:34:02 +0000580 if (!getPositionalOpts().empty() &&
581 getPositionalOpts().front()->getNumOccurrencesFlag() == ConsumeAfter)
582 error("Cannot specify more than one option with cl::ConsumeAfter!");
Chris Lattner331de232002-07-22 02:07:59 +0000583 getPositionalOpts().insert(getPositionalOpts().begin(), this);
584 }
585}
586
Chris Lattneraa852bb2002-07-23 17:15:12 +0000587void Option::removeArgument(const char *ArgStr) {
Chris Lattner9cf3d472003-07-30 17:34:02 +0000588 if (ArgStr[0])
Chris Lattnere8e258b2002-07-29 20:58:42 +0000589 RemoveArgument(ArgStr, this);
Chris Lattner9cf3d472003-07-30 17:34:02 +0000590
591 if (getFormattingFlag() == Positional) {
Chris Lattnerca6433f2003-05-22 20:06:43 +0000592 std::vector<Option*>::iterator I =
Chris Lattneraa852bb2002-07-23 17:15:12 +0000593 std::find(getPositionalOpts().begin(), getPositionalOpts().end(), this);
594 assert(I != getPositionalOpts().end() && "Arg not registered!");
595 getPositionalOpts().erase(I);
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000596 } else if (getNumOccurrencesFlag() == ConsumeAfter) {
Chris Lattneraa852bb2002-07-23 17:15:12 +0000597 assert(!getPositionalOpts().empty() && getPositionalOpts()[0] == this &&
598 "Arg not registered correctly!");
599 getPositionalOpts().erase(getPositionalOpts().begin());
600 }
601}
602
Chris Lattner331de232002-07-22 02:07:59 +0000603
604// getValueStr - Get the value description string, using "DefaultMsg" if nothing
605// has been specified yet.
606//
607static const char *getValueStr(const Option &O, const char *DefaultMsg) {
608 if (O.ValueStr[0] == 0) return DefaultMsg;
609 return O.ValueStr;
610}
611
612//===----------------------------------------------------------------------===//
613// cl::alias class implementation
614//
615
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000616// Return the width of the option tag for printing...
Chris Lattner331de232002-07-22 02:07:59 +0000617unsigned alias::getOptionWidth() const {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000618 return std::strlen(ArgStr)+6;
619}
620
Chris Lattner331de232002-07-22 02:07:59 +0000621// Print out the option for the alias...
622void alias::printOptionInfo(unsigned GlobalWidth) const {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000623 unsigned L = std::strlen(ArgStr);
Chris Lattnerca6433f2003-05-22 20:06:43 +0000624 std::cerr << " -" << ArgStr << std::string(GlobalWidth-L-6, ' ') << " - "
625 << HelpStr << "\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000626}
627
628
Chris Lattner331de232002-07-22 02:07:59 +0000629
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000630//===----------------------------------------------------------------------===//
Chris Lattner331de232002-07-22 02:07:59 +0000631// Parser Implementation code...
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000632//
633
Chris Lattner9b14eb52002-08-07 18:36:37 +0000634// basic_parser implementation
635//
636
637// Return the width of the option tag for printing...
638unsigned basic_parser_impl::getOptionWidth(const Option &O) const {
639 unsigned Len = std::strlen(O.ArgStr);
640 if (const char *ValName = getValueName())
641 Len += std::strlen(getValueStr(O, ValName))+3;
642
643 return Len + 6;
644}
645
646// printOptionInfo - Print out information about this option. The
647// to-be-maintained width is specified.
648//
649void basic_parser_impl::printOptionInfo(const Option &O,
650 unsigned GlobalWidth) const {
Chris Lattnerca6433f2003-05-22 20:06:43 +0000651 std::cerr << " -" << O.ArgStr;
Chris Lattner9b14eb52002-08-07 18:36:37 +0000652
653 if (const char *ValName = getValueName())
Chris Lattnerca6433f2003-05-22 20:06:43 +0000654 std::cerr << "=<" << getValueStr(O, ValName) << ">";
Chris Lattner9b14eb52002-08-07 18:36:37 +0000655
Chris Lattnerca6433f2003-05-22 20:06:43 +0000656 std::cerr << std::string(GlobalWidth-getOptionWidth(O), ' ') << " - "
657 << O.HelpStr << "\n";
Chris Lattner9b14eb52002-08-07 18:36:37 +0000658}
659
660
661
662
Chris Lattner331de232002-07-22 02:07:59 +0000663// parser<bool> implementation
664//
Chris Lattner9b14eb52002-08-07 18:36:37 +0000665bool parser<bool>::parse(Option &O, const char *ArgName,
Chris Lattnerca6433f2003-05-22 20:06:43 +0000666 const std::string &Arg, bool &Value) {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000667 if (Arg == "" || Arg == "true" || Arg == "TRUE" || Arg == "True" ||
668 Arg == "1") {
669 Value = true;
670 } else if (Arg == "false" || Arg == "FALSE" || Arg == "False" || Arg == "0") {
671 Value = false;
672 } else {
Chris Lattner331de232002-07-22 02:07:59 +0000673 return O.error(": '" + Arg +
674 "' is invalid value for boolean argument! Try 0 or 1");
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000675 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000676 return false;
677}
678
Chris Lattner331de232002-07-22 02:07:59 +0000679// parser<int> implementation
680//
Chris Lattner9b14eb52002-08-07 18:36:37 +0000681bool parser<int>::parse(Option &O, const char *ArgName,
Chris Lattnerca6433f2003-05-22 20:06:43 +0000682 const std::string &Arg, int &Value) {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000683 char *End;
Chris Lattnerd2a6fc32003-06-28 15:47:20 +0000684 Value = (int)strtol(Arg.c_str(), &End, 0);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000685 if (*End != 0)
Chris Lattner331de232002-07-22 02:07:59 +0000686 return O.error(": '" + Arg + "' value invalid for integer argument!");
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000687 return false;
688}
689
Chris Lattnerd2a6fc32003-06-28 15:47:20 +0000690// parser<unsigned> implementation
691//
692bool parser<unsigned>::parse(Option &O, const char *ArgName,
693 const std::string &Arg, unsigned &Value) {
694 char *End;
Brian Gaeke2d6a2362003-10-10 17:01:36 +0000695 errno = 0;
696 unsigned long V = strtoul(Arg.c_str(), &End, 0);
Chris Lattnerd2a6fc32003-06-28 15:47:20 +0000697 Value = (unsigned)V;
Brian Gaeke2d6a2362003-10-10 17:01:36 +0000698 if (((V == ULONG_MAX) && (errno == ERANGE))
699 || (*End != 0)
700 || (Value != V))
Chris Lattnerd2a6fc32003-06-28 15:47:20 +0000701 return O.error(": '" + Arg + "' value invalid for uint argument!");
702 return false;
703}
704
Chris Lattner9b14eb52002-08-07 18:36:37 +0000705// parser<double>/parser<float> implementation
Chris Lattnerd215fd12001-10-13 06:53:19 +0000706//
Chris Lattnerca6433f2003-05-22 20:06:43 +0000707static bool parseDouble(Option &O, const std::string &Arg, double &Value) {
Chris Lattner331de232002-07-22 02:07:59 +0000708 const char *ArgStart = Arg.c_str();
709 char *End;
710 Value = strtod(ArgStart, &End);
711 if (*End != 0)
712 return O.error(": '" +Arg+ "' value invalid for floating point argument!");
Chris Lattnerd215fd12001-10-13 06:53:19 +0000713 return false;
714}
715
Chris Lattner9b14eb52002-08-07 18:36:37 +0000716bool parser<double>::parse(Option &O, const char *AN,
717 const std::string &Arg, double &Val) {
718 return parseDouble(O, Arg, Val);
Chris Lattner331de232002-07-22 02:07:59 +0000719}
720
Chris Lattner9b14eb52002-08-07 18:36:37 +0000721bool parser<float>::parse(Option &O, const char *AN,
722 const std::string &Arg, float &Val) {
723 double dVal;
724 if (parseDouble(O, Arg, dVal))
725 return true;
726 Val = (float)dVal;
727 return false;
Chris Lattner331de232002-07-22 02:07:59 +0000728}
729
730
Chris Lattner331de232002-07-22 02:07:59 +0000731
732// generic_parser_base implementation
733//
734
Chris Lattneraa852bb2002-07-23 17:15:12 +0000735// findOption - Return the option number corresponding to the specified
736// argument string. If the option is not found, getNumOptions() is returned.
737//
738unsigned generic_parser_base::findOption(const char *Name) {
739 unsigned i = 0, e = getNumOptions();
Chris Lattnerca6433f2003-05-22 20:06:43 +0000740 std::string N(Name);
Chris Lattneraa852bb2002-07-23 17:15:12 +0000741
742 while (i != e)
743 if (getOption(i) == N)
744 return i;
745 else
746 ++i;
747 return e;
748}
749
750
Chris Lattner331de232002-07-22 02:07:59 +0000751// Return the width of the option tag for printing...
752unsigned generic_parser_base::getOptionWidth(const Option &O) const {
753 if (O.hasArgStr()) {
754 unsigned Size = std::strlen(O.ArgStr)+6;
755 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
756 Size = std::max(Size, (unsigned)std::strlen(getOption(i))+8);
757 return Size;
758 } else {
759 unsigned BaseSize = 0;
760 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
761 BaseSize = std::max(BaseSize, (unsigned)std::strlen(getOption(i))+8);
762 return BaseSize;
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000763 }
764}
765
Chris Lattner331de232002-07-22 02:07:59 +0000766// printOptionInfo - Print out information about this option. The
767// to-be-maintained width is specified.
768//
769void generic_parser_base::printOptionInfo(const Option &O,
770 unsigned GlobalWidth) const {
771 if (O.hasArgStr()) {
772 unsigned L = std::strlen(O.ArgStr);
Chris Lattnerca6433f2003-05-22 20:06:43 +0000773 std::cerr << " -" << O.ArgStr << std::string(GlobalWidth-L-6, ' ')
774 << " - " << O.HelpStr << "\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000775
Chris Lattner331de232002-07-22 02:07:59 +0000776 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
777 unsigned NumSpaces = GlobalWidth-strlen(getOption(i))-8;
Chris Lattnerca6433f2003-05-22 20:06:43 +0000778 std::cerr << " =" << getOption(i) << std::string(NumSpaces, ' ')
779 << " - " << getDescription(i) << "\n";
Chris Lattner9c9be482002-01-31 00:42:56 +0000780 }
Chris Lattner331de232002-07-22 02:07:59 +0000781 } else {
782 if (O.HelpStr[0])
Chris Lattnerca6433f2003-05-22 20:06:43 +0000783 std::cerr << " " << O.HelpStr << "\n";
Chris Lattner331de232002-07-22 02:07:59 +0000784 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
785 unsigned L = std::strlen(getOption(i));
Chris Lattnerca6433f2003-05-22 20:06:43 +0000786 std::cerr << " -" << getOption(i) << std::string(GlobalWidth-L-8, ' ')
787 << " - " << getDescription(i) << "\n";
Chris Lattner331de232002-07-22 02:07:59 +0000788 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000789 }
790}
791
792
793//===----------------------------------------------------------------------===//
Chris Lattner331de232002-07-22 02:07:59 +0000794// --help and --help-hidden option implementation
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000795//
796namespace {
797
Chris Lattner331de232002-07-22 02:07:59 +0000798class HelpPrinter {
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000799 unsigned MaxArgLen;
800 const Option *EmptyArg;
801 const bool ShowHidden;
802
Chris Lattner331de232002-07-22 02:07:59 +0000803 // isHidden/isReallyHidden - Predicates to be used to filter down arg lists.
Chris Lattnerca6433f2003-05-22 20:06:43 +0000804 inline static bool isHidden(std::pair<std::string, Option *> &OptPair) {
Chris Lattner331de232002-07-22 02:07:59 +0000805 return OptPair.second->getOptionHiddenFlag() >= Hidden;
806 }
Chris Lattnerca6433f2003-05-22 20:06:43 +0000807 inline static bool isReallyHidden(std::pair<std::string, Option *> &OptPair) {
Chris Lattner331de232002-07-22 02:07:59 +0000808 return OptPair.second->getOptionHiddenFlag() == ReallyHidden;
809 }
810
811public:
812 HelpPrinter(bool showHidden) : ShowHidden(showHidden) {
813 EmptyArg = 0;
814 }
815
816 void operator=(bool Value) {
817 if (Value == false) return;
818
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000819 // Copy Options into a vector so we can sort them as we like...
Chris Lattnerca6433f2003-05-22 20:06:43 +0000820 std::vector<std::pair<std::string, Option*> > Options;
Chris Lattner697954c2002-01-20 22:54:45 +0000821 copy(getOpts().begin(), getOpts().end(), std::back_inserter(Options));
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000822
823 // Eliminate Hidden or ReallyHidden arguments, depending on ShowHidden
Chris Lattner331de232002-07-22 02:07:59 +0000824 Options.erase(std::remove_if(Options.begin(), Options.end(),
825 std::ptr_fun(ShowHidden ? isReallyHidden : isHidden)),
Misha Brukmanb5c520b2003-07-10 17:05:26 +0000826 Options.end());
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000827
828 // Eliminate duplicate entries in table (from enum flags options, f.e.)
Chris Lattner331de232002-07-22 02:07:59 +0000829 { // Give OptionSet a scope
830 std::set<Option*> OptionSet;
831 for (unsigned i = 0; i != Options.size(); ++i)
832 if (OptionSet.count(Options[i].second) == 0)
833 OptionSet.insert(Options[i].second); // Add new entry to set
834 else
835 Options.erase(Options.begin()+i--); // Erase duplicate
836 }
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000837
838 if (ProgramOverview)
Chris Lattnerca6433f2003-05-22 20:06:43 +0000839 std::cerr << "OVERVIEW:" << ProgramOverview << "\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000840
Chris Lattnerca6433f2003-05-22 20:06:43 +0000841 std::cerr << "USAGE: " << ProgramName << " [options]";
Chris Lattner331de232002-07-22 02:07:59 +0000842
843 // Print out the positional options...
Chris Lattnerca6433f2003-05-22 20:06:43 +0000844 std::vector<Option*> &PosOpts = getPositionalOpts();
Chris Lattner331de232002-07-22 02:07:59 +0000845 Option *CAOpt = 0; // The cl::ConsumeAfter option, if it exists...
Misha Brukmandd6cb6a2003-07-10 16:49:51 +0000846 if (!PosOpts.empty() && PosOpts[0]->getNumOccurrencesFlag() == ConsumeAfter)
Chris Lattner331de232002-07-22 02:07:59 +0000847 CAOpt = PosOpts[0];
848
Chris Lattner9cf3d472003-07-30 17:34:02 +0000849 for (unsigned i = CAOpt != 0, e = PosOpts.size(); i != e; ++i) {
850 if (PosOpts[i]->ArgStr[0])
851 std::cerr << " --" << PosOpts[i]->ArgStr;
Chris Lattnerca6433f2003-05-22 20:06:43 +0000852 std::cerr << " " << PosOpts[i]->HelpStr;
Chris Lattner9cf3d472003-07-30 17:34:02 +0000853 }
Chris Lattner331de232002-07-22 02:07:59 +0000854
855 // Print the consume after option info if it exists...
Chris Lattnerca6433f2003-05-22 20:06:43 +0000856 if (CAOpt) std::cerr << " " << CAOpt->HelpStr;
Chris Lattner331de232002-07-22 02:07:59 +0000857
Chris Lattnerca6433f2003-05-22 20:06:43 +0000858 std::cerr << "\n\n";
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000859
860 // Compute the maximum argument length...
861 MaxArgLen = 0;
Chris Lattner331de232002-07-22 02:07:59 +0000862 for (unsigned i = 0, e = Options.size(); i != e; ++i)
863 MaxArgLen = std::max(MaxArgLen, Options[i].second->getOptionWidth());
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000864
Chris Lattnerca6433f2003-05-22 20:06:43 +0000865 std::cerr << "OPTIONS:\n";
Chris Lattner331de232002-07-22 02:07:59 +0000866 for (unsigned i = 0, e = Options.size(); i != e; ++i)
867 Options[i].second->printOptionInfo(MaxArgLen);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000868
Chris Lattner331de232002-07-22 02:07:59 +0000869 // Halt the program if help information is printed
870 exit(1);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000871 }
872};
873
Chris Lattner331de232002-07-22 02:07:59 +0000874
875
876// Define the two HelpPrinter instances that are used to print out help, or
877// help-hidden...
878//
879HelpPrinter NormalPrinter(false);
880HelpPrinter HiddenPrinter(true);
881
882cl::opt<HelpPrinter, true, parser<bool> >
883HOp("help", cl::desc("display available options (--help-hidden for more)"),
Chris Lattner9b14eb52002-08-07 18:36:37 +0000884 cl::location(NormalPrinter), cl::ValueDisallowed);
Chris Lattner331de232002-07-22 02:07:59 +0000885
886cl::opt<HelpPrinter, true, parser<bool> >
887HHOp("help-hidden", cl::desc("display all available options"),
Chris Lattner9b14eb52002-08-07 18:36:37 +0000888 cl::location(HiddenPrinter), cl::Hidden, cl::ValueDisallowed);
Chris Lattnerdbab15a2001-07-23 17:17:47 +0000889
890} // End anonymous namespace