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