EvolvingObjects
|
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>
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 ¶m, 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 |
eoParam * | getParamWithLongName (const std::string &_name) const |
get a handle on a param from its longName | |
eoParam * | getParam (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 ¶m) |
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 |
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.
Definition at line 110 of file eoParser.h.
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
_argc | command line arguments count |
_argv | command line parameters |
_programDescription | Description of the work the program does |
_lFileParamName | Name of the parameter specifying the configuration file (--param-file) |
_shortHand | Single charachter shorthand for specifying the configuration file |
Definition at line 79 of file eoParser.cpp.
References processParam(), and readFrom().
void eoParser::readFrom | ( | std::istream & | is | ) | [virtual] |
Read from a stream.
is | the input stream |
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.
os | the 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().
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().
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().
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.
_defaultValue | Default value. |
_longName | Long name of the argument. |
_description | Description of the parameter. |
_shortHand | Short name of the argument (Optional) |
_section | Name of the section where the parameter belongs. |
_required | Is the parameter mandatory? |
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] |
Definition at line 173 of file eoParser.cpp.
References eoParam::longName(), and eoParam::shortName().
Referenced by isItThere().