iRoCS Toolbox  1.1.0
Public Types | Public Member Functions | Protected Member Functions | Protected Attributes
CmdArgType< T > Class Template Reference

The CmdArgType<T> class is a CmdArg that takes one value of type T. More...

#include <CmdArgs.hh>

Inheritance diagram for CmdArgType< T >:
Collaboration diagram for CmdArgType< T >:

Public Types

enum  CmdLineFlags {
  GIVEN = 0x01, VALGIVEN = 0x02, OPTION = 0x04, KEYWORD = 0x08,
  POSITIONAL = 0x10, VALSEP = 0x20, ALLFLAGS = 0x3F
}
 Flags that say how the argument was specied on the command-line. More...
 
enum  Syntax {
  isOPT = 0x000, isREQ = 0x001, isVALOPT = 0x002, isVALREQ = 0x004,
  isVALSEP = 0x008, isVALSTICKY = 0x010, isLIST = 0x020, isPOS = 0x040,
  isHIDDEN = 0x080, isFIXEDLIST = 0x100, isVALTAKEN = (isVALREQ | isVALOPT), isOPTVALOPT = (isOPT | isVALOPT),
  isOPTVALREQ = (isOPT | isVALREQ), isPOSVALOPT = (isPOS | isVALOPT), isPOSVALREQ = (isPOS | isVALREQ)
}
 Flags that define the argument syntax. More...
 

Public Member Functions

 CmdArgType (char aShortName, const std::string aLongName, const std::string aValueName, const std::string aDescription, Syntax aSyntax=isOPTVALREQ)
 Constructor. More...
 
 CmdArgType (const std::string aValueName, const std::string aDescription, Syntax aSyntax=isPOSVALREQ)
 Constructor. More...
 
virtual ~CmdArgType ()
 Destructor. More...
 
void clearErrorMsg ()
 Clears the error message. More...
 
virtual void clearFixedList ()
 If the argument is a fixed list, this function clears all its values. More...
 
const std::string description () const
 Get the description (help-message) of this argument. More...
 
const std::string errorMsg () const
 Returns error message of last parsing. More...
 
virtual unsigned int fixedSize () const
 Return fixed size if this CmdArg is a fixed list, return 0 otherwise. More...
 
CmdArg::CmdLineFlags flags () const
 Retrieve the flags that say how this argument was specified. More...
 
virtual std::string getDefaultValueString () const
 Returns the default value as a string. More...
 
bool given () const
 Returns true if the argument was parsed in any of the parsing runs. More...
 
virtual bool hasDefaultValue () const
 Returns true if a default value was specified. More...
 
bool hasFlag (CmdArg::CmdLineFlags f) const
 Returns true if the argument's flags contain f. More...
 
bool hasSyntax (CmdArg::Syntax s) const
 Returns true if the argument's syntax flags contain s. More...
 
const std::string longName () const
 Returns the argument's long name. More...
 
bool modified () const
 Returns true if the argument was parsed at the last parsing run. More...
 
void modified (bool m)
 Sets the modified flag. More...
 
virtual bool parse (const char *arg, const char *&endptr, CmdLine &)
 Parse value string. More...
 
virtual unsigned int remainingSize () const
 Return number of remaining values if this CmdArg is a fixed list, return 0 otherwise. More...
 
unsigned int sequence () const
 Get the sequence number corresponding to the last time this argument was matched on the command-line. More...
 
virtual void setDefaultValue (const T &v)
 Specify default value. More...
 
void setErrorMsg (const std::string &msg)
 Sets the error message. More...
 
char shortName () const
 Returns the argument's short name. More...
 
const std::string synopsis (bool displayShort=true, bool displayLong=true) const
 Generates and returns the arguments synopsis. More...
 
CmdArg::Syntax syntax () const
 Retrieve the syntax flags for this argument. More...
 
virtual T & value ()
 Return the reference to the value of the argument. More...
 
virtual const T & value () const
 Return the const reference to the value of the argument. More...
 
virtual std::string valueAsString () const
 Return the value as a std::string. More...
 
const std::string valueName () const
 Returns the argument's value name. More...
 
virtual std::string valueTypeAsString () const
 Return the value type as a std::string. More...
 

Protected Member Functions

void adjustSyntax ()
 Adjust and correct specified syntax flags. More...
 
