# HG changeset patch # User alpar # Date 1173789220 0 # Node ID 0ffc78641b345333200318503517392e74cf7ac4 # Parent 57586d25f415cff3858e985b05a1efd681c0b83e Better doc. diff -r 57586d25f415 -r 0ffc78641b34 lemon/arg_parser.cc --- a/lemon/arg_parser.cc Mon Mar 12 15:00:33 2007 +0000 +++ b/lemon/arg_parser.cc Tue Mar 13 12:33:40 2007 +0000 @@ -28,7 +28,7 @@ ArgParser::ArgParser(int argc, char **argv) :_argc(argc), _argv(argv), _command_name(argv[0]) { - refOption("-help","Print a short help message",_showHelp,this); + funcOption("-help","Print a short help message",_showHelp,this); synonym("help","-help"); synonym("h","-help"); @@ -121,10 +121,10 @@ ArgParser &ArgParser::refOption(const std::string &name, const std::string &help, - int &value, bool obl) + int &ref, bool obl) { ParData p; - p.int_p=&value; + p.int_p=&ref; p.self_delete=false; p.help=help; p.type=INTEGER; @@ -135,10 +135,10 @@ ArgParser &ArgParser::refOption(const std::string &name, const std::string &help, - double &value, bool obl) + double &ref, bool obl) { ParData p; - p.double_p=&value; + p.double_p=&ref; p.self_delete=false; p.help=help; p.type=DOUBLE; @@ -149,27 +149,27 @@ ArgParser &ArgParser::refOption(const std::string &name, const std::string &help, - bool &value, bool obl) + bool &ref, bool obl) { ParData p; - p.bool_p=&value; + p.bool_p=&ref; p.self_delete=false; p.help=help; p.type=BOOL; p.mandatory=obl; _opts[name]=p; - value = false; + ref = false; return *this; } ArgParser &ArgParser::refOption(const std::string &name, const std::string &help, - std::string &value, bool obl) + std::string &ref, bool obl) { ParData p; - p.string_p=&value; + p.string_p=&ref; p.self_delete=false; p.help=help; p.type=STRING; @@ -178,7 +178,7 @@ return *this; } - ArgParser &ArgParser::refOption(const std::string &name, + ArgParser &ArgParser::funcOption(const std::string &name, const std::string &help, void (*func)(void *),void *data) { @@ -197,8 +197,8 @@ const std::string &opt) { Opts::iterator i = _opts.find(opt); - if(i==_opts.end()) exit(3); ///\todo throw exception instead - else if(i->second.ingroup) exit(3); ///\todo throw exception instead + if(i==_opts.end()) throw LogicError(); + else if(i->second.ingroup) throw LogicError(); else { GroupData &g=_groups[group]; g.opts.push_back(opt); @@ -220,7 +220,7 @@ Opts::iterator o = _opts.find(opt); Opts::iterator s = _opts.find(syn); if(o==_opts.end()||s!=_opts.end()) - exit(3); ///\todo throw exception instead + throw LogicError(); else { ParData p; p.help=opt; diff -r 57586d25f415 -r 0ffc78641b34 lemon/arg_parser.h --- a/lemon/arg_parser.h Mon Mar 12 15:00:33 2007 +0000 +++ b/lemon/arg_parser.h Tue Mar 13 12:33:40 2007 +0000 @@ -26,6 +26,7 @@ #include #include #include +#include ///\ingroup misc ///\file @@ -151,50 +152,6 @@ const std::string &help, std::string value="", bool obl=false); - - - - ///Add a new integer type option - - ///\param name The name of the option. The leading '-' must be omitted. - ///\param help A help string. - ///\retval value The value of the argument will be written to this variable. - ///\param obl Indicate if the option is mandatory. - ArgParser &refOption(const std::string &name, - const std::string &help, - int &value, bool obl=false); - - ///Add a new floating type option - - ///\param name The name of the option. The leading '-' must be omitted. - ///\param help A help string. - ///\retval value The value of the argument will be written to this variable. - ///\param obl Indicate if the option is mandatory. - ArgParser &refOption(const std::string &name, - const std::string &help, - double &value, bool obl=false); - - ///Add a new bool type option - - ///\param name The name of the option. The leading '-' must be omitted. - ///\param help A help string. - ///\retval value The value of the argument will be written to this variable. - ///\param obl Indicate if the option is mandatory. - ////\note A mandatory bool obtion is of very little use.) - ArgParser &refOption(const std::string &name, - const std::string &help, - bool &value, bool obl=false); - - ///Add a new string type option - - ///\param name The name of the option. The leading '-' must be omitted. - ///\param help A help string. - ///\retval value The value of the argument will be written to this variable. - ///\param obl Indicate if the option is mandatory. - ArgParser &refOption(const std::string &name, - const std::string &help, - std::string &value, bool obl=false); - ///Bind a function to an option. ///\param name The name of the option. The leading '-' must be omitted. @@ -202,9 +159,63 @@ ///\retval func The function to be called when the option is given. It /// must be of type "void f(void *)" ///\param data Data to be passed to \c func + ArgParser &funcOption(const std::string &name, + const std::string &help, + void (*func)(void *),void *data); + + ///\name Options with an external strorage. + ///Using this functions, the value of the option will be directly written + ///into a variable once the option appears in the command line. + + ///@{ + + ///Add a new integer type option with a storage reference + + ///\param name The name of the option. The leading '-' must be omitted. + ///\param help A help string. + ///\retval ref The value of the argument will be written to this variable. + ///\param obl Indicate if the option is mandatory. ArgParser &refOption(const std::string &name, const std::string &help, - void (*func)(void *),void *data); + int &ref, bool obl=false); + + ///Add a new floating type option with a storage reference + + ///\param name The name of the option. The leading '-' must be omitted. + ///\param help A help string. + ///\retval ref The value of the argument will be written to this variable. + ///\param obl Indicate if the option is mandatory. + ArgParser &refOption(const std::string &name, + const std::string &help, + double &ref, bool obl=false); + + ///Add a new bool type option with a storage reference + + ///\param name The name of the option. The leading '-' must be omitted. + ///\param help A help string. + ///\retval ref The value of the argument will be written to this variable. + ///\param obl Indicate if the option is mandatory. + ////\note A mandatory bool obtion is of very little use.) + ArgParser &refOption(const std::string &name, + const std::string &help, + bool &ref, bool obl=false); + + ///Add a new string type option with a storage reference + + ///\param name The name of the option. The leading '-' must be omitted. + ///\param help A help string. + ///\retval ref The value of the argument will be written to this variable. + ///\param obl Indicate if the option is mandatory. + ArgParser &refOption(const std::string &name, + const std::string &help, + std::string &ref, bool obl=false); + + ///@} + + ///\name Option Groups and Synonyms + /// + + ///@{ ///Boundle some options into a group @@ -221,6 +232,12 @@ ///given at the same time ArgParser &onlyOneGroup(const std::string &group); + ///Make a group mandatory + + ///Using this function, at least one of the members of \c group + ///must be given. + ArgParser &mandatoryGroup(const std::string &group); + ///Create synonym to an option ///With this function you can create a sysnonym called \c sys of the @@ -228,12 +245,8 @@ ArgParser &synonym(const std::string &syn, const std::string &opt); - ///Make a group mandatory + ///@} - ///Using this function, at least one of the members of \c group - ///must be given. - ArgParser &mandatoryGroup(const std::string &group); - ///Give help string for non-parsed arguments. ///With this function you can give help string for non-parsed arguments. @@ -263,7 +276,7 @@ void requiresValue(std::string arg, OptType t); void checkMandatories(); - ///\e + ///Start the parsing process ArgParser &parse(); /// Synonym for parse() @@ -280,49 +293,67 @@ } + ///Magic type for operator[] + + ///This is the type of the return value of ArgParser::operator[](). + ///It automatically converts to int, double, bool or std::string, if it + ///match the type of the option, otherwise it throws an exception. + ///(i.e. it performs runtime type checking). class RefType { ArgParser &_parser; std::string _name; public: + ///\e RefType(ArgParser &p,const std::string &n) :_parser(p),_name(n) {} + ///\e operator bool() { Opts::iterator i = _parser._opts.find(_name); - if(i==_parser._opts.end()) exit(3); ///\todo throw exception instead - else if(i->second.type!=ArgParser::BOOL) exit(3); + if(i==_parser._opts.end()) throw LogicError(); + else if(i->second.type!=ArgParser::BOOL) + throw LogicError(); else return *(i->second.bool_p); } + ///\e operator std::string() { Opts::iterator i = _parser._opts.find(_name); - if(i==_parser._opts.end()) exit(3); ///\todo throw exception instead - else if(i->second.type!=ArgParser::STRING) exit(3); + if(i==_parser._opts.end()) throw LogicError(); + else if(i->second.type!=ArgParser::STRING) + throw LogicError(); else return *(i->second.string_p); } + ///\e operator double() { Opts::iterator i = _parser._opts.find(_name); - if(i==_parser._opts.end()) exit(3); ///\todo throw exception instead - else if(i->second.type!=ArgParser::DOUBLE) exit(3); + if(i==_parser._opts.end()) throw LogicError(); + else if(i->second.type!=ArgParser::DOUBLE) + throw LogicError(); else return *(i->second.double_p); } + ///\e operator int() { Opts::iterator i = _parser._opts.find(_name); - if(i==_parser._opts.end()) exit(3); ///\todo throw exception instead - else if(i->second.type!=ArgParser::INTEGER) exit(3); + if(i==_parser._opts.end()) throw LogicError(); + else if(i->second.type!=ArgParser::INTEGER) + throw LogicError(); else return *(i->second.int_p); } }; + ///Give back the value of an option + + ///Give back the value of an option + ///\sa RefType RefType operator[](const std::string &n) { return RefType(*this, n); - } - - + } + }; }