lemon/arg_parser.h
changeset 2402 da8eb8f4ea41
parent 2391 14a343be7a5a
child 2406 0ffc78641b34
     1.1 --- a/lemon/arg_parser.h	Wed Mar 07 13:32:12 2007 +0000
     1.2 +++ b/lemon/arg_parser.h	Mon Mar 12 13:26:56 2007 +0000
     1.3 @@ -70,9 +70,9 @@
     1.4        bool ingroup;
     1.5        bool has_syn;
     1.6        bool syn;
     1.7 -	     
     1.8 +      bool self_delete;
     1.9        ParData() : mandatory(false), type(UNKNOWN), set(false), ingroup(false),
    1.10 -		  has_syn(false), syn(false) {}
    1.11 +		  has_syn(false), syn(false), self_delete(false) {}
    1.12      };
    1.13  
    1.14      typedef std::map<std::string,ParData> Opts;
    1.15 @@ -108,13 +108,59 @@
    1.16      ///\e
    1.17      ArgParser(int argc, char **argv);
    1.18  
    1.19 +    ~ArgParser();
    1.20 +
    1.21      ///Add a new integer type option
    1.22  
    1.23      ///\param name The name of the option. The leading '-' must be omitted.
    1.24      ///\param help A help string.
    1.25      ///\retval value The value of the argument will be written to this variable.
    1.26      ///\param obl Indicate if the option is mandatory.
    1.27 -    ArgParser &option(const std::string &name,
    1.28 +    ArgParser &intOption(const std::string &name,
    1.29 +		    const std::string &help,
    1.30 +		    int value=0, bool obl=false);
    1.31 +
    1.32 +    ///Add a new floating type option
    1.33 +
    1.34 +    ///\param name The name of the option. The leading '-' must be omitted.
    1.35 +    ///\param help A help string.
    1.36 +    ///\retval value The value of the argument will be written to this variable.
    1.37 +    ///\param obl Indicate if the option is mandatory.
    1.38 +    ArgParser &doubleOption(const std::string &name,
    1.39 +		      const std::string &help,
    1.40 +		      double value=0, bool obl=false);
    1.41 +
    1.42 +    ///Add a new bool type option
    1.43 +
    1.44 +    ///\param name The name of the option. The leading '-' must be omitted.
    1.45 +    ///\param help A help string.
    1.46 +    ///\retval value The value of the argument will be written to this variable.
    1.47 +    ///\param obl Indicate if the option is mandatory.
    1.48 +    ////\note A mandatory bool obtion is of very little use.)
    1.49 +    ArgParser &boolOption(const std::string &name,
    1.50 +		      const std::string &help,
    1.51 +		      bool value=false, bool obl=false);
    1.52 +
    1.53 +    ///Add a new string type option
    1.54 +
    1.55 +    ///\param name The name of the option. The leading '-' must be omitted.
    1.56 +    ///\param help A help string.
    1.57 +    ///\retval value The value of the argument will be written to this variable.
    1.58 +    ///\param obl Indicate if the option is mandatory.
    1.59 +    ArgParser &stringOption(const std::string &name,
    1.60 +		      const std::string &help,
    1.61 +		      std::string value="", bool obl=false);
    1.62 +    
    1.63 +
    1.64 +
    1.65 +
    1.66 +    ///Add a new integer type option
    1.67 +
    1.68 +    ///\param name The name of the option. The leading '-' must be omitted.
    1.69 +    ///\param help A help string.
    1.70 +    ///\retval value The value of the argument will be written to this variable.
    1.71 +    ///\param obl Indicate if the option is mandatory.
    1.72 +    ArgParser &refOption(const std::string &name,
    1.73  		    const std::string &help,
    1.74  		    int &value, bool obl=false);
    1.75  
    1.76 @@ -124,7 +170,7 @@
    1.77      ///\param help A help string.
    1.78      ///\retval value The value of the argument will be written to this variable.
    1.79      ///\param obl Indicate if the option is mandatory.
    1.80 -    ArgParser &option(const std::string &name,
    1.81 +    ArgParser &refOption(const std::string &name,
    1.82  		      const std::string &help,
    1.83  		      double &value, bool obl=false);
    1.84  
    1.85 @@ -135,7 +181,7 @@
    1.86      ///\retval value The value of the argument will be written to this variable.
    1.87      ///\param obl Indicate if the option is mandatory.
    1.88      ////\note A mandatory bool obtion is of very little use.)
    1.89 -    ArgParser &option(const std::string &name,
    1.90 +    ArgParser &refOption(const std::string &name,
    1.91  		      const std::string &help,
    1.92  		      bool &value, bool obl=false);
    1.93  
    1.94 @@ -145,7 +191,7 @@
    1.95      ///\param help A help string.
    1.96      ///\retval value The value of the argument will be written to this variable.
    1.97      ///\param obl Indicate if the option is mandatory.
    1.98 -    ArgParser &option(const std::string &name,
    1.99 +    ArgParser &refOption(const std::string &name,
   1.100  		      const std::string &help,
   1.101  		      std::string &value, bool obl=false);
   1.102      
   1.103 @@ -156,7 +202,7 @@
   1.104      ///\retval func The function to be called when the option is given. It
   1.105      ///  must be of type "void f(void *)"
   1.106      ///\param data Data to be passed to \c func
   1.107 -    ArgParser &option(const std::string &name,
   1.108 +    ArgParser &refOption(const std::string &name,
   1.109  		    const std::string &help,
   1.110  		    void (*func)(void *),void *data);
   1.111  
   1.112 @@ -232,7 +278,51 @@
   1.113        Opts::iterator i = _opts.find(op);
   1.114        return i!=_opts.end()?i->second.set:false;
   1.115      }
   1.116 +
   1.117 +
   1.118 +    class RefType 
   1.119 +    {
   1.120 +      ArgParser &_parser;
   1.121 +      std::string _name;
   1.122 +    public:
   1.123 +      RefType(ArgParser &p,const std::string &n) :_parser(p),_name(n) {}
   1.124 +      operator bool() 
   1.125 +      {
   1.126 +	Opts::iterator i = _parser._opts.find(_name);
   1.127 +	if(i==_parser._opts.end()) exit(3); ///\todo throw exception instead
   1.128 +	else if(i->second.type!=ArgParser::BOOL) exit(3);
   1.129 +	else return *(i->second.bool_p);
   1.130 +      }
   1.131 +      operator std::string()
   1.132 +      {
   1.133 +	Opts::iterator i = _parser._opts.find(_name);
   1.134 +	if(i==_parser._opts.end()) exit(3); ///\todo throw exception instead
   1.135 +	else if(i->second.type!=ArgParser::STRING) exit(3);
   1.136 +	else return *(i->second.string_p);
   1.137 +      }
   1.138 +      operator double() 
   1.139 +      {
   1.140 +	Opts::iterator i = _parser._opts.find(_name);
   1.141 +	if(i==_parser._opts.end()) exit(3); ///\todo throw exception instead
   1.142 +	else if(i->second.type!=ArgParser::DOUBLE) exit(3);
   1.143 +	else return *(i->second.double_p);
   1.144 +      }
   1.145 +      operator int() 
   1.146 +      {
   1.147 +	Opts::iterator i = _parser._opts.find(_name);
   1.148 +	if(i==_parser._opts.end()) exit(3); ///\todo throw exception instead
   1.149 +	else if(i->second.type!=ArgParser::INTEGER) exit(3);
   1.150 +	else return *(i->second.int_p);
   1.151 +      }
   1.152 +
   1.153 +    };
   1.154 +
   1.155 +    RefType operator[](const std::string &n)
   1.156 +    {
   1.157 +      return RefType(*this, n);
   1.158 +    }
   1.159      
   1.160 +      
   1.161    };
   1.162  }
   1.163