Args Class Reference

Handle the command line arguments. More...

#include <Args.h>

List of all members.

Public Member Functions

 Args ()
 The constructor.
void setGroup (const string &groupName)
 Defines the current groups of arguments.
void declareArgument (const string &name)
 Declare a simple argument.
void declareArgument (const string &name, const string &description, const int numOptions=0, const string &options="")
 Declare an argument.
void printGroup (const string &groupName, ostream &out=cout, const int indSpaces=3)
 Display the arguments (with description and options) associated with the groupName passed.
ArgsOutType readArguments (const int argc, char *argv[])
 Read the arguments passed with the commandline.
bool hasArgument (const string &argument) const
 Ask if an argument has been provided via command line.
vector< string > & getValuesVector (const string &argument)
 Returns the vector of the values that belongs to the given argument.
template<typename T>
void getValue (const string &argument, int index, T &valueToFill)
 Get the nth value of the given argument, where n = index.

Private Member Functions

string getWrappedString (const string &str, const int leftSpace=0, const bool spacesInFirstLine=true) const
 Return a string which has been wrapped to fit into _maxColumns columns.

Private Attributes

string _currentGroup
 The current group.
int _maxColumns
 The maximum number of column (in a loosely sense) allowed to be printed.
map< string, vector< Argument > > _groupedList
 The list of arguments per group.
map< string, int > _argsList
 Arguments declared.
map< string, vector< string > > _resArgs
 Arguments found.

Classes

struct  Argument
 Holds the informations of a single argument. More...


Detailed Description

Handle the command line arguments.

Provide an easy (but simple) way to handle parameters in a command line program. It works by first declaring the arguments via declareArgument, for instance:

 // declare a simple argument
 args.declareArgument("-help"); 
 // Declare an argument -test which takes two following arguments.
 args.declareArgument("-test", "Test the model.", 2, "<dataFile> <shypFile>");

Once the arguments are declared, readArguments() takes the standard C arguments and fill a map with the ones selected by the user. If they do not follow the declaration's constraints, readArguments returns an error. When the user needs the values of an argument, it just calls the getValue() or getValuesVector().

It is also possible to defines groups of options, using setGroup before the call of any declareArgument. The name passed, will be used to print a nicely formatted list of the group's arguments using printGroup().

Date:
10/11/2005
Todo:
Right now it is only possible to declare a fixed number of possible options for each argument. This is obviously limiting, and must be fixed.

Definition at line 81 of file Args.h.


Constructor & Destructor Documentation

Args  )  [inline]
 

The constructor.

Set the current group to "general".

Date:
10/11/2005

Definition at line 89 of file Args.h.


Member Function Documentation

void declareArgument const string &  name,
const string &  description,
const int  numOptions = 0,
const string &  options = ""
 

Declare an argument.

This type of argument has a description, and might have options. For instance an argument such as -file filename.txt is declared as

 declareArgument( "-file", "Open a file", 1, "<filename>" );
Parameters:
name The name of the argument.
description The description of the argument.
numOptions The number of options that follow the argument.
options The names list of the options that will be printed with the argument, when help is requested.
See also:
printGroup

setGroup

Date:
28/11/2005

Definition at line 33 of file Args.cpp.

References Args::_argsList, Args::_currentGroup, and Args::_groupedList.

void declareArgument const string &  name  ) 
 

Declare a simple argument.

Just declare an argument without following options, nor descriptions.

Parameters:
name The name of the argument.
Date:
28/11/2005

Definition at line 26 of file Args.cpp.

References Args::_argsList.

Referenced by StumpLearner::declareArguments().

void getValue const string &  argument,
int  index,
T &  valueToFill
[inline]
 

Get the nth value of the given argument, where n = index.

The value is stored in the variable valueToFill which can be any type, thanks to the use of templates and string stream. For instance with the command line:

myProgram -arg val1 val2 10 11 
we have:
 int val;
 getValue("-arg", 2, val); // -> val = 10
Parameters:
argument The name of the argument.
index The index of the value belonging to the argument.
valueToFill The value read.
Remarks:
The index start counting from zero.
Date:
16/11/2005

Definition at line 209 of file Args.h.

References Args::_resArgs.

Referenced by AdaBoostLearner::AdaBoostLearner(), Classifier::Classifier(), and StumpLearner::initOptions().

vector< string > & getValuesVector const string &  argument  ) 
 

