#include <CommandLineParser.h>
Public Member Functions | |
CommandLineParser (string description_text, int width_tokenfield=30) | |
virtual | ~CommandLineParser () |
void | addParameter (string token, string helptext, string defaultvalue=string()) |
method for adding a parameter. | |
void | parse (int argc, char **argv, bool zeroArgsForHelp=false) throw (string) |
method for parsing the command line arguments. Throws a help string if the -h switch has been activated or zeroArgsForHelp is true. | |
string | getArgument (string parameter) throw (string) |
method for retrieving a string argument. Throws a string exception if a non-optional parameter has not being set. | |
bool | isSet (string parameter) |
long int | getLongIntArgument (string parameter, int base=0) throw (string) |
method for retrieving an integer argument. Throws a string exception if a non-optional parameter has not been set or when the conversion to a number value fails. | |
double | getDoubleArgument (string parameter) throw (string) |
method for retrieving a floating-point argument. Throws a string exception if a non-optional parameter has not been set or when the conversion to a number value fails. | |
bool | getBooleanArgument (string parameter) throw (string) |
method for retrieving a boolean argument. Throws a string exception if a non-optional parameter has not been set | |
list< int > | getIntegerListArgument (string parameter) throw (string) |
method for retrieving a list of integer values. Throws a string exception if a non-optional parameter has not been set or when the conversion to a number value fails. | |
list< double > | getDoubleListArgument (string parameter) throw (string) |
method for retrieving a list of double values. Throws a string exception if a non-optional parameter has not been set or when the conversion to a number value fails. | |
string | getHelpMessage () const |
Method for getting the help text. | |
const list< string > & | getRemainingArguments () const |
Method for getting the list of remaining arguments after the last parameter argument. |
Example program:
#include <iostream> #include <new> #include <string> #include "CommandLineParser.h" using namespace std; int main(int argc, char **argv) { try{ CommandLineParser parser("CommandLineParser class example program."); parser.addParameter("-name","optional string parameter","MyName"); parser.addParameter("I","required integer parameter"); parser.addParameter("-OptionalInteger","optional integer parameter","1"); parser.addParameter("D","required double parameter"); parser.addParameter("S","command line switch"); parser.addParameter("iV","integer parameter (e.g. 0,1,2,3)"); parser.parse(argc,argv); cout << "--name argument: " << parser.getArgument("-name") << endl << "-I (integer) : " << parser.getLongIntArgument("I") << endl << "--OptionalInteger : " << parser.getLongIntArgument("-OptionalInteger") << endl << "-D (double) : " << parser.getDoubleArgument("D") << endl << "switch S " << ((parser.isSet("S")) ? "has" : "hasn't") << " been set." << endl; list<int> intlist = parser.getIntegerListArgument("iV"); for(list<int>::const_iterator it=intlist.begin(); it != intlist.end(); it++) { cout << " integer list entry: " << *it << endl; } cout << "remaining arguments: " << endl; const list<string> &remaining_args = parser.getRemainingArguments(); for(list<string>::const_iterator it=remaining_args.begin(); it != remaining_args.end(); it++) { cout << *it << endl; } } catch(string msg) { cerr << msg << endl; return -1; } catch(bad_alloc e) { cerr << "Memory allocation failed!" << endl << e.what() << endl; return -2; } catch(...) { cerr << "An unknown exception occured!" << endl; return -3; } return 0; }
When calling the program with the -h switch it prints out:
CommandLineParser class example program. Command line switches: -h - help --name - optional string parameter (default value: MyName ) -I - required integer parameter --OptionalInteger - optional integer parameter (default value: 1.0 ) -D - required double parameter -S - command line switch
./CommandLineParser -I 2 -D 2.1 -S -iV 0,2,4,6 file1 file2 --name argument: MyName -I (integer) : 2 --OptionalInteger : 1 -D (double) : 2.1 switch S has been set. integer list entry: 0 integer list entry: 2 integer list entry: 4 integer list entry: 6
CommandLineParser::CommandLineParser | ( | string | description_text, | |
int | width_tokenfield = 30 | |||
) | [inline] |
default constructor
description_text | text describing the function of the program | |
width_tokenfield | width of the token field in the help text |
virtual CommandLineParser::~CommandLineParser | ( | ) | [inline, virtual] |
destructor
void CommandLineParser::addParameter | ( | string | token, | |
string | helptext, | |||
string | defaultvalue = string() | |||
) |
method for adding a parameter.
token | the parameter has the command line switch -token | |
helptext | helptext describing the function of the switch | |
defaultvalue | default value for the parameter. Omit for required parameters; for optional parameters enter at least some nonsene |
string CommandLineParser::getArgument | ( | string | parameter | ) | throw (string) |
method for retrieving a string argument. Throws a string exception if a non-optional parameter has not being set.
parameter | token of the parameter to retrieve |
bool CommandLineParser::getBooleanArgument | ( | string | parameter | ) | throw (string) |
method for retrieving a boolean argument. Throws a string exception if a non-optional parameter has not been set
parameter | token of the parameter to retrieve |
double CommandLineParser::getDoubleArgument | ( | string | parameter | ) | throw (string) |
method for retrieving a floating-point argument. Throws a string exception if a non-optional parameter has not been set or when the conversion to a number value fails.
parameter | token of the parameter to retrieve |
list< double > CommandLineParser::getDoubleListArgument | ( | string | parameter | ) | throw (string) |
method for retrieving a list of double values. Throws a string exception if a non-optional parameter has not been set or when the conversion to a number value fails.
parameter | token of the parameter to retrieve. |
string CommandLineParser::getHelpMessage | ( | ) | const [inline] |
Method for getting the help text.
list< int > CommandLineParser::getIntegerListArgument | ( | string | parameter | ) | throw (string) |
method for retrieving a list of integer values. Throws a string exception if a non-optional parameter has not been set or when the conversion to a number value fails.
parameter | token of the parameter to retrieve. |
long int CommandLineParser::getLongIntArgument | ( | string | parameter, | |
int | base = 0 | |||
) | throw (string) |
method for retrieving an integer argument. Throws a string exception if a non-optional parameter has not been set or when the conversion to a number value fails.
parameter | token of the parameter to retrieve | |
base | base of the number system (must be between 2 and 36, for default: 10) |
bool CommandLineParser::isSet | ( | string | parameter | ) | [inline] |
method returning true if an parameter has been set
parameter | token of the parameter to retrieve |
void CommandLineParser::parse | ( | int | argc, | |
char ** | argv, | |||
bool | zeroArgsForHelp = false | |||
) | throw (string) |
method for parsing the command line arguments. Throws a help string if the -h switch has been activated or zeroArgsForHelp is true.
argc | number of strings in the array argv | |
argv | Array of C-strings with the command line parameters. | |
zeroArgsForHelp | set to true if the parser should interpret a call of the program without args as a call with the -h option |