1.1 --- a/lemon/arg_parser.h Mon Mar 12 15:00:33 2007 +0000
1.2 +++ b/lemon/arg_parser.h Tue Mar 13 12:33:40 2007 +0000
1.3 @@ -26,6 +26,7 @@
1.4 #include <iostream>
1.5 #include <sstream>
1.6 #include <algorithm>
1.7 +#include <lemon/error.h>
1.8
1.9 ///\ingroup misc
1.10 ///\file
1.11 @@ -151,50 +152,6 @@
1.12 const std::string &help,
1.13 std::string value="", bool obl=false);
1.14
1.15 -
1.16 -
1.17 -
1.18 - ///Add a new integer type option
1.19 -
1.20 - ///\param name The name of the option. The leading '-' must be omitted.
1.21 - ///\param help A help string.
1.22 - ///\retval value The value of the argument will be written to this variable.
1.23 - ///\param obl Indicate if the option is mandatory.
1.24 - ArgParser &refOption(const std::string &name,
1.25 - const std::string &help,
1.26 - int &value, bool obl=false);
1.27 -
1.28 - ///Add a new floating type option
1.29 -
1.30 - ///\param name The name of the option. The leading '-' must be omitted.
1.31 - ///\param help A help string.
1.32 - ///\retval value The value of the argument will be written to this variable.
1.33 - ///\param obl Indicate if the option is mandatory.
1.34 - ArgParser &refOption(const std::string &name,
1.35 - const std::string &help,
1.36 - double &value, bool obl=false);
1.37 -
1.38 - ///Add a new bool type option
1.39 -
1.40 - ///\param name The name of the option. The leading '-' must be omitted.
1.41 - ///\param help A help string.
1.42 - ///\retval value The value of the argument will be written to this variable.
1.43 - ///\param obl Indicate if the option is mandatory.
1.44 - ////\note A mandatory bool obtion is of very little use.)
1.45 - ArgParser &refOption(const std::string &name,
1.46 - const std::string &help,
1.47 - bool &value, bool obl=false);
1.48 -
1.49 - ///Add a new string type option
1.50 -
1.51 - ///\param name The name of the option. The leading '-' must be omitted.
1.52 - ///\param help A help string.
1.53 - ///\retval value The value of the argument will be written to this variable.
1.54 - ///\param obl Indicate if the option is mandatory.
1.55 - ArgParser &refOption(const std::string &name,
1.56 - const std::string &help,
1.57 - std::string &value, bool obl=false);
1.58 -
1.59 ///Bind a function to an option.
1.60
1.61 ///\param name The name of the option. The leading '-' must be omitted.
1.62 @@ -202,9 +159,63 @@
1.63 ///\retval func The function to be called when the option is given. It
1.64 /// must be of type "void f(void *)"
1.65 ///\param data Data to be passed to \c func
1.66 + ArgParser &funcOption(const std::string &name,
1.67 + const std::string &help,
1.68 + void (*func)(void *),void *data);
1.69 +
1.70 + ///\name Options with an external strorage.
1.71 + ///Using this functions, the value of the option will be directly written
1.72 + ///into a variable once the option appears in the command line.
1.73 +
1.74 + ///@{
1.75 +
1.76 + ///Add a new integer type option with a storage reference
1.77 +
1.78 + ///\param name The name of the option. The leading '-' must be omitted.
1.79 + ///\param help A help string.
1.80 + ///\retval ref The value of the argument will be written to this variable.
1.81 + ///\param obl Indicate if the option is mandatory.
1.82 ArgParser &refOption(const std::string &name,
1.83 const std::string &help,
1.84 - void (*func)(void *),void *data);
1.85 + int &ref, bool obl=false);
1.86 +
1.87 + ///Add a new floating type option with a storage reference
1.88 +
1.89 + ///\param name The name of the option. The leading '-' must be omitted.
1.90 + ///\param help A help string.
1.91 + ///\retval ref The value of the argument will be written to this variable.
1.92 + ///\param obl Indicate if the option is mandatory.
1.93 + ArgParser &refOption(const std::string &name,
1.94 + const std::string &help,
1.95 + double &ref, bool obl=false);
1.96 +
1.97 + ///Add a new bool type option with a storage reference
1.98 +
1.99 + ///\param name The name of the option. The leading '-' must be omitted.
1.100 + ///\param help A help string.
1.101 + ///\retval ref The value of the argument will be written to this variable.
1.102 + ///\param obl Indicate if the option is mandatory.
1.103 + ////\note A mandatory bool obtion is of very little use.)
1.104 + ArgParser &refOption(const std::string &name,
1.105 + const std::string &help,
1.106 + bool &ref, bool obl=false);
1.107 +
1.108 + ///Add a new string type option with a storage reference
1.109 +
1.110 + ///\param name The name of the option. The leading '-' must be omitted.
1.111 + ///\param help A help string.
1.112 + ///\retval ref The value of the argument will be written to this variable.
1.113 + ///\param obl Indicate if the option is mandatory.
1.114 + ArgParser &refOption(const std::string &name,
1.115 + const std::string &help,
1.116 + std::string &ref, bool obl=false);
1.117 +
1.118 + ///@}
1.119 +
1.120 + ///\name Option Groups and Synonyms
1.121 + ///
1.122 +
1.123 + ///@{
1.124
1.125 ///Boundle some options into a group
1.126
1.127 @@ -221,6 +232,12 @@
1.128 ///given at the same time
1.129 ArgParser &onlyOneGroup(const std::string &group);
1.130
1.131 + ///Make a group mandatory
1.132 +
1.133 + ///Using this function, at least one of the members of \c group
1.134 + ///must be given.
1.135 + ArgParser &mandatoryGroup(const std::string &group);
1.136 +
1.137 ///Create synonym to an option
1.138
1.139 ///With this function you can create a sysnonym called \c sys of the
1.140 @@ -228,12 +245,8 @@
1.141 ArgParser &synonym(const std::string &syn,
1.142 const std::string &opt);
1.143
1.144 - ///Make a group mandatory
1.145 + ///@}
1.146
1.147 - ///Using this function, at least one of the members of \c group
1.148 - ///must be given.
1.149 - ArgParser &mandatoryGroup(const std::string &group);
1.150 -
1.151 ///Give help string for non-parsed arguments.
1.152
1.153 ///With this function you can give help string for non-parsed arguments.
1.154 @@ -263,7 +276,7 @@
1.155 void requiresValue(std::string arg, OptType t);
1.156 void checkMandatories();
1.157
1.158 - ///\e
1.159 + ///Start the parsing process
1.160 ArgParser &parse();
1.161
1.162 /// Synonym for parse()
1.163 @@ -280,49 +293,67 @@
1.164 }
1.165
1.166
1.167 + ///Magic type for operator[]
1.168 +
1.169 + ///This is the type of the return value of ArgParser::operator[]().
1.170 + ///It automatically converts to int, double, bool or std::string, if it
1.171 + ///match the type of the option, otherwise it throws an exception.
1.172 + ///(i.e. it performs runtime type checking).
1.173 class RefType
1.174 {
1.175 ArgParser &_parser;
1.176 std::string _name;
1.177 public:
1.178 + ///\e
1.179 RefType(ArgParser &p,const std::string &n) :_parser(p),_name(n) {}
1.180 + ///\e
1.181 operator bool()
1.182 {
1.183 Opts::iterator i = _parser._opts.find(_name);
1.184 - if(i==_parser._opts.end()) exit(3); ///\todo throw exception instead
1.185 - else if(i->second.type!=ArgParser::BOOL) exit(3);
1.186 + if(i==_parser._opts.end()) throw LogicError();
1.187 + else if(i->second.type!=ArgParser::BOOL)
1.188 + throw LogicError();
1.189 else return *(i->second.bool_p);
1.190 }
1.191 + ///\e
1.192 operator std::string()
1.193 {
1.194 Opts::iterator i = _parser._opts.find(_name);
1.195 - if(i==_parser._opts.end()) exit(3); ///\todo throw exception instead
1.196 - else if(i->second.type!=ArgParser::STRING) exit(3);
1.197 + if(i==_parser._opts.end()) throw LogicError();
1.198 + else if(i->second.type!=ArgParser::STRING)
1.199 + throw LogicError();
1.200 else return *(i->second.string_p);
1.201 }
1.202 + ///\e
1.203 operator double()
1.204 {
1.205 Opts::iterator i = _parser._opts.find(_name);
1.206 - if(i==_parser._opts.end()) exit(3); ///\todo throw exception instead
1.207 - else if(i->second.type!=ArgParser::DOUBLE) exit(3);
1.208 + if(i==_parser._opts.end()) throw LogicError();
1.209 + else if(i->second.type!=ArgParser::DOUBLE)
1.210 + throw LogicError();
1.211 else return *(i->second.double_p);
1.212 }
1.213 + ///\e
1.214 operator int()
1.215 {
1.216 Opts::iterator i = _parser._opts.find(_name);
1.217 - if(i==_parser._opts.end()) exit(3); ///\todo throw exception instead
1.218 - else if(i->second.type!=ArgParser::INTEGER) exit(3);
1.219 + if(i==_parser._opts.end()) throw LogicError();
1.220 + else if(i->second.type!=ArgParser::INTEGER)
1.221 + throw LogicError();
1.222 else return *(i->second.int_p);
1.223 }
1.224
1.225 };
1.226
1.227 + ///Give back the value of an option
1.228 +
1.229 + ///Give back the value of an option
1.230 + ///\sa RefType
1.231 RefType operator[](const std::string &n)
1.232 {
1.233 return RefType(*this, n);
1.234 - }
1.235 -
1.236 -
1.237 + }
1.238 +
1.239 };
1.240 }
1.241