Debian Codegraph @

podofo/3rdparty/tclap/StdOutput.h

raw symbols SCIP
1 // -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*-
2
3 /******************************************************************************
4 *
5 * file: StdOutput.h
6 *
7 * Copyright (c) 2004, Michael E. Smoot
8 * Copyright (c) 2017, Google LLC
9 * All rights reserved.
10 *
11 * See the file COPYING in the top directory of this distribution for
12 * more information.
13 *
14 * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
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 * A class that isolates any output from the CmdLine object so that it
45 * may be easily modified.
46 */
47 class StdOutput : public CmdLineOutput {
48 public:
49 /**
50 * Prints the usage to stdout. Can be overridden to
51 * produce alternative behavior.
52 * \param c - The CmdLine object the output is generated for.
53 */
54 virtual void usage(CmdLineInterface &c);
55
56 /**
57 * Prints the version to stdout. Can be overridden
58 * to produce alternative behavior.
59 * \param c - The CmdLine object the output is generated for.
60 */
61 virtual void version(CmdLineInterface &c);
62
63 /**
64 * Prints (to stderr) an error message, short usage
65 * Can be overridden to produce alternative behavior.
66 * \param c - The CmdLine object the output is generated for.
67 * \param e - The ArgException that caused the failure.
68 */
69 virtual void failure(CmdLineInterface &c, ArgException &e);
70
71 protected:
72 /**
73 * Writes a brief usage message with short args.
74 * \param c - The CmdLine object the output is generated for.
75 * \param os - The stream to write the message to.
76 */
77 void _shortUsage(CmdLineInterface &c, std::ostream &os) const;
78
79 /**
80 * Writes a longer usage message with long and short args,
81 * provides descriptions and prints message.
82 * \param c - The CmdLine object the output is generated for.
83 * \param os - The stream to write the message to.
84 */
85 void _longUsage(CmdLineInterface &c, std::ostream &os) const;
86
87 /**
88 * This function inserts line breaks and indents long strings
89 * according the params input. It will only break lines at spaces,
90 * commas and pipes.
91 * \param os - The stream to be printed to.
92 * \param s - The string to be printed.
93 * \param maxWidth - The maxWidth allowed for the output line.
94 * \param indentSpaces - The number of spaces to indent the first line.
95 * \param secondLineOffset - The number of spaces to indent the second
96 * and all subsequent lines in addition to indentSpaces.
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 // TODO: Remove this
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 // TODO: Fix me not to put --gopt before -f
196 inline bool CompareOptions(std::pair<const Arg *, bool> a,
197 std::pair<const Arg *, bool> b) {
198 // First optional, then required
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 } // namespace internal
209
210 /**
211 * Usage statements should look like the manual pages. Options w/o
212 * operands come first, in alphabetical order inside a single set of
213 * braces, upper case before lower case (AaBbCc...). Next are options
214 * with operands, in the same order, each in braces. Then required
215 * arguments in the order they are specified, followed by optional
216 * arguments in the order they are specified. A bar (`|') separates
217 * either/or options/arguments, and multiple options/arguments which
218 * are specified together are placed in a single set of braces.
219 *
220 * Use getprogname() instead of hardcoding the program name.
221 *
222 * "usage: f [-aDde] [-b b_arg] [-m m_arg] req1 req2 [opt1 [opt2]]\n"
223 * "usage: f [-a | -b] [-c [-de] [-n number]]\n"
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 // Move "exclusive groups" that have at most a single item to
250 // non-exclusive groups as exclusivit doesn't make sense with a
251 // single option. This can happen if args are hidden in help for
252 // example.
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 // First short switches (needs to be special because they are all
264 // stuck together).
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 // Now do long switches (e.g., --version, but no -v)
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 // Now do all exclusive groups
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 // Next do options, we sort them later by optional first.
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 // Next do argsuments ("unlabled") in order of definition
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 // if the program name is too long, then adjust the second line offset
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 // Rest of string fits on line, just print the remainder
448 os << indentString << s.substr(from) << std::endl;
449 return;
450 }
451
452 // Find the next place where it is good to break the string
453 // (to) by finding the place where it is too late (tooFar) and
454 // taking the previous one.
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 // In case there was no good place to break the string,
463 // just break it in the middle of a word at line length.
464 to = from + maxChars - 1;
465 }
466
467 if (s[to] != ' ') {
468 // Include delimiter before line break, unless it's a space
469 to++;
470 }
471
472 os << indentString << s.substr(from, to - from) << '\n';
473
474 // Avoid printing extra white space at start of a line
475 for (; s[to] == ' '; to++) {
476 }
477 from = to;
478
479 if (secondLineOffset != 0) {
480 // Adjust offset for following lines
481 indentString.insert(indentString.end(), secondLineOffset, ' ');
482 maxChars -= secondLineOffset;
483 secondLineOffset = 0;
484 }
485 }
486 }
487 } // namespace
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 } // namespace TCLAP
504
505 #endif // TCLAP_STD_OUTPUT_H