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