EvolvingObjects
eoParser Class Reference

eoParser: command line parser and configuration file reader This class is persistent, so it can be stored and reloaded to restore parameter settings. More...

#include <eoParser.h>

Inheritance diagram for eoParser:
eoParameterLoader eoObject eoPersistent eoPrintable

List of all members.

Public Member Functions

 eoParser (unsigned _argc, char **_argv, std::string _programDescription="", std::string _lFileParamName="param-file", char _shortHand= 'p')
 Constructor a complete constructor that reads the command line an optionally reads a configuration file.
void processParam (eoParam &param, std::string section="")
 Processes the parameter and puts it in the appropriate section for readability.
void readFrom (std::istream &is)
 Read from a stream.
void printOn (std::ostream &os) const
 Pint on a stream.
std::string className (void) const
 className for readibility
bool userNeedsHelp (void)
 true if the user made an error or asked for help
void printHelp (std::ostream &os)
 Prints an automatic help in the specified output using the information provided by parameters.
std::string ProgramName ()
virtual bool isItThere (eoParam &_param) const
 Has param been entered by user?
std::string get (const std::string &name) const
eoParamgetParamWithLongName (const std::string &_name) const
 get a handle on a param from its longName
eoParamgetParam (const std::string &_name) const
 Get a handle on a param from its long name If not found, raise an eoMissingParamException.
template<class ValueType >
ValueType valueOf (const std::string &_name) const
 Get the value of a param from its long name If not found, raise an eoMissingParamException.
template<class ValueType >
eoValueParam< ValueType > & getORcreateParam (ValueType _defaultValue, std::string _longName, std::string _description, char _shortHand=0, std::string _section="", bool _required=false)
 Get or create parameter.
template<class ValueType >
eoValueParam< ValueType > & setORcreateParam (ValueType _defaultValue, std::string _longName, std::string _description, char _shortHand=0, std::string _section="", bool _required=false)
 Set parameter value or create parameter.
void setStopOnUnknownParam (bool _b)
 accessors to the stopOnUnknownParam value
bool getStopOnUnknownParam ()
void setPrefix (const std::string &_prefix)
 Prefix handling.
void resetPrefix ()
std::string getPrefix ()

Private Types

typedef std::multimap
< std::string, eoParam * > 
MultiMapType
typedef std::map< char,
std::string > 
ShortNameMapType
typedef std::map< std::string,
std::string > 
LongNameMapType

Private Member Functions

void doRegisterParam (eoParam &param)
std::pair< bool, std::string > getValue (eoParam &_param) const
void updateParameters ()

Private Attributes

MultiMapType params
std::string programName
std::string programDescription
ShortNameMapType shortNameMap
LongNameMapType longNameMap
bool needHelpMessage
eoValueParam< bool > needHelp
eoValueParam< bool > stopOnUnknownParam
std::vector< std::string > messages
std::string prefix

Detailed Description

eoParser: command line parser and configuration file reader This class is persistent, so it can be stored and reloaded to restore parameter settings.

Parameters can be read from argv, strings or streams, and must be specified using the following convention: --name=value or -n=value

You should not use space as a separator between the parameter and its value.

Examples:
t-eoFitnessAssembledEA.cpp, t-eoGA.cpp, t-eoGenOp.cpp, t-eoLogger.cpp, t-eoReal.cpp, t-eoSharing.cpp, and t-eoStateAndParser.cpp.

Definition at line 110 of file eoParser.h.


Constructor & Destructor Documentation

eoParser::eoParser ( unsigned  _argc,
char **  _argv,
std::string  _programDescription = "",
std::string  _lFileParamName = "param-file",
char  _shortHand = 'p' 
)

Constructor a complete constructor that reads the command line an optionally reads a configuration file.

myEo --param-file=param.rc will then load using the parameter file param.rc

