24#ifndef TCLAP_STD_OUTPUT_H
25#define TCLAP_STD_OUTPUT_H
100 void spacePrint(std::ostream &os,
const std::string &s,
int maxWidth,
101 int indentSpaces,
int secondLineOffset)
const;
104 static constexpr int _usageWidth = 75;
106 void _partitionGroups(std::list<ArgGroup *> &groups,
107 std::list<ArgGroup *> &exclusiveGroups,
108 std::list<ArgGroup *> &nonExclusiveGroups)
const;
109 void _printShortSwitches(std::ostream &os, std::string &switches,
110 std::list<ArgGroup *> &groups)
const;
111 void _printLongSwitches(std::ostream &os,
112 std::list<ArgGroup *> &groups)
const;
113 void _printShortOptions(std::ostream &os,
114 std::list<ArgGroup *> &groups)
const;
116 std::list<ArgGroup *> &groups)
const;
118 std::list<ArgGroup *> &groups)
const;
120 std::list<ArgGroup *> &groups)
const;
125 void _printLongEntry(std::ostream &os,
const Arg &arg,
bool required,
137 std::cout << std::format(
142 std::cout << std::format(
147 std::cout <<
'\n' << std::flush;
151 std::cerr << std::format(
157 std::cerr << std::format(
162 std::cerr << std::format(
163 "\n{}\n {} {}help\n\n",
165 "complete_usage_hint",
166 "For complete USAGE and HELP type: "),
179 while ((p = s.find_first_of(r)) != std::string::npos) {
185 int lowa = std::tolower(a);
186 int lowb = std::tolower(b);
196inline bool IsVisibleShortSwitch(
const Arg &arg) {
198 arg.isValueRequired() || arg.getFlag() ==
"") &&
202inline bool IsVisibleLongSwitch(
const Arg &arg) {
204 !arg.isValueRequired() && arg.getFlag() ==
"" &&
205 arg.visibleInHelp());
208inline bool IsVisibleOption(
const Arg &arg) {
210 arg.hasLabel() && arg.visibleInHelp());
213inline bool CompareShortID(
const Arg *a,
const Arg *b) {
214 if (a->getFlag() ==
"" && b->getFlag() !=
"") {
217 if (b->getFlag() ==
"" && a->getFlag() !=
"") {
221 return a->shortID() < b->shortID();
225inline bool CompareOptions(std::pair<const Arg *, bool> a,
226 std::pair<const Arg *, bool> b) {
228 if (!a.second && b.second) {
231 if (a.second && !b.second) {
235 return CompareShortID(a.first, b.first);
238inline ArgGroup *FindArgGroup(
const std::list<ArgGroup *> &groups,
240 for (ArgGroup *group : groups)
241 if (std::ranges::find(*group, arg) != group->end()) return group;
245inline bool HasVisiblePositional(
const ArgGroup &group) {
246 for (
const Arg *arg : group)
247 if (!arg->hasLabel() && arg->visibleInHelp()) return true;
251inline void PrintExclusiveShortGroup(std::ostream &os,
const ArgGroup &group) {
252 os << (group.isRequired() ?
" {" :
" [");
253 std::vector<Arg *> args;
254 for (Arg *arg : group)
255 if (arg->visibleInHelp()) args.push_back(arg);
256 std::ranges::sort(args, CompareShortID);
257 const char *separator =
"";
258 for (
const Arg *arg : args) {
259 os << separator << arg->shortID();
262 os << (group.isRequired() ?
'}' :
']');
282 std::ostream &os)
const {
285 std::ostringstream outp;
289 std::list<ArgGroup *> exclusiveGroups;
290 std::list<ArgGroup *> nonExclusiveGroups;
291 _partitionGroups(argSets, exclusiveGroups, nonExclusiveGroups);
292 _printShortSwitches(outp, switches, nonExclusiveGroups);
293 _printLongSwitches(outp, nonExclusiveGroups);
296 for (
const ArgGroup *group : exclusiveGroups) {
297 if (!internal::HasVisiblePositional(*group))
298 internal::PrintExclusiveShortGroup(outp, *group);
301 _printShortOptions(outp, nonExclusiveGroups);
302 _printShortOperands(_cmd, outp, argSets);
305 int secondLineOffset =
static_cast<int>(_cmd.
getProgramName().length()) + 2;
306 if (secondLineOffset > _usageWidth / 2)
307 secondLineOffset =
static_cast<int>(_usageWidth / 2);
309 spacePrint(os, outp.str(), _usageWidth, 3, secondLineOffset);
312inline void StdOutput::_partitionGroups(
313 std::list<ArgGroup *> &groups, std::list<ArgGroup *> &exclusiveGroups,
314 std::list<ArgGroup *> &nonExclusiveGroups)
const {
320 if (groupPtr->isExclusive()) {
321 exclusiveGroups.push_back(groupPtr);
323 nonExclusiveGroups.push_back(groupPtr);
331 for (
auto it = exclusiveGroups.begin(); it != exclusiveGroups.end();) {
333 nonExclusiveGroups.push_back(*it);
334 it = exclusiveGroups.erase(it);
341inline void StdOutput::_printShortSwitches(
342 std::ostream &os, std::string &switches,
343 std::list<ArgGroup *> &groups)
const {
344 bool hasShortSwitches =
false;
348 for (ArgGroup *groupPtr : groups) {
349 for (Arg *argPtr : *groupPtr) {
350 if (internal::IsVisibleShortSwitch(*argPtr)) {
351 switches += argPtr->getFlag();
352 hasShortSwitches =
true;
359 if (hasShortSwitches) os <<
" [" << switches <<
']';
362inline void StdOutput::_printLongSwitches(std::ostream &os,
363 std::list<ArgGroup *> &groups)
const {
365 std::vector<Arg *> longSwitches;
366 for (ArgGroup *groupPtr : groups) {
367 for (Arg *argPtr : *groupPtr) {
369 if (internal::IsVisibleLongSwitch(arg)) {
370 longSwitches.push_back(&arg);
375 std::ranges::sort(longSwitches, internal::CompareShortID);
376 for (
const Arg *arg : longSwitches) {
377 os <<
" [" << arg->shortID() <<
']';
381inline void StdOutput::_printShortOptions(std::ostream &os,
382 std::list<ArgGroup *> &groups)
const {
384 std::vector<std::pair<const Arg *, bool> > options;
385 for (ArgGroup *groupPtr : groups) {
386 for (Arg *argPtr : *groupPtr) {
389 bool required = arg.isRequired();
390 if (internal::IsVisibleOption(arg)) {
391 if (visible == 1 && groupPtr->isRequired()) {
395 options.emplace_back(&arg, required);
400 std::ranges::sort(options, internal::CompareOptions);
401 for (
const auto &[argPtr, required] : options) {
402 const Arg &arg = *argPtr;
403 os << (required ?
" " :
" [");
405 os << (required ?
"" :
"]");
409inline void StdOutput::_printShortOperands(
410 CmdLineInterface &cmd, std::ostream &os,
411 std::list<ArgGroup *> &groups)
const {
413 const std::list<Arg *> operands = cmd.getArgList();
414 std::list<ArgGroup *> shown;
415 for (
const Arg *argPtr : operands) {
416 const Arg &arg = *argPtr;
417 if (arg.hasLabel() || !arg.isValueRequired() || !arg.visibleInHelp())
419 ArgGroup *group = internal::FindArgGroup(groups, &arg);
421 if (std::ranges::find(shown, group) == shown.end()) {
422 internal::PrintExclusiveShortGroup(os, *group);
423 shown.push_back(group);
427 bool required = arg.isRequired() ||
429 os << (required ?
" " :
" [") << arg.shortID() << (required ?
"" :
"]");
434 std::ostream &os)
const {
438 _printLabeledGroups(_cmd, os, argSets);
439 _printLongOperands(_cmd, os, argSets);
441 if (!message.empty()) {
448inline void StdOutput::_printLabeledGroups(
450 std::list<ArgGroup *> &groups)
const {
455 bool exclusive = visible > 1 && argGroup.
isExclusive();
456 if (exclusive && internal::HasVisiblePositional(argGroup))
continue;
457 bool forceRequired = visible == 1 && argGroup.
isRequired();
459 _printGroupHeading(cmd, os, argGroup);
462 for (Arg *argPtr : argGroup) {
464 if (!arg.visibleInHelp()) {
468 if (!arg.hasLabel()) {
472 bool required = arg.isRequired() || forceRequired;
473 _printLongEntry(os, arg, required, exclusive ? 6 : 3);
478inline void StdOutput::_printLongOperands(CmdLineInterface &cmd,
480 std::list<ArgGroup *> &groups)
const {
481 const std::list<Arg *> operands = cmd.getArgList();
482 std::list<ArgGroup *> shown;
483 for (
const Arg *argPtr : operands) {
484 const Arg &arg = *argPtr;
485 if (arg.hasLabel() || !arg.visibleInHelp())
continue;
486 ArgGroup *group = internal::FindArgGroup(groups, &arg);
488 if (std::ranges::find(shown, group) != shown.end())
continue;
489 shown.push_back(group);
490 _printExclusiveLongGroup(cmd, os, *group);
493 const bool required = arg.isRequired() ||
495 _printLongEntry(os, arg, required, 3);
499inline void StdOutput::_printExclusiveLongGroup(CmdLineInterface &cmd,
501 ArgGroup &group)
const {
502 _printGroupHeading(cmd, os, group);
503 for (
const Arg *member : group) {
504 if (!member->visibleInHelp())
continue;
506 spacePrint(os, member->longID(), _usageWidth, 6, 3);
507 spacePrint(os, member->getDescription(member->isRequired()),
513inline void StdOutput::_printGroupHeading(CmdLineInterface &cmd,
515 const ArgGroup &group)
const {
518 ? cmd.translateMessage(
"one_of_group",
"One of:")
519 : cmd.translateMessage(
"either_of_group",
"Either of:"),
523inline void StdOutput::_printLongEntry(std::ostream &os,
const Arg &arg,
524 bool required,
int indent)
const {
525 spacePrint(os, arg.longID(), _usageWidth, indent, 3);
526 spacePrint(os, arg.getDescription(required), _usageWidth, indent + 2, 0);
531inline void fmtPrintLine(std::ostream &os,
const std::string &text,
532 int maxWidth,
int indentSpaces,
int secondLineOffset) {
533 const std::string breakCharacters(
" ,|");
535 indentSpaces = std::max(indentSpaces, 0);
536 secondLineOffset = std::max(secondLineOffset, 0);
537 size_t availableWidth = std::max(maxWidth - indentSpaces, 0);
538 std::string indentString(indentSpaces,
' ');
539 size_t lineStart = 0;
541 size_t textLength = text.length();
543 if (textLength - lineStart <= availableWidth) {
545 os << indentString << text.substr(lineStart) << std::endl;
550 size_t nextBreak = lineEnd;
551 while (nextBreak - lineStart <= availableWidth &&
552 nextBreak != std::string::npos) {
554 nextBreak = text.find_first_of(breakCharacters, lineEnd + 1);
557 if (lineEnd == lineStart) {
561 availableWidth > 0 ? lineStart + availableWidth - 1 : lineStart;
564 if (text[lineEnd] !=
' ') {
569 os << indentString << text.substr(lineStart, lineEnd - lineStart)
573 for (; text[lineEnd] ==
' '; lineEnd++) {
577 if (secondLineOffset != 0) {
579 indentString.insert(indentString.end(), secondLineOffset,
' ');
582 std::min(availableWidth,
static_cast<size_t>(secondLineOffset));
583 secondLineOffset = 0;
590 int maxWidth,
int indentSpaces,
591 int secondLineOffset)
const {
592 std::stringstream ss(s);
594 std::getline(ss, line);
595 fmtPrintLine(os, line, maxWidth, indentSpaces, secondLineOffset);
596 indentSpaces += secondLineOffset;
598 while (std::getline(ss, line)) {
599 fmtPrintLine(os, line, maxWidth, indentSpaces, 0);
A simple class that defines and argument exception.
std::string argId() const
Returns the argument id.
std::string error() const
Returns the error text.
ArgGroup is the base class for implementing groups of arguments that are mutually exclusive (it repla...
virtual bool isExclusive() const =0
Returns true if this argument group is exclusive.
virtual bool isRequired() const =0
Returns true if this argument group is required.
A virtual base class that defines the essential data for all arguments.
static std::string flagStartString()
static std::string nameStartString()
static std::string ignoreNameString()
The name used to identify the ignore rest argument.
The base class that manages the command line definition and passes along the parsing to the appropria...
virtual std::string getVersion() const =0
Returns the version string.
virtual std::string getMessage() const =0
Returns the message string.
virtual bool hasHelpAndVersion() const =0
Indicates whether or not the help and version switches were created automatically.
virtual std::string translateMessage(const std::string &messageId, const std::string &fallback) const =0
Translates a built-in message using the current translator.
virtual std::string getProgramName() const =0
Returns the program name string.
virtual std::list< ArgGroup * > getArgGroups()=0
Returns the list of ArgGroups.
The interface that any output object must implement.
Thrown when TCLAP thinks the program should exit.
A class that isolates any output from the CmdLine object so that it may be easily modified.
void failure(CmdLineInterface &c, ArgException &e) override
Prints (to stderr) an error message, short usage Can be overridden to produce alternative behavior.
void usage(CmdLineInterface &c) override
Prints the usage to stdout.
void spacePrint(std::ostream &os, const std::string &s, int maxWidth, int indentSpaces, int secondLineOffset) const
This function inserts line breaks and indents long strings according the params input.
void _shortUsage(CmdLineInterface &c, std::ostream &os) const
Writes a brief usage message with short args.
void _longUsage(CmdLineInterface &c, std::ostream &os) const
Writes a longer usage message with long and short args, provides descriptions and prints message.
void version(CmdLineInterface &c) override
Prints the version to stdout.
int CountVisibleArgs(const ArgGroup &g)
void removeChar(std::string &s, char r)
bool cmpSwitch(const char &a, const char &b)