lemon/arg_parser.h
author alpar
Mon, 12 Mar 2007 13:26:56 +0000
changeset 2402 da8eb8f4ea41
parent 2391 14a343be7a5a
child 2406 0ffc78641b34
permissions -rw-r--r--
An improved version of ArgParser: You don't need to give an explicit storage
for each option.
TODO: Documentation must be updated
alpar@2389
     1
/* -*- C++ -*-
alpar@2389
     2
 *
alpar@2391
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@2391
     4
 *
alpar@2391
     5
 * Copyright (C) 2003-2007
alpar@2391
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@2389
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@2389
     8
 *
alpar@2389
     9
 * Permission to use, modify and distribute this software is granted
alpar@2389
    10
 * provided that this copyright notice appears in all copies. For
alpar@2389
    11
 * precise terms see the accompanying LICENSE file.
alpar@2389
    12
 *
alpar@2389
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@2389
    14
 * express or implied, and with no claim as to its suitability for any
alpar@2389
    15
 * purpose.
alpar@2389
    16
 *
alpar@2389
    17
 */
alpar@2389
    18
alpar@2389
    19
#ifndef LEMON_ARG_PARSER
alpar@2389
    20
#define LEMON_ARG_PARSER
alpar@2389
    21
alpar@2389
    22
#include <vector>
alpar@2389
    23
#include <map>
alpar@2389
    24
#include <list>
alpar@2389
    25
#include <string>
alpar@2389
    26
#include <iostream>
alpar@2389
    27
#include <sstream>
alpar@2389
    28
#include <algorithm>
alpar@2389
    29
alpar@2389
    30
///\ingroup misc
alpar@2389
    31
///\file
alpar@2389
    32
///\brief A tools to parse command line arguments.
alpar@2389
    33
///
alpar@2389
    34
///\author Alpar Juttner
alpar@2389
    35
alpar@2389
    36
