| 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_ARG_H |
| 26 |
#define TCLAP_ARG_H |
| 27 |
|
| 28 |
#ifdef HAVE_TCLAP_CONFIG_H |
| 29 |
#include <tclap/TCLAPConfig.h> |
| 30 |
#endif |
| 31 |
|
| 32 |
#include <tclap/ArgException.h> |
| 33 |
#include <tclap/ArgTraits.h> |
| 34 |
#include <tclap/CmdLineInterface.h> |
| 35 |
#include <tclap/StandardTraits.h> |
| 36 |
#include <tclap/Visitor.h> |
| 37 |
#include <tclap/sstream.h> |
| 38 |
|
| 39 |
#include <cstdio> |
| 40 |
#include <iomanip> |
| 41 |
#include <iostream> |
| 42 |
#include <list> |
| 43 |
#include <string> |
| 44 |
#include <vector> |
| 45 |
|
| 46 |
namespace TCLAP { |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
class Arg { |
| 54 |
private: |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
Arg(const Arg &rhs); |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
Arg &operator=(const Arg &rhs); |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
static char &delimiterRef() { |
| 70 |
static char delim = ' '; |
| 71 |
return delim; |
| 72 |
} |
| 73 |
|
| 74 |
protected: |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
std::string _flag; |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
std::string _name; |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
std::string _description; |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
const bool _required; |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
std::string _requireLabel; |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
bool _valueRequired; |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
bool _alreadySet; |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
std::string _setBy; |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
Visitor *_visitor; |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
bool _ignoreable; |
| 140 |
|
| 141 |
bool _acceptsMultipleValues; |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
bool _visibleInHelp; |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
void _checkWithVisitor() const; |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
Arg(const std::string &flag, const std::string &name, |
| 168 |
const std::string &desc, bool req, bool valreq, Visitor *v = NULL); |
| 169 |
|
| 170 |
public: |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
virtual ~Arg(); |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
virtual void addToList(std::list<Arg *> &argList) const; |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
static char delimiter() { return delimiterRef(); } |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
static char blankChar() { return '\a'; } |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
#ifndef TCLAP_FLAGSTARTCHAR |
| 199 |
#define TCLAP_FLAGSTARTCHAR '-' |
| 200 |
#endif |
| 201 |
static char flagStartChar() { return TCLAP_FLAGSTARTCHAR; } |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
#ifndef TCLAP_FLAGSTARTSTRING |
| 209 |
#define TCLAP_FLAGSTARTSTRING "-" |
| 210 |
#endif |
| 211 |
static const std::string flagStartString() { return TCLAP_FLAGSTARTSTRING; } |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
#ifndef TCLAP_NAMESTARTSTRING |
| 218 |
#define TCLAP_NAMESTARTSTRING "--" |
| 219 |
#endif |
| 220 |
static const std::string nameStartString() { return TCLAP_NAMESTARTSTRING; } |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
static const std::string ignoreNameString() { return "ignore_rest"; } |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
static void setDelimiter(char c) { delimiterRef() = c; } |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
virtual bool processArg(int *i, std::vector<std::string> &args) = 0; |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
virtual bool operator==(const Arg &a) const; |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
const std::string &getFlag() const; |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
const std::string &getName() const; |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
std::string getDescription() const { return getDescription(_required); } |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
std::string getDescription(bool required) const { |
| 271 |
return (required ? "(" + _requireLabel + ") " : "") + _description; |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
virtual bool isRequired() const; |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
bool isValueRequired() const; |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
bool isSet() const; |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
const std::string &setBy() const { return _setBy; } |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
bool isIgnoreable() const; |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
virtual bool argMatches(const std::string &s) const; |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
virtual std::string toString() const; |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
virtual std::string shortID(const std::string &valueId = "val") const; |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
virtual std::string longID(const std::string &valueId = "val") const; |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
virtual void trimFlag(std::string &flag, std::string &value) const; |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
bool _hasBlanks(const std::string &s) const; |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
virtual bool allowMore(); |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
virtual bool acceptsMultipleValues(); |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
virtual void reset(); |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
virtual void hideFromHelp(bool hide = true) { _visibleInHelp = !hide; } |
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
virtual bool visibleInHelp() const { return _visibleInHelp; } |
| 373 |
|
| 374 |
virtual bool hasLabel() const { return true; } |
| 375 |
}; |
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
typedef std::list<Arg *>::const_iterator ArgListIterator; |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
typedef std::vector<Arg *>::const_iterator ArgVectorIterator; |
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
typedef std::list<Visitor *>::const_iterator VisitorListIterator; |
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
template <typename T> |
| 399 |
void ExtractValue(T &destVal, const std::string &strVal, ValueLike vl) { |
| 400 |
static_cast<void>(vl); |
| 401 |
istringstream is(strVal.c_str()); |
| 402 |
|
| 403 |
int valuesRead = 0; |
| 404 |
while (is.good()) { |
| 405 |
if (is.peek() != EOF) |
| 406 |
#ifdef TCLAP_SETBASE_ZERO |
| 407 |
is >> std::setbase(0) >> destVal; |
| 408 |
#else |
| 409 |
is >> destVal; |
| 410 |
#endif |
| 411 |
else |
| 412 |
break; |
| 413 |
|
| 414 |
valuesRead++; |
| 415 |
} |
| 416 |
|
| 417 |
if (is.fail()) |
| 418 |
throw( |
| 419 |
ArgParseException("Couldn't read argument value " |
| 420 |
"from string '" + |
| 421 |
strVal + "'")); |
| 422 |
|
| 423 |
if (valuesRead > 1) |
| 424 |
throw( |
| 425 |
ArgParseException("More than one valid value parsed from " |
| 426 |
"string '" + |
| 427 |
strVal + "'")); |
| 428 |
} |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
template <typename T> |
| 437 |
void ExtractValue(T &destVal, const std::string &strVal, StringLike sl) { |
| 438 |
static_cast<void>(sl); |
| 439 |
SetString(destVal, strVal); |
| 440 |
} |
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
inline Arg::Arg(const std::string &flag, const std::string &name, |
| 447 |
const std::string &desc, bool req, bool valreq, Visitor *v) |
| 448 |
: _flag(flag), |
| 449 |
_name(name), |
| 450 |
_description(desc), |
| 451 |
_required(req), |
| 452 |
_requireLabel("required"), |
| 453 |
_valueRequired(valreq), |
| 454 |
_alreadySet(false), |
| 455 |
_setBy(), |
| 456 |
_visitor(v), |
| 457 |
_ignoreable(true), |
| 458 |
_acceptsMultipleValues(false), |
| 459 |
_visibleInHelp(true) { |
| 460 |
if (_flag.length() > 1) |
| 461 |
throw(SpecificationException( |
| 462 |
"Argument flag can only be one character long", toString())); |
| 463 |
|
| 464 |
if (_name != ignoreNameString() && |
| 465 |
(_flag == Arg::flagStartString() || _flag == Arg::nameStartString() || |
| 466 |
_flag == " ")) |
| 467 |
throw(SpecificationException( |
| 468 |
"Argument flag cannot be either '" + Arg::flagStartString() + |
| 469 |
"' or '" + Arg::nameStartString() + "' or a space.", |
| 470 |
toString())); |
| 471 |
|
| 472 |
if ((_name.substr(0, Arg::flagStartString().length()) == |
| 473 |
Arg::flagStartString()) || |
| 474 |
(_name.substr(0, Arg::nameStartString().length()) == |
| 475 |
Arg::nameStartString()) || |
| 476 |
(_name.find(" ", 0) != std::string::npos)) |
| 477 |
throw(SpecificationException("Argument name begin with either '" + |
| 478 |
Arg::flagStartString() + "' or '" + |
| 479 |
Arg::nameStartString() + "' or space.", |
| 480 |
toString())); |
| 481 |
} |
| 482 |
|
| 483 |
inline Arg::~Arg() {} |
| 484 |
|
| 485 |
inline std::string Arg::shortID(const std::string &valueId) const { |
| 486 |
std::string id = ""; |
| 487 |
|
| 488 |
if (_flag != "") |
| 489 |
id = Arg::flagStartString() + _flag; |
| 490 |
else |
| 491 |
id = Arg::nameStartString() + _name; |
| 492 |
|
| 493 |
if (_valueRequired) id += std::string(1, Arg::delimiter()) + valueId; |
| 494 |
|
| 495 |
return id; |
| 496 |
} |
| 497 |
|
| 498 |
inline std::string Arg::longID(const std::string &valueId) const { |
| 499 |
std::string id = ""; |
| 500 |
|
| 501 |
if (_flag != "") { |
| 502 |
id += Arg::flagStartString() + _flag; |
| 503 |
|
| 504 |
if (_valueRequired) id += std::string(1, Arg::delimiter()) + valueId; |
| 505 |
|
| 506 |
id += ", "; |
| 507 |
} |
| 508 |
|
| 509 |
id += Arg::nameStartString() + _name; |
| 510 |
|
| 511 |
if (_valueRequired) id += std::string(1, Arg::delimiter()) + valueId; |
| 512 |
|
| 513 |
return id; |
| 514 |
} |
| 515 |
|
| 516 |
inline bool Arg::operator==(const Arg &a) const { |
| 517 |
if ((_flag != "" && _flag == a._flag) || _name == a._name) |
| 518 |
return true; |
| 519 |
else |
| 520 |
return false; |
| 521 |
} |
| 522 |
|
| 523 |
inline const std::string &Arg::getFlag() const { return _flag; } |
| 524 |
|
| 525 |
inline const std::string &Arg::getName() const { return _name; } |
| 526 |
|
| 527 |
inline bool Arg::isRequired() const { return _required; } |
| 528 |
|
| 529 |
inline bool Arg::isValueRequired() const { return _valueRequired; } |
| 530 |
|
| 531 |
inline bool Arg::isSet() const { return _alreadySet; } |
| 532 |
|
| 533 |
inline bool Arg::isIgnoreable() const { return _ignoreable; } |
| 534 |
|
| 535 |
inline bool Arg::argMatches(const std::string &argFlag) const { |
| 536 |
if ((argFlag == Arg::flagStartString() + _flag && _flag != "") || |
| 537 |
argFlag == Arg::nameStartString() + _name) |
| 538 |
return true; |
| 539 |
else |
| 540 |
return false; |
| 541 |
} |
| 542 |
|
| 543 |
inline std::string Arg::toString() const { |
| 544 |
std::string s = ""; |
| 545 |
|
| 546 |
if (_flag != "") s += Arg::flagStartString() + _flag + " "; |
| 547 |
|
| 548 |
s += "(" + Arg::nameStartString() + _name + ")"; |
| 549 |
|
| 550 |
return s; |
| 551 |
} |
| 552 |
|
| 553 |
inline void Arg::_checkWithVisitor() const { |
| 554 |
if (_visitor != NULL) _visitor->visit(); |
| 555 |
} |
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
inline void Arg::trimFlag(std::string &flag, std::string &value) const { |
| 561 |
int stop = 0; |
| 562 |
for (int i = 0; static_cast<unsigned int>(i) < flag.length(); i++) |
| 563 |
if (flag[i] == Arg::delimiter()) { |
| 564 |
stop = i; |
| 565 |
break; |
| 566 |
} |
| 567 |
|
| 568 |
if (stop > 1) { |
| 569 |
value = flag.substr(stop + 1); |
| 570 |
flag = flag.substr(0, stop); |
| 571 |
} |
| 572 |
} |
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
inline bool Arg::_hasBlanks(const std::string &s) const { |
| 578 |
for (int i = 1; static_cast<unsigned int>(i) < s.length(); i++) |
| 579 |
if (s[i] == Arg::blankChar()) return true; |
| 580 |
|
| 581 |
return false; |
| 582 |
} |
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
inline void Arg::addToList(std::list<Arg *> &argList) const { |
| 588 |
argList.push_front(const_cast<Arg *>(this)); |
| 589 |
} |
| 590 |
|
| 591 |
inline bool Arg::allowMore() { return false; } |
| 592 |
|
| 593 |
inline bool Arg::acceptsMultipleValues() { return _acceptsMultipleValues; } |
| 594 |
|
| 595 |
inline void Arg::reset() { _alreadySet = false; } |
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
} |
| 602 |
|
| 603 |
#endif |