iRoCS Toolbox  1.1.0
Public Types | Public Member Functions | Protected Member Functions | Protected Attributes | Friends
CmdArg Class Referenceabstract

The CmdArg class is the abstract base class for all command line arguments. More...

#include <CmdArg.hh>

Inheritance diagram for CmdArg:
Collaboration diagram for CmdArg:

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

 CmdArg (char shortName, const std::string longName, const std::string valueName, const std::string description, CmdArg::Syntax syntax_flags=isOPTVALREQ)
 Create an argument that takes a value. More...
 
 CmdArg (char shortName, const std::string longName, const std::string description, CmdArg::Syntax syntax_flags=isOPT)
 Create an argument that takes no value. More...
 
 CmdArg (const std::string value, const std::string description, CmdArg::Syntax syntax=isPOSVALREQ)
 Create a positional argument (no short and long names). More...
 
virtual ~CmdArg ()
 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 &cmd)=0
 Parse a 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...
 
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 std::string valueAsString () const =0
 Returns the argument's value. More...
 
const std::string valueName () const
 Returns the argument's value name. More...
 
virtual std::string valueTypeAsString () const =0
 Returns the argument's value type. 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
 

Friends

class CmdLine
 

Detailed Description

The CmdArg class is the abstract base class for all command line arguments.

Definition at line 102 of file CmdArg.hh.

Member Enumeration Documentation

◆ Syntax

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

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

◆ CmdArg() [1/3]

CmdArg::CmdArg ( char  shortName,
const std::string  longName,
const std::string  valueName,
const std::string  description,
CmdArg::Syntax  syntax_flags = isOPTVALREQ 
)

Create an argument that takes a value.

The default flags are to assume that the argument is optional and that the value is required. If you don't want the argument to have a short name, set shortName=0. If you don't want the argument to have a long name, set longName="".

Parameters
shortNamethe short name of the argument
longNamethe long name of the argument
valueNamethe name of the value
descriptionthe argument's description
syntax_flags
Exceptions
CmdLineSyntaxError

◆ CmdArg() [2/3]

CmdArg::CmdArg ( char  shortName,
const std::string  longName,
const std::string  description,
CmdArg::Syntax  syntax_flags = isOPT 
)

Create an argument that takes no value.

The default flags are to assume that the argument is optional. If you don't want the argument to have a short name, set shortName=0. If you don't want the argument to have a long name, set longName="".

Parameters
shortNamethe short name of the argument
longNamethe long name of the argument
descriptionthe argument's description
syntax_flags
Exceptions
CmdLineSyntaxError

◆ CmdArg() [3/3]

CmdArg::CmdArg ( const std::string  value,
const std::string  description,
CmdArg::Syntax  syntax = isPOSVALREQ 
)

Create a positional argument (no short and long names).

The default flags are to assume that the argument is positional and that the value is required.

Parameters
valueNamethe name of the value
descriptionthe argument's description
syntax_flags
Exceptions
CmdLineSyntaxError

◆ ~CmdArg()

virtual CmdArg::~CmdArg ( )
virtual

Destructor.

Member Function Documentation

◆ fixedSize()

virtual unsigned int CmdArg::fixedSize ( ) const
virtual

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
virtual

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().

◆ parse()

virtual bool CmdArg::parse ( const char *  arg,
const char *&  endptr,
CmdLine cmd 
)
pure virtual

Parse a value string.

If this argument does not take a value, or the value is optional and was NOT supplied, then "" is passed as arg. If arg was completely accepted, endptr is set to NULL. Otherwise it points to the first unparsed character in arg. If the parsing was successful, true is returned.

Parameters
argstring to parse
endptrpointer to the first unused char of arg, or NULL if all chars were parsed successfully
cmdreference to the CmdLine object that called the function
Returns
true if parsing was successful

Implemented in CmdArgThrow< E >, CmdArgThrow< CmdLineUsageXMLError >, CmdArgThrow< CmdLineUsageHTMLError >, CmdArgThrow< CmdLineUsageError >, CmdArgSwitch, CmdArgTypeFixedVector< T >, CmdArgTypeVector< T >, and CmdArgType< T >.

