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