Core Library
Classes | Public Types | Public Member Functions | Private Types | Private Member Functions | Static Private Member Functions | Private Attributes
Core::CmdLineParser Class Reference

A command line parser. More...

#include <CmdLineParser.hpp>

List of all members.

Classes

struct  IsLongName
 The predicate for matching the switches long name. More...
struct  IsSeparator
 The predicate for finding the name/value separator. More...
struct  IsShortName
 The predicate for matching the switches short name. More...

Public Types

enum  Format { WINDOWS = 0x0001, UNIX = 0x0002 }
 The switch formats. More...
enum  Flag {
  ALLOW_WIN_FORMAT = WINDOWS, ALLOW_UNIX_FORMAT = UNIX, ALLOW_UNNAMED = 0x0004, ALLOW_ANY_FORMAT = (ALLOW_WIN_FORMAT | ALLOW_UNIX_FORMAT),
  DEFAULTS = ALLOW_ANY_FORMAT | ALLOW_UNNAMED
}
 The parsing flags. More...
typedef const CmdLineSwitchSwitchCIter
 An iterator into command line switch table.
typedef std::vector< tstringStringVector
 A vector of strings.
typedef std::map< int,
StringVector
NamedArgs
 The type used to store the named arguments.
typedef StringVector UnnamedArgs
 The type used to store the unnamed arguments.

Public Member Functions

 CmdLineParser (SwitchCIter itFirstSwitch, SwitchCIter itLastSwitch)
 Default constructor.
 ~CmdLineParser ()
 Destructor.
const NamedArgsgetNamedArgs () const
 Get the collection of named arguments.
const UnnamedArgsgetUnnamedArgs () const
 Get the collection of unnamed arguments.
void parse (int argc, tchar *argv[], int nFlags=DEFAULTS)
 Parse the command line.
bool isSwitchSet (int nID) const
 Check if a switch was provided.
tstring getSwitchValue (int nID) const
 Get the value for a switch.
tstring formatSwitches (Format eFormat) const
 Generate the list of switches for a usage message.

Private Types

typedef const tcharCharCIter
 An iterator for c-style strings.

Private Member Functions

void reset ()
 Reset the internal state.
SwitchCIter findSwitch (CharCIter itNameFirst, CharCIter itNameLast)
 Try and match the argument to a switch definition.

Static Private Member Functions

static void validateSwitches (SwitchCIter first, SwitchCIter last)
 Validate the switch definitions.

Private Attributes

SwitchCIter m_itFirstSwitch
 The first switch in the table.
SwitchCIter m_itLastSwitch
 The last switch in the table.
NamedArgs m_mapNamedArgs
 The collection of named arguments.
UnnamedArgs m_vecUnnamedArgs
 The collection of unnamed arguments.

Detailed Description

A command line parser.

The parser supports both Unix style (eg -h & --help) and Windows style (eg /h & /help) switches. The value for the switch can be specifed after the switch name (eg -p <file>) or inline in either format (eg /p:<file> or --print=<file>). A switch can also be referenced multiple times and/or have multiple values (eg -x <value> -x <value> or -x <value>

).


Member Typedef Documentation

An iterator into command line switch table.

A vector of strings.

The type used to store the named arguments.

The type used to store the unnamed arguments.

typedef const tchar* Core::CmdLineParser::CharCIter [private]

An iterator for c-style strings.


Member Enumeration Documentation

The switch formats.

Enumerator:
WINDOWS 

Allow Windows format switches.

UNIX 

Allow Unix format switches.

The parsing flags.

Enumerator:
ALLOW_WIN_FORMAT 

Allow Windows format switches.

ALLOW_UNIX_FORMAT 

Allow Unix format switches.

ALLOW_UNNAMED 

Allow unnamed arguments.

ALLOW_ANY_FORMAT 
DEFAULTS 

Constructor & Destructor Documentation

Core::CmdLineParser::CmdLineParser ( SwitchCIter  itFirstSwitch,
SwitchCIter  itLastSwitch 
)

Default constructor.

Destructor.


Member Function Documentation

Get the collection of named arguments.

Get the collection of unnamed arguments.

void Core::CmdLineParser::parse ( int  argc,
tchar argv[],
int  nFlags = DEFAULTS 
)

Parse the command line.

bool Core::CmdLineParser::isSwitchSet ( int  nID) const

Check if a switch was provided.

Get the value for a switch.

This only returns the first value that was parsed. To obtain the full list iterate the NamedArgs collection directly.

Generate the list of switches for a usage message.

void Core::CmdLineParser::reset ( ) [private]

Reset the internal state.

Try and match the argument to a switch definition.

void Core::CmdLineParser::validateSwitches ( SwitchCIter  first,
SwitchCIter  last 
) [static, private]

Validate the switch definitions.


Member Data Documentation

The first switch in the table.

The last switch in the table.

The collection of named arguments.

The collection of unnamed arguments.


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