lemon/arg_parser.h
author deba
Wed, 07 Mar 2007 11:56:14 +0000
changeset 2396 658c04d74729
parent 2389 df6a32249b46
child 2402 da8eb8f4ea41
permissions -rw-r--r--
naming convention:
header sentry
     1 /* -*- C++ -*-
     2  *
     3  * This file is a part of LEMON, a generic C++ optimization library
     4  *
     5  * Copyright (C) 2003-2007
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     8  *
     9  * Permission to use, modify and distribute this software is granted
    10  * provided that this copyright notice appears in all copies. For
    11  * precise terms see the accompanying LICENSE file.
    12  *
    13  * This software is provided "AS IS" with no warranty of any kind,
    14  * express or implied, and with no claim as to its suitability for any
    15  * purpose.
    16  *
    17  */
    18 
    19 #ifndef LEMON_ARG_PARSER
    20 #define LEMON_ARG_PARSER
    21 
    22 #include <vector>
    23 #include <map>
    24 #include <list>
    25 #include <string>
    26 #include <iostream>
    27 #include <sstream>
    28 #include <algorithm>
    29 
    30 ///\ingroup misc
    31 ///\file
    32 ///\brief A tools to parse command line arguments.
    33 ///
    34 ///\author Alpar Juttner
    35 
    36 namespace lemon {
    37 
    38   ///Command line arguments parser
    39 
    40   ///\ingroup misc
    41   ///Command line arguments parser
    42   ///
    43   class ArgParser {
    44     
    45     static void _showHelp(void *p);
    46   protected:
    47     
    48     int _argc;
    49     char **_argv;
    50     
    51     enum OptType { UNKNOWN=0, BOOL=1, STRING=2, DOUBLE=3, INTEGER=4, FUNC=5 };
    52     
    53     class ParData {
    54     public:
    55       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 	  
    65       };
    66       std::string help;
    67       bool mandatory;
    68       OptType type;
    69       bool set;
    70       bool ingroup;
    71       bool has_syn;
    72       bool syn;
    73 	     
    74       ParData() : mandatory(false), type(UNKNOWN), set(false), ingroup(false),
    75 		  has_syn(false), syn(false) {}
    76     };
    77 
    78     typedef std::map<std::string,ParData> Opts;
    79     Opts _opts;
    80 
    81     class GroupData 
    82     {
    83     public:
    84       typedef std::list<std::string> Opts;
    85       Opts opts;
    86       bool only_one;
    87       bool mandatory;
    88       GroupData() :only_one(false), mandatory(false) {}
    89     };
    90       
    91     typedef std::map<std::string,GroupData> Groups;
    92     Groups _groups;
    93 
    94     struct OtherArg
    95     {
    96       std::string name;
    97       std::string help;
    98       OtherArg(std::string n, std::string h) :name(n), help(h) {}
    99 
   100     };
   101       
   102     std::vector<OtherArg> _others_help;
   103     std::vector<std::string> _file_args;
   104     std::string _command_name;
   105     
   106   public:
   107 
   108     ///\e
   109     ArgParser(int argc, char **argv);
   110 
   111     ///Add a new integer type option
   112 
   113     ///\param name The name of the option. The leading '-' must be omitted.
   114     ///\param help A help string.
   115     ///\retval value The value of the argument will be written to this variable.
   116     ///\param obl Indicate if the option is mandatory.
   117     ArgParser &option(const std::string &name,
   118 		    const std::string &help,
   119 		    int &value, bool obl=false);
   120 
   121     ///Add a new floating type option
   122 
   123     ///\param name The name of the option. The leading '-' must be omitted.
   124     ///\param help A help string.
   125     ///\retval value The value of the argument will be written to this variable.
   126     ///\param obl Indicate if the option is mandatory.
   127     ArgParser &option(const std::string &name,
   128 		      const std::string &help,
   129 		      double &value, bool obl=false);
   130 
   131     ///Add a new bool type option
   132 
   133     ///\param name The name of the option. The leading '-' must be omitted.
   134     ///\param help A help string.
   135     ///\retval value The value of the argument will be written to this variable.
   136     ///\param obl Indicate if the option is mandatory.
   137     ////\note A mandatory bool obtion is of very little use.)
   138     ArgParser &option(const std::string &name,
   139 		      const std::string &help,
   140 		      bool &value, bool obl=false);
   141 
   142     ///Add a new string type option
   143 
   144     ///\param name The name of the option. The leading '-' must be omitted.
   145     ///\param help A help string.
   146     ///\retval value The value of the argument will be written to this variable.
   147     ///\param obl Indicate if the option is mandatory.
   148     ArgParser &option(const std::string &name,
   149 		      const std::string &help,
   150 		      std::string &value, bool obl=false);
   151     
   152     ///Bind a function to an option.
   153 
   154     ///\param name The name of the option. The leading '-' must be omitted.
   155     ///\param help A help string.
   156     ///\retval func The function to be called when the option is given. It
   157     ///  must be of type "void f(void *)"
   158     ///\param data Data to be passed to \c func
   159     ArgParser &option(const std::string &name,
   160 		    const std::string &help,
   161 		    void (*func)(void *),void *data);
   162 
   163     ///Boundle some options into a group
   164 
   165     /// You can group some option by calling this function repeatedly for each
   166     /// option to be grupped with the same groupname.
   167     ///\param group The group name
   168     ///\param opt The option name
   169     ArgParser &optionGroup(const std::string &group,
   170 			   const std::string &opt);
   171 
   172     ///Make the members of a group exclusive
   173 
   174     ///If you call this function for a group, than at most one of them can be
   175     ///given at the same time
   176     ArgParser &onlyOneGroup(const std::string &group);
   177   
   178     ///Create synonym to an option
   179 
   180     ///With this function you can create a sysnonym called \c sys of the
   181     ///option \c opt.
   182     ArgParser &synonym(const std::string &syn,
   183 			   const std::string &opt);
   184     
   185     ///Make a group mandatory
   186 
   187     ///Using this function, at least one of the members of \c group
   188     ///must be given.
   189     ArgParser &mandatoryGroup(const std::string &group);
   190     
   191     ///Give help string for non-parsed arguments.
   192 
   193     ///With this function you can give help string for non-parsed arguments.
   194     ///the parameter \c name will be printed in the short usage line, while
   195     ///\c help gives a more detailed description.
   196     ArgParser &other(const std::string &name,
   197 		     const std::string &help="");
   198     
   199     ///Non option type arguments.
   200 
   201     ///Gives back a reference to a vector consisting of the program arguments
   202     ///not starting with a '-' character.
   203     std::vector<std::string> &files() { return _file_args; }
   204 
   205     ///Give back the command name (the 0th argument)
   206     const std::string &commandName() { return _command_name; }
   207 
   208     void show(std::ostream &os,Opts::iterator i);
   209     void show(std::ostream &os,Groups::iterator i);
   210     void showHelp(Opts::iterator i);
   211     void showHelp(std::vector<OtherArg>::iterator i);
   212     void shortHelp();
   213     void showHelp();
   214 
   215     void unknownOpt(std::string arg);
   216 
   217     void requiresValue(std::string arg, OptType t);
   218     void checkMandatories();
   219     
   220     ///\e
   221     ArgParser &parse();
   222 
   223     /// Synonym for parse()
   224     ArgParser &run() 
   225     {
   226       return parse();
   227     }
   228     
   229     ///Check if an opion has been given to the command.
   230     bool given(std::string op) 
   231     {
   232       Opts::iterator i = _opts.find(op);
   233       return i!=_opts.end()?i->second.set:false;
   234     }
   235     
   236   };
   237 }
   238 
   239     
   240 
   241 #endif // LEMON_MAIN_PARAMS