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