| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
#ifndef TCLAP_CMD_LINE_H |
| 26 |
#define TCLAP_CMD_LINE_H |
| 27 |
|
| 28 |
#include <tclap/MultiSwitchArg.h> |
| 29 |
#include <tclap/SwitchArg.h> |
| 30 |
#include <tclap/UnlabeledMultiArg.h> |
| 31 |
#include <tclap/UnlabeledValueArg.h> |
| 32 |
|
| 33 |
#include <tclap/HelpVisitor.h> |
| 34 |
#include <tclap/IgnoreRestVisitor.h> |
| 35 |
#include <tclap/VersionVisitor.h> |
| 36 |
|
| 37 |
#include <tclap/CmdLineOutput.h> |
| 38 |
#include <tclap/StdOutput.h> |
| 39 |
|
| 40 |
#include <tclap/Constraint.h> |
| 41 |
#include <tclap/ValuesConstraint.h> |
| 42 |
|
| 43 |
#include <tclap/ArgGroup.h> |
| 44 |
#include <tclap/DeferDelete.h> |
| 45 |
|
| 46 |
#include <algorithm> |
| 47 |
#include <cstdlib> |
| 48 |
#include <iomanip> |
| 49 |
#include <iostream> |
| 50 |
#include <list> |
| 51 |
#include <string> |
| 52 |
#include <vector> |
| 53 |
|
| 54 |
namespace TCLAP { |
| 55 |
|
| 56 |
class StandaloneArgs : public AnyOf { |
| 57 |
public: |
| 58 |
StandaloneArgs() {} |
| 59 |
|
| 60 |
ArgContainer &add(Arg *arg) { |
| 61 |
|
| 62 |
for (iterator it = begin(); it != end(); it++) { |
| 63 |
if (*arg == **it) { |
| 64 |
throw SpecificationException( |
| 65 |
"Argument with same flag/name already exists!", |
| 66 |
arg->longID()); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
_args.push_back(arg); |
| 71 |
|
| 72 |
return *this; |
| 73 |
} |
| 74 |
|
| 75 |
bool showAsGroup() const { return false; } |
| 76 |
}; |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
class CmdLine : public CmdLineInterface { |
| 83 |
protected: |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
std::list<Arg *> _argList; |
| 89 |
|
| 90 |
StandaloneArgs _standaloneArgs; |
| 91 |
StandaloneArgs _autoArgs; |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
std::list<ArgGroup *> _argGroups; |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
std::string _progName; |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
std::string _message; |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
std::string _version; |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
int _numRequired; |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
char _delimiter; |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
DeferDelete _deleteOnExit; |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
StdOutput _defaultOutput; |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
CmdLineOutput *_output; |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
bool _handleExceptions; |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
void missingArgsException(const std::list<ArgGroup *> &missing); |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
bool _emptyCombined(const std::string &s); |
| 161 |
|
| 162 |
private: |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
CmdLine(const CmdLine &rhs); |
| 167 |
CmdLine &operator=(const CmdLine &rhs); |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
void _constructor(); |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
bool _helpAndVersion; |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
bool _ignoreUnmatched; |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
bool _ignoring; |
| 189 |
|
| 190 |
public: |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
CmdLine(const std::string &message, const char delimiter = ' ', |
| 204 |
const std::string &version = "none", bool helpAndVersion = true); |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
virtual ~CmdLine() {} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
ArgContainer &add(Arg &a); |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
ArgContainer &add(Arg *a); |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
ArgContainer &add(ArgGroup &args); |
| 238 |
|
| 239 |
|
| 240 |
void addToArgList(Arg *a); |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
void xorAdd(Arg &a, Arg &b); |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
void xorAdd(const std::vector<Arg *> &xors); |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
void parse(int argc, const char *const *argv); |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
void parse(std::vector<std::string> &args); |
| 265 |
|
| 266 |
void setOutput(CmdLineOutput *co); |
| 267 |
|
| 268 |
std::string getVersion() const { return _version; } |
| 269 |
|
| 270 |
std::string getProgramName() const { return _progName; } |
| 271 |
|
| 272 |
|
| 273 |
std::list<Arg *> getArgList() const { return _argList; } |
| 274 |
std::list<ArgGroup *> getArgGroups() { |
| 275 |
std::list<ArgGroup *> groups = _argGroups; |
| 276 |
groups.push_back(&_autoArgs); |
| 277 |
return groups; |
| 278 |
} |
| 279 |
|
| 280 |
char getDelimiter() const { return _delimiter; } |
| 281 |
std::string getMessage() const { return _message; } |
| 282 |
bool hasHelpAndVersion() const { return _helpAndVersion; } |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
void setExceptionHandling(const bool state); |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
bool hasExceptionHandling() const { return _handleExceptions; } |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
void reset(); |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
void ignoreUnmatched(const bool ignore); |
| 311 |
|
| 312 |
void beginIgnoring() { _ignoring = true; } |
| 313 |
bool ignoreRest() { return _ignoring; } |
| 314 |
}; |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
inline CmdLine::CmdLine(const std::string &m, char delim, const std::string &v, |
| 321 |
bool help) |
| 322 |
: _argList(), |
| 323 |
_standaloneArgs(), |
| 324 |
_autoArgs(), |
| 325 |
_argGroups(), |
| 326 |
_progName("not_set_yet"), |
| 327 |
_message(m), |
| 328 |
_version(v), |
| 329 |
_numRequired(0), |
| 330 |
_delimiter(delim), |
| 331 |
_deleteOnExit(), |
| 332 |
_defaultOutput(), |
| 333 |
_output(&_defaultOutput), |
| 334 |
_handleExceptions(true), |
| 335 |
_helpAndVersion(help), |
| 336 |
_ignoreUnmatched(false), |
| 337 |
_ignoring(false) { |
| 338 |
_constructor(); |
| 339 |
} |
| 340 |
|
| 341 |
inline void CmdLine::_constructor() { |
| 342 |
Arg::setDelimiter(_delimiter); |
| 343 |
|
| 344 |
Visitor *v; |
| 345 |
add(_standaloneArgs); |
| 346 |
_autoArgs.setParser(*this); |
| 347 |
|
| 348 |
|
| 349 |
v = new IgnoreRestVisitor(*this); |
| 350 |
SwitchArg *ignore = new SwitchArg( |
| 351 |
Arg::flagStartString(), Arg::ignoreNameString(), |
| 352 |
"Ignores the rest of the labeled arguments following this flag.", false, |
| 353 |
v); |
| 354 |
_deleteOnExit(ignore); |
| 355 |
_deleteOnExit(v); |
| 356 |
_autoArgs.add(ignore); |
| 357 |
addToArgList(ignore); |
| 358 |
|
| 359 |
if (_helpAndVersion) { |
| 360 |
v = new HelpVisitor(this, &_output); |
| 361 |
SwitchArg *help = new SwitchArg( |
| 362 |
"h", "help", "Displays usage information and exits.", false, v); |
| 363 |
_deleteOnExit(help); |
| 364 |
_deleteOnExit(v); |
| 365 |
|
| 366 |
v = new VersionVisitor(this, &_output); |
| 367 |
SwitchArg *vers = new SwitchArg( |
| 368 |
"", "version", "Displays version information and exits.", false, v); |
| 369 |
_deleteOnExit(vers); |
| 370 |
_deleteOnExit(v); |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
_autoArgs.add(vers); |
| 375 |
addToArgList(vers); |
| 376 |
_autoArgs.add(help); |
| 377 |
addToArgList(help); |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
inline void CmdLine::xorAdd(const std::vector<Arg *> &args) { |
| 382 |
OneOf *group = new OneOf(*this); |
| 383 |
_deleteOnExit(group); |
| 384 |
|
| 385 |
for (std::vector<Arg *>::const_iterator it = args.begin(); it != args.end(); |
| 386 |
++it) { |
| 387 |
group->add(**it); |
| 388 |
} |
| 389 |
} |
| 390 |
|
| 391 |
inline void CmdLine::xorAdd(Arg &a, Arg &b) { |
| 392 |
std::vector<Arg *> ors; |
| 393 |
ors.push_back(&a); |
| 394 |
ors.push_back(&b); |
| 395 |
xorAdd(ors); |
| 396 |
} |
| 397 |
|
| 398 |
inline ArgContainer &CmdLine::add(ArgGroup &args) { |
| 399 |
args.setParser(*this); |
| 400 |
_argGroups.push_back(&args); |
| 401 |
|
| 402 |
return *this; |
| 403 |
} |
| 404 |
|
| 405 |
inline ArgContainer &CmdLine::add(Arg &a) { return add(&a); } |
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
inline void CmdLine::addToArgList(Arg *a) { |
| 410 |
for (ArgListIterator it = _argList.begin(); it != _argList.end(); it++) |
| 411 |
if (*a == *(*it)) |
| 412 |
throw(SpecificationException( |
| 413 |
"Argument with same flag/name already exists!", a->longID())); |
| 414 |
|
| 415 |
a->addToList(_argList); |
| 416 |
|
| 417 |
if (a->isRequired()) _numRequired++; |
| 418 |
} |
| 419 |
|
| 420 |
inline ArgContainer &CmdLine::add(Arg *a) { |
| 421 |
addToArgList(a); |
| 422 |
_standaloneArgs.add(a); |
| 423 |
|
| 424 |
return *this; |
| 425 |
} |
| 426 |
|
| 427 |
inline void CmdLine::parse(int argc, const char *const *argv) { |
| 428 |
|
| 429 |
|
| 430 |
std::vector<std::string> args; |
| 431 |
for (int i = 0; i < argc; i++) args.push_back(argv[i]); |
| 432 |
|
| 433 |
parse(args); |
| 434 |
} |
| 435 |
|
| 436 |
inline void CmdLine::parse(std::vector<std::string> &args) { |
| 437 |
bool shouldExit = false; |
| 438 |
int estat = 0; |
| 439 |
|
| 440 |
try { |
| 441 |
if (args.empty()) { |
| 442 |
|
| 443 |
throw CmdLineParseException( |
| 444 |
"The args vector must not be empty, " |
| 445 |
"the first entry should contain the " |
| 446 |
"program's name."); |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
_progName = basename(args.front()); |
| 451 |
args.erase(args.begin()); |
| 452 |
|
| 453 |
int requiredCount = 0; |
| 454 |
std::list<ArgGroup *> missingArgGroups; |
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
for (int i = 0; static_cast<unsigned int>(i) < args.size(); i++) { |
| 471 |
bool matched = false; |
| 472 |
for (ArgListIterator it = _argList.begin(); it != _argList.end(); |
| 473 |
it++) { |
| 474 |
Arg &arg = **it; |
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
bool alreadySet = arg.isSet(); |
| 484 |
bool ignore = arg.isIgnoreable() && ignoreRest(); |
| 485 |
if (!ignore && arg.processArg(&i, args)) { |
| 486 |
requiredCount += (!alreadySet && arg.isRequired()) ? 1 : 0; |
| 487 |
matched = true; |
| 488 |
break; |
| 489 |
} |
| 490 |
} |
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
if (!matched && _emptyCombined(args[i])) matched = true; |
| 495 |
|
| 496 |
if (!matched && !ignoreRest() && !_ignoreUnmatched) |
| 497 |
throw( |
| 498 |
CmdLineParseException("Couldn't find match " |
| 499 |
"for argument", |
| 500 |
args[i])); |
| 501 |
} |
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
for (std::list<ArgGroup *>::iterator it = _argGroups.begin(); |
| 506 |
it != _argGroups.end(); ++it) { |
| 507 |
bool missingRequired = (*it)->validate(); |
| 508 |
if (missingRequired) { |
| 509 |
missingArgGroups.push_back(*it); |
| 510 |
} |
| 511 |
} |
| 512 |
|
| 513 |
if (requiredCount < _numRequired || !missingArgGroups.empty()) { |
| 514 |
missingArgsException(missingArgGroups); |
| 515 |
} |
| 516 |
|
| 517 |
if (requiredCount > _numRequired) { |
| 518 |
throw(CmdLineParseException("Too many arguments!")); |
| 519 |
} |
| 520 |
} catch (ArgException &e) { |
| 521 |
|
| 522 |
if (!_handleExceptions) { |
| 523 |
throw; |
| 524 |
} |
| 525 |
|
| 526 |
try { |
| 527 |
_output->failure(*this, e); |
| 528 |
} catch (ExitException &ee) { |
| 529 |
estat = ee.getExitStatus(); |
| 530 |
shouldExit = true; |
| 531 |
} |
| 532 |
} catch (ExitException &ee) { |
| 533 |
|
| 534 |
if (!_handleExceptions) { |
| 535 |
throw; |
| 536 |
} |
| 537 |
|
| 538 |
estat = ee.getExitStatus(); |
| 539 |
shouldExit = true; |
| 540 |
} |
| 541 |
|
| 542 |
if (shouldExit) exit(estat); |
| 543 |
} |
| 544 |
|
| 545 |
inline bool CmdLine::_emptyCombined(const std::string &s) { |
| 546 |
if (s.length() > 0 && s[0] != Arg::flagStartChar()) return false; |
| 547 |
|
| 548 |
for (int i = 1; static_cast<unsigned int>(i) < s.length(); i++) |
| 549 |
if (s[i] != Arg::blankChar()) return false; |
| 550 |
|
| 551 |
return true; |
| 552 |
} |
| 553 |
|
| 554 |
inline void CmdLine::missingArgsException( |
| 555 |
const std::list<ArgGroup *> &missing) { |
| 556 |
int count = 0; |
| 557 |
|
| 558 |
std::string missingArgList; |
| 559 |
for (ArgListIterator it = _argList.begin(); it != _argList.end(); it++) { |
| 560 |
if ((*it)->isRequired() && !(*it)->isSet()) { |
| 561 |
missingArgList += (*it)->getName(); |
| 562 |
missingArgList += ", "; |
| 563 |
count++; |
| 564 |
} |
| 565 |
} |
| 566 |
|
| 567 |
for (std::list<ArgGroup *>::const_iterator it = missing.begin(); |
| 568 |
it != missing.end(); it++) { |
| 569 |
missingArgList += (*it)->getName(); |
| 570 |
missingArgList += ", "; |
| 571 |
count++; |
| 572 |
} |
| 573 |
|
| 574 |
missingArgList = missingArgList.substr(0, missingArgList.length() - 2); |
| 575 |
|
| 576 |
std::string msg; |
| 577 |
if (count > 1) |
| 578 |
msg = "Required arguments missing: "; |
| 579 |
else |
| 580 |
msg = "Required argument missing: "; |
| 581 |
|
| 582 |
msg += missingArgList; |
| 583 |
|
| 584 |
throw(CmdLineParseException(msg)); |
| 585 |
} |
| 586 |
|
| 587 |
inline void CmdLine::setOutput(CmdLineOutput *co) { _output = co; } |
| 588 |
|
| 589 |
inline void CmdLine::setExceptionHandling(const bool state) { |
| 590 |
_handleExceptions = state; |
| 591 |
} |
| 592 |
|
| 593 |
inline void CmdLine::reset() { |
| 594 |
|
| 595 |
for (ArgListIterator it = _argList.begin(); it != _argList.end(); it++) |
| 596 |
(*it)->reset(); |
| 597 |
|
| 598 |
_progName.clear(); |
| 599 |
} |
| 600 |
|
| 601 |
inline void CmdLine::ignoreUnmatched(const bool ignore) { |
| 602 |
_ignoreUnmatched = ignore; |
| 603 |
} |
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
} |
| 610 |
|
| 611 |
#endif |