void clearFlags (CmdArg::CmdLineFlags f=ALLFLAGS)
 Clears the specified flags. More...
 
void flags (CmdArg::CmdLineFlags f)
 Sets the argument's flags. More...
 
void sequence (unsigned int num)
 Sets the sequence number. More...
 
void setFlags (CmdArg::CmdLineFlags f)
 Toggles specified flags. More...
 

Protected Attributes

std::string pDescription
 
std::string pErrorMessage
 
CmdArg::CmdLineFlags pFlags
 
std::string pLongName
 
bool pModified
 
unsigned int pSequence
 
char pShortName
 
CmdArg::Syntax pSyntax
 
std::string pValueName
 

Detailed Description

template<typename T>
class CmdArgType< T >

The CmdArgType<T> class is a CmdArg that takes one value of type T.

The CmdArgType<T> class is a CmdArg that (optionally) takes one value of type T. For parsing it uses the Compiler<T> template class. You may specify a default value, which is returned if no value was given on the command line, by using the function. setDefaultValue

Example:
int main(int argc, char** argv)
{
CmdLine cmd("fileview", "views a file");
'f', "file", "filename",
"The name of the file that you want to view.");
argFile.setDefaultValue("foo.bar");
ArgvIter iter(--argc, ++argv);
cmd.append(&argFile);
cmd.parse(iter);
std::cout << "View file " << argFile.value() << std::endl;
return 0;
}

If you execute the program like this

fileview -f image.jpg 

or

fileview --file image.jpg 

the output is

View file image.jpg 

Definition at line 170 of file CmdArgs.hh.

Member Enumeration Documentation

◆ Syntax

enum CmdArg::Syntax
inherited

Flags that define the argument syntax.

Enumerator
isOPT 

argument is optional

isREQ 

argument is required

isVALOPT 

argument value is optional

isVALREQ 

argument value is required

isVALSEP 

argument value must be in a separate token

isVALSTICKY 

argument value must be in the same token

isLIST 

argument is a list

isPOS 

argument is positional

isHIDDEN 

argument is not to be printed in usage

isFIXEDLIST 

argument value is a list of fixed size

isVALTAKEN 

argument takes a value

isOPTVALOPT 
isOPTVALREQ 
isPOSVALOPT 
isPOSVALREQ 

Definition at line 112 of file CmdArg.hh.

◆ CmdLineFlags

enum CmdArg::CmdLineFlags
inherited

Flags that say how the argument was specied on the command-line.

Enumerator
GIVEN 

argument was given

VALGIVEN 

argument value was given

OPTION 

item was matched as an option

KEYWORD 

item was matched as a keyword

POSITIONAL 

item was matched as a positional argument

VALSEP 

value was in a separate token

ALLFLAGS 

Definition at line 136 of file CmdArg.hh.

Constructor & Destructor Documentation

◆ CmdArgType() [1/2]

template<typename T>
CmdArgType< T >::CmdArgType ( char  aShortName,
const std::string  aLongName,
const std::string  aValueName,
const std::string  aDescription,
Syntax  aSyntax = isOPTVALREQ 
)
inline

Constructor.

Creates a CmdArgType with short and long name. By default the syntax flags are set so that the argument is optional and its value is required.

Parameters
aShortNameshort name of the argument
aLongNamelong name of the argument
aValueNamename of the agrument's value
aDescriptiondescription of the argument
aSyntaxsyntax flags
Exceptions
CmdLineSyntaxError

Definition at line 188 of file CmdArgs.hh.

◆ CmdArgType() [2/2]

template<typename T>
CmdArgType< T >::CmdArgType ( const std::string  aValueName,
const std::string  aDescription,
Syntax  aSyntax = isPOSVALREQ 
)
inline

Constructor.

Creates a positional CmdArgType. By default the syntax flags are set so that the argument is optional and its value is required.

Parameters
aValueNamename of the agrument's value
aDescriptiondescription of the argument
aSyntaxsyntax flags
Exceptions
CmdLineSyntaxError

Definition at line 212 of file CmdArgs.hh.

◆ ~CmdArgType()

template<typename T>
virtual CmdArgType< T >::~CmdArgType ( )
inlinevirtual

Destructor.

Definition at line 226 of file CmdArgs.hh.

Member Function Documentation

◆ parse()

