lemon/arg_parser.cc
author Alpar Juttner <alpar@cs.elte.hu>
Tue, 16 Sep 2008 16:07:35 +0100
changeset 262 0181b7f12a2a
parent 210 81cfc04531e8
child 311 c887e703b566
permissions -rw-r--r--
Update NEWS file (ticket #121)
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@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
#include <lemon/arg_parser.h>
alpar@85
    20
alpar@85
    21
namespace lemon {
alpar@85
    22
alpar@85
    23
  void ArgParser::_showHelp(void *p)
alpar@85
    24
  {
alpar@85
    25
    (static_cast<ArgParser*>(p))->showHelp();
alpar@85
    26
    exit(1);
alpar@85
    27
  }
alpar@85
    28
alpar@85
    29
  ArgParser::ArgParser(int argc, const char **argv) :_argc(argc), _argv(argv),
alpar@85
    30
                                                     _command_name(argv[0]) {
alpar@85
    31
    funcOption("-help","Print a short help message",_showHelp,this);
alpar@85
    32
    synonym("help","-help");
alpar@85
    33
    synonym("h","-help");
alpar@85
    34
alpar@85
    35
  }
alpar@85
    36
alpar@85
    37
  ArgParser::~ArgParser()
alpar@85
    38
  {
alpar@85
    39
    for(Opts::iterator i=_opts.begin();i!=_opts.end();++i)
alpar@85
    40
      if(i->second.self_delete)
alpar@209
    41
        switch(i->second.type) {
alpar@209
    42
        case BOOL:
alpar@209
    43
          delete i->second.bool_p;
alpar@209
    44
          break;
alpar@209
    45
        case STRING:
alpar@209
    46
          delete i->second.string_p;
alpar@209
    47
          break;
alpar@209
    48
        case DOUBLE:
alpar@209
    49
          delete i->second.double_p;
alpar@209
    50
          break;
alpar@209
    51
        case INTEGER:
alpar@209
    52
          delete i->second.int_p;
alpar@209
    53
          break;
alpar@209
    54
        case UNKNOWN:
alpar@209
    55
          break;
alpar@209
    56
        case FUNC:
alpar@209
    57
          break;
alpar@209
    58
        }
alpar@85
    59
  }
alpar@209
    60
alpar@85
    61
alpar@85
    62
  ArgParser &ArgParser::intOption(const std::string &name,
alpar@209
    63
                               const std::string &help,
alpar@209
    64
                               int value, bool obl)
alpar@85
    65
  {
alpar@85
    66
    ParData p;
alpar@85
    67
    p.int_p=new int(value);
alpar@85
    68
    p.self_delete=true;
alpar@85
    69
    p.help=help;
alpar@85
    70
    p.type=INTEGER;
alpar@85
    71
    p.mandatory=obl;
alpar@85
    72
    _opts[name]=p;
alpar@85
    73
    return *this;
alpar@85
    74
  }
alpar@85
    75
alpar@85
    76
  ArgParser &ArgParser::doubleOption(const std::string &name,
alpar@209
    77
                               const std::string &help,
alpar@209
    78
                               double value, bool obl)
alpar@85
    79
  {
alpar@85
    80
    ParData p;
alpar@85
    81
    p.double_p=new double(value);
alpar@85
    82
    p.self_delete=true;
alpar@85
    83
    p.help=help;
alpar@85
    84
    p.type=DOUBLE;
alpar@85
    85
    p.mandatory=obl;
alpar@85
    86
    _opts[name]=p;
alpar@85
    87
    return *this;
alpar@85
    88
  }
alpar@85
    89
alpar@85
    90
  ArgParser &ArgParser::boolOption(const std::string &name,
alpar@209
    91
                               const std::string &help,
alpar@209
    92
                               bool value, bool obl)
alpar@85
    93
  {
alpar@85
    94
    ParData p;
alpar@85
    95
    p.bool_p=new bool(value);
alpar@85
    96
    p.self_delete=true;
alpar@85
    97
    p.help=help;
alpar@85
    98
    p.type=BOOL;
alpar@85
    99
    p.mandatory=obl;
alpar@85
   100
    _opts[name]=p;
alpar@85
   101
    return *this;
alpar@85
   102
  }
alpar@85
   103
alpar@85
   104
  ArgParser &ArgParser::stringOption(const std::string &name,
alpar@209
   105
                               const std::string &help,
alpar@209
   106
                               std::string value, bool obl)
alpar@85
   107
  {
alpar@85
   108
    ParData p;
alpar@85
   109
    p.string_p=new std::string(value);
alpar@85
   110
    p.self_delete=true;
alpar@85
   111
    p.help=help;
alpar@85
   112
    p.type=STRING;
alpar@85
   113
    p.mandatory=obl;
alpar@85
   114
    _opts[name]=p;
alpar@85
   115
    return *this;
alpar@85
   116
  }
alpar@85
   117
alpar@85
   118
  ArgParser &ArgParser::refOption(const std::string &name,
alpar@209
   119
                               const std::string &help,
alpar@209
   120
                               int &ref, bool obl)
alpar@85
   121
  {
alpar@85
   122
    ParData p;
alpar@85
   123
    p.int_p=&ref;
alpar@85
   124
    p.self_delete=false;
alpar@85
   125
    p.help=help;
alpar@85
   126
    p.type=INTEGER;
alpar@85
   127
    p.mandatory=obl;
alpar@85
   128
    _opts[name]=p;
alpar@85
   129
    return *this;
alpar@85
   130
  }
alpar@85
   131
alpar@85
   132
  ArgParser &ArgParser::refOption(const std::string &name,
alpar@85
   133
                                  const std::string &help,
alpar@85
   134
                                  double &ref, bool obl)
alpar@85
   135
  {
alpar@85
   136
    ParData p;
alpar@85
   137
    p.double_p=&ref;
alpar@85
   138
    p.self_delete=false;
alpar@85
   139
    p.help=help;
alpar@85
   140
    p.type=DOUBLE;
alpar@85
   141
    p.mandatory=obl;
alpar@85
   142
    _opts[name]=p;
alpar@85
   143
    return *this;
alpar@85
   144
  }
alpar@85
   145
alpar@85
   146
  ArgParser &ArgParser::refOption(const std::string &name,
alpar@85
   147
                                  const std::string &help,
alpar@85
   148
                                  bool &ref, bool obl)
alpar@85
   149
  {
alpar@85
   150
    ParData p;
alpar@85
   151
    p.bool_p=&ref;
alpar@85
   152
    p.self_delete=false;
alpar@85
   153
    p.help=help;
alpar@85
   154
    p.type=BOOL;
alpar@85
   155
    p.mandatory=obl;
alpar@85
   156
    _opts[name]=p;
alpar@85
   157
alpar@85
   158
    ref = false;
alpar@85
   159
alpar@85
   160
    return *this;
alpar@85
   161
  }
alpar@85
   162
alpar@85
   163
  ArgParser &ArgParser::refOption(const std::string &name,
alpar@209
   164
                               const std::string &help,
alpar@209
   165
                               std::string &ref, bool obl)
alpar@85
   166
  {
alpar@85
   167
    ParData p;
alpar@85
   168
    p.string_p=&ref;
alpar@85
   169
    p.self_delete=false;
alpar@85
   170
    p.help=help;
alpar@85
   171
    p.type=STRING;
alpar@85
   172
    p.mandatory=obl;
alpar@85
   173
    _opts[name]=p;
alpar@85
   174
    return *this;
alpar@85
   175
  }
alpar@85
   176
alpar@85
   177
  ArgParser &ArgParser::funcOption(const std::string &name,
alpar@209
   178
                               const std::string &help,
alpar@209
   179
                               void (*func)(void *),void *data)
alpar@85
   180
  {
alpar@85
   181
    ParData p;
alpar@85
   182
    p.func_p.p=func;
alpar@85
   183
    p.func_p.data=data;
alpar@85
   184
    p.self_delete=false;
alpar@85
   185
    p.help=help;
alpar@85
   186
    p.type=FUNC;
alpar@85
   187
    p.mandatory=false;
alpar@85
   188
    _opts[name]=p;
alpar@85
   189
    return *this;
alpar@85
   190
  }
alpar@85
   191
alpar@85
   192
  ArgParser &ArgParser::optionGroup(const std::string &group,
alpar@209
   193
                                    const std::string &opt)
alpar@85
   194
  {
alpar@85
   195
    Opts::iterator i = _opts.find(opt);
alpar@85
   196
    LEMON_ASSERT(i!=_opts.end(), "Unknown option: '"+opt+"'");
alpar@209
   197
    LEMON_ASSERT(!(i->second.ingroup),
alpar@85
   198
                 "Option already in option group: '"+opt+"'");
alpar@85
   199
    GroupData &g=_groups[group];
alpar@85
   200
    g.opts.push_back(opt);
alpar@85
   201
    i->second.ingroup=true;
alpar@85
   202
    return *this;
alpar@85
   203
  }
alpar@85
   204
alpar@85
   205
  ArgParser &ArgParser::onlyOneGroup(const std::string &group)
alpar@85
   206
  {
alpar@85
   207
    GroupData &g=_groups[group];
alpar@85
   208
    g.only_one=true;
alpar@85
   209
    return *this;
alpar@85
   210
  }
alpar@85
   211
alpar@85
   212
  ArgParser &ArgParser::synonym(const std::string &syn,
alpar@209
   213
                                const std::string &opt)
alpar@85
   214
  {
alpar@85
   215
    Opts::iterator o = _opts.find(opt);
alpar@85
   216
    Opts::iterator s = _opts.find(syn);
alpar@85
   217
    LEMON_ASSERT(o!=_opts.end(), "Unknown option: '"+opt+"'");
alpar@85
   218
    LEMON_ASSERT(s==_opts.end(), "Option already used: '"+syn+"'");
alpar@85
   219
    ParData p;
alpar@85
   220
    p.help=opt;
alpar@85
   221
    p.mandatory=false;
alpar@85
   222
    p.syn=true;
alpar@85
   223
    _opts[syn]=p;
alpar@85
   224
    o->second.has_syn=true;
alpar@85
   225
    return *this;
alpar@85
   226
  }
alpar@85
   227
alpar@85
   228
  ArgParser &ArgParser::mandatoryGroup(const std::string &group)
alpar@85
   229
  {
alpar@85
   230
    GroupData &g=_groups[group];
alpar@85
   231
    g.mandatory=true;
alpar@85
   232
    return *this;
alpar@85
   233
  }
alpar@85
   234
alpar@85
   235
  ArgParser &ArgParser::other(const std::string &name,
alpar@209
   236
                              const std::string &help)
alpar@85
   237
  {
alpar@85
   238
    _others_help.push_back(OtherArg(name,help));
alpar@85
   239
    return *this;
alpar@85
   240
  }
alpar@85
   241
alpar@214
   242
  void ArgParser::show(std::ostream &os,Opts::const_iterator i) const
alpar@85
   243
  {
alpar@85
   244
    os << "-" << i->first;
alpar@85
   245
    if(i->second.has_syn)
alpar@214
   246
      for(Opts::const_iterator j=_opts.begin();j!=_opts.end();++j)
alpar@209
   247
        if(j->second.syn&&j->second.help==i->first)
alpar@209
   248
          os << "|-" << j->first;
alpar@85
   249
    switch(i->second.type) {
alpar@85
   250
    case STRING:
alpar@85
   251
      os << " str";
alpar@85
   252
      break;
alpar@85
   253
    case INTEGER:
alpar@85
   254
      os << " int";
alpar@85
   255
      break;
alpar@85
   256
    case DOUBLE:
alpar@85
   257
      os << " num";
alpar@85
   258
      break;
alpar@85
   259
    default:
alpar@85
   260
      break;
alpar@85
   261
    }
alpar@85
   262
  }
alpar@85
   263
alpar@214
   264
  void ArgParser::show(std::ostream &os,Groups::const_iterator i) const
alpar@85
   265
  {
alpar@214
   266
    GroupData::Opts::const_iterator o=i->second.opts.begin();
alpar@85
   267
    while(o!=i->second.opts.end()) {
alpar@85
   268
      show(os,_opts.find(*o));
alpar@85
   269
      ++o;
alpar@85
   270
      if(o!=i->second.opts.end()) os<<'|';
alpar@85
   271
    }
alpar@85
   272
  }
alpar@209
   273
alpar@214
   274
  void ArgParser::showHelp(Opts::const_iterator i) const
alpar@85
   275
  {
alpar@85
   276
    if(i->second.help.size()==0||i->second.syn) return;
alpar@85
   277
    std::cerr << "  ";
alpar@85
   278
    show(std::cerr,i);
alpar@85
   279
    std::cerr << std::endl;
alpar@85
   280
    std::cerr << "     " << i->second.help << std::endl;
alpar@85
   281
  }
alpar@214
   282
  void ArgParser::showHelp(std::vector<ArgParser::OtherArg>::const_iterator i)
alpar@214
   283
    const
alpar@85
   284
  {
alpar@85
   285
    if(i->help.size()==0) return;
alpar@85
   286
    std::cerr << "  " << i->name << std::endl
alpar@209
   287
              << "     " << i->help << std::endl;
alpar@85
   288
  }
alpar@209
   289
alpar@214
   290
  void ArgParser::shortHelp() const
alpar@85
   291
  {
alpar@85
   292
    const unsigned int LINE_LEN=77;
alpar@85
   293
    const std::string indent("    ");
alpar@85
   294
    std::cerr << "Usage:\n  " << _command_name;
alpar@85
   295
    int pos=_command_name.size()+2;
alpar@214
   296
    for(Groups::const_iterator g=_groups.begin();g!=_groups.end();++g) {
alpar@85
   297
      std::ostringstream cstr;
alpar@85
   298
      cstr << ' ';
alpar@85
   299
      if(!g->second.mandatory) cstr << '[';
alpar@85
   300
      show(cstr,g);
alpar@85
   301
      if(!g->second.mandatory) cstr << ']';
alpar@85
   302
      if(pos+cstr.str().size()>LINE_LEN) {
alpar@209
   303
        std::cerr << std::endl << indent;
alpar@209
   304
        pos=indent.size();
alpar@85
   305
      }
alpar@85
   306
      std::cerr << cstr.str();
alpar@85
   307
      pos+=cstr.str().size();
alpar@85
   308
    }
alpar@214
   309
    for(Opts::const_iterator i=_opts.begin();i!=_opts.end();++i)
alpar@85
   310
      if(!i->second.ingroup&&!i->second.syn) {
alpar@209
   311
        std::ostringstream cstr;
alpar@209
   312
        cstr << ' ';
alpar@209
   313
        if(!i->second.mandatory) cstr << '[';
alpar@209
   314
        show(cstr,i);
alpar@209
   315
        if(!i->second.mandatory) cstr << ']';
alpar@209
   316
        if(pos+cstr.str().size()>LINE_LEN) {
alpar@209
   317
          std::cerr << std::endl << indent;
alpar@209
   318
          pos=indent.size();
alpar@209
   319
        }
alpar@209
   320
        std::cerr << cstr.str();
alpar@209
   321
        pos+=cstr.str().size();
alpar@85
   322
      }
alpar@214
   323
    for(std::vector<OtherArg>::const_iterator i=_others_help.begin();
alpar@209
   324
        i!=_others_help.end();++i)
alpar@85
   325
      {
alpar@209
   326
        std::ostringstream cstr;
alpar@209
   327
        cstr << ' ' << i->name;
alpar@209
   328
alpar@209
   329
        if(pos+cstr.str().size()>LINE_LEN) {
alpar@209
   330
          std::cerr << std::endl << indent;
alpar@209
   331
          pos=indent.size();
alpar@209
   332
        }
alpar@209
   333
        std::cerr << cstr.str();
alpar@209
   334
        pos+=cstr.str().size();
alpar@85
   335
      }
alpar@85
   336
    std::cerr << std::endl;
alpar@85
   337
  }
alpar@209
   338
alpar@214
   339
  void ArgParser::showHelp() const
alpar@85
   340
  {
alpar@85
   341
    shortHelp();
alpar@85
   342
    std::cerr << "Where:\n";
alpar@214
   343
    for(std::vector<OtherArg>::const_iterator i=_others_help.begin();
alpar@209
   344
        i!=_others_help.end();++i) showHelp(i);
alpar@214
   345
    for(Opts::const_iterator i=_opts.begin();i!=_opts.end();++i) showHelp(i);
alpar@85
   346
    exit(1);
alpar@85
   347
  }
alpar@209
   348
alpar@209
   349
alpar@214
   350
  void ArgParser::unknownOpt(std::string arg) const
alpar@85
   351
  {
alpar@85
   352
    std::cerr << "\nUnknown option: " << arg << "\n";
alpar@85
   353
    std::cerr << "\nType '" << _command_name <<
alpar@85
   354
      " --help' to obtain a short summary on the usage.\n\n";
alpar@85
   355
    exit(1);
alpar@85
   356
  }
alpar@209
   357
alpar@214
   358
  void ArgParser::requiresValue(std::string arg, OptType t) const
alpar@85
   359
  {
alpar@85
   360
    std::cerr << "Argument '" << arg << "' requires a";
alpar@85
   361
    switch(t) {
alpar@85
   362
    case STRING:
alpar@85
   363
      std::cerr << " string";
alpar@85
   364
      break;
alpar@85
   365
    case INTEGER:
alpar@85
   366
      std::cerr << "n integer";
alpar@85
   367
      break;
alpar@85
   368
    case DOUBLE:
alpar@85
   369
      std::cerr << " floating point";
alpar@85
   370
      break;
alpar@85
   371
    default:
alpar@85
   372
      break;
alpar@85
   373
    }
alpar@85
   374
    std::cerr << " value\n\n";
alpar@85
   375
    showHelp();
alpar@85
   376
  }
alpar@209
   377
alpar@85
   378
alpar@214
   379
  void ArgParser::checkMandatories() const
alpar@85
   380
  {
alpar@85
   381
    bool ok=true;
alpar@214
   382
    for(Opts::const_iterator i=_opts.begin();i!=_opts.end();++i)
alpar@209
   383
      if(i->second.mandatory&&!i->second.set)
alpar@209
   384
        {
alpar@209
   385
          if(ok)
alpar@209
   386
            std::cerr << _command_name
alpar@209
   387
                      << ": The following mandatory arguments are missing.\n";
alpar@209
   388
          ok=false;
alpar@209
   389
          showHelp(i);
alpar@209
   390
        }
alpar@214
   391
    for(Groups::const_iterator i=_groups.begin();i!=_groups.end();++i)
alpar@85
   392
      if(i->second.mandatory||i->second.only_one)
alpar@209
   393
        {
alpar@209
   394
          int set=0;
alpar@214
   395
          for(GroupData::Opts::const_iterator o=i->second.opts.begin();
alpar@209
   396
              o!=i->second.opts.end();++o)
alpar@209
   397
            if(_opts.find(*o)->second.set) ++set;
alpar@209
   398
          if(i->second.mandatory&&!set) {
alpar@210
   399
            std::cerr << _command_name <<
alpar@210
   400
              ": At least one of the following arguments is mandatory.\n";
alpar@209
   401
            ok=false;
alpar@214
   402
            for(GroupData::Opts::const_iterator o=i->second.opts.begin();
alpar@209
   403
                o!=i->second.opts.end();++o)
alpar@209
   404
              showHelp(_opts.find(*o));
alpar@209
   405
          }
alpar@209
   406
          if(i->second.only_one&&set>1) {
alpar@210
   407
            std::cerr << _command_name <<
alpar@210
   408
              ": At most one of the following arguments can be given.\n";
alpar@209
   409
            ok=false;
alpar@214
   410
            for(GroupData::Opts::const_iterator o=i->second.opts.begin();
alpar@209
   411
                o!=i->second.opts.end();++o)
alpar@209
   412
              showHelp(_opts.find(*o));
alpar@209
   413
          }
alpar@209
   414
        }
alpar@85
   415
    if(!ok) {
alpar@85
   416
      std::cerr << "\nType '" << _command_name <<
alpar@209
   417
        " --help' to obtain a short summary on the usage.\n\n";
alpar@85
   418
      exit(1);
alpar@85
   419
    }
alpar@85
   420
  }
alpar@85
   421
alpar@85
   422
  ArgParser &ArgParser::parse()
alpar@85
   423
  {
alpar@85
   424
    for(int ar=1; ar<_argc; ++ar) {
alpar@85
   425
      std::string arg(_argv[ar]);
alpar@85
   426
      if (arg[0] != '-' || arg.size() == 1) {
alpar@209
   427
        _file_args.push_back(arg);
alpar@85
   428
      }
alpar@85
   429
      else {
alpar@209
   430
        Opts::iterator i = _opts.find(arg.substr(1));
alpar@209
   431
        if(i==_opts.end()) unknownOpt(arg);
alpar@209
   432
        else {
alpar@209
   433
          if(i->second.syn) i=_opts.find(i->second.help);
alpar@209
   434
          ParData &p(i->second);
alpar@209
   435
          if (p.type==BOOL) *p.bool_p=true;
alpar@209
   436
          else if (p.type==FUNC) p.func_p.p(p.func_p.data);
alpar@209
   437
          else if(++ar==_argc) requiresValue(arg, p.type);
alpar@209
   438
          else {
alpar@209
   439
            std::string val(_argv[ar]);
alpar@209
   440
            std::istringstream vals(val);
alpar@209
   441
            switch(p.type) {
alpar@209
   442
            case STRING:
alpar@209
   443
              *p.string_p=val;
alpar@209
   444
              break;
alpar@209
   445
            case INTEGER:
alpar@209
   446
              vals >> *p.int_p;
alpar@209
   447
              break;
alpar@209
   448
            case DOUBLE:
alpar@209
   449
              vals >> *p.double_p;
alpar@209
   450
              break;
alpar@209
   451
            default:
alpar@209
   452
              break;
alpar@209
   453
            }
alpar@209
   454
            if(p.type!=STRING&&(!vals||!vals.eof()))
alpar@209
   455
              requiresValue(arg, p.type);
alpar@209
   456
          }
alpar@209
   457
          p.set = true;
alpar@209
   458
        }
alpar@85
   459
      }
alpar@85
   460
    }
alpar@85
   461
    checkMandatories();
alpar@85
   462
alpar@85
   463
    return *this;
alpar@209
   464
  }
alpar@85
   465
alpar@85
   466
}