COIN-OR::LEMON - Graph Library

Changeset 2406:0ffc78641b34 in lemon-0.x for lemon


Ignore:
Timestamp:
03/13/07 13:33:40 (13 years ago)
Author:
Alpar Juttner
Branch:
default
Phase:
public
Convert:
svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@3237
Message:

Better doc.

Location:
lemon
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • lemon/arg_parser.cc

    r2402 r2406  
    2929  ArgParser::ArgParser(int argc, char **argv) :_argc(argc), _argv(argv),
    3030                                               _command_name(argv[0]) {
    31     refOption("-help","Print a short help message",_showHelp,this);
     31    funcOption("-help","Print a short help message",_showHelp,this);
    3232    synonym("help","-help");
    3333    synonym("h","-help");
     
    122122  ArgParser &ArgParser::refOption(const std::string &name,
    123123                               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;
    128128    p.self_delete=false;
    129129    p.help=help;
     
    136136  ArgParser &ArgParser::refOption(const std::string &name,
    137137                               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;
    142142    p.self_delete=false;
    143143    p.help=help;
     
    150150  ArgParser &ArgParser::refOption(const std::string &name,
    151151                               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;
    156156    p.self_delete=false;
    157157    p.help=help;
     
    160160    _opts[name]=p;
    161161
    162     value = false;
     162    ref = false;
    163163
    164164    return *this;
     
    167167  ArgParser &ArgParser::refOption(const std::string &name,
    168168                               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;
    173173    p.self_delete=false;
    174174    p.help=help;
     
    179179  }
    180180
    181   ArgParser &ArgParser::refOption(const std::string &name,
     181  ArgParser &ArgParser::funcOption(const std::string &name,
    182182                               const std::string &help,
    183183                               void (*func)(void *),void *data)
     
    198198  {
    199199    Opts::iterator i = _opts.find(opt);
    200     if(i==_opts.end()) exit(3); ///\todo throw exception instead
    201     else if(i->second.ingroup) exit(3); ///\todo throw exception instead
     200    if(i==_opts.end()) throw LogicError();
     201    else if(i->second.ingroup) throw LogicError();
    202202    else {
    203203      GroupData &g=_groups[group];
     
    221221    Opts::iterator s = _opts.find(syn);
    222222    if(o==_opts.end()||s!=_opts.end())
    223       exit(3); ///\todo throw exception instead
     223      throw LogicError();
    224224    else {
    225225      ParData p;
  • lemon/arg_parser.h

    r2402 r2406  
    2727#include <sstream>
    2828#include <algorithm>
     29#include <lemon/error.h>
    2930
    3031///\ingroup misc
     
    152153                      std::string value="", bool obl=false);
    153154   
    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,
    164                     const std::string &help,
    165                     int &value, bool obl=false);
    166 
    167     ///Add a new floating type option
    168 
    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 option
    178 
    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 option
    189 
    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    
    198155    ///Bind a function to an option.
    199156
     
    203160    ///  must be of type "void f(void *)"
    204161    ///\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.
    205178    ArgParser &refOption(const std::string &name,
    206179                    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    ///@{
    208219
    209220    ///Boundle some options into a group
     
    222233    ArgParser &onlyOneGroup(const std::string &group);
    223234 
     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   
    224241    ///Create synonym to an option
    225242
     
    229246                           const std::string &opt);
    230247   
    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
    237250    ///Give help string for non-parsed arguments.
    238251
     
    264277    void checkMandatories();
    265278   
    266     ///\e
     279    ///Start the parsing process
    267280    ArgParser &parse();
    268281
     
    281294
    282295
     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).
    283302    class RefType
    284303    {
     
    286305      std::string _name;
    287306    public:
     307      ///\e
    288308      RefType(ArgParser &p,const std::string &n) :_parser(p),_name(n) {}
     309      ///\e
    289310      operator bool()
    290311      {
    291312        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();
    294316        else return *(i->second.bool_p);
    295317      }
     318      ///\e
    296319      operator std::string()
    297320      {
    298321        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();
    301325        else return *(i->second.string_p);
    302326      }
     327      ///\e
    303328      operator double()
    304329      {
    305330        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();
    308334        else return *(i->second.double_p);
    309335      }
     336      ///\e
    310337      operator int()
    311338      {
    312339        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();
    315343        else return *(i->second.int_p);
    316344      }
     
    318346    };
    319347
     348    ///Give back the value of an option
     349   
     350    ///Give back the value of an option
     351    ///\sa RefType
    320352    RefType operator[](const std::string &n)
    321353    {
    322354      return RefType(*this, n);
    323     }
    324    
    325      
     355    }   
     356 
    326357  };
    327358}
Note: See TracChangeset for help on using the changeset viewer.