CommandLineParser Class Reference

This class provides an easy-to-use command line argument parser. More...

#include <CommandLineParser.h>

List of all members.

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.


Detailed Description

This class provides an easy-to-use command line argument parser.

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
 
Full example:
 ./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
 
Author:
Ruediger Knoerig

Constructor & Destructor Documentation

CommandLineParser::CommandLineParser ( string  description_text,
int  width_tokenfield = 30 
) [inline]

default constructor

Parameters:
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


Member Function Documentation

void CommandLineParser::addParameter ( string  token,
string  helptext,
string  defaultvalue = string() 
)

method for adding a parameter.

Parameters:
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.

Parameters:
parameter token of the parameter to retrieve
Returns:
assigned argument for the parameter

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

Parameters:
parameter token of the parameter to retrieve
Returns:
assigned argument for the parameter

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.

Parameters:
parameter token of the parameter to retrieve
Returns:
assigned argument for the parameter

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.

Parameters:
parameter token of the parameter to retrieve.
Returns:
assigned argument for the parameter.

string CommandLineParser::getHelpMessage (  )  const [inline]

Method for getting the help text.

Returns:
help message

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.

Parameters:
parameter token of the parameter to retrieve.
Returns:
assigned argument for the parameter.

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.

Parameters:
parameter token of the parameter to retrieve
base base of the number system (must be between 2 and 36, for default: 10)
Returns:
assigned argument for the parameter

bool CommandLineParser::isSet ( string  parameter  )  [inline]

method returning true if an parameter has been set

Parameters:
parameter token of the parameter to retrieve
Returns:
true if the command line switch was in use

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.

Parameters:
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


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

Generated on Mon Feb 9 18:06:22 2009 for CommandLineParser by  doxygen 1.5.8