Changeset 2406:0ffc78641b34 in lemon-0.x
- Timestamp:
- 03/13/07 13:33:40 (17 years ago)
- Branch:
- default
- Phase:
- public
- Convert:
- svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@3237
- Location:
- lemon
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
lemon/arg_parser.cc
r2402 r2406 29 29 ArgParser::ArgParser(int argc, char **argv) :_argc(argc), _argv(argv), 30 30 _command_name(argv[0]) { 31 refOption("-help","Print a short help message",_showHelp,this);31 funcOption("-help","Print a short help message",_showHelp,this); 32 32 synonym("help","-help"); 33 33 synonym("h","-help"); … … 122 122 ArgParser &ArgParser::refOption(const std::string &name, 123 123 const std::string &help, 124 int & value, bool obl)125 { 126 ParData p; 127 p.int_p=& value;124 int &ref, bool obl) 125 { 126 ParData p; 127 p.int_p=&ref; 128 128 p.self_delete=false; 129 129 p.help=help; … … 136 136 ArgParser &ArgParser::refOption(const std::string &name, 137 137 const std::string &help, 138 double & value, bool obl)139 { 140 ParData p; 141 p.double_p=& value;138 double &ref, bool obl) 139 { 140 ParData p; 141 p.double_p=&ref; 142 142 p.self_delete=false; 143 143 p.help=help; … … 150 150 ArgParser &ArgParser::refOption(const std::string &name, 151 151 const std::string &help, 152 bool & value, bool obl)153 { 154 ParData p; 155 p.bool_p=& value;152 bool &ref, bool obl) 153 { 154 ParData p; 155 p.bool_p=&ref; 156 156 p.self_delete=false; 157 157 p.help=help; … … 160 160 _opts[name]=p; 161 161 162 value= false;162 ref = false; 163 163 164 164 return *this; … … 167 167 ArgParser &ArgParser::refOption(const std::string &name, 168 168 const std::string &help, 169 std::string & value, bool obl)170 { 171 ParData p; 172 p.string_p=& value;169 std::string &ref, bool obl) 170 { 171 ParData p; 172 p.string_p=&ref; 173 173 p.self_delete=false; 174 174 p.help=help; … … 179 179 } 180 180 181 ArgParser &ArgParser:: refOption(const std::string &name,181 ArgParser &ArgParser::funcOption(const std::string &name, 182 182 const std::string &help, 183 183 void (*func)(void *),void *data) … … 198 198 { 199 199 Opts::iterator i = _opts.find(opt); 200 if(i==_opts.end()) exit(3); ///\todo throw exception instead201 else if(i->second.ingroup) exit(3); ///\todo throw exception instead200 if(i==_opts.end()) throw LogicError(); 201 else if(i->second.ingroup) throw LogicError(); 202 202 else { 203 203 GroupData &g=_groups[group]; … … 221 221 Opts::iterator s = _opts.find(syn); 222 222 if(o==_opts.end()||s!=_opts.end()) 223 exit(3); ///\todo throw exception instead223 throw LogicError(); 224 224 else { 225 225 ParData p; -
lemon/arg_parser.h
r2402 r2406 27 27 #include <sstream> 28 28 #include <algorithm> 29 #include <lemon/error.h> 29 30 30 31 ///\ingroup misc … … 152 153 std::string value="", bool obl=false); 153 154 154 155 156 157 ///Add a new integer type option158 159 ///\param name The name of the option. The leading '-' must be omitted.160 ///\param help A help string.161 ///\retval value The value of the argument will be written to this variable.162 ///\param obl Indicate if the option is mandatory.163 ArgParser &refOption(const std::string &name,164 const std::string &help,165 int &value, bool obl=false);166 167 ///Add a new floating type option168 169 ///\param name The name of the option. The leading '-' must be omitted.170 ///\param help A help string.171 ///\retval value The value of the argument will be written to this variable.172 ///\param obl Indicate if the option is mandatory.173 ArgParser &refOption(const std::string &name,174 const std::string &help,175 double &value, bool obl=false);176 177 ///Add a new bool type option178 179 ///\param name The name of the option. The leading '-' must be omitted.180 ///\param help A help string.181 ///\retval value The value of the argument will be written to this variable.182 ///\param obl Indicate if the option is mandatory.183 ////\note A mandatory bool obtion is of very little use.)184 ArgParser &refOption(const std::string &name,185 const std::string &help,186 bool &value, bool obl=false);187 188 ///Add a new string type option189 190 ///\param name The name of the option. The leading '-' must be omitted.191 ///\param help A help string.192 ///\retval value The value of the argument will be written to this variable.193 ///\param obl Indicate if the option is mandatory.194 ArgParser &refOption(const std::string &name,195 const std::string &help,196 std::string &value, bool obl=false);197 198 155 ///Bind a function to an option. 199 156 … … 203 160 /// must be of type "void f(void *)" 204 161 ///\param data Data to be passed to \c func 162 ArgParser &funcOption(const std::string &name, 163 const std::string &help, 164 void (*func)(void *),void *data); 165 166 ///\name Options with an external strorage. 167 ///Using this functions, the value of the option will be directly written 168 ///into a variable once the option appears in the command line. 169 170 ///@{ 171 172 ///Add a new integer type option with a storage reference 173 174 ///\param name The name of the option. The leading '-' must be omitted. 175 ///\param help A help string. 176 ///\retval ref The value of the argument will be written to this variable. 177 ///\param obl Indicate if the option is mandatory. 205 178 ArgParser &refOption(const std::string &name, 206 179 const std::string &help, 207 void (*func)(void *),void *data); 180 int &ref, bool obl=false); 181 182 ///Add a new floating type option with a storage reference 183 184 ///\param name The name of the option. The leading '-' must be omitted. 185 ///\param help A help string. 186 ///\retval ref The value of the argument will be written to this variable. 187 ///\param obl Indicate if the option is mandatory. 188 ArgParser &refOption(const std::string &name, 189 const std::string &help, 190 double &ref, bool obl=false); 191 192 ///Add a new bool type option with a storage reference 193 194 ///\param name The name of the option. The leading '-' must be omitted. 195 ///\param help A help string. 196 ///\retval ref The value of the argument will be written to this variable. 197 ///\param obl Indicate if the option is mandatory. 198 ////\note A mandatory bool obtion is of very little use.) 199 ArgParser &refOption(const std::string &name, 200 const std::string &help, 201 bool &ref, bool obl=false); 202 203 ///Add a new string type option with a storage reference 204 205 ///\param name The name of the option. The leading '-' must be omitted. 206 ///\param help A help string. 207 ///\retval ref The value of the argument will be written to this variable. 208 ///\param obl Indicate if the option is mandatory. 209 ArgParser &refOption(const std::string &name, 210 const std::string &help, 211 std::string &ref, bool obl=false); 212 213 ///@} 214 215 ///\name Option Groups and Synonyms 216 /// 217 218 ///@{ 208 219 209 220 ///Boundle some options into a group … … 222 233 ArgParser &onlyOneGroup(const std::string &group); 223 234 235 ///Make a group mandatory 236 237 ///Using this function, at least one of the members of \c group 238 ///must be given. 239 ArgParser &mandatoryGroup(const std::string &group); 240 224 241 ///Create synonym to an option 225 242 … … 229 246 const std::string &opt); 230 247 231 ///Make a group mandatory 232 233 ///Using this function, at least one of the members of \c group 234 ///must be given. 235 ArgParser &mandatoryGroup(const std::string &group); 236 248 ///@} 249 237 250 ///Give help string for non-parsed arguments. 238 251 … … 264 277 void checkMandatories(); 265 278 266 /// \e279 ///Start the parsing process 267 280 ArgParser &parse(); 268 281 … … 281 294 282 295 296 ///Magic type for operator[] 297 298 ///This is the type of the return value of ArgParser::operator[](). 299 ///It automatically converts to int, double, bool or std::string, if it 300 ///match the type of the option, otherwise it throws an exception. 301 ///(i.e. it performs runtime type checking). 283 302 class RefType 284 303 { … … 286 305 std::string _name; 287 306 public: 307 ///\e 288 308 RefType(ArgParser &p,const std::string &n) :_parser(p),_name(n) {} 309 ///\e 289 310 operator bool() 290 311 { 291 312 Opts::iterator i = _parser._opts.find(_name); 292 if(i==_parser._opts.end()) exit(3); ///\todo throw exception instead 293 else if(i->second.type!=ArgParser::BOOL) exit(3); 313 if(i==_parser._opts.end()) throw LogicError(); 314 else if(i->second.type!=ArgParser::BOOL) 315 throw LogicError(); 294 316 else return *(i->second.bool_p); 295 317 } 318 ///\e 296 319 operator std::string() 297 320 { 298 321 Opts::iterator i = _parser._opts.find(_name); 299 if(i==_parser._opts.end()) exit(3); ///\todo throw exception instead 300 else if(i->second.type!=ArgParser::STRING) exit(3); 322 if(i==_parser._opts.end()) throw LogicError(); 323 else if(i->second.type!=ArgParser::STRING) 324 throw LogicError(); 301 325 else return *(i->second.string_p); 302 326 } 327 ///\e 303 328 operator double() 304 329 { 305 330 Opts::iterator i = _parser._opts.find(_name); 306 if(i==_parser._opts.end()) exit(3); ///\todo throw exception instead 307 else if(i->second.type!=ArgParser::DOUBLE) exit(3); 331 if(i==_parser._opts.end()) throw LogicError(); 332 else if(i->second.type!=ArgParser::DOUBLE) 333 throw LogicError(); 308 334 else return *(i->second.double_p); 309 335 } 336 ///\e 310 337 operator int() 311 338 { 312 339 Opts::iterator i = _parser._opts.find(_name); 313 if(i==_parser._opts.end()) exit(3); ///\todo throw exception instead 314 else if(i->second.type!=ArgParser::INTEGER) exit(3); 340 if(i==_parser._opts.end()) throw LogicError(); 341 else if(i->second.type!=ArgParser::INTEGER) 342 throw LogicError(); 315 343 else return *(i->second.int_p); 316 344 } … … 318 346 }; 319 347 348 ///Give back the value of an option 349 350 ///Give back the value of an option 351 ///\sa RefType 320 352 RefType operator[](const std::string &n) 321 353 { 322 354 return RefType(*this, n); 323 } 324 325 355 } 356 326 357 }; 327 358 }
Note: See TracChangeset
for help on using the changeset viewer.