COIN-OR::LEMON - Graph Library

Changeset 209:765619b7cbb2 in lemon for lemon/arg_parser.h


Ignore:
Timestamp:
07/13/08 20:51:02 (16 years ago)
Author:
Alpar Juttner <alpar@…>
Branch:
default
Phase:
public
Message:

Apply unify-sources.sh to the source tree

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.
    44 *
    55 * Copyright (C) 2003-2008
     
    4242  ///For a complete example see the \ref arg_parser_demo.cc demo file.
    4343  class ArgParser {
    44    
     44
    4545    static void _showHelp(void *p);
    4646  protected:
    47    
     47
    4848    int _argc;
    4949    const char **_argv;
    50    
     50
    5151    enum OptType { UNKNOWN=0, BOOL=1, STRING=2, DOUBLE=3, INTEGER=4, FUNC=5 };
    52    
     52
    5353    class ParData {
    5454    public:
    5555      union {
    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          
     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
    6565      };
    6666      std::string help;
     
    7373      bool self_delete;
    7474      ParData() : mandatory(false), type(UNKNOWN), set(false), ingroup(false),
    75                   has_syn(false), syn(false), self_delete(false) {}
     75                  has_syn(false), syn(false), self_delete(false) {}
    7676    };
    7777
     
    7979    Opts _opts;
    8080
    81     class GroupData 
     81    class GroupData
    8282    {
    8383    public:
     
    8888      GroupData() :only_one(false), mandatory(false) {}
    8989    };
    90      
     90
    9191    typedef std::map<std::string,GroupData> Groups;
    9292    Groups _groups;
     
    9999
    100100    };
    101      
     101
    102102    std::vector<OtherArg> _others_help;
    103103    std::vector<std::string> _file_args;
    104104    std::string _command_name;
    105105
    106    
     106
    107107  private:
    108108    //Bind a function to an option.
     
    114114    //\param data Data to be passed to \c func
    115115    ArgParser &funcOption(const std::string &name,
    116                     const std::string &help,
    117                     void (*func)(void *),void *data);
    118    
     116                    const std::string &help,
     117                    void (*func)(void *),void *data);
     118
    119119  public:
    120120
     
    137137    ///\param obl Indicate if the option is mandatory.
    138138    ArgParser &intOption(const std::string &name,
    139                     const std::string &help,
    140                     int value=0, bool obl=false);
     139                    const std::string &help,
     140                    int value=0, bool obl=false);
    141141
    142142    ///Add a new floating point type option
     
    148148    ///\param obl Indicate if the option is mandatory.
    149149    ArgParser &doubleOption(const std::string &name,
    150                       const std::string &help,
    151                       double value=0, bool obl=false);
     150                      const std::string &help,
     151                      double value=0, bool obl=false);
    152152
    153153    ///Add a new bool type option
     
    160160    ///\note A mandatory bool obtion is of very little use.
    161161    ArgParser &boolOption(const std::string &name,
    162                       const std::string &help,
    163                       bool value=false, bool obl=false);
     162                      const std::string &help,
     163                      bool value=false, bool obl=false);
    164164
    165165    ///Add a new string type option
     
    171171    ///\param obl Indicate if the option is mandatory.
    172172    ArgParser &stringOption(const std::string &name,
    173                       const std::string &help,
    174                       std::string value="", bool obl=false);
     173                      const std::string &help,
     174                      std::string value="", bool obl=false);
    175175
    176176    ///Give help string for non-parsed arguments.
     
    180180    ///\c help gives a more detailed description.
    181181    ArgParser &other(const std::string &name,
    182                      const std::string &help="");
    183    
     182                     const std::string &help="");
     183
    184184    ///@}
    185185
     
    198198    ///\retval ref The value of the argument will be written to this variable.
    199199    ArgParser &refOption(const std::string &name,
    200                     const std::string &help,
    201                     int &ref, bool obl=false);
     200                    const std::string &help,
     201                    int &ref, bool obl=false);
    202202
    203203    ///Add a new floating type option with a storage reference
     
    209209    ///\retval ref The value of the argument will be written to this variable.
    210210    ArgParser &refOption(const std::string &name,
    211                       const std::string &help,
    212                       double &ref, bool obl=false);
     211                      const std::string &help,
     212                      double &ref, bool obl=false);
    213213
    214214    ///Add a new bool type option with a storage reference
     
    221221    ///\note A mandatory bool obtion is of very little use.
    222222    ArgParser &refOption(const std::string &name,
    223                       const std::string &help,
    224                       bool &ref, bool obl=false);
     223                      const std::string &help,
     224                      bool &ref, bool obl=false);
    225225
    226226    ///Add a new string type option with a storage reference
     
    232232    ///\retval ref The value of the argument will be written to this variable.
    233233    ArgParser &refOption(const std::string &name,
    234                       const std::string &help,
    235                       std::string &ref, bool obl=false);
    236    
     234                      const std::string &help,
     235                      std::string &ref, bool obl=false);
     236
    237237    ///@}
    238238
    239239    ///\name Option Groups and Synonyms
    240240    ///
    241    
     241
    242242    ///@{
    243243
     
    249249    ///\param opt The option name.
    250250    ArgParser &optionGroup(const std::string &group,
    251                            const std::string &opt);
     251                           const std::string &opt);
    252252
    253253    ///Make the members of a group exclusive
     
    256256    ///given at the same time.
    257257    ArgParser &onlyOneGroup(const std::string &group);
    258  
     258
    259259    ///Make a group mandatory
    260260
     
    262262    ///must be given.
    263263    ArgParser &mandatoryGroup(const std::string &group);
    264    
     264
    265265    ///Create synonym to an option
    266266
     
    268268    ///option \c opt.
    269269    ArgParser &synonym(const std::string &syn,
    270                            const std::string &opt);
    271    
     270                           const std::string &opt);
     271
    272272    ///@}
    273273
     
    283283    void requiresValue(std::string arg, OptType t);
    284284    void checkMandatories();
    285    
     285
    286286    ///Start the parsing process
    287287    ArgParser &parse();
    288288
    289289    /// Synonym for parse()
    290     ArgParser &run() 
     290    ArgParser &run()
    291291    {
    292292      return parse();
    293293    }
    294    
     294
    295295    ///Give back the command name (the 0th argument)
    296296    const std::string &commandName() { return _command_name; }
    297297
    298298    ///Check if an opion has been given to the command.
    299     bool given(std::string op) 
     299    bool given(std::string op)
    300300    {
    301301      Opts::iterator i = _opts.find(op);
     
    305305
    306306    ///Magic type for operator[]
    307    
     307
    308308    ///This is the type of the return value of ArgParser::operator[]().
    309309    ///It automatically converts to \c int, \c double, \c bool or
    310310    ///\c std::string if the type of the option matches, otherwise it
    311311    ///throws an exception (i.e. it performs runtime type checking).
    312     class RefType 
     312    class RefType
    313313    {
    314314      ArgParser &_parser;
     
    318318      RefType(ArgParser &p,const std::string &n) :_parser(p),_name(n) {}
    319319      ///\e
    320       operator bool() 
     320      operator bool()
    321321      {
    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);
     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);
    328328      }
    329329      ///\e
    330330      operator std::string()
    331331      {
    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);
     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);
    338338      }
    339339      ///\e
    340       operator double() 
     340      operator double()
    341341      {
    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);
     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);
    350350      }
    351351      ///\e
    352       operator int() 
     352      operator int()
    353353      {
    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);
     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);
    360360      }
    361361
     
    363363
    364364    ///Give back the value of an option
    365    
     365
    366366    ///Give back the value of an option.
    367367    ///\sa RefType
     
    369369    {
    370370      return RefType(*this, n);
    371     }   
     371    }
    372372
    373373    ///Give back the non-option type arguments.
     
    376376    ///not starting with a '-' character.
    377377    std::vector<std::string> &files() { return _file_args; }
    378  
     378
    379379  };
    380380}
Note: See TracChangeset for help on using the changeset viewer.