namespace lemon {
alpar@2389
    37
alpar@2389
    38
  ///Command line arguments parser
alpar@2389
    39
alpar@2389
    40
  ///\ingroup misc
alpar@2389
    41
  ///Command line arguments parser
alpar@2389
    42
  ///
alpar@2389
    43
  class ArgParser {
alpar@2389
    44
    
alpar@2389
    45
    static void _showHelp(void *p);
alpar@2389
    46
  protected:
alpar@2389
    47
    
alpar@2389
    48
    int _argc;
alpar@2389
    49
    char **_argv;
alpar@2389
    50
    
alpar@2389
    51
    enum OptType { UNKNOWN=0, BOOL=1, STRING=2, DOUBLE=3, INTEGER=4, FUNC=5 };
alpar@2389
    52
    
alpar@2389
    53
    class ParData {
alpar@2389
    54
    public:
alpar@2389
    55
      union {
alpar@2389
    56
	bool *bool_p;
alpar@2389
    57
	int *int_p;
alpar@2389
    58
	double *double_p;
alpar@2389
    59
	std::string *string_p;
alpar@2389
    60
	struct {
alpar@2389
    61
	  void (*p)(void *);
alpar@2389
    62
	  void *data;
alpar@2389
    63
	} func_p;
alpar@2389
    64
	  
alpar@2389
    65
      };
alpar@2389
    66
      std::string help;
alpar@2389
    67
      bool mandatory;
alpar@2389
    68
      OptType type;
alpar@2389
    69
      bool set;
alpar@2389
    70
      bool ingroup;
alpar@2389
    71
      bool has_syn;
alpar@2389
    72
      bool syn;
alpar@2402
    73
      bool self_delete;
alpar@2389
    74
      ParData() : mandatory(false), type(UNKNOWN), set(false), ingroup(false),
alpar@2402
    75
		  has_syn(false), syn(false), self_delete(false) {}
alpar@2389
    76
    };
alpar@2389
    77
alpar@2389
    78
    typedef std::map<std::string,ParData> Opts;
alpar@2389
    79
    Opts _opts;
alpar@2389
    80
alpar@2389
    81
    class GroupData 
alpar@2389
    82
    {
alpar@2389
    83
    public:
alpar@2389
    84
      typedef std::list<std::string> Opts;
alpar@2389
    85
      Opts opts;
alpar@2389
    86
      bool only_one;
alpar@2389
    87
      bool mandatory;
alpar@2389
    88
      GroupData() :only_one(false), mandatory(false) {}
alpar@2389
    89
    };
alpar@2389
    90
      
alpar@2389
    91
    typedef std::map<std::string,GroupData> Groups;
alpar@2389
    92
    Groups _groups;
alpar@2389
    93
alpar@2389
    94
    struct OtherArg
alpar@2389
    95
    {
alpar@2389
    96
      std::string name;
alpar@2389
    97
      std::string help;
alpar@2389
    98
      OtherArg(std::string n, std::string h) :name(n), help(h) {}
alpar@2389
    99
alpar@2389
   100
    };
alpar@2389
   101
      
alpar@2389
   102
    std::vector<OtherArg> _others_help;
alpar@2389
   103
    std::vector<std::string> _file_args;
alpar@2389
   104
    std::string _command_name;
alpar@2389
   105
    
alpar@2389
   106
  public:
alpar@2389
   107
alpar@2389
   108
    ///\e
alpar@2389
   109
    ArgParser(int argc, char **argv);
alpar@2389
   110
alpar@2402
   111
    ~ArgParser();
alpar@2402
   112
alpar@2389
   113
    ///Add a new integer type option
alpar@2389
   114
alpar@2389
   115
    ///\param name The name of the option. The leading '-' must be omitted.
alpar@2389
   116
    ///\param help A help string.
alpar@2389
   117
    ///\retval value The value of the argument will be written to this variable.
alpar@2389
   118
    ///\param obl Indicate if the option is mandatory.
alpar@2402
   119
    ArgParser &intOption(const std::string &name,
alpar@2402
   120
		    const std::string &help,
alpar@2402
   121
		    int value=0, bool obl=false);
alpar@2402
   122
alpar@2402
   123
    ///Add a new floating type option
alpar@2402
   124
alpar@2402
   125
    ///\param name The name of the option. The leading '-' must be omitted.
alpar@2402
   126
    ///\param help A help string.
alpar@2402
   127
    ///\retval value The value of the argument will be written to this variable.
alpar@2402
   128
    ///\param obl Indicate if the option is mandatory.
alpar@2402
   129
    ArgParser &doubleOption(const std::string &name,
alpar@2402
   130
		      const std::string &help,
alpar@2402
   131
		      double value=0, bool obl=false);
alpar@2402
   132
alpar@2402
   133
    ///Add a new bool type option
alpar@2402
   134
alpar@2402
   135
    ///\param name The name of the option. The leading '-' must be omitted.
alpar@2402
   136
    ///\param help A help string.
alpar@2402
   137
    ///\retval value The value of the argument will be written to this variable.
alpar@2402
   138
    ///\param obl Indicate if the option is mandatory.
alpar@2402
   139
    ////\note A mandatory bool obtion is of very little use.)
alpar@2402
   140
    ArgParser &boolOption(const std::string &name,
alpar@2402
   141
		      const std::string &help,
alpar@2402
   142
		      bool value=false, bool obl=false);
alpar@2402
   143
alpar@2402
   144
    ///Add a new string type option
alpar@2402
   145
alpar@2402
   146
    ///\param name The name of the option. The leading '-' must be omitted.
alpar@2402
   147
    ///\param help A help string.
alpar@2402
   148
    ///\retval value The value of the argument will be written to this variable.
alpar@2402
   149
    ///\param obl Indicate if the option is mandatory.
alpar@2402
   150
    ArgParser &stringOption(const std::string &name,
alpar@2402
   151
		      const std::string &help,
alpar@2402
   152
		      std::string value="", bool obl=false);
alpar@2402
   153
    
alpar@2402
   154
alpar@2402
   155
alpar@2402
   156
alpar@2402
   157
    ///Add a new integer type option
alpar@2402
   158
alpar@2402
   159
    ///\param name The name of the option. The leading '-' must be omitted.
alpar@2402
   160
    ///\param help A help string.
alpar@2402
   161
    ///\retval value The value of the argument will be written to this variable.
alpar@2402
   162
    ///\param obl Indicate if the option is mandatory.
alpar@2402
   163
    ArgParser &refOption(const std::string &name,
alpar@2389
   164
		    const std::string &help,
alpar@2389
   165
		    int &value, bool obl=false);
alpar@2389
   166
alpar@2389
   167
    ///Add a new floating type option
alpar@2389
   168
alpar@2389
   169
    ///\param name The name of the option. The leading '-' must be omitted.
alpar@2389
   170
    ///\param help A help string.
alpar@2389
   171
    ///\retval value The value of the argument will be written to this variable.
alpar@2389
   172
    ///\param obl Indicate if the option is mandatory.
alpar@2402
   173
    ArgParser &refOption(const std::string &name,
alpar@2389
   174
		      const std::string &help,
alpar@2389
   175
		      double &value, bool obl=false);
alpar@2389
   176
alpar@2389
   177
    ///Add a new bool type option
alpar@2389
   178
alpar@2389
   179
    ///\param name The name of the option. The leading '-' must be omitted.
alpar@2389
   180
    ///\param help A help string.
alpar@2389
   181
    ///\retval value The value of the argument will be written to this variable.
alpar@2389
   182
    ///\param obl Indicate if the option is mandatory.
alpar@2389
   183
    ////\note A mandatory bool obtion is of very little use.)
alpar@2402
   184
    ArgParser &refOption(const std::string &name,
alpar@2389
   185
		      const std::string &help,
alpar@2389
   186
		      bool &value, bool obl=false);
alpar@2389
   187
alpar@2389
   188
    ///Add a new string type option
alpar@2389
   189
alpar@2389
   190
    ///\param name The name of the option. The leading '-' must be omitted.
alpar@2389
   191
    ///\param help A help string.
alpar@2389
   192
    ///\retval value The value of the argument will be written to this variable.
alpar@2389
   193
    ///\param obl Indicate if the option is mandatory.
alpar@2402
   194
    ArgParser &refOption(const std::string &name,
alpar@2389
   195
		      const std::string &help,
alpar@2389
   196
		      std::string &value, bool obl=false);
alpar@2389
   197
    
alpar@2389
   198
    ///Bind a function to an option.
alpar@2389
   199
alpar@2389
   200
    ///\param name The name of the option. The leading '-' must be omitted.
alpar@2389
   201
    ///\param help A help string.
alpar@2389
   202
    ///\retval func The function to be called when the option is given. It
alpar@2389
   203
    ///  must be of type "void f(void *)"
alpar@2389
   204
    ///\param data Data to be passed to \c func
alpar@2402
   205
    ArgParser &refOption(const std::string &name,
alpar@2389
   206
		    const std::string &help,
alpar@2389
   207
		    void (*func)(void *),void *data);
alpar@2389
   208
alpar@2389
   209
    ///Boundle some options into a group
alpar@2389
   210
alpar@2389
   211
    /// You can group some option by calling this function repeatedly for each
alpar@2389
   212
    /// option to be grupped with the same groupname.
alpar@2389
   213
    ///\param group The group name
alpar@2389
   214
    ///\param opt The option name
alpar@2389
   215
    ArgParser &optionGroup(const std::string &group,
alpar@2389
   216
			   const std::string &opt);
alpar@2389
   217
alpar@2389
   218
    ///Make the members of a group exclusive
alpar@2389
   219
alpar@2389
   220
    ///If you call this function for a group, than at most one of them can be
alpar@2389
   221
    ///given at the same time
alpar@2389
   222
    ArgParser &onlyOneGroup(const std::string &group);
alpar@2389
   223
  
alpar@2389
   224
    ///Create synonym to an option
alpar@2389
   225
alpar@2389
   226
    ///With this function you can create a sysnonym called \c sys of the
alpar@2389
   227
    ///option \c opt.
alpar@2389
   228
    ArgParser &synonym(const std::string &syn,
alpar@2389
   229
			   const std::string &opt);
alpar@2389
   230
    
alpar@2389
   231
    ///Make a group mandatory
alpar@2389
   232
alpar@2389
   233
    ///Using this function, at least one of the members of \c group
alpar@2389
   234
    ///must be given.
alpar@2389
   235
    ArgParser &mandatoryGroup(const std::string &group);
alpar@2389
   236
    
alpar@2389
   237
    ///Give help string for non-parsed arguments.
alpar@2389
   238
alpar@2389
   239
    ///With this function you can give help string for non-parsed arguments.
alpar@2389
   240
    ///the parameter \c name will be printed in the short usage line, while
alpar@2389
   241
    ///\c help gives a more detailed description.
alpar@2389
   242
    ArgParser &other(const std::string &name,
alpar@2389
   243
		     const std::string &help="");
alpar@2389
   244
    
alpar@2389
   245
    ///Non option type arguments.
alpar@2389
   246
alpar@2389
   247
    ///Gives back a reference to a vector consisting of the program arguments
alpar@2389
   248
    ///not starting with a '-' character.
alpar@2389
   249
    std::vector<std::string> &files() { return _file_args; }
alpar@2389
   250
alpar@2389
   251
    ///Give back the command name (the 0th argument)
alpar@2389
   252
    const std::string &commandName() { return _command_name; }
alpar@2389
   253
alpar@2389
   254
    void show(std::ostream &os,Opts::iterator i);
alpar@2389
   255
    void show(std::ostream &os,Groups::iterator i);
alpar@2389
   256
    void showHelp(Opts::iterator i);
alpar@2389
   257
    void showHelp(std::vector<OtherArg>::iterator i);
alpar@2389
   258
    void shortHelp();
alpar@2389
   259
    void showHelp();
alpar@2389
   260
alpar@2389
   261
    void unknownOpt(std::string arg);
alpar@2389
   262
alpar@2389
   263
    void requiresValue(std::string arg, OptType t);
alpar@2389
   264
    void checkMandatories();
alpar@2389
   265
    
alpar@2389
   266
    ///\e
alpar@2389
   267
    ArgParser &parse();
alpar@2389
   268
alpar@2389
   269
    /// Synonym for parse()
alpar@2389
   270
    ArgParser &run() 
alpar@2389
   271
    {
alpar@2389
   272
      return parse();
alpar@2389
   273
    }
alpar@2389
   274
    
alpar@2389
   275
    ///Check if an opion has been given to the command.
alpar@2389
   276
    bool given(std::string op) 
alpar@2389
   277
    {
alpar@2389
   278
      Opts::iterator i = _opts.find(op);
alpar@2389
   279
      return i!=_opts.end()?i->second.set:false;
alpar@2389
   280
    }
alpar@2402
   281
alpar@2402
   282
alpar@2402
   283
    class RefType 
alpar@2402
   284
    {
alpar@2402
   285
      ArgParser &_parser;
alpar@2402
   286
      std::string _name;
alpar@2402
   287
    public:
alpar@2402
   288
      RefType(ArgParser &p,const std::string &n) :_parser(p),_name(n) {}
alpar@2402
   289
      operator bool() 
alpar@2402
   290
      {
alpar@2402
   291
	Opts::iterator i = _parser._opts.find(_name);
alpar@2402
   292
	if(i==_parser._opts.end()) exit(3); ///\todo throw exception instead
alpar@2402
   293
	else if(i->second.type!=ArgParser::BOOL) exit(3);
alpar@2402
   294
	else return *(i->second.bool_p);
alpar@2402
   295
      }
alpar@2402
   296
      operator std::string()
alpar@2402
   297
      {
alpar@2402
   298
	Opts::iterator i = _parser._opts.find(_name);
alpar@2402
   299
	if(i==_parser._opts.end()) exit(3); ///\todo throw exception instead
alpar@2402
   300
	else if(i->second.type!=ArgParser::STRING) exit(3);
alpar@2402
   301
	else return *(i->second.string_p);
alpar@2402
   302
      }
alpar@2402
   303
      operator double() 
alpar@2402
   304
      {
alpar@2402
   305
	Opts::iterator i = _parser._opts.find(_name);
alpar@2402
   306
	if(i==_parser._opts.end()) exit(3); ///\todo throw exception instead
alpar@2402
   307
	else if(i->second.type!=ArgParser::DOUBLE) exit(3);
alpar@2402
   308
	else return *(i->second.double_p);
alpar@2402
   309
      }
alpar@2402
   310
      operator int() 
alpar@2402
   311
      {
alpar@2402
   312
	Opts::iterator i = _parser._opts.find(_name);
alpar@2402
   313
	if(i==_parser._opts.end()) exit(3); ///\todo throw exception instead
alpar@2402
   314
	else if(i->second.type!=ArgParser::INTEGER) exit(3);
alpar@2402
   315
	else return *(i->second.int_p);
alpar@2402
   316
      }
alpar@2402
   317
alpar@2402
   318
    };
alpar@2402
   319
alpar@2402
   320
    RefType operator[](const std::string &n)
alpar@2402
   321
    {
alpar@2402
   322
      return RefType(*this, n);
alpar@2402
   323
    }
alpar@2389
   324
    
alpar@2402
   325
      
alpar@2389
   326
  };
alpar@2389
   327
}
alpar@2389
   328
alpar@2389
   329
    
alpar@2389
   330
alpar@2389
   331
#endif // LEMON_MAIN_PARAMS