Chris Lattner | dbab15a | 2001-07-23 17:17:47 +0000 | [diff] [blame] | 1 | //===-- CommandLine.cpp - Command line parser implementation --------------===// |
| 2 | // |
| 3 | // This class implements a command line argument processor that is useful when |
| 4 | // creating a tool. It provides a simple, minimalistic interface that is easily |
| 5 | // extensible and supports nonlocal (library) command line options. |
| 6 | // |
Chris Lattner | 03fe1bd | 2001-07-23 23:04:07 +0000 | [diff] [blame] | 7 | // Note that rather than trying to figure out what this code does, you could try |
| 8 | // reading the library documentation located in docs/CommandLine.html |
| 9 | // |
Chris Lattner | dbab15a | 2001-07-23 17:17:47 +0000 | [diff] [blame] | 10 | //===----------------------------------------------------------------------===// |
| 11 | |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 12 | #include "Support/CommandLine.h" |
| 13 | #include "Support/STLExtras.h" |
Chris Lattner | dbab15a | 2001-07-23 17:17:47 +0000 | [diff] [blame] | 14 | #include <algorithm> |
| 15 | #include <map> |
| 16 | #include <set> |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 17 | #include <iostream> |
Chris Lattner | 7f1576f | 2002-02-24 23:02:12 +0000 | [diff] [blame] | 18 | |
Chris Lattner | dbab15a | 2001-07-23 17:17:47 +0000 | [diff] [blame] | 19 | using namespace cl; |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 20 | using std::map; |
| 21 | using std::pair; |
| 22 | using std::vector; |
| 23 | using std::string; |
| 24 | using std::cerr; |
Chris Lattner | dbab15a | 2001-07-23 17:17:47 +0000 | [diff] [blame] | 25 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 26 | //===----------------------------------------------------------------------===// |
| 27 | // Basic, shared command line option processing machinery... |
| 28 | // |
| 29 | |
Chris Lattner | dbab15a | 2001-07-23 17:17:47 +0000 | [diff] [blame] | 30 | // Return the global command line option vector. Making it a function scoped |
Chris Lattner | f78032f | 2001-11-26 18:58:34 +0000 | [diff] [blame] | 31 | // static ensures that it will be initialized correctly before its first use. |
Chris Lattner | dbab15a | 2001-07-23 17:17:47 +0000 | [diff] [blame] | 32 | // |
| 33 | static map<string, Option*> &getOpts() { |
| 34 | static map<string,Option*> CommandLineOptions; |
| 35 | return CommandLineOptions; |
| 36 | } |
| 37 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 38 | static vector<Option*> &getPositionalOpts() { |
| 39 | static vector<Option*> Positional; |
| 40 | return Positional; |
| 41 | } |
| 42 | |
Chris Lattner | dbab15a | 2001-07-23 17:17:47 +0000 | [diff] [blame] | 43 | static void AddArgument(const string &ArgName, Option *Opt) { |
| 44 | if (getOpts().find(ArgName) != getOpts().end()) { |
| 45 | cerr << "CommandLine Error: Argument '" << ArgName |
Chris Lattner | 9c9be48 | 2002-01-31 00:42:56 +0000 | [diff] [blame] | 46 | << "' defined more than once!\n"; |
Chris Lattner | dbab15a | 2001-07-23 17:17:47 +0000 | [diff] [blame] | 47 | } else { |
Chris Lattner | f78032f | 2001-11-26 18:58:34 +0000 | [diff] [blame] | 48 | // Add argument to the argument map! |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 49 | getOpts().insert(std::make_pair(ArgName, Opt)); |
Chris Lattner | dbab15a | 2001-07-23 17:17:47 +0000 | [diff] [blame] | 50 | } |
| 51 | } |
| 52 | |
| 53 | static const char *ProgramName = 0; |
| 54 | static const char *ProgramOverview = 0; |
| 55 | |
Chris Lattner | caccd76 | 2001-10-27 05:54:17 +0000 | [diff] [blame] | 56 | static inline bool ProvideOption(Option *Handler, const char *ArgName, |
| 57 | const char *Value, int argc, char **argv, |
| 58 | int &i) { |
| 59 | // Enforce value requirements |
| 60 | switch (Handler->getValueExpectedFlag()) { |
| 61 | case ValueRequired: |
| 62 | if (Value == 0 || *Value == 0) { // No value specified? |
| 63 | if (i+1 < argc) { // Steal the next argument, like for '-o filename' |
| 64 | Value = argv[++i]; |
| 65 | } else { |
| 66 | return Handler->error(" requires a value!"); |
| 67 | } |
| 68 | } |
| 69 | break; |
| 70 | case ValueDisallowed: |
| 71 | if (*Value != 0) |
| 72 | return Handler->error(" does not allow a value! '" + |
| 73 | string(Value) + "' specified."); |
| 74 | break; |
| 75 | case ValueOptional: break; |
| 76 | default: cerr << "Bad ValueMask flag! CommandLine usage error:" |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 77 | << Handler->getValueExpectedFlag() << "\n"; abort(); |
Chris Lattner | caccd76 | 2001-10-27 05:54:17 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | // Run the handler now! |
| 81 | return Handler->addOccurance(ArgName, Value); |
| 82 | } |
| 83 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 84 | static bool ProvidePositionalOption(Option *Handler, string &Arg) { |
| 85 | int Dummy; |
| 86 | return ProvideOption(Handler, "", Arg.c_str(), 0, 0, Dummy); |
| 87 | } |
Chris Lattner | f78032f | 2001-11-26 18:58:34 +0000 | [diff] [blame] | 88 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 89 | |
| 90 | // Option predicates... |
| 91 | static inline bool isGrouping(const Option *O) { |
| 92 | return O->getFormattingFlag() == cl::Grouping; |
| 93 | } |
| 94 | static inline bool isPrefixedOrGrouping(const Option *O) { |
| 95 | return isGrouping(O) || O->getFormattingFlag() == cl::Prefix; |
| 96 | } |
| 97 | |
| 98 | // getOptionPred - Check to see if there are any options that satisfy the |
| 99 | // specified predicate with names that are the prefixes in Name. This is |
| 100 | // checked by progressively stripping characters off of the name, checking to |
| 101 | // see if there options that satisfy the predicate. If we find one, return it, |
| 102 | // otherwise return null. |
| 103 | // |
| 104 | static Option *getOptionPred(std::string Name, unsigned &Length, |
| 105 | bool (*Pred)(const Option*)) { |
| 106 | |
| 107 | map<string, Option*>::iterator I = getOpts().find(Name); |
| 108 | if (I != getOpts().end() && Pred(I->second)) { |
| 109 | Length = Name.length(); |
| 110 | return I->second; |
Chris Lattner | f78032f | 2001-11-26 18:58:34 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 113 | if (Name.size() == 1) return 0; |
| 114 | do { |
| 115 | Name.erase(Name.end()-1, Name.end()); // Chop off the last character... |
| 116 | I = getOpts().find(Name); |
| 117 | |
| 118 | // Loop while we haven't found an option and Name still has at least two |
| 119 | // characters in it (so that the next iteration will not be the empty |
| 120 | // string... |
| 121 | } while ((I == getOpts().end() || !Pred(I->second)) && Name.size() > 1); |
| 122 | |
| 123 | if (I != getOpts().end() && Pred(I->second)) { |
| 124 | Length = Name.length(); |
| 125 | return I->second; // Found one! |
| 126 | } |
| 127 | return 0; // No option found! |
| 128 | } |
| 129 | |
| 130 | static bool RequiresValue(const Option *O) { |
| 131 | return O->getNumOccurancesFlag() == cl::Required || |
| 132 | O->getNumOccurancesFlag() == cl::OneOrMore; |
| 133 | } |
| 134 | |
| 135 | static bool EatsUnboundedNumberOfValues(const Option *O) { |
| 136 | return O->getNumOccurancesFlag() == cl::ZeroOrMore || |
| 137 | O->getNumOccurancesFlag() == cl::OneOrMore; |
Chris Lattner | f78032f | 2001-11-26 18:58:34 +0000 | [diff] [blame] | 138 | } |
Chris Lattner | caccd76 | 2001-10-27 05:54:17 +0000 | [diff] [blame] | 139 | |
Chris Lattner | dbab15a | 2001-07-23 17:17:47 +0000 | [diff] [blame] | 140 | void cl::ParseCommandLineOptions(int &argc, char **argv, |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 141 | const char *Overview = 0) { |
| 142 | assert((!getOpts().empty() || !getPositionalOpts().empty()) && |
| 143 | "No options specified, or ParseCommandLineOptions called more" |
| 144 | " than once!"); |
Chris Lattner | dbab15a | 2001-07-23 17:17:47 +0000 | [diff] [blame] | 145 | ProgramName = argv[0]; // Save this away safe and snug |
| 146 | ProgramOverview = Overview; |
| 147 | bool ErrorParsing = false; |
| 148 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 149 | map<string, Option*> &Opts = getOpts(); |
| 150 | vector<Option*> &PositionalOpts = getPositionalOpts(); |
| 151 | |
| 152 | // Check out the positional arguments to collect information about them. |
| 153 | unsigned NumPositionalRequired = 0; |
| 154 | Option *ConsumeAfterOpt = 0; |
| 155 | if (!PositionalOpts.empty()) { |
| 156 | if (PositionalOpts[0]->getNumOccurancesFlag() == cl::ConsumeAfter) { |
| 157 | assert(PositionalOpts.size() > 1 && |
| 158 | "Cannot specify cl::ConsumeAfter without a positional argument!"); |
| 159 | ConsumeAfterOpt = PositionalOpts[0]; |
| 160 | } |
| 161 | |
| 162 | // Calculate how many positional values are _required_. |
| 163 | bool UnboundedFound = false; |
| 164 | for (unsigned i = ConsumeAfterOpt != 0, e = PositionalOpts.size(); |
| 165 | i != e; ++i) { |
| 166 | Option *Opt = PositionalOpts[i]; |
| 167 | if (RequiresValue(Opt)) |
| 168 | ++NumPositionalRequired; |
| 169 | else if (ConsumeAfterOpt) { |
| 170 | // ConsumeAfter cannot be combined with "optional" positional options |
Chris Lattner | 54ec7ae | 2002-07-22 02:21:57 +0000 | [diff] [blame] | 171 | // unless there is only one positional argument... |
| 172 | if (PositionalOpts.size() > 2) |
| 173 | ErrorParsing |= |
| 174 | Opt->error(" error - this positional option will never be matched, " |
| 175 | "because it does not Require a value, and a " |
| 176 | "cl::ConsumeAfter option is active!"); |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 177 | } else if (UnboundedFound) { // This option does not "require" a value... |
| 178 | // Make sure this option is not specified after an option that eats all |
| 179 | // extra arguments, or this one will never get any! |
| 180 | // |
| 181 | ErrorParsing |= Opt->error(" error - option can never match, because " |
| 182 | "another positional argument will match an " |
| 183 | "unbounded number of values, and this option" |
| 184 | " does not require a value!"); |
| 185 | } |
| 186 | UnboundedFound |= EatsUnboundedNumberOfValues(Opt); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | // PositionalVals - A vector of "positional" arguments we accumulate into to |
| 191 | // processes at the end... |
| 192 | // |
| 193 | vector<string> PositionalVals; |
| 194 | |
Chris Lattner | dbab15a | 2001-07-23 17:17:47 +0000 | [diff] [blame] | 195 | // Loop over all of the arguments... processing them. |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 196 | bool DashDashFound = false; // Have we read '--'? |
Chris Lattner | dbab15a | 2001-07-23 17:17:47 +0000 | [diff] [blame] | 197 | for (int i = 1; i < argc; ++i) { |
| 198 | Option *Handler = 0; |
| 199 | const char *Value = ""; |
| 200 | const char *ArgName = ""; |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 201 | |
| 202 | // Check to see if this is a positional argument. This argument is |
| 203 | // considered to be positional if it doesn't start with '-', if it is "-" |
| 204 | // itself, or if we have see "--" already. |
| 205 | // |
| 206 | if (argv[i][0] != '-' || argv[i][1] == 0 || DashDashFound) { |
| 207 | // Positional argument! |
| 208 | if (!PositionalOpts.empty()) { |
| 209 | PositionalVals.push_back(argv[i]); |
| 210 | |
| 211 | // All of the positional arguments have been fulfulled, give the rest to |
| 212 | // the consume after option... if it's specified... |
| 213 | // |
| 214 | if (PositionalVals.size() == NumPositionalRequired && |
| 215 | ConsumeAfterOpt != 0) { |
| 216 | for (++i; i < argc; ++i) |
| 217 | PositionalVals.push_back(argv[i]); |
| 218 | break; // Handle outside of the argument processing loop... |
| 219 | } |
| 220 | |
| 221 | // Delay processing positional arguments until the end... |
| 222 | continue; |
| 223 | } |
| 224 | } else { // We start with a '-', must be an argument... |
Chris Lattner | dbab15a | 2001-07-23 17:17:47 +0000 | [diff] [blame] | 225 | ArgName = argv[i]+1; |
| 226 | while (*ArgName == '-') ++ArgName; // Eat leading dashes |
| 227 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 228 | if (*ArgName == 0 && !DashDashFound) { // Is this the mythical "--"? |
| 229 | DashDashFound = true; // Yup, take note of that fact... |
| 230 | continue; // Don't try to process it as an argument iself. |
| 231 | } |
| 232 | |
Chris Lattner | dbab15a | 2001-07-23 17:17:47 +0000 | [diff] [blame] | 233 | const char *ArgNameEnd = ArgName; |
Chris Lattner | f78032f | 2001-11-26 18:58:34 +0000 | [diff] [blame] | 234 | while (*ArgNameEnd && *ArgNameEnd != '=') |
| 235 | ++ArgNameEnd; // Scan till end of argument name... |
Chris Lattner | dbab15a | 2001-07-23 17:17:47 +0000 | [diff] [blame] | 236 | |
| 237 | Value = ArgNameEnd; |
| 238 | if (*Value) // If we have an equals sign... |
| 239 | ++Value; // Advance to value... |
| 240 | |
| 241 | if (*ArgName != 0) { |
Chris Lattner | f78032f | 2001-11-26 18:58:34 +0000 | [diff] [blame] | 242 | string RealName(ArgName, ArgNameEnd); |
Chris Lattner | 3805e4c | 2001-07-25 18:40:49 +0000 | [diff] [blame] | 243 | // Extract arg name part |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 244 | map<string, Option*>::iterator I = Opts.find(RealName); |
Chris Lattner | f78032f | 2001-11-26 18:58:34 +0000 | [diff] [blame] | 245 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 246 | if (I == Opts.end() && !*Value && RealName.size() > 1) { |
| 247 | // Check to see if this "option" is really a prefixed or grouped |
| 248 | // argument... |
| 249 | // |
| 250 | unsigned Length = 0; |
| 251 | Option *PGOpt = getOptionPred(RealName, Length, isPrefixedOrGrouping); |
Chris Lattner | f78032f | 2001-11-26 18:58:34 +0000 | [diff] [blame] | 252 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 253 | // If the option is a prefixed option, then the value is simply the |
| 254 | // rest of the name... so fall through to later processing, by |
| 255 | // setting up the argument name flags and value fields. |
| 256 | // |
| 257 | if (PGOpt && PGOpt->getFormattingFlag() == cl::Prefix) { |
| 258 | ArgNameEnd = ArgName+Length; |
| 259 | Value = ArgNameEnd; |
| 260 | I = Opts.find(string(ArgName, ArgNameEnd)); |
| 261 | assert(I->second == PGOpt); |
| 262 | } else if (PGOpt) { |
| 263 | // This must be a grouped option... handle all of them now... |
| 264 | assert(isGrouping(PGOpt) && "Broken getOptionPred!"); |
| 265 | |
| 266 | do { |
| 267 | // Move current arg name out of RealName into RealArgName... |
| 268 | string RealArgName(RealName.begin(), RealName.begin()+Length); |
| 269 | RealName.erase(RealName.begin(), RealName.begin()+Length); |
Chris Lattner | f78032f | 2001-11-26 18:58:34 +0000 | [diff] [blame] | 270 | |
| 271 | // Because ValueRequired is an invalid flag for grouped arguments, |
| 272 | // we don't need to pass argc/argv in... |
| 273 | // |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 274 | assert(PGOpt->getValueExpectedFlag() != cl::ValueRequired && |
| 275 | "Option can not be cl::Grouping AND cl::ValueRequired!"); |
| 276 | int Dummy; |
| 277 | ErrorParsing |= ProvideOption(PGOpt, RealArgName.c_str(), "", |
| 278 | 0, 0, Dummy); |
| 279 | |
| 280 | // Get the next grouping option... |
| 281 | if (!RealName.empty()) |
| 282 | PGOpt = getOptionPred(RealName, Length, isGrouping); |
| 283 | } while (!RealName.empty() && PGOpt); |
| 284 | |
| 285 | if (RealName.empty()) // Processed all of the options, move on |
| 286 | continue; // to the next argv[] value... |
| 287 | |
| 288 | // If RealName is not empty, that means we did not match one of the |
| 289 | // options! This is an error. |
| 290 | // |
| 291 | I = Opts.end(); |
| 292 | } |
Chris Lattner | f78032f | 2001-11-26 18:58:34 +0000 | [diff] [blame] | 293 | } |
| 294 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 295 | Handler = I != Opts.end() ? I->second : 0; |
Chris Lattner | dbab15a | 2001-07-23 17:17:47 +0000 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | |
| 299 | if (Handler == 0) { |
| 300 | cerr << "Unknown command line argument '" << argv[i] << "'. Try: " |
Chris Lattner | f038acb | 2001-10-24 06:21:56 +0000 | [diff] [blame] | 301 | << argv[0] << " --help'\n"; |
Chris Lattner | dbab15a | 2001-07-23 17:17:47 +0000 | [diff] [blame] | 302 | ErrorParsing = true; |
| 303 | continue; |
| 304 | } |
| 305 | |
Chris Lattner | caccd76 | 2001-10-27 05:54:17 +0000 | [diff] [blame] | 306 | ErrorParsing |= ProvideOption(Handler, ArgName, Value, argc, argv, i); |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 307 | } |
Chris Lattner | dbab15a | 2001-07-23 17:17:47 +0000 | [diff] [blame] | 308 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 309 | // Check and handle positional arguments now... |
| 310 | if (NumPositionalRequired > PositionalVals.size()) { |
| 311 | cerr << "Not enough positional command line arguments specified!\n"; |
| 312 | cerr << "Must specify at least " << NumPositionalRequired |
| 313 | << " positional arguments: See: " << argv[0] << " --help\n"; |
| 314 | ErrorParsing = true; |
| 315 | |
| 316 | |
| 317 | } else if (ConsumeAfterOpt == 0) { |
| 318 | // Positional args have already been handled if ConsumeAfter is specified... |
| 319 | unsigned ValNo = 0, NumVals = PositionalVals.size(); |
| 320 | for (unsigned i = 0, e = PositionalOpts.size(); i != e; ++i) { |
| 321 | if (RequiresValue(PositionalOpts[i])) { |
| 322 | ProvidePositionalOption(PositionalOpts[i], PositionalVals[ValNo++]); |
| 323 | --NumPositionalRequired; // We fulfilled our duty... |
| 324 | } |
| 325 | |
| 326 | // If we _can_ give this option more arguments, do so now, as long as we |
| 327 | // do not give it values that others need. 'Done' controls whether the |
| 328 | // option even _WANTS_ any more. |
| 329 | // |
| 330 | bool Done = PositionalOpts[i]->getNumOccurancesFlag() == cl::Required; |
| 331 | while (NumVals-ValNo > NumPositionalRequired && !Done) { |
| 332 | switch (PositionalOpts[i]->getNumOccurancesFlag()) { |
| 333 | case cl::Optional: |
| 334 | Done = true; // Optional arguments want _at most_ one value |
| 335 | // FALL THROUGH |
| 336 | case cl::ZeroOrMore: // Zero or more will take all they can get... |
| 337 | case cl::OneOrMore: // One or more will take all they can get... |
| 338 | ProvidePositionalOption(PositionalOpts[i], PositionalVals[ValNo++]); |
| 339 | break; |
| 340 | default: |
| 341 | assert(0 && "Internal error, unexpected NumOccurances flag in " |
| 342 | "positional argument processing!"); |
| 343 | } |
| 344 | } |
Chris Lattner | caccd76 | 2001-10-27 05:54:17 +0000 | [diff] [blame] | 345 | } |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 346 | } else { |
| 347 | assert(ConsumeAfterOpt && NumPositionalRequired <= PositionalVals.size()); |
| 348 | unsigned ValNo = 0; |
| 349 | for (unsigned j = 1, e = PositionalOpts.size(); j != e; ++j) |
| 350 | if (RequiresValue(PositionalOpts[j])) |
Chris Lattner | faba809 | 2002-07-24 20:15:13 +0000 | [diff] [blame^] | 351 | ErrorParsing |= ProvidePositionalOption(PositionalOpts[j], |
| 352 | PositionalVals[ValNo++]); |
| 353 | |
| 354 | // Handle the case where there is just one positional option, and it's |
| 355 | // optional. In this case, we want to give JUST THE FIRST option to the |
| 356 | // positional option and keep the rest for the consume after. The above |
| 357 | // loop would have assigned no values to positional options in this case. |
| 358 | // |
| 359 | if (PositionalOpts.size() == 2 && ValNo == 0) |
| 360 | ErrorParsing |= ProvidePositionalOption(PositionalOpts[1], |
| 361 | PositionalVals[ValNo++]); |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 362 | |
| 363 | // Handle over all of the rest of the arguments to the |
| 364 | // cl::ConsumeAfter command line option... |
| 365 | for (; ValNo != PositionalVals.size(); ++ValNo) |
| 366 | ErrorParsing |= ProvidePositionalOption(ConsumeAfterOpt, |
| 367 | PositionalVals[ValNo]); |
Chris Lattner | dbab15a | 2001-07-23 17:17:47 +0000 | [diff] [blame] | 368 | } |
| 369 | |
Chris Lattner | dc4693d | 2001-07-23 23:02:45 +0000 | [diff] [blame] | 370 | // Loop over args and make sure all required args are specified! |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 371 | for (map<string, Option*>::iterator I = Opts.begin(), |
| 372 | E = Opts.end(); I != E; ++I) { |
Chris Lattner | dc4693d | 2001-07-23 23:02:45 +0000 | [diff] [blame] | 373 | switch (I->second->getNumOccurancesFlag()) { |
| 374 | case Required: |
| 375 | case OneOrMore: |
Chris Lattner | f038acb | 2001-10-24 06:21:56 +0000 | [diff] [blame] | 376 | if (I->second->getNumOccurances() == 0) { |
Chris Lattner | dc4693d | 2001-07-23 23:02:45 +0000 | [diff] [blame] | 377 | I->second->error(" must be specified at least once!"); |
Chris Lattner | f038acb | 2001-10-24 06:21:56 +0000 | [diff] [blame] | 378 | ErrorParsing = true; |
| 379 | } |
Chris Lattner | dc4693d | 2001-07-23 23:02:45 +0000 | [diff] [blame] | 380 | // Fall through |
| 381 | default: |
| 382 | break; |
| 383 | } |
| 384 | } |
Chris Lattner | dbab15a | 2001-07-23 17:17:47 +0000 | [diff] [blame] | 385 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 386 | // Free all of the memory allocated to the map. Command line options may only |
| 387 | // be processed once! |
| 388 | Opts.clear(); |
| 389 | PositionalOpts.clear(); |
Chris Lattner | dbab15a | 2001-07-23 17:17:47 +0000 | [diff] [blame] | 390 | |
| 391 | // If we had an error processing our arguments, don't let the program execute |
| 392 | if (ErrorParsing) exit(1); |
| 393 | } |
| 394 | |
| 395 | //===----------------------------------------------------------------------===// |
| 396 | // Option Base class implementation |
| 397 | // |
Chris Lattner | dbab15a | 2001-07-23 17:17:47 +0000 | [diff] [blame] | 398 | |
| 399 | bool Option::error(string Message, const char *ArgName = 0) { |
| 400 | if (ArgName == 0) ArgName = ArgStr; |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 401 | if (ArgName[0] == 0) |
| 402 | cerr << HelpStr; // Be nice for positional arguments |
| 403 | else |
| 404 | cerr << "-" << ArgName; |
| 405 | cerr << " option" << Message << "\n"; |
Chris Lattner | dbab15a | 2001-07-23 17:17:47 +0000 | [diff] [blame] | 406 | return true; |
| 407 | } |
| 408 | |
| 409 | bool Option::addOccurance(const char *ArgName, const string &Value) { |
| 410 | NumOccurances++; // Increment the number of times we have been seen |
| 411 | |
Chris Lattner | dc4693d | 2001-07-23 23:02:45 +0000 | [diff] [blame] | 412 | switch (getNumOccurancesFlag()) { |
Chris Lattner | dbab15a | 2001-07-23 17:17:47 +0000 | [diff] [blame] | 413 | case Optional: |
| 414 | if (NumOccurances > 1) |
| 415 | return error(": may only occur zero or one times!", ArgName); |
| 416 | break; |
| 417 | case Required: |
| 418 | if (NumOccurances > 1) |
| 419 | return error(": must occur exactly one time!", ArgName); |
| 420 | // Fall through |
| 421 | case OneOrMore: |
Chris Lattner | caccd76 | 2001-10-27 05:54:17 +0000 | [diff] [blame] | 422 | case ZeroOrMore: |
| 423 | case ConsumeAfter: break; |
Chris Lattner | dbab15a | 2001-07-23 17:17:47 +0000 | [diff] [blame] | 424 | default: return error(": bad num occurances flag value!"); |
| 425 | } |
| 426 | |
| 427 | return handleOccurance(ArgName, Value); |
| 428 | } |
| 429 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 430 | // addArgument - Tell the system that this Option subclass will handle all |
| 431 | // occurances of -ArgStr on the command line. |
| 432 | // |
| 433 | void Option::addArgument(const char *ArgStr) { |
| 434 | if (ArgStr[0]) |
| 435 | AddArgument(ArgStr, this); |
| 436 | else if (getFormattingFlag() == Positional) |
| 437 | getPositionalOpts().push_back(this); |
| 438 | else if (getNumOccurancesFlag() == ConsumeAfter) { |
| 439 | assert((getPositionalOpts().empty() || |
| 440 | getPositionalOpts().front()->getNumOccurancesFlag() != ConsumeAfter) |
| 441 | && "Cannot specify more than one option with cl::ConsumeAfter " |
| 442 | "specified!"); |
| 443 | getPositionalOpts().insert(getPositionalOpts().begin(), this); |
| 444 | } |
| 445 | } |
| 446 | |
Chris Lattner | aa852bb | 2002-07-23 17:15:12 +0000 | [diff] [blame] | 447 | void Option::removeArgument(const char *ArgStr) { |
| 448 | if (ArgStr[0]) { |
| 449 | assert(getOpts()[ArgStr] == this && "Arg not in map!"); |
| 450 | getOpts().erase(ArgStr); |
| 451 | } else if (getFormattingFlag() == Positional) { |
| 452 | vector<Option*>::iterator I = |
| 453 | std::find(getPositionalOpts().begin(), getPositionalOpts().end(), this); |
| 454 | assert(I != getPositionalOpts().end() && "Arg not registered!"); |
| 455 | getPositionalOpts().erase(I); |
| 456 | } else if (getNumOccurancesFlag() == ConsumeAfter) { |
| 457 | assert(!getPositionalOpts().empty() && getPositionalOpts()[0] == this && |
| 458 | "Arg not registered correctly!"); |
| 459 | getPositionalOpts().erase(getPositionalOpts().begin()); |
| 460 | } |
| 461 | } |
| 462 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 463 | |
| 464 | // getValueStr - Get the value description string, using "DefaultMsg" if nothing |
| 465 | // has been specified yet. |
| 466 | // |
| 467 | static const char *getValueStr(const Option &O, const char *DefaultMsg) { |
| 468 | if (O.ValueStr[0] == 0) return DefaultMsg; |
| 469 | return O.ValueStr; |
| 470 | } |
| 471 | |
| 472 | //===----------------------------------------------------------------------===// |
| 473 | // cl::alias class implementation |
| 474 | // |
| 475 | |
Chris Lattner | dbab15a | 2001-07-23 17:17:47 +0000 | [diff] [blame] | 476 | // Return the width of the option tag for printing... |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 477 | unsigned alias::getOptionWidth() const { |
Chris Lattner | dbab15a | 2001-07-23 17:17:47 +0000 | [diff] [blame] | 478 | return std::strlen(ArgStr)+6; |
| 479 | } |
| 480 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 481 | // Print out the option for the alias... |
| 482 | void alias::printOptionInfo(unsigned GlobalWidth) const { |
Chris Lattner | dbab15a | 2001-07-23 17:17:47 +0000 | [diff] [blame] | 483 | unsigned L = std::strlen(ArgStr); |
Chris Lattner | dbab15a | 2001-07-23 17:17:47 +0000 | [diff] [blame] | 484 | cerr << " -" << ArgStr << string(GlobalWidth-L-6, ' ') << " - " |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 485 | << HelpStr << "\n"; |
Chris Lattner | dbab15a | 2001-07-23 17:17:47 +0000 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 489 | |
Chris Lattner | dbab15a | 2001-07-23 17:17:47 +0000 | [diff] [blame] | 490 | //===----------------------------------------------------------------------===// |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 491 | // Parser Implementation code... |
Chris Lattner | dbab15a | 2001-07-23 17:17:47 +0000 | [diff] [blame] | 492 | // |
| 493 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 494 | // parser<bool> implementation |
| 495 | // |
| 496 | bool parser<bool>::parseImpl(Option &O, const string &Arg, bool &Value) { |
Chris Lattner | dbab15a | 2001-07-23 17:17:47 +0000 | [diff] [blame] | 497 | if (Arg == "" || Arg == "true" || Arg == "TRUE" || Arg == "True" || |
| 498 | Arg == "1") { |
| 499 | Value = true; |
| 500 | } else if (Arg == "false" || Arg == "FALSE" || Arg == "False" || Arg == "0") { |
| 501 | Value = false; |
| 502 | } else { |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 503 | return O.error(": '" + Arg + |
| 504 | "' is invalid value for boolean argument! Try 0 or 1"); |
Chris Lattner | dbab15a | 2001-07-23 17:17:47 +0000 | [diff] [blame] | 505 | } |
Chris Lattner | dbab15a | 2001-07-23 17:17:47 +0000 | [diff] [blame] | 506 | return false; |
| 507 | } |
| 508 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 509 | // Return the width of the option tag for printing... |
| 510 | unsigned parser<bool>::getOptionWidth(const Option &O) const { |
| 511 | return std::strlen(O.ArgStr)+6; |
| 512 | } |
| 513 | |
| 514 | // printOptionInfo - Print out information about this option. The |
| 515 | // to-be-maintained width is specified. |
Chris Lattner | dbab15a | 2001-07-23 17:17:47 +0000 | [diff] [blame] | 516 | // |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 517 | void parser<bool>::printOptionInfo(const Option &O, unsigned GlobalWidth) const{ |
| 518 | unsigned L = std::strlen(O.ArgStr); |
| 519 | cerr << " -" << O.ArgStr << string(GlobalWidth-L-6, ' ') << " - " |
| 520 | << O.HelpStr << "\n"; |
| 521 | } |
| 522 | |
| 523 | |
| 524 | |
| 525 | // parser<int> implementation |
| 526 | // |
| 527 | bool parser<int>::parseImpl(Option &O, const string &Arg, int &Value) { |
Chris Lattner | dbab15a | 2001-07-23 17:17:47 +0000 | [diff] [blame] | 528 | const char *ArgStart = Arg.c_str(); |
| 529 | char *End; |
| 530 | Value = (int)strtol(ArgStart, &End, 0); |
| 531 | if (*End != 0) |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 532 | return O.error(": '" + Arg + "' value invalid for integer argument!"); |
Chris Lattner | dbab15a | 2001-07-23 17:17:47 +0000 | [diff] [blame] | 533 | return false; |
| 534 | } |
| 535 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 536 | // Return the width of the option tag for printing... |
| 537 | unsigned parser<int>::getOptionWidth(const Option &O) const { |
| 538 | return std::strlen(O.ArgStr)+std::strlen(getValueStr(O, "int"))+9; |
| 539 | } |
| 540 | |
| 541 | // printOptionInfo - Print out information about this option. The |
| 542 | // to-be-maintained width is specified. |
Chris Lattner | d215fd1 | 2001-10-13 06:53:19 +0000 | [diff] [blame] | 543 | // |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 544 | void parser<int>::printOptionInfo(const Option &O, unsigned GlobalWidth) const{ |
| 545 | cerr << " -" << O.ArgStr << "=<" << getValueStr(O, "int") << ">" |
| 546 | << string(GlobalWidth-getOptionWidth(O), ' ') << " - " |
| 547 | << O.HelpStr << "\n"; |
| 548 | } |
| 549 | |
| 550 | |
| 551 | // parser<double> implementation |
| 552 | // |
| 553 | bool parser<double>::parseImpl(Option &O, const string &Arg, double &Value) { |
| 554 | const char *ArgStart = Arg.c_str(); |
| 555 | char *End; |
| 556 | Value = strtod(ArgStart, &End); |
| 557 | if (*End != 0) |
| 558 | return O.error(": '" +Arg+ "' value invalid for floating point argument!"); |
Chris Lattner | d215fd1 | 2001-10-13 06:53:19 +0000 | [diff] [blame] | 559 | return false; |
| 560 | } |
| 561 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 562 | // Return the width of the option tag for printing... |
| 563 | unsigned parser<double>::getOptionWidth(const Option &O) const { |
| 564 | return std::strlen(O.ArgStr)+std::strlen(getValueStr(O, "number"))+9; |
| 565 | } |
| 566 | |
| 567 | // printOptionInfo - Print out information about this option. The |
| 568 | // to-be-maintained width is specified. |
Chris Lattner | dbab15a | 2001-07-23 17:17:47 +0000 | [diff] [blame] | 569 | // |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 570 | void parser<double>::printOptionInfo(const Option &O, |
| 571 | unsigned GlobalWidth) const{ |
| 572 | cerr << " -" << O.ArgStr << "=<" << getValueStr(O, "number") << ">" |
| 573 | << string(GlobalWidth-getOptionWidth(O), ' ') |
| 574 | << " - " << O.HelpStr << "\n"; |
| 575 | } |
| 576 | |
| 577 | |
| 578 | // parser<string> implementation |
| 579 | // |
| 580 | |
| 581 | // Return the width of the option tag for printing... |
| 582 | unsigned parser<string>::getOptionWidth(const Option &O) const { |
| 583 | return std::strlen(O.ArgStr)+std::strlen(getValueStr(O, "string"))+9; |
| 584 | } |
| 585 | |
| 586 | // printOptionInfo - Print out information about this option. The |
| 587 | // to-be-maintained width is specified. |
| 588 | // |
| 589 | void parser<string>::printOptionInfo(const Option &O, |
| 590 | unsigned GlobalWidth) const{ |
| 591 | cerr << " -" << O.ArgStr << " <" << getValueStr(O, "string") << ">" |
| 592 | << string(GlobalWidth-getOptionWidth(O), ' ') |
| 593 | << " - " << O.HelpStr << "\n"; |
| 594 | } |
| 595 | |
| 596 | // generic_parser_base implementation |
| 597 | // |
| 598 | |
Chris Lattner | aa852bb | 2002-07-23 17:15:12 +0000 | [diff] [blame] | 599 | // findOption - Return the option number corresponding to the specified |
| 600 | // argument string. If the option is not found, getNumOptions() is returned. |
| 601 | // |
| 602 | unsigned generic_parser_base::findOption(const char *Name) { |
| 603 | unsigned i = 0, e = getNumOptions(); |
| 604 | string N(Name); |
| 605 | |
| 606 | while (i != e) |
| 607 | if (getOption(i) == N) |
| 608 | return i; |
| 609 | else |
| 610 | ++i; |
| 611 | return e; |
| 612 | } |
| 613 | |
| 614 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 615 | // Return the width of the option tag for printing... |
| 616 | unsigned generic_parser_base::getOptionWidth(const Option &O) const { |
| 617 | if (O.hasArgStr()) { |
| 618 | unsigned Size = std::strlen(O.ArgStr)+6; |
| 619 | for (unsigned i = 0, e = getNumOptions(); i != e; ++i) |
| 620 | Size = std::max(Size, (unsigned)std::strlen(getOption(i))+8); |
| 621 | return Size; |
| 622 | } else { |
| 623 | unsigned BaseSize = 0; |
| 624 | for (unsigned i = 0, e = getNumOptions(); i != e; ++i) |
| 625 | BaseSize = std::max(BaseSize, (unsigned)std::strlen(getOption(i))+8); |
| 626 | return BaseSize; |
Chris Lattner | dbab15a | 2001-07-23 17:17:47 +0000 | [diff] [blame] | 627 | } |
| 628 | } |
| 629 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 630 | // printOptionInfo - Print out information about this option. The |
| 631 | // to-be-maintained width is specified. |
| 632 | // |
| 633 | void generic_parser_base::printOptionInfo(const Option &O, |
| 634 | unsigned GlobalWidth) const { |
| 635 | if (O.hasArgStr()) { |
| 636 | unsigned L = std::strlen(O.ArgStr); |
| 637 | cerr << " -" << O.ArgStr << string(GlobalWidth-L-6, ' ') |
| 638 | << " - " << O.HelpStr << "\n"; |
Chris Lattner | dbab15a | 2001-07-23 17:17:47 +0000 | [diff] [blame] | 639 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 640 | for (unsigned i = 0, e = getNumOptions(); i != e; ++i) { |
| 641 | unsigned NumSpaces = GlobalWidth-strlen(getOption(i))-8; |
| 642 | cerr << " =" << getOption(i) << string(NumSpaces, ' ') << " - " |
| 643 | << getDescription(i) << "\n"; |
Chris Lattner | 9c9be48 | 2002-01-31 00:42:56 +0000 | [diff] [blame] | 644 | } |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 645 | } else { |
| 646 | if (O.HelpStr[0]) |
| 647 | cerr << " " << O.HelpStr << "\n"; |
| 648 | for (unsigned i = 0, e = getNumOptions(); i != e; ++i) { |
| 649 | unsigned L = std::strlen(getOption(i)); |
| 650 | cerr << " -" << getOption(i) << string(GlobalWidth-L-8, ' ') << " - " |
| 651 | << getDescription(i) << "\n"; |
| 652 | } |
Chris Lattner | dbab15a | 2001-07-23 17:17:47 +0000 | [diff] [blame] | 653 | } |
| 654 | } |
| 655 | |
| 656 | |
| 657 | //===----------------------------------------------------------------------===// |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 658 | // --help and --help-hidden option implementation |
Chris Lattner | dbab15a | 2001-07-23 17:17:47 +0000 | [diff] [blame] | 659 | // |
| 660 | namespace { |
| 661 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 662 | class HelpPrinter { |
Chris Lattner | dbab15a | 2001-07-23 17:17:47 +0000 | [diff] [blame] | 663 | unsigned MaxArgLen; |
| 664 | const Option *EmptyArg; |
| 665 | const bool ShowHidden; |
| 666 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 667 | // isHidden/isReallyHidden - Predicates to be used to filter down arg lists. |
| 668 | inline static bool isHidden(pair<string, Option *> &OptPair) { |
| 669 | return OptPair.second->getOptionHiddenFlag() >= Hidden; |
| 670 | } |
| 671 | inline static bool isReallyHidden(pair<string, Option *> &OptPair) { |
| 672 | return OptPair.second->getOptionHiddenFlag() == ReallyHidden; |
| 673 | } |
| 674 | |
| 675 | public: |
| 676 | HelpPrinter(bool showHidden) : ShowHidden(showHidden) { |
| 677 | EmptyArg = 0; |
| 678 | } |
| 679 | |
| 680 | void operator=(bool Value) { |
| 681 | if (Value == false) return; |
| 682 | |
Chris Lattner | dbab15a | 2001-07-23 17:17:47 +0000 | [diff] [blame] | 683 | // Copy Options into a vector so we can sort them as we like... |
| 684 | vector<pair<string, Option*> > Options; |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 685 | copy(getOpts().begin(), getOpts().end(), std::back_inserter(Options)); |
Chris Lattner | dbab15a | 2001-07-23 17:17:47 +0000 | [diff] [blame] | 686 | |
| 687 | // Eliminate Hidden or ReallyHidden arguments, depending on ShowHidden |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 688 | Options.erase(std::remove_if(Options.begin(), Options.end(), |
| 689 | std::ptr_fun(ShowHidden ? isReallyHidden : isHidden)), |
Chris Lattner | dbab15a | 2001-07-23 17:17:47 +0000 | [diff] [blame] | 690 | Options.end()); |
| 691 | |
| 692 | // Eliminate duplicate entries in table (from enum flags options, f.e.) |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 693 | { // Give OptionSet a scope |
| 694 | std::set<Option*> OptionSet; |
| 695 | for (unsigned i = 0; i != Options.size(); ++i) |
| 696 | if (OptionSet.count(Options[i].second) == 0) |
| 697 | OptionSet.insert(Options[i].second); // Add new entry to set |
| 698 | else |
| 699 | Options.erase(Options.begin()+i--); // Erase duplicate |
| 700 | } |
Chris Lattner | dbab15a | 2001-07-23 17:17:47 +0000 | [diff] [blame] | 701 | |
| 702 | if (ProgramOverview) |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 703 | cerr << "OVERVIEW:" << ProgramOverview << "\n"; |
Chris Lattner | dbab15a | 2001-07-23 17:17:47 +0000 | [diff] [blame] | 704 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 705 | cerr << "USAGE: " << ProgramName << " [options]"; |
| 706 | |
| 707 | // Print out the positional options... |
| 708 | vector<Option*> &PosOpts = getPositionalOpts(); |
| 709 | Option *CAOpt = 0; // The cl::ConsumeAfter option, if it exists... |
| 710 | if (!PosOpts.empty() && PosOpts[0]->getNumOccurancesFlag() == ConsumeAfter) |
| 711 | CAOpt = PosOpts[0]; |
| 712 | |
| 713 | for (unsigned i = CAOpt != 0, e = PosOpts.size(); i != e; ++i) { |
| 714 | cerr << " " << PosOpts[i]->HelpStr; |
| 715 | switch (PosOpts[i]->getNumOccurancesFlag()) { |
| 716 | case Optional: cerr << "?"; break; |
| 717 | case ZeroOrMore: cerr << "*"; break; |
| 718 | case Required: break; |
| 719 | case OneOrMore: cerr << "+"; break; |
| 720 | case ConsumeAfter: |
| 721 | default: |
| 722 | assert(0 && "Unknown NumOccurances Flag Value!"); |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | // Print the consume after option info if it exists... |
| 727 | if (CAOpt) cerr << " " << CAOpt->HelpStr; |
| 728 | |
| 729 | cerr << "\n\n"; |
Chris Lattner | dbab15a | 2001-07-23 17:17:47 +0000 | [diff] [blame] | 730 | |
| 731 | // Compute the maximum argument length... |
| 732 | MaxArgLen = 0; |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 733 | for (unsigned i = 0, e = Options.size(); i != e; ++i) |
| 734 | MaxArgLen = std::max(MaxArgLen, Options[i].second->getOptionWidth()); |
Chris Lattner | dbab15a | 2001-07-23 17:17:47 +0000 | [diff] [blame] | 735 | |
| 736 | cerr << "OPTIONS:\n"; |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 737 | for (unsigned i = 0, e = Options.size(); i != e; ++i) |
| 738 | Options[i].second->printOptionInfo(MaxArgLen); |
Chris Lattner | dbab15a | 2001-07-23 17:17:47 +0000 | [diff] [blame] | 739 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 740 | // Halt the program if help information is printed |
| 741 | exit(1); |
Chris Lattner | dbab15a | 2001-07-23 17:17:47 +0000 | [diff] [blame] | 742 | } |
| 743 | }; |
| 744 | |
Chris Lattner | 331de23 | 2002-07-22 02:07:59 +0000 | [diff] [blame] | 745 | |
| 746 | |
| 747 | // Define the two HelpPrinter instances that are used to print out help, or |
| 748 | // help-hidden... |
| 749 | // |
| 750 | HelpPrinter NormalPrinter(false); |
| 751 | HelpPrinter HiddenPrinter(true); |
| 752 | |
| 753 | cl::opt<HelpPrinter, true, parser<bool> > |
| 754 | HOp("help", cl::desc("display available options (--help-hidden for more)"), |
| 755 | cl::location(NormalPrinter)); |
| 756 | |
| 757 | cl::opt<HelpPrinter, true, parser<bool> > |
| 758 | HHOp("help-hidden", cl::desc("display all available options"), |
| 759 | cl::location(HiddenPrinter), cl::Hidden); |
Chris Lattner | dbab15a | 2001-07-23 17:17:47 +0000 | [diff] [blame] | 760 | |
| 761 | } // End anonymous namespace |