Deepsolver
|
The command line parser. More...
Classes | |
struct | Key |
An available command line argument. More... | |
struct | Param |
The internal entry with information about mentioned argument. | |
Public Types | |
typedef std::list< Key > | KeyList |
typedef std::vector< Key > | KeyVector |
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 ¶ms, 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. |
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.
void CliParser::addKey | ( | const std::string & | name, |
const std::string & | descr | ||
) |
use this method for an argument without synonyms and additional parameter.
[in] | name | The name of the argument to add |
[in] | descr | The 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.
[in] | name | The name of the argument to add |
[in] | argName | The name of additional parameter (used in help screen) |
[in] | descr | The 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.
[in] | name1 | The name of the argument to add |
[in] | name2 | The synonym of the argument to add |
[in] | descr | The 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.
[in] | name1 | The name of the argument to add |
[in] | name2 | The synonym of the argument to add |
[in] | argName | The name of additional parameter (used in help screen) |
[in] | descr | The 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.
[in] | name | Name of the argument to search |
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.
name | The name of the argument to check |
DEEPSOLVER_BEGIN_NAMESPACE void CliParser::init | ( | int | argc, |
char * | argv[] | ||
) |
Always start parsing procedure with this method invocation.
[in] | argc | The argc value provided to application main()function |
[in] | argv | The argv value provided to application main()function |
void CliParser::parse | ( | ) |
This method makes consequent calls of recognizeCluster() and parseCluster() methods.
virtual void Deepsolver::CliParser::parseCluster | ( | const StringVector & | cluster, |
int & | mode | ||
) | [protected, virtual] |
The default implementation of this method parses arguments according to user-defined table.
[in] | cluster | The 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 |
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.
[in] | s | The 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.
[in] | params | The list of all arguments potentially included into cluster |
[in/out] | mode The additional mode variable with user-defined purpose |
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.
[in] | keyname | The 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 Deepsolver::CliParser::wasKeyUsed | ( | const std::string & | keyName, |
std::string & | arg | ||
) | const |
This method should be used for arguments with additional parameter.
[in] | keyName | The name of an argument to check |
[out] | arg | The reference to the variable to receive additional parameter value |
bool Deepsolver::CliParser::wasKeyUsed | ( | const std::string & | keyName | ) | const |
Use this method for argument without additional parameter.
[in] | keyName | The name of an argument to check |