Better doc.
1.1 --- a/lemon/arg_parser.cc Mon Mar 12 15:00:33 2007 +0000
1.2 +++ b/lemon/arg_parser.cc Tue Mar 13 12:33:40 2007 +0000
1.3 @@ -28,7 +28,7 @@
1.4
1.5 ArgParser::ArgParser(int argc, char **argv) :_argc(argc), _argv(argv),
1.6 _command_name(argv[0]) {
1.7 - refOption("-help","Print a short help message",_showHelp,this);
1.8 + funcOption("-help","Print a short help message",_showHelp,this);
1.9 synonym("help","-help");
1.10 synonym("h","-help");
1.11
1.12 @@ -121,10 +121,10 @@
1.13
1.14 ArgParser &ArgParser::refOption(const std::string &name,
1.15 const std::string &help,
1.16 - int &value, bool obl)
1.17 + int &ref, bool obl)
1.18 {
1.19 ParData p;
1.20 - p.int_p=&value;
1.21 + p.int_p=&ref;
1.22 p.self_delete=false;
1.23 p.help=help;
1.24 p.type=INTEGER;
1.25 @@ -135,10 +135,10 @@
1.26
1.27 ArgParser &ArgParser::refOption(const std::string &name,
1.28 const std::string &help,
1.29 - double &value, bool obl)
1.30 + double &ref, bool obl)
1.31 {
1.32 ParData p;
1.33 - p.double_p=&value;
1.34 + p.double_p=&ref;
1.35 p.self_delete=false;
1.36 p.help=help;
1.37 p.type=DOUBLE;
1.38 @@ -149,27 +149,27 @@
1.39
1.40 ArgParser &ArgParser::refOption(const std::string &name,
1.41 const std::string &help,
1.42 - bool &value, bool obl)
1.43 + bool &ref, bool obl)
1.44 {
1.45 ParData p;
1.46 - p.bool_p=&value;
1.47 + p.bool_p=&ref;
1.48 p.self_delete=false;
1.49 p.help=help;
1.50 p.type=BOOL;
1.51 p.mandatory=obl;
1.52 _opts[name]=p;
1.53
1.54 - value = false;
1.55 + ref = false;
1.56
1.57 return *this;
1.58 }
1.59
1.60 ArgParser &ArgParser::refOption(const std::string &name,
1.61 const std::string &help,
1.62 - std::string &value, bool obl)
1.63 + std::string &ref, bool obl)
1.64 {
1.65 ParData p;
1.66 - p.string_p=&value;
1.67 + p.string_p=&ref;
1.68 p.self_delete=false;
1.69 p.help=help;
1.70 p.type=STRING;
1.71 @@ -178,7 +178,7 @@
1.72 return *this;
1.73 }
1.74
1.75 - ArgParser &ArgParser::refOption(const std::string &name,
1.76 + ArgParser &ArgParser::funcOption(const std::string &name,
1.77 const std::string &help,
1.78 void (*func)(void *),void *data)
1.79 {
1.80 @@ -197,8 +197,8 @@
1.81 const std::string &opt)
1.82 {
1.83 Opts::iterator i = _opts.find(opt);
1.84 - if(i==_opts.end()) exit(3); ///\todo throw exception instead
1.85 - else if(i->second.ingroup) exit(3); ///\todo throw exception instead
1.86 + if(i==_opts.end()) throw LogicError();
1.87 + else if(i->second.ingroup) throw LogicError();
1.88 else {
1.89 GroupData &g=_groups[group];
1.90 g.opts.push_back(opt);
1.91 @@ -220,7 +220,7 @@
1.92 Opts::iterator o = _opts.find(opt);
1.93 Opts::iterator s = _opts.find(syn);
1.94 if(o==_opts.end()||s!=_opts.end())
1.95 - exit(3); ///\todo throw exception instead
1.96 + throw LogicError();
1.97 else {
1.98 ParData p;
1.99 p.help=opt;
2.1 --- a/lemon/arg_parser.h Mon Mar 12 15:00:33 2007 +0000
2.2 +++ b/lemon/arg_parser.h Tue Mar 13 12:33:40 2007 +0000
2.3 @@ -26,6 +26,7 @@
2.4 #include <iostream>
2.5 #include <sstream>
2.6 #include <algorithm>
2.7 +#include <lemon/error.h>
2.8
2.9 ///\ingroup misc
2.10 ///\file
2.11 @@ -151,50 +152,6 @@
2.12 const std::string &help,
2.13 std::string value="", bool obl=false);
2.14
2.15 -
2.16 -
2.17 -
2.18 - ///Add a new integer type option
2.19 -
2.20 - ///\param name The name of the option. The leading '-' must be omitted.
2.21 - ///\param help A help string.
2.22 - ///\retval value The value of the argument will be written to this variable.
2.23 - ///\param obl Indicate if the option is mandatory.
2.24 - ArgParser &refOption(const std::string &name,
2.25 - const std::string &help,
2.26 - int &value, bool obl=false);
2.27 -
2.28 - ///Add a new floating type option
2.29 -
2.30 - ///\param name The name of the option. The leading '-' must be omitted.
2.31 - ///\param help A help string.
2.32 - ///\retval value The value of the argument will be written to this variable.
2.33 - ///\param obl Indicate if the option is mandatory.
2.34 - ArgParser &refOption(const std::string &name,
2.35 - const std::string &help,
2.36 - double &value, bool obl=false);
2.37 -
2.38 - ///Add a new bool type option
2.39 -
2.40 - ///\param name The name of the option. The leading '-' must be omitted.
2.41 - ///\param help A help string.
2.42 - ///\retval value The value of the argument will be written to this variable.
2.43 - ///\param obl Indicate if the option is mandatory.
2.44 - ////\note A mandatory bool obtion is of very little use.)
2.45 - ArgParser &refOption(const std::string &name,
2.46 - const std::string &help,
2.47 - bool &value, bool obl=false);
2.48 -
2.49 - ///Add a new string type option
2.50 -
2.51 - ///\param name The name of the option. The leading '-' must be omitted.
2.52 - ///\param help A help string.
2.53 - ///\retval value The value of the argument will be written to this variable.
2.54 - ///\param obl Indicate if the option is mandatory.
2.55 - ArgParser &refOption(const std::string &name,
2.56 - const std::string &help,
2.57 - std::string &value, bool obl=false);
2.58 -
2.59 ///Bind a function to an option.
2.60
2.61 ///\param name The name of the option. The leading '-' must be omitted.
2.62 @@ -202,9 +159,63 @@
2.63 ///\retval func The function to be called when the option is given. It
2.64 /// must be of type "void f(void *)"
2.65 ///\param data Data to be passed to \c func
2.66 + ArgParser &funcOption(const std::string &name,
2.67 + const std::string &help,
2.68 + void (*func)(void *),void *data);
2.69 +
2.70 + ///\name Options with an external strorage.
2.71 + ///Using this functions, the value of the option will be directly written
2.72 + ///into a variable once the option appears in the command line.
2.73 +
2.74 + ///@{
2.75 +
2.76 + ///Add a new integer type option with a storage reference
2.77 +
2.78 + ///\param name The name of the option. The leading '-' must be omitted.
2.79 + ///\param help A help string.
2.80 + ///\retval ref The value of the argument will be written to this variable.
2.81 + ///\param obl Indicate if the option is mandatory.
2.82 ArgParser &refOption(const std::string &name,
2.83 const std::string &help,
2.84 - void (*func)(void *),void *data);
2.85 + int &ref, bool obl=false);
2.86 +
2.87 + ///Add a new floating type option with a storage reference
2.88 +
2.89 + ///\param name The name of the option. The leading '-' must be omitted.
2.90 + ///\param help A help string.
2.91 + ///\retval ref The value of the argument will be written to this variable.
2.92 + ///\param obl Indicate if the option is mandatory.
2.93 + ArgParser &refOption(const std::string &name,
2.94 + const std::string &help,
2.95 + double &ref, bool obl=false);
2.96 +
2.97 + ///Add a new bool type option with a storage reference
2.98 +
2.99 + ///\param name The name of the option. The leading '-' must be omitted.
2.100 + ///\param help A help string.
2.101 + ///\retval ref The value of the argument will be written to this variable.
2.102 + ///\param obl Indicate if the option is mandatory.
2.103 + ////\note A mandatory bool obtion is of very little use.)
2.104 + ArgParser &refOption(const std::string &name,
2.105 + const std::string &help,
2.106 + bool &ref, bool obl=false);
2.107 +
2.108 + ///Add a new string type option with a storage reference
2.109 +
2.110 + ///\param name The name of the option. The leading '-' must be omitted.
2.111 + ///\param help A help string.
2.112 + ///\retval ref The value of the argument will be written to this variable.
2.113 + ///\param obl Indicate if the option is mandatory.
2.114 + ArgParser &refOption(const std::string &name,
2.115 + const std::string &help,
2.116 + std::string &ref, bool obl=false);
2.117 +
2.118 + ///@}
2.119 +
2.120 + ///\name Option Groups and Synonyms
2.121 + ///
2.122 +
2.123 + ///@{
2.124
2.125 ///Boundle some options into a group
2.126
2.127 @@ -221,6 +232,12 @@
2.128 ///given at the same time
2.129 ArgParser &onlyOneGroup(const std::string &group);
2.130
2.131 + ///Make a group mandatory
2.132 +
2.133 + ///Using this function, at least one of the members of \c group
2.134 + ///must be given.
2.135 + ArgParser &mandatoryGroup(const std::string &group);
2.136 +
2.137 ///Create synonym to an option
2.138
2.139 ///With this function you can create a sysnonym called \c sys of the
2.140 @@ -228,12 +245,8 @@
2.141 ArgParser &synonym(const std::string &syn,
2.142 const std::string &opt);
2.143
2.144 - ///Make a group mandatory
2.145 + ///@}
2.146
2.147 - ///Using this function, at least one of the members of \c group
2.148 - ///must be given.
2.149 - ArgParser &mandatoryGroup(const std::string &group);
2.150 -
2.151 ///Give help string for non-parsed arguments.
2.152
2.153 ///With this function you can give help string for non-parsed arguments.
2.154 @@ -263,7 +276,7 @@
2.155 void requiresValue(std::string arg, OptType t);
2.156 void checkMandatories();
2.157
2.158 - ///\e
2.159 + ///Start the parsing process
2.160 ArgParser &parse();
2.161
2.162 /// Synonym for parse()
2.163 @@ -280,49 +293,67 @@
2.164 }
2.165
2.166
2.167 + ///Magic type for operator[]
2.168 +
2.169 + ///This is the type of the return value of ArgParser::operator[]().
2.170 + ///It automatically converts to int, double, bool or std::string, if it
2.171 + ///match the type of the option, otherwise it throws an exception.
2.172 + ///(i.e. it performs runtime type checking).
2.173 class RefType
2.174 {
2.175 ArgParser &_parser;
2.176 std::string _name;
2.177 public:
2.178 + ///\e
2.179 RefType(ArgParser &p,const std::string &n) :_parser(p),_name(n) {}
2.180 + ///\e
2.181 operator bool()
2.182 {
2.183 Opts::iterator i = _parser._opts.find(_name);
2.184 - if(i==_parser._opts.end()) exit(3); ///\todo throw exception instead
2.185 - else if(i->second.type!=ArgParser::BOOL) exit(3);
2.186 + if(i==_parser._opts.end()) throw LogicError();
2.187 + else if(i->second.type!=ArgParser::BOOL)
2.188 + throw LogicError();
2.189 else return *(i->second.bool_p);
2.190 }
2.191 + ///\e
2.192 operator std::string()
2.193 {
2.194 Opts::iterator i = _parser._opts.find(_name);
2.195 - if(i==_parser._opts.end()) exit(3); ///\todo throw exception instead
2.196 - else if(i->second.type!=ArgParser::STRING) exit(3);
2.197 + if(i==_parser._opts.end()) throw LogicError();
2.198 + else if(i->second.type!=ArgParser::STRING)
2.199 + throw LogicError();
2.200 else return *(i->second.string_p);
2.201 }
2.202 + ///\e
2.203 operator double()
2.204 {
2.205 Opts::iterator i = _parser._opts.find(_name);
2.206 - if(i==_parser._opts.end()) exit(3); ///\todo throw exception instead
2.207 - else if(i->second.type!=ArgParser::DOUBLE) exit(3);
2.208 + if(i==_parser._opts.end()) throw LogicError();
2.209 + else if(i->second.type!=ArgParser::DOUBLE)
2.210 + throw LogicError();
2.211 else return *(i->second.double_p);
2.212 }
2.213 + ///\e
2.214 operator int()
2.215 {
2.216 Opts::iterator i = _parser._opts.find(_name);
2.217 - if(i==_parser._opts.end()) exit(3); ///\todo throw exception instead
2.218 - else if(i->second.type!=ArgParser::INTEGER) exit(3);
2.219 + if(i==_parser._opts.end()) throw LogicError();
2.220 + else if(i->second.type!=ArgParser::INTEGER)
2.221 + throw LogicError();
2.222 else return *(i->second.int_p);
2.223 }
2.224
2.225 };
2.226
2.227 + ///Give back the value of an option
2.228 +
2.229 + ///Give back the value of an option
2.230 + ///\sa RefType
2.231 RefType operator[](const std::string &n)
2.232 {
2.233 return RefType(*this, n);
2.234 - }
2.235 -
2.236 -
2.237 + }
2.238 +
2.239 };
2.240 }
2.241