Deepsolver
Classes | Public Types | Public Member Functions | Public Attributes | Protected Member Functions
CliParser Class Reference

The command line parser. More...

Inheritance diagram for CliParser:
DsPatchCliParser

List of all members.

Classes

struct  Key
 An available command line argument. More...
struct  Param
 The internal entry with information about mentioned argument.

Public Types

typedef std::list< KeyKeyList
typedef std::vector< KeyKeyVector

Public Member Functions

void addKey (const std::string &name, const std::string &descr)
 Adds new argument to the table of available arguments.
void addKey (const std::string &name, const std::string &argName, const std::string &descr)
 Adds new argument to the table of available arguments.
void addKeyDoubleName (const std::string &name1, const std::string &name2, const std::string &descr)
 Adds new argument to the table of available arguments.
void addKeyDoubleName (const std::string &name1, const std::string &name2, const std::string &argName, const std::string &descr)
 Adds new argument to the table of available arguments.
 CliParser ()
 The default constructor.
void init (int argc, char *argv[])
 Fills internal vector of arguments mentioned by a user.
void parse ()
 Runs main parsing process.
void printHelp (std::ostream &s) const
 Prints help screen.
bool wasKeyUsed (const std::string &keyName, std::string &arg) const
 Checks if the argument was mentioned by a user.
bool wasKeyUsed (const std::string &keyName) const
 Checks if the argument was mentioned by a user.
virtual ~CliParser ()
 The destructor.

Public Attributes

StringVector files
 Arguments not present in available arguments table or mentioned after "--" sequence.
KeyVector keys
 Table of all available arguments.

Protected Member Functions

KeyVector::size_type findKey (const std::string &name) const
 Searches the argument in the table of available arguments.
bool hasKeyArgument (const std::string &name) const
virtual void parseCluster (const StringVector &cluster, int &mode)
 Parses one cluster.
virtual size_t recognizeCluster (const StringVector &params, int &mode) const
 Recognizes cluster of command line arguments.
virtual void stopMissedArgument (const std::string &keyName) const
 Indicates the argument was mentioned without required parameter.
virtual void stopNoPrgName () const
 Indicates the provided argc value equals zero.

Detailed Description

This class implements flexible approach to the command line parsing procedure. General difference from usual command line parsing utilities is in adding preliminary step before checking what arguments a user has mentioned in command line. During this step entire arguments set is divided onto several clusters. Each cluster gathers a subset of arguments which should be parsed together. The second step performs parsing itself by analyzing each constructed cluster one by one.

A user should implement recognizeCluster() and parseCluster() methods to get the exact behaviour he wants. CliParser class has its own implementation of these methods with default behaviour. Default behaviour performs clustering and parsing based on user-defined table of available command line arguments. On custom implementation internal table of available arguments will be used only for help screen printing unless custom approach is not explicitly involve it.

See also:
CliParserException

Member Function Documentation

void CliParser::addKey ( const std::string &  name,
const std::string &  descr 
)

use this method for an argument without synonyms and additional parameter.

Parameters:
[in]nameThe name of the argument to add
[in]descrThe argument description for help screen
void CliParser::addKey ( const std::string &  name,
const std::string &  argName,
const std::string &  descr 
)

use this method for an argument without synonyms but with an additional parameter.

Parameters:
[in]nameThe name of the argument to add
[in]argNameThe name of additional parameter (used in help screen)
[in]descrThe argument description for help screen
void CliParser::addKeyDoubleName ( const std::string &  name1,
const std::string &  name2,
const std::string &  descr 
)

use this method for an argument with synonym but without an additional parameter.

Parameters:
[in]name1The name of the argument to add
[in]name2The synonym of the argument to add
[in]descrThe argument description for help screen
void CliParser::addKeyDoubleName ( const std::string &  name1,
const std::string &  name2,
const std::string &  argName,
const std::string &  descr 
)

use this method for an argument with synonym and with an additional parameter.

Parameters:
[in]name1The name of the argument to add
[in]name2The synonym of the argument to add
[in]argNameThe name of additional parameter (used in help screen)
[in]descrThe argument description for help screen
CliParser::KeyVector::size_type CliParser::findKey ( const std::string &  name) const [protected]

This method performs searching respecting all argument names.

Parameters:
[in]nameName of the argument to search
Returns:
The index in the table of available arguments or (KeyVector::size_type)-1 if there is no such argument
bool CliParser::hasKeyArgument ( const std::string &  name) const [protected]

Checks if specified argument has an additional parameter

Use this method to check if argument has an additional parameter.

Parameters:
nameThe name of the argument to check
Returns:
Non-zero if specified argument has an additional parameter
void CliParser::init ( int  argc,
char *  argv[] 
)

Always start parsing procedure with this method invocation.

Parameters:
[in]argcThe argc value provided to application main()function
[in]argvThe argv value provided to application main()function
void CliParser::parse ( )

This method makes consequent calls of recognizeCluster() and parseCluster() methods.

void CliParser::parseCluster ( const StringVector &  cluster,
int &  mode 
) [protected, virtual]

The default implementation of this method parses arguments according to user-defined table.

Parameters:
[in]clusterThe arguments of one cluster to parse
[in/out]mode The additional mode variable with user-defined purpose

Reimplemented in DsPatchCliParser.

void CliParser::printHelp ( std::ostream &  s) const

This method uses user-defined table of available arguments for help screen content. Use addKey() and addKeyDoubleName() methods to fill that table with proper values.

Parameters:
[in]sThe stream to print help screen to
size_t CliParser::recognizeCluster ( const StringVector &  params,
int &  mode 
) const [protected, virtual]

The descent class may provide custom implementation of this method.

Parameters:
[in]paramsThe list of all arguments potentially included into cluster
[in/out]mode The additional mode variable with user-defined purpose
Returns:
The number of additional (excluding first) items in provided vector making the cluster

Reimplemented in DsPatchCliParser.

void CliParser::stopMissedArgument ( const std::string &  keyName) const [protected, virtual]

This method must throw an exception or call the exit() function to interrupt parsing procedure.

Parameters:
[in]keynameThe name of the argument mentioned without parameter
void CliParser::stopNoPrgName ( ) const [protected, virtual]

This method must throw an exception or call the exit() function to interrupt parsing procedure.

bool CliParser::wasKeyUsed ( const std::string &  keyName,
std::string &  arg 
) const

This method should be used for arguments with additional parameter.

Parameters:
[in]keyNameThe name of an argument to check
[out]argThe reference to the variable to receive additional parameter value
Returns:
Non-zero if command line argument was mentioned by a user
bool CliParser::wasKeyUsed ( const std::string &  keyName) const

Use this method for argument without additional parameter.

Parameters:
[in]keyNameThe name of an argument to check
Returns:
Non-zero if the argument was mentioned by a user