Returns the vector of the values that belongs to the given argument.

For instance with the command line:

myProgram -arg val1 val2 10 11 
we have:
 getValuesVector("-arg"); // -> ["val1", "val2", "10", "11"]
Parameters:
argument The name of the argument.
Returns:
A vector with the values belonging to the argument.
Date:
16/11/2005

Definition at line 112 of file Args.cpp.

References Args::_resArgs.

string getWrappedString const string &  str,
const int  leftSpace = 0,
const bool  spacesInFirstLine = true
const [private]
 

Return a string which has been wrapped to fit into _maxColumns columns.

For instance, if _maxColumns is set to 20:

 string s = "A simple test of wrapping a line into some more. Here is how it goes";
 string w1 = getWrappedString(s, 5);
 string w2 = getWrappedString(s, 5, false);
 cout << "w1: " << endl << w1 << endl;
 cout << "w2: " << endl << w1 << endl;
will print:
w1:
     A simple test of wrapping 
     a line into some more.
     Here is how it goes.
w2:
A simple test of wrapping 
     a line into some more.
     Here is how it goes.
Parameters:
str The string to be wrapped.
leftSpace The number of spaces before the line gets printed.
spacesInFirstLine If true, the first line will have spaces as all the other lines, otherwise it will begin without spaces.
Remarks:
The _maxColumns limit is not strict. It is checked only at the end of each word.
See also:
printGroup
Date:
28/11/2005

Definition at line 125 of file Args.cpp.

References Args::_maxColumns.

Referenced by Args::printGroup().

bool hasArgument const string &  argument  )  const [inline]
 

Ask if an argument has been provided via command line.

Parameters:
argument The argument to be checked.
Returns:
True if the argument has been written by the user, false otherwise.
Remarks:
It does not check if the argument has been declared, but if it has been called by the user at the command line.
Date:
16/11/2005

Definition at line 174 of file Args.h.

References Args::_resArgs.

Referenced by AdaBoostLearner::AdaBoostLearner(), Classifier::Classifier(), StumpLearner::initOptions(), and InputData::initOptions().

void printGroup const string &  groupName,
ostream &  out = cout,
const int  indSpaces = 3
 

Display the arguments (with description and options) associated with the groupName passed.

Example:

 Args a;
 a.setGroup("group1");
 a.declareArgument("-arg1", "The first argument");
 a.declareArgument("-arg2", "The second argument");
 a.setGroup("group2");
 a.declareArgument("-arg3", "Another argument");
Then, printGroup("group1"), will print:
group1:
   -arg1:
      The first argument
   -arg2:
      The second argument
Parameters:
groupName The name of the group.
out The output stream. By default is cout.
indSpaces The number of spaces for the indentation.
Date:
28/11/2005

Definition at line 45 of file Args.cpp.

References Args::_groupedList, and Args::getWrappedString().

ArgsOutType readArguments const int  argc,
char *  argv[]
 

Read the arguments passed with the commandline.

Parameters:
argc The number of arguments (standard C in main()).
argv The arguments (standard C in main()).
Returns:
An enum that report on the result of the reading.
See also:
ArgsOutType
Date:
16/11/2005

Definition at line 71 of file Args.cpp.

References Args::_argsList, Args::_resArgs, nor_utils::AOT_NO_ARGUMENTS, nor_utils::AOT_TOO_FEW_VALUES, and nor_utils::AOT_UNKOWN_ARGUMENT.

void setGroup const string &  groupName  )  [inline]
 

Defines the current groups of arguments.

The group defines which argument has to be printed together. To be used before the subsequent call of declareArgument.

Parameters:
groupName The name of the group.
See also:
printGroup

declareArgument(const string&)

declareArgument(const string&, const string&, const int, const string&)

Date:
28/11/2005

Definition at line 101 of file Args.h.

References Args::_currentGroup.


Member Data Documentation

string _currentGroup [private]
 

The current group.

See also:
setGroup

declareArgument

Definition at line 293 of file Args.h.

Referenced by Args::declareArgument(), and Args::setGroup().

int _maxColumns [private]
 

The maximum number of column (in a loosely sense) allowed to be printed.

See also:
getWrappedString

printGroup

Definition at line 300 of file Args.h.

Referenced by Args::getWrappedString().


The documentation for this class was generated from the following files:
Generated on Mon Nov 28 21:43:48 2005 for MultiBoost by  doxygen 1.4.5