Changeset 209:765619b7cbb2 in lemon for lemon/arg_parser.h
- Timestamp:
- 07/13/08 20:51:02 (17 years ago)
- Branch:
- default
- Phase:
- public
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
lemon/arg_parser.h
r204 r209 1 /* -*- C++-*-2 * 3 * This file is a part of LEMON, a generic C++ optimization library 1 /* -*- mode: C++; indent-tabs-mode: nil; -*- 2 * 3 * This file is a part of LEMON, a generic C++ optimization library. 4 4 * 5 5 * Copyright (C) 2003-2008 … … 42 42 ///For a complete example see the \ref arg_parser_demo.cc demo file. 43 43 class ArgParser { 44 44 45 45 static void _showHelp(void *p); 46 46 protected: 47 47 48 48 int _argc; 49 49 const char **_argv; 50 50 51 51 enum OptType { UNKNOWN=0, BOOL=1, STRING=2, DOUBLE=3, INTEGER=4, FUNC=5 }; 52 52 53 53 class ParData { 54 54 public: 55 55 union { 56 57 58 59 60 61 62 63 64 56 bool *bool_p; 57 int *int_p; 58 double *double_p; 59 std::string *string_p; 60 struct { 61 void (*p)(void *); 62 void *data; 63 } func_p; 64 65 65 }; 66 66 std::string help; … … 73 73 bool self_delete; 74 74 ParData() : mandatory(false), type(UNKNOWN), set(false), ingroup(false), 75 75 has_syn(false), syn(false), self_delete(false) {} 76 76 }; 77 77 … … 79 79 Opts _opts; 80 80 81 class GroupData 81 class GroupData 82 82 { 83 83 public: … … 88 88 GroupData() :only_one(false), mandatory(false) {} 89 89 }; 90 90 91 91 typedef std::map<std::string,GroupData> Groups; 92 92 Groups _groups; … … 99 99 100 100 }; 101 101 102 102 std::vector<OtherArg> _others_help; 103 103 std::vector<std::string> _file_args; 104 104 std::string _command_name; 105 105 106 106 107 107 private: 108 108 //Bind a function to an option. … … 114 114 //\param data Data to be passed to \c func 115 115 ArgParser &funcOption(const std::string &name, 116 117 118 116 const std::string &help, 117 void (*func)(void *),void *data); 118 119 119 public: 120 120 … … 137 137 ///\param obl Indicate if the option is mandatory. 138 138 ArgParser &intOption(const std::string &name, 139 140 139 const std::string &help, 140 int value=0, bool obl=false); 141 141 142 142 ///Add a new floating point type option … … 148 148 ///\param obl Indicate if the option is mandatory. 149 149 ArgParser &doubleOption(const std::string &name, 150 151 150 const std::string &help, 151 double value=0, bool obl=false); 152 152 153 153 ///Add a new bool type option … … 160 160 ///\note A mandatory bool obtion is of very little use. 161 161 ArgParser &boolOption(const std::string &name, 162 163 162 const std::string &help, 163 bool value=false, bool obl=false); 164 164 165 165 ///Add a new string type option … … 171 171 ///\param obl Indicate if the option is mandatory. 172 172 ArgParser &stringOption(const std::string &name, 173 174 173 const std::string &help, 174 std::string value="", bool obl=false); 175 175 176 176 ///Give help string for non-parsed arguments. … … 180 180 ///\c help gives a more detailed description. 181 181 ArgParser &other(const std::string &name, 182 183 182 const std::string &help=""); 183 184 184 ///@} 185 185 … … 198 198 ///\retval ref The value of the argument will be written to this variable. 199 199 ArgParser &refOption(const std::string &name, 200 201 200 const std::string &help, 201 int &ref, bool obl=false); 202 202 203 203 ///Add a new floating type option with a storage reference … … 209 209 ///\retval ref The value of the argument will be written to this variable. 210 210 ArgParser &refOption(const std::string &name, 211 212 211 const std::string &help, 212 double &ref, bool obl=false); 213 213 214 214 ///Add a new bool type option with a storage reference … … 221 221 ///\note A mandatory bool obtion is of very little use. 222 222 ArgParser &refOption(const std::string &name, 223 224 223 const std::string &help, 224 bool &ref, bool obl=false); 225 225 226 226 ///Add a new string type option with a storage reference … … 232 232 ///\retval ref The value of the argument will be written to this variable. 233 233 ArgParser &refOption(const std::string &name, 234 235 236 234 const std::string &help, 235 std::string &ref, bool obl=false); 236 237 237 ///@} 238 238 239 239 ///\name Option Groups and Synonyms 240 240 /// 241 241 242 242 ///@{ 243 243 … … 249 249 ///\param opt The option name. 250 250 ArgParser &optionGroup(const std::string &group, 251 251 const std::string &opt); 252 252 253 253 ///Make the members of a group exclusive … … 256 256 ///given at the same time. 257 257 ArgParser &onlyOneGroup(const std::string &group); 258 258 259 259 ///Make a group mandatory 260 260 … … 262 262 ///must be given. 263 263 ArgParser &mandatoryGroup(const std::string &group); 264 264 265 265 ///Create synonym to an option 266 266 … … 268 268 ///option \c opt. 269 269 ArgParser &synonym(const std::string &syn, 270 271 270 const std::string &opt); 271 272 272 ///@} 273 273 … … 283 283 void requiresValue(std::string arg, OptType t); 284 284 void checkMandatories(); 285 285 286 286 ///Start the parsing process 287 287 ArgParser &parse(); 288 288 289 289 /// Synonym for parse() 290 ArgParser &run() 290 ArgParser &run() 291 291 { 292 292 return parse(); 293 293 } 294 294 295 295 ///Give back the command name (the 0th argument) 296 296 const std::string &commandName() { return _command_name; } 297 297 298 298 ///Check if an opion has been given to the command. 299 bool given(std::string op) 299 bool given(std::string op) 300 300 { 301 301 Opts::iterator i = _opts.find(op); … … 305 305 306 306 ///Magic type for operator[] 307 307 308 308 ///This is the type of the return value of ArgParser::operator[](). 309 309 ///It automatically converts to \c int, \c double, \c bool or 310 310 ///\c std::string if the type of the option matches, otherwise it 311 311 ///throws an exception (i.e. it performs runtime type checking). 312 class RefType 312 class RefType 313 313 { 314 314 ArgParser &_parser; … … 318 318 RefType(ArgParser &p,const std::string &n) :_parser(p),_name(n) {} 319 319 ///\e 320 operator bool() 320 operator bool() 321 321 { 322 323 324 325 326 327 322 Opts::iterator i = _parser._opts.find(_name); 323 LEMON_ASSERT(i!=_parser._opts.end(), 324 std::string()+"Unkown option: '"+_name+"'"); 325 LEMON_ASSERT(i->second.type==ArgParser::BOOL, 326 std::string()+"'"+_name+"' is a bool option"); 327 return *(i->second.bool_p); 328 328 } 329 329 ///\e 330 330 operator std::string() 331 331 { 332 333 334 335 336 337 332 Opts::iterator i = _parser._opts.find(_name); 333 LEMON_ASSERT(i!=_parser._opts.end(), 334 std::string()+"Unkown option: '"+_name+"'"); 335 LEMON_ASSERT(i->second.type==ArgParser::STRING, 336 std::string()+"'"+_name+"' is a string option"); 337 return *(i->second.string_p); 338 338 } 339 339 ///\e 340 operator double() 340 operator double() 341 341 { 342 343 344 345 346 347 348 349 342 Opts::iterator i = _parser._opts.find(_name); 343 LEMON_ASSERT(i!=_parser._opts.end(), 344 std::string()+"Unkown option: '"+_name+"'"); 345 LEMON_ASSERT(i->second.type==ArgParser::DOUBLE || 346 i->second.type==ArgParser::INTEGER, 347 std::string()+"'"+_name+"' is a floating point option"); 348 return i->second.type==ArgParser::DOUBLE ? 349 *(i->second.double_p) : *(i->second.int_p); 350 350 } 351 351 ///\e 352 operator int() 352 operator int() 353 353 { 354 355 356 357 358 359 354 Opts::iterator i = _parser._opts.find(_name); 355 LEMON_ASSERT(i!=_parser._opts.end(), 356 std::string()+"Unkown option: '"+_name+"'"); 357 LEMON_ASSERT(i->second.type==ArgParser::INTEGER, 358 std::string()+"'"+_name+"' is an integer option"); 359 return *(i->second.int_p); 360 360 } 361 361 … … 363 363 364 364 ///Give back the value of an option 365 365 366 366 ///Give back the value of an option. 367 367 ///\sa RefType … … 369 369 { 370 370 return RefType(*this, n); 371 } 371 } 372 372 373 373 ///Give back the non-option type arguments. … … 376 376 ///not starting with a '-' character. 377 377 std::vector<std::string> &files() { return _file_args; } 378 378 379 379 }; 380 380 }
Note: See TracChangeset
for help on using the changeset viewer.