Debian Codegraph @

podofo/3rdparty/tclap/Arg.h

raw symbols SCIP
1 // -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*-
2
3 /******************************************************************************
4 *
5 * file: Arg.h
6 *
7 * Copyright (c) 2003, Michael E. Smoot .
8 * Copyright (c) 2004, Michael E. Smoot, Daniel Aarno .
9 * Copyright (c) 2017 Google Inc.
10 * All rights reserved.
11 *
12 * See the file COPYING in the top directory of this distribution for
13 * more information.
14 *
15 * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
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 * A virtual base class that defines the essential data for all arguments.
50 * This class, or one of its existing children, must be subclassed to do
51 * anything.
52 */
53 class Arg {
54 private:
55 /**
56 * Prevent accidental copying.
57 */
58 Arg(const Arg &rhs);
59
60 /**
61 * Prevent accidental copying.
62 */
63 Arg &operator=(const Arg &rhs);
64
65 /**
66 * The delimiter that separates an argument flag/name from the
67 * value.
68 */
69 static char &delimiterRef() {
70 static char delim = ' ';
71 return delim;
72 }
73
74 protected:
75 /**
76 * The single char flag used to identify the argument.
77 * This value (preceded by a dash {-}), can be used to identify
78 * an argument on the command line. The _flag can be blank,
79 * in fact this is how unlabeled args work. Unlabeled args must
80 * override appropriate functions to get correct handling. Note
81 * that the _flag does NOT include the dash as part of the flag.
82 */
83 std::string _flag;
84
85 /**
86 * A single word namd identifying the argument.
87 * This value (preceded by two dashed {--}) can also be used
88 * to identify an argument on the command line. Note that the
89 * _name does NOT include the two dashes as part of the _name. The
90 * _name cannot be blank.
91 */
92 std::string _name;
93
94 /**
95 * Description of the argument.
96 */
97 std::string _description;
98
99 /**
100 * Indicating whether the argument is required.
101 */
102 const bool _required;
103
104 /**
105 * Label to be used in usage description. Normally set to
106 * "required", but can be changed when necessary.
107 */
108 std::string _requireLabel;
109
110 /**
111 * Indicates whether a value is required for the argument.
112 * Note that the value may be required but the argument/value
113 * combination may not be, as specified by _required.
114 */
115 bool _valueRequired;
116
117 /**
118 * Indicates whether the argument has been set.
119 * Indicates that a value on the command line has matched the
120 * name/flag of this argument and the values have been set accordingly.
121 */
122 bool _alreadySet;
123
124 /** Indicates the value specified to set this flag (like -a or --all).
125 */
126 std::string _setBy;
127
128 /**
129 * A pointer to a visitor object.
130 * The visitor allows special handling to occur as soon as the
131 * argument is matched. This defaults to NULL and should not
132 * be used unless absolutely necessary.
133 */
134 Visitor *_visitor;
135
136 /**
137 * Whether this argument can be ignored, if desired.
138 */
139 bool _ignoreable;
140
141 bool _acceptsMultipleValues;
142
143 /**
144 * Indicates if the argument is visible in the help output (e.g.,
145 * when specifying --help).
146 */
147 bool _visibleInHelp;
148
149 /**
150 * Performs the special handling described by the Visitor.
151 */
152 void _checkWithVisitor() const;
153
154 /**
155 * Primary constructor. YOU (yes you) should NEVER construct an Arg
156 * directly, this is a base class that is extended by various children
157 * that are meant to be used. Use SwitchArg, ValueArg, MultiArg,
158 * UnlabeledValueArg, or UnlabeledMultiArg instead.
159 *
160 * \param flag - The flag identifying the argument.
161 * \param name - The name identifying the argument.
162 * \param desc - The description of the argument, used in the usage.
163 * \param req - Whether the argument is required.
164 * \param valreq - Whether the a value is required for the argument.
165 * \param v - The visitor checked by the argument. Defaults to NULL.
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 * Destructor.
173 */
174 virtual ~Arg();
175
176 /**
177 * Adds this to the specified list of Args.
178 * \param argList - The list to add this to.
179 */
180 virtual void addToList(std::list<Arg *> &argList) const;
181
182 /**
183 * The delimiter that separates an argument flag/name from the
184 * value.
185 */
186 static char delimiter() { return delimiterRef(); }
187
188 /**
189 * The char used as a place holder when SwitchArgs are combined.
190 * Currently set to the bell char (ASCII 7).
191 */
192 static char blankChar() { return '\a'; }
193
194 /**
195 * The char that indicates the beginning of a flag. Defaults to '-', but
196 * clients can define TCLAP_FLAGSTARTCHAR to override.
197 */
198 #ifndef TCLAP_FLAGSTARTCHAR
199 #define TCLAP_FLAGSTARTCHAR '-'
200 #endif
201 static char flagStartChar() { return TCLAP_FLAGSTARTCHAR; }
202
203 /**
204 * The sting that indicates the beginning of a flag. Defaults to "-", but
205 * clients can define TCLAP_FLAGSTARTSTRING to override. Should be the same
206 * as TCLAP_FLAGSTARTCHAR.
207 */
208 #ifndef TCLAP_FLAGSTARTSTRING
209 #define TCLAP_FLAGSTARTSTRING "-"
210 #endif
211 static const std::string flagStartString() { return TCLAP_FLAGSTARTSTRING; }
212
213 /**
214 * The sting that indicates the beginning of a name. Defaults to "--", but
215 * clients can define TCLAP_NAMESTARTSTRING to override.
216 */
217 #ifndef TCLAP_NAMESTARTSTRING
218 #define TCLAP_NAMESTARTSTRING "--"
219 #endif
220 static const std::string nameStartString() { return TCLAP_NAMESTARTSTRING; }
221
222 /**
223 * The name used to identify the ignore rest argument.
224 */
225 static const std::string ignoreNameString() { return "ignore_rest"; }
226
227 /**
228 * Sets the delimiter for all arguments.
229 * \param c - The character that delimits flags/names from values.
230 */
231 static void setDelimiter(char c) { delimiterRef() = c; }
232
233 /**
234 * Pure virtual method meant to handle the parsing and value assignment
235 * of the string on the command line.
236 * \param i - Pointer the the current argument in the list.
237 * \param args - Mutable list of strings. What is
238 * passed in from main.
239 */
240 virtual bool processArg(int *i, std::vector<std::string> &args) = 0;
241
242 /**
243 * Operator ==.
244 * Equality operator. Must be virtual to handle unlabeled args.
245 * \param a - The Arg to be compared to this.
246 */
247 virtual bool operator==(const Arg &a) const;
248
249 /**
250 * Returns the argument flag.
251 */
252 const std::string &getFlag() const;
253
254 /**
255 * Returns the argument name.
256 */
257 const std::string &getName() const;
258
259 /**
260 * Returns the argument description.
261 */
262 std::string getDescription() const { return getDescription(_required); }
263
264 /**
265 * Returns the argument description.
266 *
267 * @param required if the argument should be treated as
268 * required when described.
269 */
270 std::string getDescription(bool required) const {
271 return (required ? "(" + _requireLabel + ") " : "") + _description;
272 }
273
274 /**
275 * Indicates whether the argument is required.
276 */
277 virtual bool isRequired() const;
278
279 /**
280 * Indicates whether a value must be specified for argument.
281 */
282 bool isValueRequired() const;
283
284 /**
285 * Indicates whether the argument has already been set. Only true
286 * if the arg has been matched on the command line.
287 */
288 bool isSet() const;
289
290 /**
291 * Returns the value specified to set this flag (like -a or --all).
292 */
293 const std::string &setBy() const { return _setBy; }
294
295 /**
296 * Indicates whether the argument can be ignored, if desired.
297 */
298 bool isIgnoreable() const;
299
300 /**
301 * A method that tests whether a string matches this argument.
302 * This is generally called by the processArg() method. This
303 * method could be re-implemented by a child to change how
304 * arguments are specified on the command line.
305 * \param s - The string to be compared to the flag/name to determine
306 * whether the arg matches.
307 */
308 virtual bool argMatches(const std::string &s) const;
309
310 /**
311 * Returns a simple string representation of the argument.
312 * Primarily for debugging.
313 */
314 virtual std::string toString() const;
315
316 /**
317 * Returns a short ID for the usage.
318 * \param valueId - The value used in the id.
319 */
320 virtual std::string shortID(const std::string &valueId = "val") const;
321
322 /**
323 * Returns a long ID for the usage.
324 * \param valueId - The value used in the id.
325 */
326 virtual std::string longID(const std::string &valueId = "val") const;
327
328 /**
329 * Trims a value off of the flag.
330 * \param flag - The string from which the flag and value will be
331 * trimmed. Contains the flag once the value has been trimmed.
332 * \param value - Where the value trimmed from the string will
333 * be stored.
334 */
335 virtual void trimFlag(std::string &flag, std::string &value) const;
336
337 /**
338 * Checks whether a given string has blank chars, indicating that
339 * it is a combined SwitchArg. If so, return true, otherwise return
340 * false.
341 * \param s - string to be checked.
342 */
343 bool _hasBlanks(const std::string &s) const;
344
345 /**
346 * Used for MultiArgs to determine whether args can still be
347 * set.
348 */
349 virtual bool allowMore();
350
351 /**
352 * Use by output classes to determine whether an Arg accepts
353 * multiple values.
354 */
355 virtual bool acceptsMultipleValues();
356
357 /**
358 * Clears the Arg object and allows it to be reused by new
359 * command lines.
360 */
361 virtual void reset();
362
363 /**
364 * Hide this argument from the help output (e.g., when
365 * specifying the --help flag or on error.
366 */
367 virtual void hideFromHelp(bool hide = true) { _visibleInHelp = !hide; }
368
369 /**
370 * Returns true if this Arg is visible in the help output.
371 */
372 virtual bool visibleInHelp() const { return _visibleInHelp; }
373
374 virtual bool hasLabel() const { return true; }
375 };
376
377 /**
378 * Typedef of an Arg list iterator.
379 */
380 typedef std::list<Arg *>::const_iterator ArgListIterator;
381
382 /**
383 * Typedef of an Arg vector iterator.
384 */
385 typedef std::vector<Arg *>::const_iterator ArgVectorIterator;
386
387 /**
388 * Typedef of a Visitor list iterator.
389 */
390 typedef std::list<Visitor *>::const_iterator VisitorListIterator;
391
392 /*
393 * Extract a value of type T from it's string representation contained
394 * in strVal. The ValueLike parameter used to select the correct
395 * specialization of ExtractValue depending on the value traits of T.
396 * ValueLike traits use operator>> to assign the value from strVal.
397 */
398 template <typename T>
399 void ExtractValue(T &destVal, const std::string &strVal, ValueLike vl) {
400 static_cast<void>(vl); // Avoid warning about unused 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 * Extract a value of type T from it's string representation contained
432 * in strVal. The ValueLike parameter used to select the correct
433 * specialization of ExtractValue depending on the value traits of T.
434 * StringLike uses assignment (operator=) to assign from strVal.
435 */
436 template <typename T>
437 void ExtractValue(T &destVal, const std::string &strVal, StringLike sl) {
438 static_cast<void>(sl); // Avoid warning about unused sl
439 SetString(destVal, strVal);
440 }
441
442 //////////////////////////////////////////////////////////////////////
443 // BEGIN Arg.cpp
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 * Implementation of trimFlag.
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 * Implementation of _hasBlanks.
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 * Overridden by Args that need to added to the end of the list.
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 // END Arg.cpp
599 //////////////////////////////////////////////////////////////////////
600
601 } // namespace TCLAP
602
603 #endif // TCLAP_ARG_H