template<typename T>
virtual bool CmdArgType< T >::parse ( const char *  arg,
const char *&  endptr,
CmdLine  
)
inlinevirtual

Parse value string.

If the given arg could not be parsed correctly a CmdLineSyntaxError exception is thrown.

Parameters
argthe string that is to be parsed
endptris set to the first unparsed character of arg
cmdreference to the CmdLine object that initiated the parsing (is ignored)
Exceptions
CmdLineSyntaxError
Returns
true if parsing was successful

Implements CmdArg.

Definition at line 246 of file CmdArgs.hh.

◆ value() [1/2]

template<typename T>
virtual T& CmdArgType< T >::value ( )
inlinevirtual

Return the reference to the value of the argument.

Returns
reference to the value of the argument

Definition at line 270 of file CmdArgs.hh.

Referenced by CmdArgTypeVector< T >::parse(), CmdArgTypeFixedVector< T >::parse(), and CmdArgType< T >::valueAsString().

◆ setDefaultValue()

template<typename T>
virtual void CmdArgType< T >::setDefaultValue ( const T &  v)
inlinevirtual

Specify default value.

Parameters
vdefault value

Definition at line 284 of file CmdArgs.hh.

◆ hasDefaultValue()

template<typename T>
virtual bool CmdArgType< T >::hasDefaultValue ( ) const
inlinevirtual

Returns true if a default value was specified.

Returns
true if a default value was specified

Reimplemented from CmdArg.

Definition at line 300 of file CmdArgs.hh.

Referenced by CmdArgTypeVector< T >::parse().

◆ getDefaultValueString()

template<typename T>
virtual std::string CmdArgType< T >::getDefaultValueString ( ) const
inlinevirtual

Returns the default value as a string.

Returns
default value as a string

Reimplemented from CmdArg.

Definition at line 314 of file CmdArgs.hh.

◆ value() [2/2]

template<typename T>
virtual const T& CmdArgType< T >::value ( ) const
inlinevirtual

Return the const reference to the value of the argument.

Returns
const reference to the value of the argument

Definition at line 329 of file CmdArgs.hh.

◆ valueTypeAsString()

template<typename T>
virtual std::string CmdArgType< T >::valueTypeAsString ( ) const
inlinevirtual

Return the value type as a std::string.

Returns
value type as a std::string

Implements CmdArg.

Definition at line 342 of file CmdArgs.hh.

◆ valueAsString()

template<typename T>
virtual std::string CmdArgType< T >::valueAsString ( ) const
inlinevirtual

Return the value as a std::string.

Returns
value as a std::string

Implements CmdArg.

Definition at line 355 of file CmdArgs.hh.

◆ fixedSize()

virtual unsigned int CmdArg::fixedSize ( ) const
virtualinherited

Return fixed size if this CmdArg is a fixed list, return 0 otherwise.

Returns
fixed size

Reimplemented in CmdArgTypeFixedVector< T >.

Referenced by CmdArgTypeFixedVector< T >::remainingSize().

◆ remainingSize()

virtual unsigned int CmdArg::remainingSize ( ) const
virtualinherited

Return number of remaining values if this CmdArg is a fixed list, return 0 otherwise.

Returns
remaining number of values

Reimplemented in CmdArgTypeFixedVector< T >.

Referenced by CmdArgTypeFixedVector< T >::parse().

◆ syntax()

CmdArg::Syntax CmdArg::syntax ( ) const
inherited

Retrieve the syntax flags for this argument.

◆ hasSyntax()

bool CmdArg::hasSyntax ( CmdArg::Syntax  s) const
inherited

Returns true if the argument's syntax flags contain s.

Parameters
ssyntax flag to be checked
Returns
true if the argument's syntax flags contain s

◆ flags() [1/2]

CmdArg::CmdLineFlags CmdArg::flags ( ) const
inherited

Retrieve the flags that say how this argument was specified.

◆ flags() [2/2]

void CmdArg::flags ( CmdArg::CmdLineFlags  f)
protectedinherited

Sets the argument's flags.

Parameters
fnew flags

◆ hasFlag()

bool CmdArg::hasFlag ( CmdArg::CmdLineFlags  f) const
inherited

Returns true if the argument's flags contain f.

Parameters
sflag to be checked
Returns
true if the argument's flags contain f

◆ sequence() [1/2]

unsigned int CmdArg::sequence ( ) const
inherited

