2 //***************************************************************************
8 // General options processing for any program.
11 // 08/08/95 - adve - created in the dHPF compiler
12 // 10/10/96 - mpal, dbaker - converted to const member functions.
13 // 11/26/96 - adve - fixed to handle options that consume 0+ arguments
14 // 07/15/01 - vadve - Copied to LLVM system and modified
16 //**************************************************************************/
18 //************************** System Include Files **************************/
26 #define MAXINT ((1 << sizeof(int)-1) - 1)
29 //*************************** User Include Files ***************************/
31 #include "llvm/Support/ProgramOptions.h"
32 #include "llvm/Support/ProgramOption.h"
35 //************************** Method Definitions ****************************/
37 ProgramOptions::ProgramOptions(int _argc,
47 string ProgramOptions::StringOptionValue(const string &optString) const {
48 const StringOption* handler = (const StringOption*)OptionHandler(optString);
49 return (handler == NULL) ? string("") : handler->Value();
53 ProgramOptions::FlagOptionValue(const string &optString) const
55 const FlagOption* handler = (const FlagOption*) OptionHandler(optString);
56 return (handler == NULL) ? false : handler->Value();
60 ProgramOptions::RealOptionValue(const string &optString) const
62 const RealValuedOption* handler =
63 (const RealValuedOption*) OptionHandler(optString);
64 return (handler == NULL) ? MAXFLOAT : handler->Value();
68 ProgramOptions::IntOptionValue(const string &optString) const
70 const IntegerValuedOption* handler =
71 (const IntegerValuedOption*) OptionHandler(optString);
72 return (handler == NULL) ? MAXINT : handler->Value();
76 ProgramOptions::OptionSpecified(const string &optString) const
78 const ProgramOption* handler = OptionHandler(optString);
79 return handler->OptionSpecified();
83 ProgramOptions::ProgramName() const
89 ProgramOptions::NumberOfOtherOptions() const
91 return argc - argsConsumed;
95 ProgramOptions::OtherOption(int i) const
98 assert(i >= 0 && i < argc);
103 ProgramOptions::GetOriginalArgs() const
109 ProgramOptions::GetDescription() const
111 vector<string> optDesc;
113 if (optDesc.size() < (unsigned) argc)
115 for (hash_map<string,ProgramOption*>::const_iterator iter=optionRegistry.begin();
116 ! (iter == optionRegistry.end());
119 const ProgramOption* handler = iter->second;
120 optDesc.push_back(handler->ArgString()); // 1st
121 optDesc.push_back(handler->HelpMesg()); // 2nd
122 optDesc.push_back(handler->GetTextValue()); // 3rd
130 ProgramOptions::Register(ProgramOption* option)
132 optionRegistry[option->ArgString()] = option;
135 //----------------------------------------------------------------------
136 // function ProgramOptions::ParseArgs
138 // Parse command-line options until you run out of options or see
139 // an unrecognized option. `getopt' is a standard package to do this,
140 // but incredibly, it limited to single-letter options.
142 // -- Each option can consume zero or one additional arguments of argv
143 // -- "--" can be used to mark the end of options
144 // so that a program argument can also start with "-".
145 //---------------------------------------------------------------------/
148 ProgramOptions::ParseArgs(int argc,
155 // consume the program name
158 while (argsConsumed < argc)
160 const char* arg = argv[argsConsumed];
163 if (strcmp(arg, "--") == 0) { // "--" marks end of options
164 argsConsumed++; // consume and
165 break; // discontinue the for loop
167 ProgramOption* handler = OptionHandler(arg+1);
168 if (handler == NULL) {
169 cerr << "Unrecognized option: " << arg+1 << endl;
173 if (argc - argsConsumed < handler->MinExpectedArgs()) {
174 cerr << "Option " << (char*) arg+1 << " needs "
175 << handler->MinExpectedArgs() << " arguments" << endl;
179 argsConsumed++; // consumed the option
181 const char* nextArg = (argsConsumed < argc)? argv[argsConsumed]
183 int numAdditionalArgsConsumed = handler->EvalOpt(nextArg);
184 if (numAdditionalArgsConsumed < 0)
186 argsConsumed += numAdditionalArgsConsumed;
190 break; // quit the while loop
198 ProgramOptions::PrintArgs(ostream& stream) const
200 for (int i = 0; i < argc; i++) {
201 stream << argv[i] << " ";
208 ProgramOptions::PrintOptions(ostream& stream) const
210 stream << "OPTIONS:" << endl;
211 stream << "\tUse argument 0 to turn OFF a flag option: "
212 << "-<flag_opt> 0" << endl << endl;
214 for (hash_map<string,ProgramOption*>::const_iterator iter = optionRegistry.begin();
215 iter != optionRegistry.end(); ++iter) {
216 const ProgramOption* handler = (*iter).second;
218 stream << "\t-" << handler->ArgString();
220 const char* const showarg = " <arg>";
222 for (i=1; i <= handler->MinExpectedArgs(); i++)
225 int numCharsPrinted = 1 + handler->ArgString().length()
226 + 6 * handler->MinExpectedArgs();
227 for (i=1; i > numCharsPrinted / 8; i--)
230 stream << "\t" << handler->HelpMesg()
236 ProgramOptions::Usage() const
243 //**************************************************************************/