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