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