Get the sequence number corresponding to the last time this argument was matched on the command-line.

If this argument was not matched, the sequence number will be zero, otherwise it will be 'N' where the last time this argument was matched, it was the 'N'th argument encountered.

Returns
sequence number

◆ sequence() [2/2]

void CmdArg::sequence ( unsigned int  num)
protectedinherited

Sets the sequence number.

Parameters
numnew sequence number

◆ shortName()

char CmdArg::shortName ( ) const
inherited

Returns the argument's short name.

Return's 0 if it has none.

Returns
short name

◆ longName()

const std::string CmdArg::longName ( ) const
inherited

Returns the argument's long name.

Return's "" if it has none.

Returns
long name

◆ valueName()

const std::string CmdArg::valueName ( ) const
inherited

Returns the argument's value name.

Return's "" if it takes no value.

Returns
value name

◆ description()

const std::string CmdArg::description ( ) const
inherited

Get the description (help-message) of this argument.

Returns
description

◆ synopsis()

const std::string CmdArg::synopsis ( bool  displayShort = true,
bool  displayLong = true 
) const
inherited

Generates and returns the arguments synopsis.

Parameters
displayShortif true print short name
displayLongif true display long name
Returns
synopsis string

◆ errorMsg()

const std::string CmdArg::errorMsg ( ) const
inherited

Returns error message of last parsing.

Returns
last error message

◆ clearErrorMsg()

void CmdArg::clearErrorMsg ( )
inherited

Clears the error message.

◆ setErrorMsg()

void CmdArg::setErrorMsg ( const std::string &  msg)
inherited

Sets the error message.

Parameters
msgnew error message.

◆ modified() [1/2]

bool CmdArg::modified ( ) const
inherited

Returns true if the argument was parsed at the last parsing run.

Returns
if the argument was parsed

Referenced by CmdArgType< T >::parse(), CmdArgTypeVector< T >::parse(), CmdArgTypeFixedVector< T >::parse(), and CmdArgSwitch::parse().

◆ modified() [2/2]

void CmdArg::modified ( bool  m)
inherited

Sets the modified flag.

Parameters
m

◆ given()

bool CmdArg::given ( ) const
inherited

Returns true if the argument was parsed in any of the parsing runs.

Returns
if the argument was ever parsed

◆ clearFixedList()

virtual void CmdArg::clearFixedList ( )
virtualinherited

If the argument is a fixed list, this function clears all its values.

Reimplemented in CmdArgTypeFixedVector< T >.

◆ adjustSyntax()

void CmdArg::adjustSyntax ( )
protectedinherited

Adjust and correct specified syntax flags.

Exceptions
CmdLineSyntaxError

Referenced by CmdArgTypeFixedVector< T >::CmdArgTypeFixedVector().

◆ setFlags()

void CmdArg::setFlags ( CmdArg::CmdLineFlags  f)
protectedinherited

Toggles specified flags.

Parameters
fflags to toggle

◆ clearFlags()

void CmdArg::clearFlags ( CmdArg::CmdLineFlags  f = ALLFLAGS)
protectedinherited

Clears the specified flags.

Parameters
fflags to clear

Field Documentation

◆ pFlags

CmdArg::CmdLineFlags CmdArg::pFlags
protectedinherited

Definition at line 541 of file CmdArg.hh.

◆ pSyntax

CmdArg::Syntax CmdArg::pSyntax
protectedinherited

Definition at line 542 of file CmdArg.hh.

◆ pSequence

unsigned int CmdArg::pSequence
protectedinherited

Definition at line 544 of file CmdArg.hh.

◆ pShortName

char CmdArg::pShortName
protectedinherited

Definition at line 545 of file CmdArg.hh.

◆ pLongName

std::string CmdArg::pLongName
protectedinherited

Definition at line 546 of file CmdArg.hh.

◆ pValueName

std::string CmdArg::pValueName
protectedinherited

Definition at line 547 of file CmdArg.hh.

◆ pDescription

std::string CmdArg::pDescription
protectedinherited

Definition at line 548 of file CmdArg.hh.

◆ pErrorMessage

std::string CmdArg::pErrorMessage
protectedinherited

Definition at line 550 of file CmdArg.hh.

◆ pModified

bool CmdArg::pModified
protectedinherited

Definition at line 552 of file CmdArg.hh.


The documentation for this class was generated from the following file: