tclap 1.2.6
Arg.h
Go to the documentation of this file.
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
26#ifndef TCLAP_ARGUMENT_H
27#define TCLAP_ARGUMENT_H
28
29#ifdef HAVE_CONFIG_H
30#include <config.h>
31#endif
32
33#include <string>
34#include <vector>
35#include <list>
36#include <iostream>
37#include <iomanip>
38#include <cstdio>
39
40#include <tclap/sstream.h>
41
42#include <tclap/ArgException.h>
43#include <tclap/Visitor.h>
45#include <tclap/ArgTraits.h>
47
48namespace TCLAP {
49
55class Arg
56{
57 private:
61 Arg(const Arg& rhs);
62
66 Arg& operator=(const Arg& rhs);
67
71 static bool& ignoreRestRef() { static bool ign = false; return ign; }
72
77 static char& delimiterRef() { static char delim = ' '; return delim; }
78
79 protected:
80
89 std::string _flag;
90
98 std::string _name;
99
103 std::string _description;
104
109
114 std::string _requireLabel;
115
122
129
137
142
148
150
154 void _checkWithVisitor() const;
155
169 Arg( const std::string& flag,
170 const std::string& name,
171 const std::string& desc,
172 bool req,
173 bool valreq,
174 Visitor* v = NULL );
175
176 public:
180 virtual ~Arg();
181
186 virtual void addToList( std::list<Arg*>& argList ) const;
187
191 static void beginIgnoring() { ignoreRestRef() = true; }
192
194 static void stopIgnoring() { ignoreRestRef() = false; }
195
199 static bool ignoreRest() { return ignoreRestRef(); }
200
205 static char delimiter() { return delimiterRef(); }
206
211 static char blankChar() { return (char)7; }
212
217#ifndef TCLAP_FLAGSTARTCHAR
218#define TCLAP_FLAGSTARTCHAR '-'
219#endif
220 static char flagStartChar() { return TCLAP_FLAGSTARTCHAR; }
221
227#ifndef TCLAP_FLAGSTARTSTRING
228#define TCLAP_FLAGSTARTSTRING "-"
229#endif
230 static const std::string flagStartString() { return TCLAP_FLAGSTARTSTRING; }
231
236#ifndef TCLAP_NAMESTARTSTRING
237#define TCLAP_NAMESTARTSTRING "--"
238#endif
239 static const std::string nameStartString() { return TCLAP_NAMESTARTSTRING; }
240
244 static const std::string ignoreNameString() { return "ignore_rest"; }
245
250 static void setDelimiter( char c ) { delimiterRef() = c; }
251
259 virtual bool processArg(int *i, std::vector<std::string>& args) = 0;
260
266 virtual bool operator==(const Arg& a) const;
267
271 const std::string& getFlag() const;
272
276 const std::string& getName() const;
277
279 virtual bool hasLabel() const { return true; }
280
284 std::string getDescription() const;
285
289 virtual bool isRequired() const;
290
295 void forceRequired();
296
301 void xorSet();
302
306 bool isValueRequired() const;
307
312 bool isSet() const;
313
317 bool isIgnoreable() const;
318
327 virtual bool argMatches( const std::string& s ) const;
328
333 virtual std::string toString() const;
334
339 virtual std::string shortID( const std::string& valueId = "val" ) const;
340
345 virtual std::string longID( const std::string& valueId = "val" ) const;
346
354 virtual void trimFlag( std::string& flag, std::string& value ) const;
355
362 bool _hasBlanks( const std::string& s ) const;
363
369 void setRequireLabel( const std::string& s );
370
375 virtual bool allowMore();
376
381 virtual bool acceptsMultipleValues();
382
387 virtual void reset();
388};
389
393typedef std::list<Arg*>::const_iterator ArgListIterator;
394
398typedef std::vector<Arg*>::const_iterator ArgVectorIterator;
399
403typedef std::list<Visitor*>::const_iterator VisitorListIterator;
404
405/*
406 * Extract a value of type T from it's string representation contained
407 * in strVal. The ValueLike parameter used to select the correct
408 * specialization of ExtractValue depending on the value traits of T.
409 * ValueLike traits use operator>> to assign the value from strVal.
410 */
411template<typename T> void
412ExtractValue(T &destVal, const std::string& strVal, ValueLike vl)
413{
414 static_cast<void>(vl); // Avoid warning about unused vl
415 istringstream is(strVal.c_str());
416
417 int valuesRead = 0;
418 while ( is.good() ) {
419 if ( is.peek() != EOF )
420#ifdef TCLAP_SETBASE_ZERO
421 is >> std::setbase(0) >> destVal;
422#else
423 is >> destVal;
424#endif
425 else
426 break;
427
428 valuesRead++;
429 }
430
431 if ( is.fail() )
432 throw( ArgParseException("Couldn't read argument value "
433 "from string '" + strVal + "'"));
434
435
436 if ( valuesRead > 1 )
437 throw( ArgParseException("More than one valid value parsed from "
438 "string '" + strVal + "'"));
439
440}
441
442/*
443 * Extract a value of type T from it's string representation contained
444 * in strVal. The ValueLike parameter used to select the correct
445 * specialization of ExtractValue depending on the value traits of T.
446 * StringLike uses assignment (operator=) to assign from strVal.
447 */
448template<typename T> void
449ExtractValue(T &destVal, const std::string& strVal, StringLike sl)
450{
451 static_cast<void>(sl); // Avoid warning about unused sl
452 SetString(destVal, strVal);
453}
454
456//BEGIN Arg.cpp
458
459inline Arg::Arg(const std::string& flag,
460 const std::string& name,
461 const std::string& desc,
462 bool req,
463 bool valreq,
464 Visitor* v) :
465 _flag(flag),
466 _name(name),
467 _description(desc),
468 _required(req),
469 _requireLabel("required"),
470 _valueRequired(valreq),
471 _alreadySet(false),
472 _visitor( v ),
473 _ignoreable(true),
474 _xorSet(false),
475 _acceptsMultipleValues(false)
476{
477 if ( _flag.length() > 1 )
479 "Argument flag can only be one character long", toString() ) );
480
481 if ( _name != ignoreNameString() &&
484 _flag == " " ) )
485 throw(SpecificationException("Argument flag cannot be either '" +
486 Arg::flagStartString() + "' or '" +
487 Arg::nameStartString() + "' or a space.",
488 toString() ) );
489
490 if ( ( _name.substr( 0, Arg::flagStartString().length() ) == Arg::flagStartString() ) ||
491 ( _name.substr( 0, Arg::nameStartString().length() ) == Arg::nameStartString() ) ||
492 ( _name.find( " ", 0 ) != std::string::npos ) )
493 throw(SpecificationException("Argument name begin with either '" +
494 Arg::flagStartString() + "' or '" +
495 Arg::nameStartString() + "' or space.",
496 toString() ) );
497
498}
499
500inline Arg::~Arg() { }
501
502inline std::string Arg::shortID( const std::string& valueId ) const
503{
504 std::string id = "";
505
506 if ( _flag != "" )
508 else
510
511 if ( _valueRequired )
512 id += std::string( 1, Arg::delimiter() ) + "<" + valueId + ">";
513
514 if ( !_required )
515 id = "[" + id + "]";
516
517 return id;
518}
519
520inline std::string Arg::longID( const std::string& valueId ) const
521{
522 std::string id = "";
523
524 if ( _flag != "" )
525 {
526 id += Arg::flagStartString() + _flag;
527
528 if ( _valueRequired )
529 id += std::string( 1, Arg::delimiter() ) + "<" + valueId + ">";
530
531 id += ", ";
532 }
533
534 id += Arg::nameStartString() + _name;
535
536 if ( _valueRequired )
537 id += std::string( 1, Arg::delimiter() ) + "<" + valueId + ">";
538
539 return id;
540
541}
542
543inline bool Arg::operator==(const Arg& a) const
544{
545 if ( ( _flag != "" && _flag == a._flag ) || _name == a._name)
546 return true;
547 else
548 return false;
549}
550
551inline std::string Arg::getDescription() const
552{
553 std::string desc = "";
554 if ( _required )
555 desc = "(" + _requireLabel + ") ";
556
557// if ( _valueRequired )
558// desc += "(value required) ";
559
560 desc += _description;
561 return desc;
562}
563
564inline const std::string& Arg::getFlag() const { return _flag; }
565
566inline const std::string& Arg::getName() const { return _name; }
567
568inline bool Arg::isRequired() const { return _required; }
569
570inline bool Arg::isValueRequired() const { return _valueRequired; }
571
572inline bool Arg::isSet() const
573{
574 if ( _alreadySet && !_xorSet )
575 return true;
576 else
577 return false;
578}
579
580inline bool Arg::isIgnoreable() const { return _ignoreable; }
581
582inline void Arg::setRequireLabel( const std::string& s)
583{
584 _requireLabel = s;
585}
586
587inline bool Arg::argMatches( const std::string& argFlag ) const
588{
589 if ( ( argFlag == Arg::flagStartString() + _flag && _flag != "" ) ||
590 argFlag == Arg::nameStartString() + _name )
591 return true;
592 else
593 return false;
594}
595
596inline std::string Arg::toString() const
597{
598 std::string s = "";
599
600 if ( _flag != "" )
601 s += Arg::flagStartString() + _flag + " ";
602
603 s += "(" + Arg::nameStartString() + _name + ")";
604
605 return s;
606}
607
608inline void Arg::_checkWithVisitor() const
609{
610 if ( _visitor != NULL )
611 _visitor->visit();
612}
613
617inline void Arg::trimFlag(std::string& flag, std::string& value) const
618{
619 int stop = 0;
620 for ( int i = 0; static_cast<unsigned int>(i) < flag.length(); i++ )
621 if ( flag[i] == Arg::delimiter() )
622 {
623 stop = i;
624 break;
625 }
626
627 if ( stop > 1 )
628 {
629 value = flag.substr(stop+1);
630 flag = flag.substr(0,stop);
631 }
632
633}
634
638inline bool Arg::_hasBlanks( const std::string& s ) const
639{
640 for ( int i = 1; static_cast<unsigned int>(i) < s.length(); i++ )
641 if ( s[i] == Arg::blankChar() )
642 return true;
643
644 return false;
645}
646
648{
649 _required = true;
650}
651
652inline void Arg::xorSet()
653{
654 _alreadySet = true;
655 _xorSet = true;
656}
657
661inline void Arg::addToList( std::list<Arg*>& argList ) const
662{
663 argList.push_front( const_cast<Arg*>(this) );
664}
665
666inline bool Arg::allowMore()
667{
668 return false;
669}
670
672{
674}
675
676inline void Arg::reset()
677{
678 _xorSet = false;
679 _alreadySet = false;
680}
681
683//END Arg.cpp
685
686} //namespace TCLAP
687
688#endif
689
#define TCLAP_FLAGSTARTSTRING
The sting that indicates the beginning of a flag.
Definition Arg.h:228
#define TCLAP_FLAGSTARTCHAR
The char that indicates the beginning of a flag.
Definition Arg.h:218
#define TCLAP_NAMESTARTSTRING
The sting that indicates the beginning of a name.
Definition Arg.h:237
Thrown from within the child Arg classes when it fails to properly parse the argument it has been pas...
A virtual base class that defines the essential data for all arguments.
Definition Arg.h:56
static char blankChar()
The char used as a place holder when SwitchArgs are combined.
Definition Arg.h:211
void _checkWithVisitor() const
Performs the special handling described by the Visitor.
Definition Arg.h:608
bool _acceptsMultipleValues
Definition Arg.h:149
virtual ~Arg()
Destructor.
Definition Arg.h:500
bool isSet() const
Indicates whether the argument has already been set.
Definition Arg.h:572
virtual std::string longID(const std::string &valueId="val") const
Returns a long ID for the usage.
Definition Arg.h:520
static const std::string nameStartString()
Definition Arg.h:239
static void beginIgnoring()
Begin ignoring arguments since the "--" argument was specified.
Definition Arg.h:191
std::string _requireLabel
Label to be used in usage description.
Definition Arg.h:114
bool _hasBlanks(const std::string &s) const
Checks whether a given string has blank chars, indicating that it is a combined SwitchArg.
Definition Arg.h:638
static bool ignoreRest()
Whether to ignore the rest.
Definition Arg.h:199
bool isIgnoreable() const
Indicates whether the argument can be ignored, if desired.
Definition Arg.h:580
void forceRequired()
Sets _required to true.
Definition Arg.h:647
virtual bool processArg(int *i, std::vector< std::string > &args)=0
Pure virtual method meant to handle the parsing and value assignment of the string on the command lin...
const std::string & getFlag() const
Returns the argument flag.
Definition Arg.h:564
static const std::string ignoreNameString()
The name used to identify the ignore rest argument.
Definition Arg.h:244
bool _valueRequired
Indicates whether a value is required for the argument.
Definition Arg.h:121
bool isValueRequired() const
Indicates whether a value must be specified for argument.
Definition Arg.h:570
static char flagStartChar()
Definition Arg.h:220
bool _alreadySet
Indicates whether the argument has been set.
Definition Arg.h:128
bool _ignoreable
Whether this argument can be ignored, if desired.
Definition Arg.h:141
std::string _description
Description of the argument.
Definition Arg.h:103
virtual void addToList(std::list< Arg * > &argList) const
Adds this to the specified list of Args.
Definition Arg.h:661
virtual bool allowMore()
Used for MultiArgs and XorHandler to determine whether args can still be set.
Definition Arg.h:666
const std::string & getName() const
Returns the argument name.
Definition Arg.h:566
Visitor * _visitor
A pointer to a visitor object.
Definition Arg.h:136
static char delimiter()
The delimiter that separates an argument flag/name from the value.
Definition Arg.h:205
void setRequireLabel(const std::string &s)
Sets the requireLabel.
Definition Arg.h:582
std::string getDescription() const
Returns the argument description.
Definition Arg.h:551
bool _xorSet
Indicates that the arg was set as part of an XOR and not on the command line.
Definition Arg.h:147
virtual void reset()
Clears the Arg object and allows it to be reused by new command lines.
Definition Arg.h:676
std::string _name
A single word namd identifying the argument.
Definition Arg.h:98
virtual bool argMatches(const std::string &s) const
A method that tests whether a string matches this argument.
Definition Arg.h:587
static void setDelimiter(char c)
Sets the delimiter for all arguments.
Definition Arg.h:250
bool _required
Indicating whether the argument is required.
Definition Arg.h:108
virtual bool acceptsMultipleValues()
Use by output classes to determine whether an Arg accepts multiple values.
Definition Arg.h:671
virtual void trimFlag(std::string &flag, std::string &value) const
Trims a value off of the flag.
Definition Arg.h:617
virtual std::string toString() const
Returns a simple string representation of the argument.
Definition Arg.h:596
virtual bool hasLabel() const
Whether this argument has an option label rather than being an operand.
Definition Arg.h:279
std::string _flag
The single char flag used to identify the argument.
Definition Arg.h:89
void xorSet()
Sets the _alreadySet value to true.
Definition Arg.h:652
virtual bool isRequired() const
Indicates whether the argument is required.
Definition Arg.h:568
virtual std::string shortID(const std::string &valueId="val") const
Returns a short ID for the usage.
Definition Arg.h:502
static void stopIgnoring()
Clear the legacy process-wide end-of-options state.
Definition Arg.h:194
virtual bool operator==(const Arg &a) const
Operator ==.
Definition Arg.h:543
static const std::string flagStartString()
Definition Arg.h:230
Thrown from Arg and CmdLine when an Arg is improperly specified, e.g.
A base class that defines the interface for visitors.
Definition Visitor.h:35
virtual void visit()=0
This method (to implemented by children) will be called when the visitor is visited.
Definition Arg.h:48
std::vector< Arg * >::const_iterator ArgVectorIterator
Typedef of an Arg vector iterator.
Definition Arg.h:398
std::istringstream istringstream
Definition sstream.h:37
std::list< Arg * >::const_iterator ArgListIterator
Typedef of an Arg list iterator.
Definition Arg.h:393
std::list< Visitor * >::const_iterator VisitorListIterator
Typedef of a Visitor list iterator.
Definition Arg.h:403
void SetString(T &dst, const std::string &src)
void ExtractValue(T &destVal, const std::string &strVal, ValueLike vl)
Definition Arg.h:412
A string like argument value type is a value that can be set using operator=(string).
Definition ArgTraits.h:49
A value like argument value type is a value that can be set using operator>>.
Definition ArgTraits.h:39