COIN-OR::LEMON - Graph Library

Changeset 2402:da8eb8f4ea41 in lemon-0.x for lemon/arg_parser.h


Ignore:
Timestamp:
03/12/07 14:26:56 (17 years ago)
Author:
Alpar Juttner
Branch:
default
Phase:
public
Convert:
svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@3233
Message:

An improved version of ArgParser?: You don't need to give an explicit storage
for each option.
TODO: Documentation must be updated

File:
1 edited

Legend:

Unmodified
Added
Removed
  • lemon/arg_parser.h

    r2391 r2402  
    7171      bool has_syn;
    7272      bool syn;
    73              
     73      bool self_delete;
    7474      ParData() : mandatory(false), type(UNKNOWN), set(false), ingroup(false),
    75                   has_syn(false), syn(false) {}
     75                  has_syn(false), syn(false), self_delete(false) {}
    7676    };
    7777
     
    109109    ArgParser(int argc, char **argv);
    110110
     111    ~ArgParser();
     112
    111113    ///Add a new integer type option
    112114
     
    115117    ///\retval value The value of the argument will be written to this variable.
    116118    ///\param obl Indicate if the option is mandatory.
    117     ArgParser &option(const std::string &name,
     119    ArgParser &intOption(const std::string &name,
     120                    const std::string &help,
     121                    int value=0, bool obl=false);
     122
     123    ///Add a new floating type option
     124
     125    ///\param name The name of the option. The leading '-' must be omitted.
     126    ///\param help A help string.
     127    ///\retval value The value of the argument will be written to this variable.
     128    ///\param obl Indicate if the option is mandatory.
     129    ArgParser &doubleOption(const std::string &name,
     130                      const std::string &help,
     131                      double value=0, bool obl=false);
     132
     133    ///Add a new bool type option
     134
     135    ///\param name The name of the option. The leading '-' must be omitted.
     136    ///\param help A help string.
     137    ///\retval value The value of the argument will be written to this variable.
     138    ///\param obl Indicate if the option is mandatory.
     139    ////\note A mandatory bool obtion is of very little use.)
     140    ArgParser &boolOption(const std::string &name,
     141                      const std::string &help,
     142                      bool value=false, bool obl=false);
     143
     144    ///Add a new string type option
     145
     146    ///\param name The name of the option. The leading '-' must be omitted.
     147    ///\param help A help string.
     148    ///\retval value The value of the argument will be written to this variable.
     149    ///\param obl Indicate if the option is mandatory.
     150    ArgParser &stringOption(const std::string &name,
     151                      const std::string &help,
     152                      std::string value="", bool obl=false);
     153   
     154
     155
     156
     157    ///Add a new integer type option
     158
     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,
    118164                    const std::string &help,
    119165                    int &value, bool obl=false);
     
    125171    ///\retval value The value of the argument will be written to this variable.
    126172    ///\param obl Indicate if the option is mandatory.
    127     ArgParser &option(const std::string &name,
     173    ArgParser &refOption(const std::string &name,
    128174                      const std::string &help,
    129175                      double &value, bool obl=false);
     
    136182    ///\param obl Indicate if the option is mandatory.
    137183    ////\note A mandatory bool obtion is of very little use.)
    138     ArgParser &option(const std::string &name,
     184    ArgParser &refOption(const std::string &name,
    139185                      const std::string &help,
    140186                      bool &value, bool obl=false);
     
    146192    ///\retval value The value of the argument will be written to this variable.
    147193    ///\param obl Indicate if the option is mandatory.
    148     ArgParser &option(const std::string &name,
     194    ArgParser &refOption(const std::string &name,
    149195                      const std::string &help,
    150196                      std::string &value, bool obl=false);
     
    157203    ///  must be of type "void f(void *)"
    158204    ///\param data Data to be passed to \c func
    159     ArgParser &option(const std::string &name,
     205    ArgParser &refOption(const std::string &name,
    160206                    const std::string &help,
    161207                    void (*func)(void *),void *data);
     
    233279      return i!=_opts.end()?i->second.set:false;
    234280    }
    235    
     281
     282
     283    class RefType
     284    {
     285      ArgParser &_parser;
     286      std::string _name;
     287    public:
     288      RefType(ArgParser &p,const std::string &n) :_parser(p),_name(n) {}
     289      operator bool()
     290      {
     291        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);
     294        else return *(i->second.bool_p);
     295      }
     296      operator std::string()
     297      {
     298        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);
     301        else return *(i->second.string_p);
     302      }
     303      operator double()
     304      {
     305        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);
     308        else return *(i->second.double_p);
     309      }
     310      operator int()
     311      {
     312        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);
     315        else return *(i->second.int_p);
     316      }
     317
     318    };
     319
     320    RefType operator[](const std::string &n)
     321    {
     322      return RefType(*this, n);
     323    }
     324   
     325     
    236326  };
    237327}
Note: See TracChangeset for help on using the changeset viewer.