iRoCS Toolbox  1.1.0
Public Types | Public Member Functions
CmdLine Class Reference

The CmdLine class is a parser for command line arguments. More...

#include <CmdLine.hh>

Collaboration diagram for CmdLine:

Public Types

enum  CmdLineFlags {
  FLAG_ANY_CASE_OPTS = 0x001, FLAG_PROMPT_USER = 0x002, FLAG_NO_ABORT = 0x004, FLAG_OPTS_FIRST = 0x008,
  FLAG_QUIET = 0x080, FLAG_ALL = 0x0FF
}
 Configuration flags for CmdLine. More...
 
enum  CmdLineParseState {
  PARSE_START_STATE = 0x00, PARSE_TOK_REQUIRED = 0x01, PARSE_WANT_VAL = 0x02, PARSE_NEED_VAL = (PARSE_WANT_VAL | PARSE_TOK_REQUIRED),
  PARSE_ALL = 0x07
}
 Parsing state flags for CmdLine. More...
 
enum  CmdLineState {
  STATE_START_STATE = 0x00, STATE_END_OF_OPTIONS = 0x01, STATE_SHORT_USED = 0x02, STATE_LONG_USED = 0x04,
  STATE_ALL = 0x07
}
 State flags for CmdLine. More...
 
enum  CmdLineStatus {
  STATUS_NO_ERROR = 0x000, STATUS_ARG_MISSING = 0x001, STATUS_VAL_MISSING = 0x002, STATUS_VAL_NOTSTICKY = 0x004,
  STATUS_VAL_NOTSEP = 0x008, STATUS_KWD_AMBIGUOUS = 0x010, STATUS_BAD_OPTION = 0x020, STATUS_BAD_KEYWORD = 0x040,
  STATUS_BAD_VALUE = 0x080, STATUS_TOO_MANY_ARGS = 0x100
}
 Status flags for CmdLine. More...
 

Public Member Functions

 CmdLine (std::string name, std::string longname, bool help=true)
 Constructor. More...
 
 CmdLine (std::string name, std::string longname, bool help, CmdArg *cmdarg1 ...)
 Constructor. More...
 
virtual ~CmdLine ()
 Destructor. More...
 
CmdLineappend (CmdArg *cmdarg)
 Append specified CmdArg to the list of CmdArgs. More...
 
CmdLineappend (CmdArg &cmdarg)
 Append specified CmdArg to the list of CmdArgs. More...
 
void clearFlags (CmdLine::CmdLineFlags f=FLAG_ALL)
 Clear only the given flags. More...
 
void clearState (CmdLine::CmdLineState s=STATE_ALL)
 Clear only the given state flags. More...
 
std::string description () const
 Return previously specified program description. More...
 
void description (const std::string &description)
 Specify program description. More...
 
CmdLine::CmdLineStatus epilogue ()
 Perform the necessary post-processing. More...
 
std::ostream & error () const
 Return CmdLine's error output stream. More...
 
void error (std::ostream &os)
 Specify CmdLine's error output stream. More...
 
CmdLine::CmdLineFlags flags () const
 Return CmdLine's flags. More...
 
void flags (CmdLine::CmdLineFlags f)
 Specify new set of flags. More...
 
CmdArggetArg (char shortName) const
 Retrieve an argument based on its short name. More...
 
CmdArggetArg (const std::string &keyword) const
 Retrieve an argument based on its long name. More...
 
bool hasFlag (CmdLine::CmdLineFlags f) const
 Checks if the given flags are set. More...
 
bool hasState (CmdLine::CmdLineState s) const
 Checks if the given state flags are set. More...
 
bool hasStatus (CmdLine::CmdLineStatus s) const
 Checks if the given status flags are set. More...
 
std::string longname () const
 Return previously specified program long name. More...
 
void longname (const std::string &value)
 Specify program long name. More...
 
std::string name () const
 Return previously specified program name. More...
 
void name (const std::string &value)
 Specify program name. More...
 
unsigned int nargsParsed () const
 Find out the number of arguments parsed so far. More...
 
CmdLine::CmdLineStatus parse (ArgIter &argiter, bool autoProcessing=true)
 Parse a set of arguments. More...
 
CmdLine::CmdLineStatus parseArg (const char *arg)
 Parse a single argument. More...
 
CmdLine::CmdLineStatus prologue ()
 Perform the necessary pre-processing. More...
 
void setFlags (CmdLine::CmdLineFlags f)
 Set only the given flags. More...
 
void setState (CmdLine::CmdLineState s)
 Set only given state flags. More...
 
void setStatus (CmdLine::CmdLineStatus s)
 Set only given status flags. More...
 
CmdLine::CmdLineState state () const
 Return CmdLine's state. More...
 
void state (CmdLine::CmdLineState s)
 Specify new set of state flags. More...
 
CmdLine::CmdLineStatus status () const
 Return CmdLine's status. More...
 
void status (CmdLine::CmdLineStatus s)
 Specify new set of status flags. More...
 
std::ostream & usage (std::ostream &os) const
 Print usage message on the given output stream. More...
 
