lemon/arg_parser.h
changeset 104 cdbba181b786
parent 90 2981a73d49a3
child 108 889d0c289d19
equal deleted inserted replaced
4:ee443255817c 5:82629b8fa75c
   125 
   125 
   126     ///Add a new integer type option
   126     ///Add a new integer type option
   127 
   127 
   128     ///\param name The name of the option. The leading '-' must be omitted.
   128     ///\param name The name of the option. The leading '-' must be omitted.
   129     ///\param help A help string.
   129     ///\param help A help string.
   130     ///\param value A default value for the option
   130     ///\param value A default value for the option.
   131     ///\param obl Indicate if the option is mandatory.
   131     ///\param obl Indicate if the option is mandatory.
   132     ArgParser &intOption(const std::string &name,
   132     ArgParser &intOption(const std::string &name,
   133 		    const std::string &help,
   133 		    const std::string &help,
   134 		    int value=0, bool obl=false);
   134 		    int value=0, bool obl=false);
   135 
   135 
   136     ///Add a new floating point type option
   136     ///Add a new floating point type option
   137 
   137 
   138     ///\param name The name of the option. The leading '-' must be omitted.
   138     ///\param name The name of the option. The leading '-' must be omitted.
   139     ///\param help A help string.
   139     ///\param help A help string.
   140     ///\param value A default value for the option
   140     ///\param value A default value for the option.
   141     ///\param obl Indicate if the option is mandatory.
   141     ///\param obl Indicate if the option is mandatory.
   142     ArgParser &doubleOption(const std::string &name,
   142     ArgParser &doubleOption(const std::string &name,
   143 		      const std::string &help,
   143 		      const std::string &help,
   144 		      double value=0, bool obl=false);
   144 		      double value=0, bool obl=false);
   145 
   145 
   146     ///Add a new bool type option
   146     ///Add a new bool type option
   147 
   147 
   148     ///\param name The name of the option. The leading '-' must be omitted.
   148     ///\param name The name of the option. The leading '-' must be omitted.
   149     ///\param help A help string.
   149     ///\param help A help string.
   150     ///\param value A default value for the option
   150     ///\param value A default value for the option.
   151     ///\param obl Indicate if the option is mandatory.
   151     ///\param obl Indicate if the option is mandatory.
   152     ////\note A mandatory bool obtion is of very little use.)
   152     ///\note A mandatory bool obtion is of very little use.
   153     ArgParser &boolOption(const std::string &name,
   153     ArgParser &boolOption(const std::string &name,
   154 		      const std::string &help,
   154 		      const std::string &help,
   155 		      bool value=false, bool obl=false);
   155 		      bool value=false, bool obl=false);
   156 
   156 
   157     ///Add a new string type option
   157     ///Add a new string type option
   158 
   158 
   159     ///\param name The name of the option. The leading '-' must be omitted.
   159     ///\param name The name of the option. The leading '-' must be omitted.
   160     ///\param help A help string.
   160     ///\param help A help string.
   161     ///\param value A default value for the option
   161     ///\param value A default value for the option.
   162     ///\param obl Indicate if the option is mandatory.
   162     ///\param obl Indicate if the option is mandatory.
   163     ArgParser &stringOption(const std::string &name,
   163     ArgParser &stringOption(const std::string &name,
   164 		      const std::string &help,
   164 		      const std::string &help,
   165 		      std::string value="", bool obl=false);
   165 		      std::string value="", bool obl=false);
   166 
   166 
   194 
   194 
   195     ///\param name The name of the option. The leading '-' must be omitted.
   195     ///\param name The name of the option. The leading '-' must be omitted.
   196     ///\param help A help string.
   196     ///\param help A help string.
   197     ///\param obl Indicate if the option is mandatory.
   197     ///\param obl Indicate if the option is mandatory.
   198     ///\retval ref The value of the argument will be written to this variable.
   198     ///\retval ref The value of the argument will be written to this variable.
   199     ////\note A mandatory bool obtion is of very little use.)
   199     ///\note A mandatory bool obtion is of very little use.
   200     ArgParser &refOption(const std::string &name,
   200     ArgParser &refOption(const std::string &name,
   201 		      const std::string &help,
   201 		      const std::string &help,
   202 		      bool &ref, bool obl=false);
   202 		      bool &ref, bool obl=false);
   203 
   203 
   204     ///Add a new string type option with a storage reference
   204     ///Add a new string type option with a storage reference
   205 
   205 
   206     ///\param name The name of the option. The leading '-' must be omitted.
   206     ///\param name The name of the option. The leading '-' must be omitted.
   207     ///\param help A help string.
   207     ///\param help A help string.
       
   208     ///\param obl Indicate if the option is mandatory.
   208     ///\retval ref The value of the argument will be written to this variable.
   209     ///\retval ref The value of the argument will be written to this variable.
   209     ///\param obl Indicate if the option is mandatory.
       
   210     ArgParser &refOption(const std::string &name,
   210     ArgParser &refOption(const std::string &name,
   211 		      const std::string &help,
   211 		      const std::string &help,
   212 		      std::string &ref, bool obl=false);
   212 		      std::string &ref, bool obl=false);
   213     
   213     
   214     ///@}
   214     ///@}
   309       RefType(ArgParser &p,const std::string &n) :_parser(p),_name(n) {}
   309       RefType(ArgParser &p,const std::string &n) :_parser(p),_name(n) {}
   310       ///\e
   310       ///\e
   311       operator bool() 
   311       operator bool() 
   312       {
   312       {
   313 	Opts::iterator i = _parser._opts.find(_name);
   313 	Opts::iterator i = _parser._opts.find(_name);
   314 	LEMON_ASSERT(i==_parser._opts.end(),
   314 	LEMON_ASSERT(i!=_parser._opts.end(),
   315 		     std::string()+"Unkown option: '"+_name+"'");
   315 		     std::string()+"Unkown option: '"+_name+"'");
   316 	LEMON_ASSERT(i->second.type!=ArgParser::BOOL,
   316 	LEMON_ASSERT(i->second.type==ArgParser::BOOL,
   317 		     std::string()+"'"+_name+"' is a bool option");
   317 		     std::string()+"'"+_name+"' is a bool option");
   318 	return *(i->second.bool_p);
   318 	return *(i->second.bool_p);
   319       }
   319       }
   320       ///\e
   320       ///\e
   321       operator std::string()
   321       operator std::string()
   322       {
   322       {
   323 	Opts::iterator i = _parser._opts.find(_name);
   323 	Opts::iterator i = _parser._opts.find(_name);
   324 	LEMON_ASSERT(i==_parser._opts.end(),
   324 	LEMON_ASSERT(i!=_parser._opts.end(),
   325 		     std::string()+"Unkown option: '"+_name+"'");
   325 		     std::string()+"Unkown option: '"+_name+"'");
   326 	LEMON_ASSERT(i->second.type!=ArgParser::STRING,
   326 	LEMON_ASSERT(i->second.type==ArgParser::STRING,
   327 		     std::string()+"'"+_name+"' is a string option");
   327 		     std::string()+"'"+_name+"' is a string option");
   328 	return *(i->second.string_p);
   328 	return *(i->second.string_p);
   329       }
   329       }
   330       ///\e
   330       ///\e
   331       operator double() 
   331       operator double() 
   332       {
   332       {
   333 	Opts::iterator i = _parser._opts.find(_name);
   333 	Opts::iterator i = _parser._opts.find(_name);
   334 	LEMON_ASSERT(i==_parser._opts.end(),
   334 	LEMON_ASSERT(i!=_parser._opts.end(),
   335 		     std::string()+"Unkown option: '"+_name+"'");
   335 		     std::string()+"Unkown option: '"+_name+"'");
   336 	LEMON_ASSERT(i->second.type!=ArgParser::DOUBLE &&
   336 	LEMON_ASSERT(i->second.type==ArgParser::DOUBLE ||
   337 		     i->second.type!=ArgParser::INTEGER,
   337 		     i->second.type==ArgParser::INTEGER,
   338 		     std::string()+"'"+_name+"' is a floating point option");
   338 		     std::string()+"'"+_name+"' is a floating point option");
   339 	return i->second.type==ArgParser::DOUBLE ?
   339 	return i->second.type==ArgParser::DOUBLE ?
   340 	  *(i->second.double_p) : *(i->second.int_p);
   340 	  *(i->second.double_p) : *(i->second.int_p);
   341       }
   341       }
   342       ///\e
   342       ///\e
   343       operator int() 
   343       operator int() 
   344       {
   344       {
   345 	Opts::iterator i = _parser._opts.find(_name);
   345 	Opts::iterator i = _parser._opts.find(_name);
   346 	LEMON_ASSERT(i==_parser._opts.end(),
   346 	LEMON_ASSERT(i!=_parser._opts.end(),
   347 		     std::string()+"Unkown option: '"+_name+"'");
   347 		     std::string()+"Unkown option: '"+_name+"'");
   348 	LEMON_ASSERT(i->second.type!=ArgParser::INTEGER,
   348 	LEMON_ASSERT(i->second.type==ArgParser::INTEGER,
   349 		     std::string()+"'"+_name+"' is an integer option");
   349 		     std::string()+"'"+_name+"' is an integer option");
   350 	return *(i->second.int_p);
   350 	return *(i->second.int_p);
   351       }
   351       }
   352 
   352 
   353     };
   353     };