Parameters:
_argccommand line arguments count
_argvcommand line parameters
_programDescriptionDescription of the work the program does
_lFileParamNameName of the parameter specifying the configuration file (--param-file)
_shortHandSingle charachter shorthand for specifying the configuration file

Definition at line 79 of file eoParser.cpp.

References processParam(), and readFrom().


Member Function Documentation

void eoParser::readFrom ( std::istream &  is) [virtual]

Read from a stream.

Parameters:
isthe input stream
Todo:
it should be the next string

Implements eoPersistent.

Definition at line 208 of file eoParser.cpp.

References eoValueParam< ValueType >::value().

Referenced by eoParser().

void eoParser::printOn ( std::ostream &  os) const [virtual]

Pint on a stream.

Parameters:
osthe output stream

Implements eoPrintable.

Definition at line 277 of file eoParser.cpp.

References eoParam::description(), eoParam::getValue(), isItThere(), eoParam::longName(), eoParam::required(), and eoParam::shortName().

virtual bool eoParser::isItThere ( eoParam _param) const [inline, virtual]

Has param been entered by user?

Checks if _param has been actually entered by the user

Implements eoParameterLoader.

Definition at line 164 of file eoParser.h.

References getValue().

Referenced by do_make_checkpoint(), do_make_checkpoint_assembled(), printOn(), and setORcreateParam().

eoParam * eoParser::getParamWithLongName ( const std::string &  _name) const

get a handle on a param from its longName

if not found, returns 0 (null pointer :-)

Not very clean (requires hard-coding of the long name twice!) but very useful in many occasions...

Definition at line 125 of file eoParser.cpp.

Referenced by getORcreateParam(), and getParam().

template<class ValueType >
ValueType eoParser::valueOf ( const std::string &  _name) const [inline]

Get the value of a param from its long name If not found, raise an eoMissingParamException.

Remember to specify the expected return type with a templated call: unsigned int popSize = eoparser.value<unsigned int>("popSize");

If the template type is not the good one, an eoWrongParamTypeException is raised.

Definition at line 199 of file eoParser.h.

References getParam(), and eoValueParam< ValueType >::value().

template<class ValueType >
eoValueParam<ValueType>& eoParser::getORcreateParam ( ValueType  _defaultValue,
std::string  _longName,
std::string  _description,
char  _shortHand = 0,
std::string  _section = "",
bool  _required = false 
) [inline]

Get or create parameter.

It seems finally that the easiest use of the above method is through the following, whose interface is similar to that of the widely-used createParam.

Definition at line 224 of file eoParser.h.

References eoParameterLoader::createParam(), and getParamWithLongName().

Referenced by do_make_checkpoint_assembled(), do_make_genotype(), do_make_pop(), eoEsMutationInit::TauBeta(), eoEsMutationInit::TauGlb(), and eoEsMutationInit::TauLcl().

template<class ValueType >
eoValueParam<ValueType>& eoParser::setORcreateParam ( ValueType  _defaultValue,
std::string  _longName,
std::string  _description,
char  _shortHand = 0,
std::string  _section = "",
bool  _required = false 
) [inline]

Set parameter value or create parameter.

This makes sure that the specified parameter has the given value. If the parameter does not exist yet, it is created.

This requires that operator<< is defined for ValueType.

Parameters:
_defaultValueDefault value.
_longNameLong name of the argument.
_descriptionDescription of the parameter.
_shortHandShort name of the argument (Optional)
_sectionName of the section where the parameter belongs.
_requiredIs the parameter mandatory?
Returns:
Corresponding parameter.

Definition at line 263 of file eoParser.h.

References eoParameterLoader::createParam(), isItThere(), and eoValueParam< ValueType >::setValue().

pair< bool, string > eoParser::getValue ( eoParam _param) const [private]
Todo:
check environment, just long names

Definition at line 173 of file eoParser.cpp.

References eoParam::longName(), and eoParam::shortName().

Referenced by isItThere().


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Friends