19 errString =
"Unknown error for args";
28 head +=
" <options> --\n";
32 bool lastIsBoolean =
false;
34 for (
int i = 1; i <
argc; ++i) {
37 unqualified.push_back(s);
44 unqualified.push_back(s);
48 if (lastIsBoolean && (s !=
"yes" && s !=
"no")) {
49 errString =
"syntax error: " + s;
50 errString +=
" (expected yes or no)";
55 passedArgs[lastKey] =
true;
57 lastIsBoolean =
false;
58 }
else if (s[1] ==
'-') {
61 args[lastKey] =
"yes";
62 passedArgs[lastKey] =
true;
64 lastIsBoolean =
false;
66 errString =
"expected value of ";
73 if (s.length() == 2) {
79 string arg = s.substr(2);
81 string::size_type epos = arg.find(
'=');
82 if (epos != string::npos) {
83 val = arg.substr(epos + 1);
84 arg = arg.substr(0, epos);
87 if (reg.find(arg) == reg.end()) {
88 errString =
"unrecognized argument: --" + arg;
92 if (reg.find(arg)->second.b) {
93 if (val !=
"" && val !=
"yes" && val !=
"no") {
94 errString =
"syntax error: " + val;
95 errString +=
" (expected yes or no)";
97 }
else if (val ==
"") {
103 errString =
"syntax error: " + arg;
104 errString +=
" (expected --" + arg +
"=<str>)";
109 passedArgs[arg] =
true;
112 lastIsBoolean =
false;
116 args[lastKey] =
"yes";
117 passedArgs[lastKey] =
true;
119 lastIsBoolean =
false;
121 errString =
"expected value of ";
122 errString += lastKey;
128 string arg = s.substr(1);
129 if (arg.length() == 1) {
130 map<string, ArgOpts>::const_iterator it = reg.begin();
132 for (; it != reg.end(); ++it) {
133 if (it->second.c.find(arg[0]) != string::npos) {
135 if (it->second.b) lastIsBoolean =
true;
142 errString =
"unrecognized argument: -";
147 string::const_iterator its = arg.begin();
148 for (; its != arg.end(); ++its) {
149 map<string, ArgOpts>::const_iterator it = reg.begin();
151 for (; it != reg.end(); ++it) {
152 if (it->second.c.find(*its) != string::npos) {
154 errString =
"argument is not a boolean: ";
155 errString +=
"--" + it->first;
157 errString += it->second.c;
162 args[it->first] =
"yes";
163 passedArgs[it->first] =
true;
166 lastIsBoolean =
false;
172 errString =
"unrecognized argument: ";
183 args[lastKey] =
"yes";
184 passedArgs[lastKey] =
true;
186 errString =
"expected value of ";
187 errString += lastKey;
194 map<string, ArgOpts>::const_iterator it = reg.begin();
195 for (; it != reg.end(); ++it) {
196 if (args.find(it->first) == args.end()) {
198 errString =
"missing argument: ";
199 errString += it->first;
202 if (it->second.b) args[it->first] =
"no";
205 if (ac == -1) ac =
argc;
219 if (args.find(arg) == args.end())
return "";
220 return args.find(arg)->second;
227 registerArg(arg, desc,
boolean,
true);
232 const string &desc,
bool boolean)
234 registerArg(arg, desc,
boolean,
false);
238void CommandLineArgs::registerArg(
const string &arg,
const string &desc,
239 bool boolean,
bool optional)
244 while (name.size() > 1 && name[1] ==
'|') {
246 name = name.substr(2);
249 reg.insert(make_pair(name,
ArgOpts(shorts,
boolean, optional, desc)));
256 return passedArgs.find(tmp) != passedArgs.end();
265 map<string, ArgOpts>::const_iterator it = reg.begin();
266 for (; it != reg.end(); ++it) {
267 if (it->second.c !=
"") {
268 string::const_iterator sit = it->second.c.begin();
269 for (; sit != it->second.c.end(); ++sit) {
270 if (sit != it->second.c.begin()) tmp +=
'\n';
281 if (!it->second.b) tmp +=
"=<str>";
283 if (!it->second.o) tmp +=
" (required)";
285 string::size_type lineStart = tmp.rfind(
'\n');
286 if (lineStart == string::npos) lineStart = 0;
288 int pad = 21 - (tmp.length() - lineStart);
294 tmp += string(pad,
' ');
295 tmp += it->second.desc;
Declaration of the argument parser class.
void setTail(const std::string &str)
const std::string operator[](const std::string &arg) const
void addRequired(const std::string &arg, const std::string &desc, bool boolean)
std::string usageString(void) const
bool hasArg(const std::string &arg) const
bool parse(int argc, char *argv[])
std::string errorString(void) const
const std::vector< std::string > & getUnqualifiedArgs() const
void addOptional(const std::string &arg, const std::string &desc, bool boolean)
Declaration of miscellaneous convertion functions.
void lowercase(std::string &input)