| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
#ifndef TCLAP_STD_OUTPUT_H |
| 25 |
#define TCLAP_STD_OUTPUT_H |
| 26 |
|
| 27 |
#include <tclap/Arg.h> |
| 28 |
#include <tclap/ArgGroup.h> |
| 29 |
#include <tclap/CmdLineInterface.h> |
| 30 |
#include <tclap/CmdLineOutput.h> |
| 31 |
|
| 32 |
#include <algorithm> |
| 33 |
#include <cctype> |
| 34 |
#include <cstddef> |
| 35 |
#include <iostream> |
| 36 |
#include <list> |
| 37 |
#include <string> |
| 38 |
#include <utility> |
| 39 |
#include <vector> |
| 40 |
|
| 41 |
namespace TCLAP { |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
class StdOutput : public CmdLineOutput { |
| 48 |
public: |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
virtual void usage(CmdLineInterface &c); |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
virtual void version(CmdLineInterface &c); |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
virtual void failure(CmdLineInterface &c, ArgException &e); |
| 70 |
|
| 71 |
protected: |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
void _shortUsage(CmdLineInterface &c, std::ostream &os) const; |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
void _longUsage(CmdLineInterface &c, std::ostream &os) const; |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
void spacePrint(std::ostream &os, const std::string &s, int maxWidth, |
| 99 |
int indentSpaces, int secondLineOffset) const; |
| 100 |
}; |
| 101 |
|
| 102 |
inline void StdOutput::version(CmdLineInterface &_cmd) { |
| 103 |
std::string progName = _cmd.getProgramName(); |
| 104 |
std::string xversion = _cmd.getVersion(); |
| 105 |
|
| 106 |
std::cout << std::endl |
| 107 |
<< progName << " version: " << xversion << std::endl |
| 108 |
<< std::endl; |
| 109 |
} |
| 110 |
|
| 111 |
inline void StdOutput::usage(CmdLineInterface &_cmd) { |
| 112 |
std::cout << std::endl << "USAGE: " << std::endl << std::endl; |
| 113 |
|
| 114 |
_shortUsage(_cmd, std::cout); |
| 115 |
|
| 116 |
std::cout << std::endl << std::endl << "Where: " << std::endl << std::endl; |
| 117 |
|
| 118 |
_longUsage(_cmd, std::cout); |
| 119 |
|
| 120 |
std::cout << std::endl; |
| 121 |
} |
| 122 |
|
| 123 |
inline void StdOutput::failure(CmdLineInterface &_cmd, ArgException &e) { |
| 124 |
std::string progName = _cmd.getProgramName(); |
| 125 |
|
| 126 |
std::cerr << "PARSE ERROR: " << e.argId() << std::endl |
| 127 |
<< " " << e.error() << std::endl |
| 128 |
<< std::endl; |
| 129 |
|
| 130 |
if (_cmd.hasHelpAndVersion()) { |
| 131 |
std::cerr << "Brief USAGE: " << std::endl; |
| 132 |
|
| 133 |
_shortUsage(_cmd, std::cerr); |
| 134 |
|
| 135 |
std::cerr << std::endl |
| 136 |
<< "For complete USAGE and HELP type: " << std::endl |
| 137 |
<< " " << progName << " " << Arg::nameStartString() |
| 138 |
<< "help" << std::endl |
| 139 |
<< std::endl; |
| 140 |
} else { |
| 141 |
usage(_cmd); |
| 142 |
} |
| 143 |
|
| 144 |
throw ExitException(1); |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
inline void removeChar(std::string &s, char r) { |
| 149 |
size_t p; |
| 150 |
while ((p = s.find_first_of(r)) != std::string::npos) { |
| 151 |
s.erase(p, 1); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
inline bool cmpSwitch(const char &a, const char &b) { |
| 156 |
int lowa = std::tolower(a); |
| 157 |
int lowb = std::tolower(b); |
| 158 |
|
| 159 |
if (lowa == lowb) { |
| 160 |
return a < b; |
| 161 |
} |
| 162 |
|
| 163 |
return lowa < lowb; |
| 164 |
} |
| 165 |
|
| 166 |
namespace internal { |
| 167 |
inline bool IsVisibleShortSwitch(const Arg &arg) { |
| 168 |
return !(arg.getName() == Arg::ignoreNameString() || |
| 169 |
arg.isValueRequired() || arg.getFlag() == "") && |
| 170 |
arg.visibleInHelp(); |
| 171 |
} |
| 172 |
|
| 173 |
inline bool IsVisibleLongSwitch(const Arg &arg) { |
| 174 |
return (arg.getName() != Arg::ignoreNameString() && |
| 175 |
!arg.isValueRequired() && arg.getFlag() == "" && |
| 176 |
arg.visibleInHelp()); |
| 177 |
} |
| 178 |
|
| 179 |
inline bool IsVisibleOption(const Arg &arg) { |
| 180 |
return (arg.getName() != Arg::ignoreNameString() && arg.isValueRequired() && |
| 181 |
arg.hasLabel() && arg.visibleInHelp()); |
| 182 |
} |
| 183 |
|
| 184 |
inline bool CompareShortID(const Arg *a, const Arg *b) { |
| 185 |
if (a->getFlag() == "" && b->getFlag() != "") { |
| 186 |
return false; |
| 187 |
} |
| 188 |
if (b->getFlag() == "" && a->getFlag() != "") { |
| 189 |
return true; |
| 190 |
} |
| 191 |
|
| 192 |
return a->shortID() < b->shortID(); |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
inline bool CompareOptions(std::pair<const Arg *, bool> a, |
| 197 |
std::pair<const Arg *, bool> b) { |
| 198 |
|
| 199 |
if (!a.second && b.second) { |
| 200 |
return true; |
| 201 |
} |
| 202 |
if (a.second && !b.second) { |
| 203 |
return false; |
| 204 |
} |
| 205 |
|
| 206 |
return CompareShortID(a.first, b.first); |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
inline void StdOutput::_shortUsage(CmdLineInterface &_cmd, |
| 226 |
std::ostream &os) const { |
| 227 |
std::list<ArgGroup *> argSets = _cmd.getArgGroups(); |
| 228 |
|
| 229 |
std::ostringstream outp; |
| 230 |
outp << _cmd.getProgramName() + " "; |
| 231 |
|
| 232 |
std::string switches = Arg::flagStartString(); |
| 233 |
|
| 234 |
std::list<ArgGroup *> exclusiveGroups; |
| 235 |
std::list<ArgGroup *> nonExclusiveGroups; |
| 236 |
for (std::list<ArgGroup *>::iterator sit = argSets.begin(); |
| 237 |
sit != argSets.end(); ++sit) { |
| 238 |
if (CountVisibleArgs(**sit) <= 0) { |
| 239 |
continue; |
| 240 |
} |
| 241 |
|
| 242 |
if ((*sit)->isExclusive()) { |
| 243 |
exclusiveGroups.push_back(*sit); |
| 244 |
} else { |
| 245 |
nonExclusiveGroups.push_back(*sit); |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
for (std::list<ArgGroup *>::iterator it = exclusiveGroups.begin(); |
| 254 |
it != exclusiveGroups.end();) { |
| 255 |
if (CountVisibleArgs(**it) < 2) { |
| 256 |
nonExclusiveGroups.push_back(*it); |
| 257 |
it = exclusiveGroups.erase(it); |
| 258 |
} else { |
| 259 |
++it; |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
for (std::list<ArgGroup *>::iterator sit = nonExclusiveGroups.begin(); |
| 266 |
sit != nonExclusiveGroups.end(); ++sit) { |
| 267 |
for (ArgGroup::iterator it = (*sit)->begin(); it != (*sit)->end(); |
| 268 |
++it) { |
| 269 |
if (internal::IsVisibleShortSwitch(**it)) { |
| 270 |
switches += (*it)->getFlag(); |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
std::sort(switches.begin(), switches.end(), cmpSwitch); |
| 275 |
} |
| 276 |
|
| 277 |
outp << " [" << switches << ']'; |
| 278 |
|
| 279 |
|
| 280 |
std::vector<Arg *> longSwitches; |
| 281 |
for (std::list<ArgGroup *>::iterator sit = nonExclusiveGroups.begin(); |
| 282 |
sit != nonExclusiveGroups.end(); ++sit) { |
| 283 |
for (ArgGroup::iterator it = (*sit)->begin(); it != (*sit)->end(); |
| 284 |
++it) { |
| 285 |
Arg &arg = **it; |
| 286 |
if (internal::IsVisibleLongSwitch(arg)) { |
| 287 |
longSwitches.push_back(&arg); |
| 288 |
} |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
std::sort(longSwitches.begin(), longSwitches.end(), |
| 293 |
internal::CompareShortID); |
| 294 |
for (std::vector<Arg *>::const_iterator it = longSwitches.begin(); |
| 295 |
it != longSwitches.end(); ++it) { |
| 296 |
outp << " [" << (**it).shortID() << ']'; |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
for (std::list<ArgGroup *>::iterator sit = exclusiveGroups.begin(); |
| 301 |
sit != exclusiveGroups.end(); ++sit) { |
| 302 |
ArgGroup &argGroup = **sit; |
| 303 |
outp << (argGroup.isRequired() ? " {" : " ["); |
| 304 |
|
| 305 |
std::vector<Arg *> args; |
| 306 |
for (ArgGroup::iterator it = argGroup.begin(); it != argGroup.end(); |
| 307 |
++it) { |
| 308 |
if ((**it).visibleInHelp()) { |
| 309 |
args.push_back(*it); |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
std::sort(args.begin(), args.end(), internal::CompareShortID); |
| 314 |
std::string sep = ""; |
| 315 |
for (std::vector<Arg *>::const_iterator it = args.begin(); |
| 316 |
it != args.end(); ++it) { |
| 317 |
outp << sep << (**it).shortID(); |
| 318 |
sep = "|"; |
| 319 |
} |
| 320 |
|
| 321 |
outp << (argGroup.isRequired() ? '}' : ']'); |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
std::vector<std::pair<const Arg *, bool> > options; |
| 326 |
for (std::list<ArgGroup *>::iterator sit = nonExclusiveGroups.begin(); |
| 327 |
sit != nonExclusiveGroups.end(); ++sit) { |
| 328 |
for (ArgGroup::iterator it = (*sit)->begin(); it != (*sit)->end(); |
| 329 |
++it) { |
| 330 |
Arg &arg = **it; |
| 331 |
int visible = CountVisibleArgs(**sit); |
| 332 |
bool required = arg.isRequired(); |
| 333 |
if (internal::IsVisibleOption(arg)) { |
| 334 |
if (visible == 1 && (**sit).isRequired()) { |
| 335 |
required = true; |
| 336 |
} |
| 337 |
|
| 338 |
options.push_back(std::make_pair(&arg, required)); |
| 339 |
} |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
std::sort(options.begin(), options.end(), internal::CompareOptions); |
| 344 |
for (std::vector<std::pair<const Arg *, bool> >::const_iterator it = |
| 345 |
options.begin(); |
| 346 |
it != options.end(); ++it) { |
| 347 |
const Arg &arg = *it->first; |
| 348 |
bool required = it->second; |
| 349 |
outp << (required ? " " : " ["); |
| 350 |
outp << arg.shortID(); |
| 351 |
outp << (required ? "" : "]"); |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
for (std::list<ArgGroup *>::iterator sit = nonExclusiveGroups.begin(); |
| 356 |
sit != nonExclusiveGroups.end(); ++sit) { |
| 357 |
for (ArgGroup::iterator it = (*sit)->begin(); it != (*sit)->end(); |
| 358 |
++it) { |
| 359 |
Arg &arg = **it; |
| 360 |
if (arg.getName() == Arg::ignoreNameString()) { |
| 361 |
continue; |
| 362 |
} |
| 363 |
|
| 364 |
if (arg.isValueRequired() && !arg.hasLabel() && |
| 365 |
arg.visibleInHelp()) { |
| 366 |
outp << (arg.isRequired() ? " " : " ["); |
| 367 |
outp << arg.shortID(); |
| 368 |
outp << (arg.isRequired() ? "" : "]"); |
| 369 |
} |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
|
| 374 |
int secondLineOffset = static_cast<int>(_cmd.getProgramName().length()) + 2; |
| 375 |
if (secondLineOffset > 75 / 2) secondLineOffset = static_cast<int>(75 / 2); |
| 376 |
|
| 377 |
spacePrint(os, outp.str(), 75, 3, secondLineOffset); |
| 378 |
} |
| 379 |
|
| 380 |
inline void StdOutput::_longUsage(CmdLineInterface &_cmd, |
| 381 |
std::ostream &os) const { |
| 382 |
std::string message = _cmd.getMessage(); |
| 383 |
std::list<ArgGroup *> argSets = _cmd.getArgGroups(); |
| 384 |
|
| 385 |
std::list<Arg *> unlabled; |
| 386 |
for (std::list<ArgGroup *>::iterator sit = argSets.begin(); |
| 387 |
sit != argSets.end(); ++sit) { |
| 388 |
ArgGroup &argGroup = **sit; |
| 389 |
|
| 390 |
int visible = CountVisibleArgs(argGroup); |
| 391 |
bool exclusive = visible > 1 && argGroup.isExclusive(); |
| 392 |
bool forceRequired = visible == 1 && argGroup.isRequired(); |
| 393 |
if (exclusive) { |
| 394 |
spacePrint(os, argGroup.isRequired() ? "One of:" : "Either of:", 75, |
| 395 |
3, 0); |
| 396 |
} |
| 397 |
|
| 398 |
for (ArgGroup::iterator it = argGroup.begin(); it != argGroup.end(); |
| 399 |
++it) { |
| 400 |
Arg &arg = **it; |
| 401 |
if (!arg.visibleInHelp()) { |
| 402 |
continue; |
| 403 |
} |
| 404 |
|
| 405 |
if (!arg.hasLabel()) { |
| 406 |
unlabled.push_back(&arg); |
| 407 |
continue; |
| 408 |
} |
| 409 |
|
| 410 |
bool required = arg.isRequired() || forceRequired; |
| 411 |
if (exclusive) { |
| 412 |
spacePrint(os, arg.longID(), 75, 6, 3); |
| 413 |
spacePrint(os, arg.getDescription(required), 75, 8, 0); |
| 414 |
} else { |
| 415 |
spacePrint(os, arg.longID(), 75, 3, 3); |
| 416 |
spacePrint(os, arg.getDescription(required), 75, 5, 0); |
| 417 |
} |
| 418 |
os << '\n'; |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
for (ArgListIterator it = unlabled.begin(); it != unlabled.end(); ++it) { |
| 423 |
const Arg &arg = **it; |
| 424 |
spacePrint(os, arg.longID(), 75, 3, 3); |
| 425 |
spacePrint(os, arg.getDescription(), 75, 5, 0); |
| 426 |
os << '\n'; |
| 427 |
} |
| 428 |
|
| 429 |
if (!message.empty()) { |
| 430 |
spacePrint(os, message, 75, 3, 0); |
| 431 |
} |
| 432 |
|
| 433 |
os.flush(); |
| 434 |
} |
| 435 |
|
| 436 |
namespace { |
| 437 |
inline void fmtPrintLine(std::ostream &os, const std::string &s, int maxWidth, |
| 438 |
int indentSpaces, int secondLineOffset) { |
| 439 |
const std::string splitChars(" ,|"); |
| 440 |
size_t maxChars = std::max(maxWidth - indentSpaces, 0); |
| 441 |
std::string indentString(indentSpaces, ' '); |
| 442 |
size_t from = 0; |
| 443 |
size_t to = 0; |
| 444 |
size_t end = s.length(); |
| 445 |
for (;;) { |
| 446 |
if (end - from <= maxChars) { |
| 447 |
|
| 448 |
os << indentString << s.substr(from) << std::endl; |
| 449 |
return; |
| 450 |
} |
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
size_t tooFar = to; |
| 456 |
while (tooFar - from <= maxChars && tooFar != std::string::npos) { |
| 457 |
to = tooFar; |
| 458 |
tooFar = s.find_first_of(splitChars, to + 1); |
| 459 |
} |
| 460 |
|
| 461 |
if (to == from) { |
| 462 |
|
| 463 |
|
| 464 |
to = from + maxChars - 1; |
| 465 |
} |
| 466 |
|
| 467 |
if (s[to] != ' ') { |
| 468 |
|
| 469 |
to++; |
| 470 |
} |
| 471 |
|
| 472 |
os << indentString << s.substr(from, to - from) << '\n'; |
| 473 |
|
| 474 |
|
| 475 |
for (; s[to] == ' '; to++) { |
| 476 |
} |
| 477 |
from = to; |
| 478 |
|
| 479 |
if (secondLineOffset != 0) { |
| 480 |
|
| 481 |
indentString.insert(indentString.end(), secondLineOffset, ' '); |
| 482 |
maxChars -= secondLineOffset; |
| 483 |
secondLineOffset = 0; |
| 484 |
} |
| 485 |
} |
| 486 |
} |
| 487 |
} |
| 488 |
|
| 489 |
inline void StdOutput::spacePrint(std::ostream &os, const std::string &s, |
| 490 |
int maxWidth, int indentSpaces, |
| 491 |
int secondLineOffset) const { |
| 492 |
std::stringstream ss(s); |
| 493 |
std::string line; |
| 494 |
std::getline(ss, line); |
| 495 |
fmtPrintLine(os, line, maxWidth, indentSpaces, secondLineOffset); |
| 496 |
indentSpaces += secondLineOffset; |
| 497 |
|
| 498 |
while (std::getline(ss, line)) { |
| 499 |
fmtPrintLine(os, line, maxWidth, indentSpaces, 0); |
| 500 |
} |
| 501 |
} |
| 502 |
|
| 503 |
} |
| 504 |
|
| 505 |
#endif |