◆ syntax()

CmdArg::Syntax CmdArg::syntax ( ) const

Retrieve the syntax flags for this argument.

◆ hasSyntax()

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

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

Retrieve the flags that say how this argument was specified.

◆ hasFlag()

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

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

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

◆ shortName()

char CmdArg::shortName ( ) const

Returns the argument's short name.

Return's 0 if it has none.

Returns
short name

◆ longName()

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

Returns the argument's long name.

Return's "" if it has none.

Returns
long name

◆ valueName()

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

Returns the argument's value name.

Return's "" if it takes no value.

Returns
value name

◆ valueTypeAsString()

virtual std::string CmdArg::valueTypeAsString ( ) const
pure virtual

◆ valueAsString()

virtual std::string CmdArg::valueAsString ( ) const
pure virtual

◆ description()

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

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

Returns
description

◆ synopsis()

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

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

Returns error message of last parsing.

Returns
last error message

◆ clearErrorMsg()

void CmdArg::clearErrorMsg ( )

Clears the error message.

◆ setErrorMsg()

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

Sets the error message.

Parameters
msgnew error message.

◆ modified() [1/2]

bool CmdArg::modified ( ) const

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)

Sets the modified flag.

Parameters
m

◆ given()

bool CmdArg::given ( ) const

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

Returns
if the argument was ever parsed

◆ hasDefaultValue()

virtual bool CmdArg::hasDefaultValue ( ) const
virtual

Returns true if a default value was specified.

Returns
true if a default value was specified

Reimplemented in CmdArgTypeFixedVector< T >, CmdArgTypeVector< T >, and CmdArgType< T >.

◆ getDefaultValueString()

virtual std::string CmdArg::getDefaultValueString ( ) const
virtual

Returns the default value as a string.

Returns
default value as a string

Reimplemented in CmdArgTypeFixedVector< T >, CmdArgTypeVector< T >, and CmdArgType< T >.

◆ clearFixedList()

virtual void CmdArg::clearFixedList ( )
virtual

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

Reimplemented in CmdArgTypeFixedVector< T >.

◆ adjustSyntax()

void CmdArg::adjustSyntax ( )
protected

Adjust and correct specified syntax flags.

Exceptions
CmdLineSyntaxError

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

◆ flags() [2/2]

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

Sets the argument's flags.

Parameters
fnew flags

◆ setFlags()

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

Toggles specified flags.

Parameters
fflags to toggle

◆ clearFlags()

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

Clears the specified flags.

Parameters
fflags to clear

◆ sequence() [2/2]

void CmdArg::sequence ( unsigned int  num)
protected

Sets the sequence number.

Parameters
numnew sequence number

Friends And Related Function Documentation

◆ CmdLine

friend class CmdLine
friend

Definition at line 105 of file CmdArg.hh.

Field Documentation

◆ pFlags

CmdArg::CmdLineFlags CmdArg::pFlags
protected

Definition at line 541 of file CmdArg.hh.

◆ pSyntax

CmdArg::Syntax CmdArg::pSyntax
protected

Definition at line 542 of file CmdArg.hh.

◆ pSequence

unsigned int CmdArg::pSequence
protected

Definition at line 544 of file CmdArg.hh.

◆ pShortName

char CmdArg::pShortName
protected

Definition at line 545 of file CmdArg.hh.

◆ pLongName

std::string CmdArg::pLongName
protected

Definition at line 546 of file CmdArg.hh.

◆ pValueName

std::string CmdArg::pValueName
protected

Definition at line 547 of file CmdArg.hh.

◆ pDescription

std::string CmdArg::pDescription
protected

Definition at line 548 of file CmdArg.hh.

◆ pErrorMessage

std::string CmdArg::pErrorMessage
protected

Definition at line 550 of file CmdArg.hh.

◆ pModified

bool CmdArg::pModified
protected

Definition at line 552 of file CmdArg.hh.


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