lemon/arg_parser.h
author Peter Kovacs <kpeter@inf.elte.hu>
Thu, 01 Nov 2018 11:27:05 +0100
changeset 1416 f179aa1045a4
parent 959 38213abd2911
permissions -rw-r--r--
Suppress unused typdef warnings (#615)
alpar@209
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
alpar@85
     2
 *
alpar@209
     3
 * This file is a part of LEMON, a generic C++ optimization library.
alpar@85
     4
 *
alpar@956
     5
 * Copyright (C) 2003-2010
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@261
    19
#ifndef LEMON_ARG_PARSER_H
alpar@261
    20
#define LEMON_ARG_PARSER_H
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@1327
    29
#include <lemon/core.h>
deba@108
    30
#include <lemon/assert.h>
alpar@85
    31
alpar@85
    32
///\ingroup misc
alpar@85
    33
///\file
kpeter@88
    34
///\brief A tool to parse command line arguments.
alpar@85
    35
alpar@85
    36
namespace lemon {
alpar@85
    37
alpar@915
    38
  ///Exception used by ArgParser
kpeter@959
    39
kpeter@959
    40
  ///Exception used by ArgParser.
kpeter@959
    41
  ///
alpar@915
    42
  class ArgParserException : public Exception {
alpar@915
    43
  public:
kpeter@959
    44
    /// Reasons for failure
kpeter@959
    45
kpeter@959
    46
    /// Reasons for failure.
kpeter@959
    47
    ///
alpar@915
    48
    enum Reason {
kpeter@959
    49
      HELP,         ///< <tt>--help</tt> option was given.
kpeter@959
    50
      UNKNOWN_OPT,  ///< Unknown option was given.
kpeter@959
    51
      INVALID_OPT   ///< Invalid combination of options.
alpar@915
    52
    };
alpar@956
    53
alpar@915
    54
  private:
alpar@915
    55
    Reason _reason;
alpar@956
    56
alpar@915
    57
  public:
alpar@915
    58
    ///Constructor
alpar@915
    59
    ArgParserException(Reason r) throw() : _reason(r) {}
alpar@915
    60
    ///Virtual destructor
alpar@915
    61
    virtual ~ArgParserException() throw() {}
alpar@915
    62
    ///A short description of the exception
alpar@915
    63
    virtual const char* what() const throw() {
alpar@915
    64
      switch(_reason)
alpar@915
    65
        {
alpar@915
    66
        case HELP:
alpar@915
    67
          return "lemon::ArgParseException: ask for help";
alpar@915
    68
          break;
alpar@915
    69
        case UNKNOWN_OPT:
alpar@915
    70
          return "lemon::ArgParseException: unknown option";
alpar@915
    71
          break;
alpar@915
    72
        case INVALID_OPT:
alpar@915
    73
          return "lemon::ArgParseException: invalid combination of options";
alpar@915
    74
          break;
alpar@915
    75
        }
alpar@915
    76
      return "";
alpar@915
    77
    }
alpar@915
    78
    ///Return the reason for the failure
alpar@915
    79
    Reason reason() const {return _reason; }
alpar@915
    80
  };
alpar@915
    81
alpar@915
    82
alpar@85
    83
  ///Command line arguments parser
alpar@85
    84
alpar@85
    85
  ///\ingroup misc
kpeter@88
    86
  ///Command line arguments parser.
alpar@85
    87
  ///
kpeter@88
    88
  ///For a complete example see the \ref arg_parser_demo.cc demo file.
alpar@85
    89
  class ArgParser {
alpar@209
    90
alpar@85
    91
    static void _showHelp(void *p);
alpar@85
    92
  protected:
alpar@209
    93
alpar@85
    94
    int _argc;
ladanyi@311
    95
    const char * const *_argv;
alpar@209
    96
alpar@85
    97
    enum OptType { UNKNOWN=0, BOOL=1, STRING=2, DOUBLE=3, INTEGER=4, FUNC=5 };
alpar@209
    98
alpar@85
    99
    class ParData {
alpar@85
   100
    public:
alpar@85
   101
      union {
alpar@209
   102
        bool *bool_p;
alpar@209
   103
        int *int_p;
alpar@209
   104
        double *double_p;
alpar@209
   105
        std::string *string_p;
alpar@209
   106
        struct {
alpar@209
   107
          void (*p)(void *);
alpar@209
   108
          void *data;
alpar@209
   109
        } func_p;
alpar@209
   110
alpar@85
   111
      };
alpar@85
   112
      std::string help;
alpar@85
   113
      bool mandatory;
alpar@85
   114
      OptType type;
alpar@85
   115
      bool set;
alpar@85
   116
      bool ingroup;
alpar@85
   117
      bool has_syn;
alpar@85
   118
      bool syn;
alpar@85
   119
      bool self_delete;
alpar@85
   120
      ParData() : mandatory(false), type(UNKNOWN), set(false), ingroup(false),
alpar@209
   121
                  has_syn(false), syn(false), self_delete(false) {}
alpar@85
   122
    };
alpar@85
   123
alpar@85
   124
    typedef std::map<std::string,ParData> Opts;
alpar@85
   125
    Opts _opts;
alpar@85
   126
alpar@209
   127
    class GroupData
alpar@85
   128
    {
alpar@85
   129
    public:
alpar@85
   130
      typedef std::list<std::string> Opts;
alpar@85
   131
      Opts opts;
alpar@85
   132
      bool only_one;
alpar@85
   133
      bool mandatory;
alpar@85
   134
      GroupData() :only_one(false), mandatory(false) {}
alpar@85
   135
    };
alpar@209
   136
alpar@85
   137
    typedef std::map<std::string,GroupData> Groups;
alpar@85
   138
    Groups _groups;
alpar@85
   139
alpar@85
   140
    struct OtherArg
alpar@85
   141
    {
alpar@85
   142
      std::string name;
alpar@85
   143
      std::string help;
alpar@85
   144
      OtherArg(std::string n, std::string h) :name(n), help(h) {}
alpar@85
   145
alpar@85
   146
    };
alpar@209
   147
alpar@85
   148
    std::vector<OtherArg> _others_help;
alpar@85
   149
    std::vector<std::string> _file_args;
alpar@85
   150
    std::string _command_name;
alpar@87
   151
alpar@956
   152
alpar@87
   153
  private:
alpar@87
   154
    //Bind a function to an option.
alpar@87
   155
alpar@87
   156
    //\param name The name of the option. The leading '-' must be omitted.
alpar@87
   157
    //\param help A help string.
alpar@87
   158
    //\retval func The function to be called when the option is given. It
alpar@87
   159
    //  must be of type "void f(void *)"
alpar@87
   160
    //\param data Data to be passed to \c func
alpar@87
   161
    ArgParser &funcOption(const std::string &name,
alpar@209
   162
                    const std::string &help,
alpar@209
   163
                    void (*func)(void *),void *data);
alpar@209
   164
alpar@915
   165
    bool _exit_on_problems;
alpar@956
   166
alpar@915
   167
    void _terminate(ArgParserException::Reason reason) const;
alpar@915
   168
alpar@85
   169
  public:
alpar@85
   170
kpeter@204
   171
    ///Constructor
ladanyi@311
   172
    ArgParser(int argc, const char * const *argv);
alpar@85
   173
alpar@85
   174
    ~ArgParser();
alpar@85
   175
kpeter@204
   176
    ///\name Options
kpeter@204
   177
    ///
kpeter@204
   178
kpeter@204
   179
    ///@{
kpeter@204
   180
alpar@85
   181
    ///Add a new integer type option
alpar@85
   182
kpeter@204
   183
    ///Add a new integer type option.
alpar@85
   184
    ///\param name The name of the option. The leading '-' must be omitted.
alpar@85
   185
    ///\param help A help string.
kpeter@95
   186
    ///\param value A default value for the option.
alpar@85
   187
    ///\param obl Indicate if the option is mandatory.
alpar@85
   188
    ArgParser &intOption(const std::string &name,
alpar@209
   189
                    const std::string &help,
alpar@209
   190
                    int value=0, bool obl=false);
alpar@85
   191
alpar@86
   192
    ///Add a new floating point type option
alpar@85
   193
kpeter@204
   194
    ///Add a new floating point type option.
alpar@85
   195
    ///\param name The name of the option. The leading '-' must be omitted.
alpar@85
   196
    ///\param help A help string.
kpeter@95
   197
    ///\param value A default value for the option.
alpar@85
   198
    ///\param obl Indicate if the option is mandatory.
alpar@85
   199
    ArgParser &doubleOption(const std::string &name,
alpar@209
   200
                      const std::string &help,
alpar@209
   201
                      double value=0, bool obl=false);
alpar@85
   202
alpar@85
   203
    ///Add a new bool type option
alpar@85
   204
kpeter@204
   205
    ///Add a new bool type option.
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 value A default value for the option.
alpar@85
   209
    ///\param obl Indicate if the option is mandatory.
kpeter@95
   210
    ///\note A mandatory bool obtion is of very little use.
alpar@85
   211
    ArgParser &boolOption(const std::string &name,
alpar@209
   212
                      const std::string &help,
alpar@209
   213
                      bool value=false, bool obl=false);
alpar@85
   214
alpar@85
   215
    ///Add a new string type option
alpar@85
   216
kpeter@204
   217
    ///Add a new string type option.
alpar@85
   218
    ///\param name The name of the option. The leading '-' must be omitted.
alpar@85
   219
    ///\param help A help string.
kpeter@95
   220
    ///\param value A default value for the option.
alpar@85
   221
    ///\param obl Indicate if the option is mandatory.
alpar@85
   222
    ArgParser &stringOption(const std::string &name,
alpar@209
   223
                      const std::string &help,
alpar@209
   224
                      std::string value="", bool obl=false);
alpar@85
   225
kpeter@204
   226
    ///Give help string for non-parsed arguments.
kpeter@204
   227
kpeter@204
   228
    ///With this function you can give help string for non-parsed arguments.
kpeter@204
   229
    ///The parameter \c name will be printed in the short usage line, while
kpeter@204
   230
    ///\c help gives a more detailed description.
kpeter@204
   231
    ArgParser &other(const std::string &name,
alpar@209
   232
                     const std::string &help="");
alpar@209
   233
kpeter@204
   234
    ///@}
kpeter@204
   235
kpeter@204
   236
    ///\name Options with External Storage
alpar@85
   237
    ///Using this functions, the value of the option will be directly written
alpar@85
   238
    ///into a variable once the option appears in the command line.
alpar@85
   239
alpar@85
   240
    ///@{
alpar@85
   241
alpar@85
   242
    ///Add a new integer type option with a storage reference
alpar@85
   243
kpeter@204
   244
    ///Add a new integer type option with a storage reference.
alpar@85
   245
    ///\param name The name of the option. The leading '-' must be omitted.
alpar@85
   246
    ///\param help A help string.
alpar@90
   247
    ///\param obl Indicate if the option is mandatory.
alpar@85
   248
    ///\retval ref The value of the argument will be written to this variable.
alpar@85
   249
    ArgParser &refOption(const std::string &name,
alpar@209
   250
                    const std::string &help,
alpar@209
   251
                    int &ref, bool obl=false);
alpar@85
   252
alpar@85
   253
    ///Add a new floating type option with a storage reference
alpar@85
   254
kpeter@204
   255
    ///Add a new floating type option with a storage reference.
alpar@85
   256
    ///\param name The name of the option. The leading '-' must be omitted.
alpar@85
   257
    ///\param help A help string.
alpar@90
   258
    ///\param obl Indicate if the option is mandatory.
alpar@85
   259
    ///\retval ref The value of the argument will be written to this variable.
alpar@85
   260
    ArgParser &refOption(const std::string &name,
alpar@209
   261
                      const std::string &help,
alpar@209
   262
                      double &ref, bool obl=false);
alpar@85
   263
alpar@85
   264
    ///Add a new bool type option with a storage reference
alpar@85
   265
kpeter@204
   266
    ///Add a new bool type option with a storage reference.
alpar@85
   267
    ///\param name The name of the option. The leading '-' must be omitted.
alpar@85
   268
    ///\param help A help string.
alpar@90
   269
    ///\param obl Indicate if the option is mandatory.
alpar@85
   270
    ///\retval ref The value of the argument will be written to this variable.
kpeter@95
   271
    ///\note A mandatory bool obtion is of very little use.
alpar@85
   272
    ArgParser &refOption(const std::string &name,
alpar@209
   273
                      const std::string &help,
alpar@209
   274
                      bool &ref, bool obl=false);
alpar@85
   275
alpar@85
   276
    ///Add a new string type option with a storage reference
alpar@85
   277
kpeter@204
   278
    ///Add a new string type option with a storage reference.
alpar@85
   279
    ///\param name The name of the option. The leading '-' must be omitted.
alpar@85
   280
    ///\param help A help string.
kpeter@95
   281
    ///\param obl Indicate if the option is mandatory.
alpar@85
   282
    ///\retval ref The value of the argument will be written to this variable.
alpar@85
   283
    ArgParser &refOption(const std::string &name,
alpar@209
   284
                      const std::string &help,
alpar@209
   285
                      std::string &ref, bool obl=false);
alpar@209
   286
alpar@85
   287
    ///@}
alpar@85
   288
alpar@85
   289
    ///\name Option Groups and Synonyms
alpar@85
   290
    ///
alpar@209
   291
alpar@85
   292
    ///@{
alpar@85
   293
kpeter@204
   294
    ///Bundle some options into a group
alpar@85
   295
alpar@85
   296
    /// You can group some option by calling this function repeatedly for each
kpeter@88
   297
    /// option to be grouped with the same groupname.
kpeter@88
   298
    ///\param group The group name.
kpeter@88
   299
    ///\param opt The option name.
alpar@85
   300
    ArgParser &optionGroup(const std::string &group,
alpar@209
   301
                           const std::string &opt);
alpar@85
   302
alpar@85
   303
    ///Make the members of a group exclusive
alpar@85
   304
alpar@85
   305
    ///If you call this function for a group, than at most one of them can be
kpeter@204
   306
    ///given at the same time.
alpar@85
   307
    ArgParser &onlyOneGroup(const std::string &group);
alpar@209
   308
alpar@85
   309
    ///Make a group mandatory
alpar@85
   310
alpar@85
   311
    ///Using this function, at least one of the members of \c group
alpar@85
   312
    ///must be given.
alpar@85
   313
    ArgParser &mandatoryGroup(const std::string &group);
alpar@209
   314
alpar@85
   315
    ///Create synonym to an option
alpar@85
   316
kpeter@88
   317
    ///With this function you can create a synonym \c syn of the
alpar@85
   318
    ///option \c opt.
alpar@85
   319
    ArgParser &synonym(const std::string &syn,
alpar@209
   320
                           const std::string &opt);
alpar@209
   321
alpar@85
   322
    ///@}
alpar@85
   323
alpar@214
   324
  private:
alpar@214
   325
    void show(std::ostream &os,Opts::const_iterator i) const;
alpar@214
   326
    void show(std::ostream &os,Groups::const_iterator i) const;
alpar@214
   327
    void showHelp(Opts::const_iterator i) const;
alpar@214
   328
    void showHelp(std::vector<OtherArg>::const_iterator i) const;
alpar@85
   329
alpar@214
   330
    void unknownOpt(std::string arg) const;
alpar@85
   331
alpar@214
   332
    void requiresValue(std::string arg, OptType t) const;
alpar@214
   333
    void checkMandatories() const;
alpar@214
   334
alpar@214
   335
    void shortHelp() const;
alpar@214
   336
    void showHelp() const;
alpar@214
   337
  public:
alpar@209
   338
alpar@85
   339
    ///Start the parsing process
alpar@85
   340
    ArgParser &parse();
alpar@85
   341
alpar@85
   342
    /// Synonym for parse()
alpar@209
   343
    ArgParser &run()
alpar@85
   344
    {
alpar@85
   345
      return parse();
alpar@85
   346
    }
alpar@209
   347
kpeter@204
   348
    ///Give back the command name (the 0th argument)
alpar@214
   349
    const std::string &commandName() const { return _command_name; }
kpeter@204
   350
alpar@85
   351
    ///Check if an opion has been given to the command.
alpar@214
   352
    bool given(std::string op) const
alpar@85
   353
    {
alpar@214
   354
      Opts::const_iterator i = _opts.find(op);
alpar@85
   355
      return i!=_opts.end()?i->second.set:false;
alpar@85
   356
    }
alpar@85
   357
alpar@85
   358
alpar@85
   359
    ///Magic type for operator[]
alpar@209
   360
alpar@85
   361
    ///This is the type of the return value of ArgParser::operator[]().
kpeter@88
   362
    ///It automatically converts to \c int, \c double, \c bool or
deba@290
   363
    ///\c std::string if the type of the option matches, which is checked
deba@290
   364
    ///with an \ref LEMON_ASSERT "assertion" (i.e. it performs runtime
deba@290
   365
    ///type checking).
alpar@209
   366
    class RefType
alpar@85
   367
    {
alpar@214
   368
      const ArgParser &_parser;
alpar@85
   369
      std::string _name;
alpar@85
   370
    public:
alpar@85
   371
      ///\e
alpar@214
   372
      RefType(const ArgParser &p,const std::string &n) :_parser(p),_name(n) {}
alpar@85
   373
      ///\e
alpar@209
   374
      operator bool()
alpar@85
   375
      {
alpar@214
   376
        Opts::const_iterator i = _parser._opts.find(_name);
alpar@209
   377
        LEMON_ASSERT(i!=_parser._opts.end(),
alpar@209
   378
                     std::string()+"Unkown option: '"+_name+"'");
alpar@209
   379
        LEMON_ASSERT(i->second.type==ArgParser::BOOL,
alpar@209
   380
                     std::string()+"'"+_name+"' is a bool option");
alpar@209
   381
        return *(i->second.bool_p);
alpar@85
   382
      }
alpar@85
   383
      ///\e
alpar@85
   384
      operator std::string()
alpar@85
   385
      {
alpar@214
   386
        Opts::const_iterator i = _parser._opts.find(_name);
alpar@209
   387
        LEMON_ASSERT(i!=_parser._opts.end(),
alpar@209
   388
                     std::string()+"Unkown option: '"+_name+"'");
alpar@209
   389
        LEMON_ASSERT(i->second.type==ArgParser::STRING,
alpar@209
   390
                     std::string()+"'"+_name+"' is a string option");
alpar@209
   391
        return *(i->second.string_p);
alpar@85
   392
      }
alpar@85
   393
      ///\e
alpar@209
   394
      operator double()
alpar@85
   395
      {
alpar@214
   396
        Opts::const_iterator i = _parser._opts.find(_name);
alpar@209
   397
        LEMON_ASSERT(i!=_parser._opts.end(),
alpar@209
   398
                     std::string()+"Unkown option: '"+_name+"'");
alpar@209
   399
        LEMON_ASSERT(i->second.type==ArgParser::DOUBLE ||
alpar@209
   400
                     i->second.type==ArgParser::INTEGER,
alpar@209
   401
                     std::string()+"'"+_name+"' is a floating point option");
alpar@209
   402
        return i->second.type==ArgParser::DOUBLE ?
alpar@209
   403
          *(i->second.double_p) : *(i->second.int_p);
alpar@85
   404
      }
alpar@85
   405
      ///\e
alpar@209
   406
      operator int()
alpar@85
   407
      {
alpar@214
   408
        Opts::const_iterator i = _parser._opts.find(_name);
alpar@209
   409
        LEMON_ASSERT(i!=_parser._opts.end(),
alpar@209
   410
                     std::string()+"Unkown option: '"+_name+"'");
alpar@209
   411
        LEMON_ASSERT(i->second.type==ArgParser::INTEGER,
alpar@209
   412
                     std::string()+"'"+_name+"' is an integer option");
alpar@209
   413
        return *(i->second.int_p);
alpar@85
   414
      }
alpar@85
   415
alpar@85
   416
    };
alpar@85
   417
alpar@85
   418
    ///Give back the value of an option
alpar@209
   419
kpeter@88
   420
    ///Give back the value of an option.
alpar@85
   421
    ///\sa RefType
alpar@214
   422
    RefType operator[](const std::string &n) const
alpar@85
   423
    {
alpar@85
   424
      return RefType(*this, n);
alpar@209
   425
    }
kpeter@204
   426
kpeter@204
   427
    ///Give back the non-option type arguments.
kpeter@204
   428
kpeter@204
   429
    ///Give back a reference to a vector consisting of the program arguments
kpeter@204
   430
    ///not starting with a '-' character.
alpar@214
   431
    const std::vector<std::string> &files() const { return _file_args; }
alpar@209
   432
alpar@915
   433
    ///Throw instead of exit in case of problems
alpar@956
   434
    void throwOnProblems()
alpar@915
   435
    {
alpar@915
   436
      _exit_on_problems=false;
alpar@915
   437
    }
alpar@85
   438
  };
alpar@85
   439
}
alpar@85
   440
alpar@261
   441
#endif // LEMON_ARG_PARSER_H