std::ostream & usage () const
 Print usage message on CmdLine's error output stream. More...
 
std::ostream & usageHTML (std::ostream &os) const
 Print HTML usage message on the given output stream. More...
 
std::ostream & usageLATEX (std::ostream &os) const
 Print LATEX usage message on the given output stream. More...
 
std::ostream & usageXML (std::ostream &os) const
 Print XML usage information on the given output stream. More...
 

Detailed Description

The CmdLine class is a parser for command line arguments.

Todo:
Finish documentation.

Definition at line 137 of file CmdLine.hh.

Member Enumeration Documentation

◆ CmdLineFlags

Configuration flags for CmdLine.

Enumerator
FLAG_ANY_CASE_OPTS 

Ignore character-case for short-options.

FLAG_PROMPT_USER 

Prompt the user for missing required args.

FLAG_NO_ABORT 

Don't quit upon syntax error.

FLAG_OPTS_FIRST 

No options after positional parameters.

FLAG_QUIET 

Don't print syntax error messages.

FLAG_ALL 

Definition at line 145 of file CmdLine.hh.

◆ CmdLineStatus

Status flags for CmdLine.

Enumerator
STATUS_NO_ERROR 

No problems.

STATUS_ARG_MISSING 

A required argument was not specified.

STATUS_VAL_MISSING 

A required argument value was not specified.

STATUS_VAL_NOTSTICKY 

Value needs to be in same token.

STATUS_VAL_NOTSEP 

Value needs to be in separate token.

STATUS_KWD_AMBIGUOUS 

An ambiguous keyword prefix was specified.

STATUS_BAD_OPTION 

An invalid option was specified.

STATUS_BAD_KEYWORD 

An invalid keyword was specified.

STATUS_BAD_VALUE 

An invalid value was specified for an arg.

STATUS_TOO_MANY_ARGS 

Too many positional args were specified.

Definition at line 161 of file CmdLine.hh.

◆ CmdLineState

State flags for CmdLine.

Enumerator
STATE_START_STATE 
STATE_END_OF_OPTIONS 

End of options (–) occured.

STATE_SHORT_USED 

Short options were used.

STATE_LONG_USED 

Long options were used.

STATE_ALL 

Definition at line 181 of file CmdLine.hh.

◆ CmdLineParseState

Parsing state flags for CmdLine.

Enumerator
PARSE_START_STATE 

Parsing of a CmdArg is completely finished.

PARSE_TOK_REQUIRED 

The next token is required.

PARSE_WANT_VAL 

The last parsed CmdArg may optionally get a value.

PARSE_NEED_VAL 

The last parsed CmdArg requires a value.

PARSE_ALL 

Definition at line 196 of file CmdLine.hh.

Constructor & Destructor Documentation

◆ CmdLine() [1/2]

CmdLine::CmdLine ( std::string  name,
std::string  longname,
bool  help = true 
)

Constructor.

Sets the program name and longname.

Parameters
nameprogram name
longnameprogram long name
helpautomatically add default help arguments?

◆ CmdLine() [2/2]

CmdLine::CmdLine ( std::string  name,
std::string  longname,
bool  help,
CmdArg cmdarg1 ... 
)

Constructor.

Sets the program name and longname. Adds given CmdArgs. ATTENTION: the last element in the CmdArg list must be NULL!

Parameters
nameprogram name
longnameprogram long name
helpautomatically add default help arguments?
cmdarg1... CmdArgs to add
Exceptions
CmdLineDublicateArgError

◆ ~CmdLine()

virtual CmdLine::~CmdLine ( )
virtual

Destructor.

Member Function Documentation

◆ name() [1/2]

std::string CmdLine::name ( ) const

Return previously specified program name.

Returns
program name

◆ name() [2/2]

void CmdLine::name ( const std::string &  value)

Specify program name.

Parameters
nameprogram name

◆ longname() [1/2]

std::string CmdLine::longname ( ) const

Return previously specified program long name.

Returns
program long name

◆ longname() [2/2]

void CmdLine::longname ( const std::string &  value)

Specify program long name.

Parameters
nameprogram long name

◆ description() [1/2]

std::string CmdLine::description ( ) const

Return previously specified program description.

Returns
program description

◆ description() [2/2]

void CmdLine::description ( const std::string &  description)

Specify program description.

Parameters
descriptionprogram description

◆ append() [1/2]

CmdLine& CmdLine::append ( CmdArg cmdarg)

Append specified CmdArg to the list of CmdArgs.

Parameters
cmdargpointer to CmdArg to append
Exceptions
CmdLineDublicateArgError
Returns
reference to changed CmdLine object

◆ append() [2/2]

CmdLine& CmdLine::append ( CmdArg cmdarg)

Append specified CmdArg to the list of CmdArgs.

Parameters
cmdargreference to CmdArg to append
Exceptions
CmdLineDublicateArgError
Returns
reference to modified CmdLine object

◆ usage() [1/2]

std::ostream& CmdLine::usage ( std::ostream &  os) const

Print usage message on the given output stream.

