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

The command line parser. More...

Inheritance diagram for Deepsolver::CliParser:
DsInstallCliParser DsPatchCliParser

List of all members.

Classes

struct  Key
 An available command line argument. More...
struct  Param

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 the available arguments.
void addKey (const std::string &name, const std::string &argName, const std::string &descr)
 Adds new argument to the table of the 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 user.
bool isKeyUsed (const std::string &keyName, std::string &arg) const
 Checks if the argument was mentioned by a user.
bool isKeyUsed (const std::string &keyName) const
 Checks if the argument was mentioned by a user.
void parse ()
 Runs main parsing process.
void printHelp (std::ostream &s) const
 Prints the help screen.
virtual ~CliParser ()
 The destructor.

Public Attributes

StringVector files
 The arguments not present in available arguments table or mentioned after "--" sequence.
KeyVector keys
 The 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 a required parameter.
virtual void stopNoPrgName () const
 Indicates that 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 an 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 is a parsing itself, which analyzes all 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. The default behaviour performs clustering and parsing based on user-defined table of available command line arguments. On custom implementation the internal table of available arguments is used only for help screen printing, unless custom variant is not explicitly involve them.


Member Function Documentation

void CliParser::addKey ( const std::string &  name,
const std::string &  descr 
)
Parameters:
[in]nameThe name of the argument to add
[in]descrThe argument description for a help screen
void CliParser::addKey ( const std::string &  name,
const std::string &  argName,
const std::string &  descr 
)
Parameters:
[in]nameThe name of the argument to add
[in]argNameThe name of additional parameter (used in a help screen)
[in]descrThe argument description for a help screen
void CliParser::addKeyDoubleName ( const std::string &  name1,
const std::string &  name2,
const std::string &  descr 
)
Parameters:
[in]name1The name of the argument to add
[in]name2The synonym of the argument to add
[in]descrThe argument description for a help screen
void CliParser::addKeyDoubleName ( const std::string &  name1,
const std::string &  name2,
const std::string &  argName,
const std::string &  descr 
)
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 a help screen)
[in]descrThe argument description for a help screen
CliParser::KeyVector::size_type CliParser::findKey ( const std::string &  name) const [protected]
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

Parameters:
nameThe name of the argument to check
Returns:
Non-zero if the specified argument has an additional parameter
void Deepsolver::CliParser::init ( int  argc,
char *  argv[] 
)
Parameters:
[in]argcThe argc value provided to the application main()function
[in]argvThe argv value provided to the application main()function
bool Deepsolver::CliParser::isKeyUsed ( const std::string &  keyName,
std::string &  arg 
) const
Parameters:
[in]keyNameThe name of the argument to check
[out]argThe reference to the variable to receive additional parameter value
Returns:
Non-zero if the command line argument was mentioned by a user
bool Deepsolver::CliParser::isKeyUsed ( const std::string &  keyName) const
Parameters:
[in]keyNameThe name of an argument to check
Returns:
Non-zero if the argument was mentioned by a user
virtual void Deepsolver::CliParser::parseCluster ( const StringVector &  cluster,
int &  mode 
) [protected, virtual]
Parameters:
[in]clusterThe arguments of one cluster to parse
[in/out]mode The additional mode variable with user-defined purpose

Reimplemented in DsPatchCliParser, and DsInstallCliParser.

void CliParser::printHelp ( std::ostream &  s) const
Parameters:
[in]sThe stream to print help screen to
virtual size_t Deepsolver::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 a 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, and DsInstallCliParser.

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 a parameter.
void CliParser::stopNoPrgName ( ) const [protected, virtual]

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