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