Parameters
osoutput stream
Returns
modified output stream

◆ usage() [2/2]

std::ostream& CmdLine::usage ( ) const

Print usage message on CmdLine's error output stream.

Returns
modified output stream

◆ usageXML()

std::ostream& CmdLine::usageXML ( std::ostream &  os) const

Print XML usage information on the given output stream.

Parameters
osoutput stream
Returns
modified output stream

◆ usageHTML()

std::ostream& CmdLine::usageHTML ( std::ostream &  os) const

Print HTML usage message on the given output stream.

Parameters
osoutput stream
Returns
modified output stream

◆ usageLATEX()

std::ostream& CmdLine::usageLATEX ( std::ostream &  os) const

Print LATEX usage message on the given output stream.

Parameters
osoutput stream
Returns
modified output stream
Todo:
Implement function.

◆ status() [1/2]

CmdLine::CmdLineStatus CmdLine::status ( ) const

Return CmdLine's status.

Returns
status

◆ status() [2/2]

void CmdLine::status ( CmdLine::CmdLineStatus  s)

Specify new set of status flags.

Parameters
sstatus to set

◆ setStatus()

void CmdLine::setStatus ( CmdLine::CmdLineStatus  s)

Set only given status flags.

Other status flags are not modified.

Parameters
sstatus to set

◆ hasStatus()

bool CmdLine::hasStatus ( CmdLine::CmdLineStatus  s) const

Checks if the given status flags are set.

Parameters
sstatus to check
Returns
true if the given status flags are set

◆ state() [1/2]

CmdLine::CmdLineState CmdLine::state ( ) const

Return CmdLine's state.

Returns
state

◆ state() [2/2]

void CmdLine::state ( CmdLine::CmdLineState  s)

Specify new set of state flags.

Parameters
sstate to set

◆ setState()

void CmdLine::setState ( CmdLine::CmdLineState  s)

Set only given state flags.

Other state flags are not modified.

Parameters
sstate to set

◆ clearState()

void CmdLine::clearState ( CmdLine::CmdLineState  s = STATE_ALL)

Clear only the given state flags.

Other state flags are not modified.

Parameters
sstate to clear

◆ hasState()

bool CmdLine::hasState ( CmdLine::CmdLineState  s) const

Checks if the given state flags are set.

Parameters
sstate to check
Returns
true if the given state flags are set

◆ flags() [1/2]

CmdLine::CmdLineFlags CmdLine::flags ( ) const

Return CmdLine's flags.

Returns
flags

◆ flags() [2/2]

void CmdLine::flags ( CmdLine::CmdLineFlags  f)

Specify new set of flags.

Parameters
fFlags to set

◆ setFlags()

void CmdLine::setFlags ( CmdLine::CmdLineFlags  f)

Set only the given flags.

Other flags are not modified.

Parameters
fFlags to set

◆ clearFlags()

void CmdLine::clearFlags ( CmdLine::CmdLineFlags  f = FLAG_ALL)

Clear only the given flags.

Other flags are not modified.

Parameters
fFlags to clear

◆ hasFlag()

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

Checks if the given flags are set.

Parameters
fflags to check
Returns
true if the given flags are set

◆ error() [1/2]

std::ostream& CmdLine::error ( ) const

Return CmdLine's error output stream.

Returns
error output stream

◆ error() [2/2]

void CmdLine::error ( std::ostream &  os)

Specify CmdLine's error output stream.

Parameters
oserror output stream

◆ parse()

CmdLine::CmdLineStatus CmdLine::parse ( ArgIter argiter,
bool  autoProcessing = true 
)

Parse a set of arguments.

Pre- and post-processing (resetting arguments, ...) is automatically performed (by calling prologue() and epilogue()) if autoProcessing is true.

Parameters
argiterArgIter that provides the arguments
autoProcessingif true perform pre- and post-processing
Returns
resultant status

◆ prologue()

CmdLine::CmdLineStatus CmdLine::prologue ( )

Perform the necessary pre-processing.

Returns
resultant status

◆ parseArg()

CmdLine::CmdLineStatus CmdLine::parseArg ( const char *  arg)

Parse a single argument.

Parameters
argargument to be processed
Returns
resultant status

◆ epilogue()

CmdLine::CmdLineStatus CmdLine::epilogue ( )

Perform the necessary post-processing.

Exceptions
CmdLineSyntaxError
Returns
resultant status

◆ nargsParsed()

unsigned int CmdLine::nargsParsed ( ) const

Find out the number of arguments parsed so far.

Returns
number of arguments parsed

◆ getArg() [1/2]

CmdArg* CmdLine::getArg ( char  shortName) const

Retrieve an argument based on its short name.

Returns NULL if no argument matches the given character.

Parameters
shortNameshort name to find
Returns
pointer to matching CmdArg

◆ getArg() [2/2]

CmdArg* CmdLine::getArg ( const std::string &  keyword) const

Retrieve an argument based on its long name.

Returns NULL if no argument matches the given character.

Parameters
longNamelong name to find
Returns
pointer to matching CmdArg

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