arg_parser.h: A command line argument parser.
authoralpar
Sat, 03 Mar 2007 12:05:05 +0000
changeset 2389df6a32249b46
parent 2388 c6d537888fe5
child 2390 8450951a8e2d
arg_parser.h: A command line argument parser.
dist_log.h: A tool for measuring one and two dimensional distributions.
demo/Makefile.am
demo/arg_parser_demo.cc
lemon/Makefile.am
lemon/arg_parser.cc
lemon/arg_parser.h
lemon/dist_log.h
     1.1 --- a/demo/Makefile.am	Sat Mar 03 12:00:32 2007 +0000
     1.2 +++ b/demo/Makefile.am	Sat Mar 03 12:05:05 2007 +0000
     1.3 @@ -17,6 +17,7 @@
     1.4  if WANT_DEMO
     1.5  
     1.6  noinst_PROGRAMS += \
     1.7 +	demo/arg_parser_demo \
     1.8  	demo/circulation_demo \
     1.9  	demo/csp_demo \
    1.10  	demo/dim_to_dot \
    1.11 @@ -50,6 +51,8 @@
    1.12  
    1.13  endif WANT_DEMO
    1.14  
    1.15 +demo_arg_parser_demo_SOURCES = demo/arg_parser_demo.cc
    1.16 +
    1.17  demo_csp_demo_SOURCES = demo/csp_demo.cc
    1.18  
    1.19  demo_circulation_demo_SOURCES = demo/circulation_demo.cc
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/demo/arg_parser_demo.cc	Sat Mar 03 12:05:05 2007 +0000
     2.3 @@ -0,0 +1,53 @@
     2.4 +#include <lemon/arg_parser.h>
     2.5 +
     2.6 +using namespace lemon;
     2.7 +int main(int argc, char **argv)
     2.8 +{
     2.9 +  ArgParser ap(argc,argv);
    2.10 +  int i;
    2.11 +  std::string s;
    2.12 +  double d;
    2.13 +  bool b,sil;
    2.14 +  bool g1,g2,g3;
    2.15 +  ap.option("n", "an integer input", i, true)
    2.16 +    .option("val", "a double input", d)
    2.17 +    .synonym("vals","val")
    2.18 +    .option("name", "a string input", s)
    2.19 +    .option("f", "a switch", b)
    2.20 +    .option("nohelp", "", sil)
    2.21 +    .option("gra","Choise A",g1)
    2.22 +    .option("grb","Choise B",g2)
    2.23 +    .option("grc","Choise C",g3)
    2.24 +    .optionGroup("gr","gra")
    2.25 +    .optionGroup("gr","grb")
    2.26 +    .optionGroup("gr","grc")
    2.27 +    .mandatoryGroup("gr")
    2.28 +    .onlyOneGroup("gr")
    2.29 +    .other("infile","The input file")
    2.30 +    .other("...");
    2.31 +  
    2.32 +  ap.parse();
    2.33 +
    2.34 +  std::cout << "Parameters of '" << ap.commandName() << "':\n";
    2.35 +
    2.36 +  if(ap.given("n")) std::cout << "  Value of -n: " << i << std::endl;
    2.37 +  if(ap.given("val")) std::cout << "  Value of -val: " << d << std::endl;
    2.38 +  if(ap.given("name")) std::cout << "  Value of -name: " << s << std::endl;
    2.39 +  if(ap.given("f")) std::cout << "  -f is given\n";
    2.40 +  if(ap.given("nohelp")) std::cout << "  Value of -nohelp: " << sil << std::endl;
    2.41 +
    2.42 +  switch(ap.files().size()) {
    2.43 +  case 0:
    2.44 +    std::cout << "  No file argument was given.\n";
    2.45 +    break;
    2.46 +  case 1:
    2.47 +    std::cout << "  1 file argument was given. It is:\n";
    2.48 +    break;
    2.49 +  default:
    2.50 +    std::cout << "  "
    2.51 +	      << ap.files().size() << " file arguments were given. They are:\n";
    2.52 +  }
    2.53 +  for(unsigned int i=0;i<ap.files().size();++i)
    2.54 +    std::cout << "    '" << ap.files()[i] << "'\n";
    2.55 +  
    2.56 +}
     3.1 --- a/lemon/Makefile.am	Sat Mar 03 12:00:32 2007 +0000
     3.2 +++ b/lemon/Makefile.am	Sat Mar 03 12:05:05 2007 +0000
     3.3 @@ -7,6 +7,7 @@
     3.4  lib_LTLIBRARIES += lemon/libemon.la
     3.5  
     3.6  lemon_libemon_la_SOURCES = \
     3.7 +	lemon/arg_parser.cc \
     3.8  	lemon/lp_base.cc \
     3.9  	lemon/lp_skeleton.cc \
    3.10  	lemon/base.cc \
    3.11 @@ -33,7 +34,7 @@
    3.12  endif
    3.13  
    3.14  lemon_HEADERS += \
    3.15 -	lemon/circulation.h \
    3.16 +	lemon/arg_parser.h \
    3.17  	lemon/bellman_ford.h \
    3.18  	lemon/bfs.h \
    3.19  	lemon/bin_heap.h \
    3.20 @@ -41,6 +42,7 @@
    3.21  	lemon/bp_matching.h \
    3.22  	lemon/bpugraph_adaptor.h \
    3.23  	lemon/bucket_heap.h \
    3.24 +	lemon/circulation.h \
    3.25  	lemon/color.h \
    3.26  	lemon/config.h \
    3.27  	lemon/concept_check.h \
    3.28 @@ -49,6 +51,7 @@
    3.29  	lemon/dag_shortest_path.h \
    3.30  	lemon/dfs.h \
    3.31  	lemon/dijkstra.h \
    3.32 +	lemon/dist_log.h \
    3.33  	lemon/dim2.h \
    3.34  	lemon/dimacs.h \
    3.35  	lemon/edge_set.h \
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/lemon/arg_parser.cc	Sat Mar 03 12:05:05 2007 +0000
     4.3 @@ -0,0 +1,379 @@
     4.4 +/* -*- C++ -*-
     4.5 + * lemon/main_params.h - Part of LEMON, a generic C++ optimization library
     4.6 + *
     4.7 + * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     4.8 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
     4.9 + *
    4.10 + * Permission to use, modify and distribute this software is granted
    4.11 + * provided that this copyright notice appears in all copies. For
    4.12 + * precise terms see the accompanying LICENSE file.
    4.13 + *
    4.14 + * This software is provided "AS IS" with no warranty of any kind,
    4.15 + * express or implied, and with no claim as to its suitability for any
    4.16 + * purpose.
    4.17 + *
    4.18 + */
    4.19 +
    4.20 +#include <lemon/arg_parser.h>
    4.21 +
    4.22 +namespace lemon {
    4.23 +
    4.24 +  void ArgParser::_showHelp(void *p)
    4.25 +  {
    4.26 +    ((ArgParser*)p)->showHelp();
    4.27 +    exit(1);
    4.28 +  }
    4.29 +
    4.30 +  ArgParser::ArgParser(int argc, char **argv) :_argc(argc), _argv(argv),
    4.31 +					       _command_name(argv[0]) {
    4.32 +    option("-help","Print a short help message",_showHelp,this);
    4.33 +    synonym("help","-help");
    4.34 +    synonym("h","-help");
    4.35 +
    4.36 +  }
    4.37 +
    4.38 +  ArgParser &ArgParser::option(const std::string &name,
    4.39 +			       const std::string &help,
    4.40 +			       int &value, bool obl)
    4.41 +  {
    4.42 +    ParData p;
    4.43 +    p.int_p=&value;
    4.44 +    p.help=help;
    4.45 +    p.type=INTEGER;
    4.46 +    p.mandatory=obl;
    4.47 +    _opts[name]=p;
    4.48 +    return *this;
    4.49 +  }
    4.50 +
    4.51 +  ArgParser &ArgParser::option(const std::string &name,
    4.52 +			       const std::string &help,
    4.53 +			       double &value, bool obl)
    4.54 +  {
    4.55 +    ParData p;
    4.56 +    p.double_p=&value;
    4.57 +    p.help=help;
    4.58 +    p.type=DOUBLE;
    4.59 +    p.mandatory=obl;
    4.60 +    _opts[name]=p;
    4.61 +    return *this;
    4.62 +  }
    4.63 +
    4.64 +  ArgParser &ArgParser::option(const std::string &name,
    4.65 +			       const std::string &help,
    4.66 +			       bool &value, bool obl)
    4.67 +  {
    4.68 +    ParData p;
    4.69 +    p.bool_p=&value;
    4.70 +    p.help=help;
    4.71 +    p.type=BOOL;
    4.72 +    p.mandatory=obl;
    4.73 +    _opts[name]=p;
    4.74 +
    4.75 +    value = false;
    4.76 +
    4.77 +    return *this;
    4.78 +  }
    4.79 +
    4.80 +  ArgParser &ArgParser::option(const std::string &name,
    4.81 +			       const std::string &help,
    4.82 +			       std::string &value, bool obl)
    4.83 +  {
    4.84 +    ParData p;
    4.85 +    p.string_p=&value;
    4.86 +    p.help=help;
    4.87 +    p.type=STRING;
    4.88 +    p.mandatory=obl;
    4.89 +    _opts[name]=p;
    4.90 +    return *this;
    4.91 +  }
    4.92 +
    4.93 +  ArgParser &ArgParser::option(const std::string &name,
    4.94 +			       const std::string &help,
    4.95 +			       void (*func)(void *),void *data)
    4.96 +  {
    4.97 +    ParData p;
    4.98 +    p.func_p.p=func;
    4.99 +    p.func_p.data=data;
   4.100 +    p.help=help;
   4.101 +    p.type=FUNC;
   4.102 +    p.mandatory=false;
   4.103 +    _opts[name]=p;
   4.104 +    return *this;
   4.105 +  }
   4.106 +  ArgParser &ArgParser::optionGroup(const std::string &group,
   4.107 +				    const std::string &opt)
   4.108 +  {
   4.109 +    Opts::iterator i = _opts.find(opt);
   4.110 +    if(i==_opts.end()) exit(3); ///\todo throw exception instead
   4.111 +    else if(i->second.ingroup) exit(3); ///\todo throw exception instead
   4.112 +    else {
   4.113 +      GroupData &g=_groups[group];
   4.114 +      g.opts.push_back(opt);
   4.115 +      i->second.ingroup=true;
   4.116 +    }
   4.117 +    return *this;
   4.118 +  }
   4.119 +
   4.120 +  ArgParser &ArgParser::onlyOneGroup(const std::string &group)
   4.121 +  {
   4.122 +    GroupData &g=_groups[group];
   4.123 +    g.only_one=true;
   4.124 +    return *this;
   4.125 +  }
   4.126 +
   4.127 +  ArgParser &ArgParser::synonym(const std::string &syn,
   4.128 +				const std::string &opt)
   4.129 +  {
   4.130 +    Opts::iterator o = _opts.find(opt);
   4.131 +    Opts::iterator s = _opts.find(syn);
   4.132 +    if(o==_opts.end()||s!=_opts.end())
   4.133 +      exit(3); ///\todo throw exception instead
   4.134 +    else {
   4.135 +      ParData p;
   4.136 +      p.help=opt;
   4.137 +      p.mandatory=false;
   4.138 +      p.syn=true;
   4.139 +      _opts[syn]=p;
   4.140 +      o->second.has_syn=true;
   4.141 +    }
   4.142 +    return *this;
   4.143 +  }
   4.144 +
   4.145 +  ArgParser &ArgParser::mandatoryGroup(const std::string &group)
   4.146 +  {
   4.147 +    GroupData &g=_groups[group];
   4.148 +    g.mandatory=true;
   4.149 +    return *this;
   4.150 +  }
   4.151 +
   4.152 +  ArgParser &ArgParser::other(const std::string &name,
   4.153 +			      const std::string &help)
   4.154 +  {
   4.155 +    _others_help.push_back(OtherArg(name,help));
   4.156 +    return *this;
   4.157 +  }
   4.158 +
   4.159 +  void ArgParser::show(std::ostream &os,Opts::iterator i)
   4.160 +  {
   4.161 +    os << "-" << i->first;
   4.162 +    if(i->second.has_syn)
   4.163 +      for(Opts::iterator j=_opts.begin();j!=_opts.end();++j)
   4.164 +	if(j->second.syn&&j->second.help==i->first)
   4.165 +	  os << "|-" << j->first;
   4.166 +    switch(i->second.type) {
   4.167 +    case STRING:
   4.168 +      os << " str";
   4.169 +      break;
   4.170 +    case INTEGER:
   4.171 +      os << " int";
   4.172 +      break;
   4.173 +    case DOUBLE:
   4.174 +      os << " num";
   4.175 +      break;
   4.176 +    default:
   4.177 +      break;
   4.178 +    }
   4.179 +  }
   4.180 +
   4.181 +  void ArgParser::show(std::ostream &os,Groups::iterator i)
   4.182 +  {
   4.183 +    GroupData::Opts::iterator o=i->second.opts.begin();
   4.184 +    while(o!=i->second.opts.end()) {
   4.185 +      show(os,_opts.find(*o));
   4.186 +      ++o;
   4.187 +      if(o!=i->second.opts.end()) os<<'|';
   4.188 +    }
   4.189 +  }
   4.190 +    
   4.191 +  void ArgParser::showHelp(Opts::iterator i)
   4.192 +  {
   4.193 +    if(i->second.help.size()==0||i->second.syn) return;
   4.194 +    std::cerr << "  ";
   4.195 +    show(std::cerr,i);
   4.196 +    std::cerr << std::endl;
   4.197 +    std::cerr << "     " << i->second.help << std::endl;
   4.198 +  }
   4.199 +  void ArgParser::showHelp(std::vector<ArgParser::OtherArg>::iterator i)
   4.200 +  {
   4.201 +    if(i->help.size()==0) return;
   4.202 +    std::cerr << "  " << i->name << std::endl
   4.203 +	      << "     " << i->help << std::endl;
   4.204 +  }
   4.205 +    
   4.206 +  void ArgParser::shortHelp()
   4.207 +  {
   4.208 +    const unsigned int LINE_LEN=77;
   4.209 +    const std::string indent("    ");
   4.210 +    std::cerr << "Usage:\n  " << _command_name;
   4.211 +    int pos=_command_name.size()+2;
   4.212 +    for(Groups::iterator g=_groups.begin();g!=_groups.end();++g) {
   4.213 +      std::ostringstream cstr;
   4.214 +      cstr << ' ';
   4.215 +      if(!g->second.mandatory) cstr << '[';
   4.216 +      show(cstr,g);
   4.217 +      if(!g->second.mandatory) cstr << ']';
   4.218 +      if(pos+cstr.str().size()>LINE_LEN) {
   4.219 +	std::cerr << std::endl << indent;
   4.220 +	pos=indent.size();
   4.221 +      }
   4.222 +      std::cerr << cstr.str();
   4.223 +      pos+=cstr.str().size();
   4.224 +    }
   4.225 +    for(Opts::iterator i=_opts.begin();i!=_opts.end();++i)
   4.226 +      if(!i->second.ingroup&&!i->second.syn) {
   4.227 +	std::ostringstream cstr;
   4.228 +	cstr << ' ';
   4.229 +	if(!i->second.mandatory) cstr << '[';
   4.230 +	show(cstr,i);
   4.231 +	if(!i->second.mandatory) cstr << ']';
   4.232 +	if(pos+cstr.str().size()>LINE_LEN) {
   4.233 +	  std::cerr << std::endl << indent;
   4.234 +	  pos=indent.size();
   4.235 +	}
   4.236 +	std::cerr << cstr.str();
   4.237 +	pos+=cstr.str().size();
   4.238 +      }
   4.239 +    for(std::vector<OtherArg>::iterator i=_others_help.begin();
   4.240 +	i!=_others_help.end();++i)
   4.241 +      {
   4.242 +	std::ostringstream cstr;
   4.243 +	cstr << ' ' << i->name;
   4.244 +      
   4.245 +	if(pos+cstr.str().size()>LINE_LEN) {
   4.246 +	  std::cerr << std::endl << indent;
   4.247 +	  pos=indent.size();
   4.248 +	}
   4.249 +	std::cerr << cstr.str();
   4.250 +	pos+=cstr.str().size();
   4.251 +      }
   4.252 +    std::cerr << std::endl;
   4.253 +  }
   4.254 +    
   4.255 +  void ArgParser::showHelp()
   4.256 +  {
   4.257 +    shortHelp();
   4.258 +    std::cerr << "Where:\n";
   4.259 +    for(std::vector<OtherArg>::iterator i=_others_help.begin();
   4.260 +	i!=_others_help.end();++i) showHelp(i);
   4.261 +    for(Opts::iterator i=_opts.begin();i!=_opts.end();++i) showHelp(i);
   4.262 +    exit(1);
   4.263 +  }
   4.264 +    
   4.265 +      
   4.266 +  void ArgParser::unknownOpt(std::string arg) 
   4.267 +  {
   4.268 +    std::cerr << "\nUnknown option: " << arg << "\n";
   4.269 +    std::cerr << "\nType '" << _command_name <<
   4.270 +      " --help' to obtain a short summary on the usage.\n\n";
   4.271 +    exit(1);
   4.272 +  }
   4.273 +    
   4.274 +  void ArgParser::requiresValue(std::string arg, OptType t) 
   4.275 +  {
   4.276 +    std::cerr << "Argument '" << arg << "' requires a";
   4.277 +    switch(t) {
   4.278 +    case STRING:
   4.279 +      std::cerr << " string";
   4.280 +      break;
   4.281 +    case INTEGER:
   4.282 +      std::cerr << "n integer";
   4.283 +      break;
   4.284 +    case DOUBLE:
   4.285 +      std::cerr << " floating point";
   4.286 +      break;
   4.287 +    default:
   4.288 +      break;
   4.289 +    }
   4.290 +    std::cerr << " value\n\n";
   4.291 +    showHelp();
   4.292 +  }
   4.293 +    
   4.294 +
   4.295 +  void ArgParser::checkMandatories()
   4.296 +  {
   4.297 +    bool ok=true;
   4.298 +    for(Opts::iterator i=_opts.begin();i!=_opts.end();++i)
   4.299 +      if(i->second.mandatory&&!i->second.set) 
   4.300 +	{
   4.301 +	  if(ok)
   4.302 +	    std::cerr << _command_name 
   4.303 +		      << ": The following mandatory arguments are missing.\n";
   4.304 +	  ok=false;
   4.305 +	  showHelp(i);
   4.306 +	}
   4.307 +    for(Groups::iterator i=_groups.begin();i!=_groups.end();++i)
   4.308 +      if(i->second.mandatory||i->second.only_one)
   4.309 +	{
   4.310 +	  int set=0;
   4.311 +	  for(GroupData::Opts::iterator o=i->second.opts.begin();
   4.312 +	      o!=i->second.opts.end();++o)
   4.313 +	    if(_opts.find(*o)->second.set) ++set;
   4.314 +	  if(i->second.mandatory&&!set) {
   4.315 +	    std::cerr << _command_name 
   4.316 +		      << ": At least one of the following arguments is mandatory.\n";
   4.317 +	    ok=false;
   4.318 +	    for(GroupData::Opts::iterator o=i->second.opts.begin();
   4.319 +		o!=i->second.opts.end();++o)
   4.320 +	      showHelp(_opts.find(*o));
   4.321 +	  }
   4.322 +	  if(i->second.only_one&&set>1) {
   4.323 +	    std::cerr << _command_name 
   4.324 +		      << ": At most one of the following arguments can be given.\n";
   4.325 +	    ok=false;
   4.326 +	    for(GroupData::Opts::iterator o=i->second.opts.begin();
   4.327 +		o!=i->second.opts.end();++o)
   4.328 +	      showHelp(_opts.find(*o));
   4.329 +	  }
   4.330 +	}
   4.331 +    if(!ok) {
   4.332 +      std::cerr << "\nType '" << _command_name <<
   4.333 +	" --help' to obtain a short summary on the usage.\n\n";
   4.334 +      exit(1);
   4.335 +    }
   4.336 +  }
   4.337 +
   4.338 +  ArgParser &ArgParser::parse()
   4.339 +  {
   4.340 +    for(int ar=1; ar<_argc; ++ar) {
   4.341 +      std::string arg(_argv[ar]);
   4.342 +      if (arg[0] != '-' || arg.size() == 1) {
   4.343 +	_file_args.push_back(arg);
   4.344 +      }
   4.345 +      else {
   4.346 +	Opts::iterator i = _opts.find(arg.substr(1));
   4.347 +	if(i==_opts.end()) unknownOpt(arg);
   4.348 +	else {
   4.349 +	  if(i->second.syn) i=_opts.find(i->second.help);
   4.350 +	  ParData &p(i->second);
   4.351 +	  if (p.type==BOOL) *p.bool_p=true;
   4.352 +	  else if (p.type==FUNC) p.func_p.p(p.func_p.data);
   4.353 +	  else if(++ar==_argc) requiresValue(arg, p.type);
   4.354 +	  else {
   4.355 +	    std::string val(_argv[ar]);
   4.356 +	    std::istringstream vals(val);
   4.357 +	    switch(p.type) {
   4.358 +	    case STRING:
   4.359 +	      *p.string_p=val;
   4.360 +	      break;
   4.361 +	    case INTEGER:
   4.362 +	      vals >> *p.int_p;
   4.363 +	      break;
   4.364 +	    case DOUBLE:
   4.365 +	      vals >> *p.double_p;
   4.366 +	      break;
   4.367 +	    default:
   4.368 +	      break;
   4.369 +	    }
   4.370 +	    if(p.type!=STRING&&(!vals||!vals.eof()))
   4.371 +	      requiresValue(arg, p.type);
   4.372 +	  }
   4.373 +	  p.set = true;
   4.374 +	}
   4.375 +      }
   4.376 +    }
   4.377 +    checkMandatories();
   4.378 +
   4.379 +    return *this;
   4.380 +  }  
   4.381 +    
   4.382 +}
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/lemon/arg_parser.h	Sat Mar 03 12:05:05 2007 +0000
     5.3 @@ -0,0 +1,239 @@
     5.4 +/* -*- C++ -*-
     5.5 + * lemon/main_params.h - Part of LEMON, a generic C++ optimization library
     5.6 + *
     5.7 + * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     5.8 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
     5.9 + *
    5.10 + * Permission to use, modify and distribute this software is granted
    5.11 + * provided that this copyright notice appears in all copies. For
    5.12 + * precise terms see the accompanying LICENSE file.
    5.13 + *
    5.14 + * This software is provided "AS IS" with no warranty of any kind,
    5.15 + * express or implied, and with no claim as to its suitability for any
    5.16 + * purpose.
    5.17 + *
    5.18 + */
    5.19 +
    5.20 +#ifndef LEMON_ARG_PARSER
    5.21 +#define LEMON_ARG_PARSER
    5.22 +
    5.23 +#include <vector>
    5.24 +#include <map>
    5.25 +#include <list>
    5.26 +#include <string>
    5.27 +#include <iostream>
    5.28 +#include <sstream>
    5.29 +#include <algorithm>
    5.30 +
    5.31 +///\ingroup misc
    5.32 +///\file
    5.33 +///\brief A tools to parse command line arguments.
    5.34 +///
    5.35 +///\author Alpar Juttner
    5.36 +
    5.37 +namespace lemon {
    5.38 +
    5.39 +  ///Command line arguments parser
    5.40 +
    5.41 +  ///\ingroup misc
    5.42 +  ///Command line arguments parser
    5.43 +  ///
    5.44 +  class ArgParser {
    5.45 +    
    5.46 +    static void _showHelp(void *p);
    5.47 +  protected:
    5.48 +    
    5.49 +    int _argc;
    5.50 +    char **_argv;
    5.51 +    
    5.52 +    enum OptType { UNKNOWN=0, BOOL=1, STRING=2, DOUBLE=3, INTEGER=4, FUNC=5 };
    5.53 +    
    5.54 +    class ParData {
    5.55 +    public:
    5.56 +      union {
    5.57 +	bool *bool_p;
    5.58 +	int *int_p;
    5.59 +	double *double_p;
    5.60 +	std::string *string_p;
    5.61 +	struct {
    5.62 +	  void (*p)(void *);
    5.63 +	  void *data;
    5.64 +	} func_p;
    5.65 +	  
    5.66 +      };
    5.67 +      std::string help;
    5.68 +      bool mandatory;
    5.69 +      OptType type;
    5.70 +      bool set;
    5.71 +      bool ingroup;
    5.72 +      bool has_syn;
    5.73 +      bool syn;
    5.74 +	     
    5.75 +      ParData() : mandatory(false), type(UNKNOWN), set(false), ingroup(false),
    5.76 +		  has_syn(false), syn(false) {}
    5.77 +    };
    5.78 +
    5.79 +    typedef std::map<std::string,ParData> Opts;
    5.80 +    Opts _opts;
    5.81 +
    5.82 +    class GroupData 
    5.83 +    {
    5.84 +    public:
    5.85 +      typedef std::list<std::string> Opts;
    5.86 +      Opts opts;
    5.87 +      bool only_one;
    5.88 +      bool mandatory;
    5.89 +      GroupData() :only_one(false), mandatory(false) {}
    5.90 +    };
    5.91 +      
    5.92 +    typedef std::map<std::string,GroupData> Groups;
    5.93 +    Groups _groups;
    5.94 +
    5.95 +    struct OtherArg
    5.96 +    {
    5.97 +      std::string name;
    5.98 +      std::string help;
    5.99 +      OtherArg(std::string n, std::string h) :name(n), help(h) {}
   5.100 +
   5.101 +    };
   5.102 +      
   5.103 +    std::vector<OtherArg> _others_help;
   5.104 +    std::vector<std::string> _file_args;
   5.105 +    std::string _command_name;
   5.106 +    
   5.107 +  public:
   5.108 +
   5.109 +    ///\e
   5.110 +    ArgParser(int argc, char **argv);
   5.111 +
   5.112 +    ///Add a new integer type option
   5.113 +
   5.114 +    ///\param name The name of the option. The leading '-' must be omitted.
   5.115 +    ///\param help A help string.
   5.116 +    ///\retval value The value of the argument will be written to this variable.
   5.117 +    ///\param obl Indicate if the option is mandatory.
   5.118 +    ArgParser &option(const std::string &name,
   5.119 +		    const std::string &help,
   5.120 +		    int &value, bool obl=false);
   5.121 +
   5.122 +    ///Add a new floating type option
   5.123 +
   5.124 +    ///\param name The name of the option. The leading '-' must be omitted.
   5.125 +    ///\param help A help string.
   5.126 +    ///\retval value The value of the argument will be written to this variable.
   5.127 +    ///\param obl Indicate if the option is mandatory.
   5.128 +    ArgParser &option(const std::string &name,
   5.129 +		      const std::string &help,
   5.130 +		      double &value, bool obl=false);
   5.131 +
   5.132 +    ///Add a new bool type option
   5.133 +
   5.134 +    ///\param name The name of the option. The leading '-' must be omitted.
   5.135 +    ///\param help A help string.
   5.136 +    ///\retval value The value of the argument will be written to this variable.
   5.137 +    ///\param obl Indicate if the option is mandatory.
   5.138 +    ////\note A mandatory bool obtion is of very little use.)
   5.139 +    ArgParser &option(const std::string &name,
   5.140 +		      const std::string &help,
   5.141 +		      bool &value, bool obl=false);
   5.142 +
   5.143 +    ///Add a new string type option
   5.144 +
   5.145 +    ///\param name The name of the option. The leading '-' must be omitted.
   5.146 +    ///\param help A help string.
   5.147 +    ///\retval value The value of the argument will be written to this variable.
   5.148 +    ///\param obl Indicate if the option is mandatory.
   5.149 +    ArgParser &option(const std::string &name,
   5.150 +		      const std::string &help,
   5.151 +		      std::string &value, bool obl=false);
   5.152 +    
   5.153 +    ///Bind a function to an option.
   5.154 +
   5.155 +    ///\param name The name of the option. The leading '-' must be omitted.
   5.156 +    ///\param help A help string.
   5.157 +    ///\retval func The function to be called when the option is given. It
   5.158 +    ///  must be of type "void f(void *)"
   5.159 +    ///\param data Data to be passed to \c func
   5.160 +    ArgParser &option(const std::string &name,
   5.161 +		    const std::string &help,
   5.162 +		    void (*func)(void *),void *data);
   5.163 +
   5.164 +    ///Boundle some options into a group
   5.165 +
   5.166 +    /// You can group some option by calling this function repeatedly for each
   5.167 +    /// option to be grupped with the same groupname.
   5.168 +    ///\param group The group name
   5.169 +    ///\param opt The option name
   5.170 +    ArgParser &optionGroup(const std::string &group,
   5.171 +			   const std::string &opt);
   5.172 +
   5.173 +    ///Make the members of a group exclusive
   5.174 +
   5.175 +    ///If you call this function for a group, than at most one of them can be
   5.176 +    ///given at the same time
   5.177 +    ArgParser &onlyOneGroup(const std::string &group);
   5.178 +  
   5.179 +    ///Create synonym to an option
   5.180 +
   5.181 +    ///With this function you can create a sysnonym called \c sys of the
   5.182 +    ///option \c opt.
   5.183 +    ArgParser &synonym(const std::string &syn,
   5.184 +			   const std::string &opt);
   5.185 +    
   5.186 +    ///Make a group mandatory
   5.187 +
   5.188 +    ///Using this function, at least one of the members of \c group
   5.189 +    ///must be given.
   5.190 +    ArgParser &mandatoryGroup(const std::string &group);
   5.191 +    
   5.192 +    ///Give help string for non-parsed arguments.
   5.193 +
   5.194 +    ///With this function you can give help string for non-parsed arguments.
   5.195 +    ///the parameter \c name will be printed in the short usage line, while
   5.196 +    ///\c help gives a more detailed description.
   5.197 +    ArgParser &other(const std::string &name,
   5.198 +		     const std::string &help="");
   5.199 +    
   5.200 +    ///Non option type arguments.
   5.201 +
   5.202 +    ///Gives back a reference to a vector consisting of the program arguments
   5.203 +    ///not starting with a '-' character.
   5.204 +    std::vector<std::string> &files() { return _file_args; }
   5.205 +
   5.206 +    ///Give back the command name (the 0th argument)
   5.207 +    const std::string &commandName() { return _command_name; }
   5.208 +
   5.209 +    void show(std::ostream &os,Opts::iterator i);
   5.210 +    void show(std::ostream &os,Groups::iterator i);
   5.211 +    void showHelp(Opts::iterator i);
   5.212 +    void showHelp(std::vector<OtherArg>::iterator i);
   5.213 +    void shortHelp();
   5.214 +    void showHelp();
   5.215 +
   5.216 +    void unknownOpt(std::string arg);
   5.217 +
   5.218 +    void requiresValue(std::string arg, OptType t);
   5.219 +    void checkMandatories();
   5.220 +    
   5.221 +    ///\e
   5.222 +    ArgParser &parse();
   5.223 +
   5.224 +    /// Synonym for parse()
   5.225 +    ArgParser &run() 
   5.226 +    {
   5.227 +      return parse();
   5.228 +    }
   5.229 +    
   5.230 +    ///Check if an opion has been given to the command.
   5.231 +    bool given(std::string op) 
   5.232 +    {
   5.233 +      Opts::iterator i = _opts.find(op);
   5.234 +      return i!=_opts.end()?i->second.set:false;
   5.235 +    }
   5.236 +    
   5.237 +  };
   5.238 +}
   5.239 +
   5.240 +    
   5.241 +
   5.242 +#endif // LEMON_MAIN_PARAMS
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/lemon/dist_log.h	Sat Mar 03 12:05:05 2007 +0000
     6.3 @@ -0,0 +1,137 @@
     6.4 +/* -*- C++ -*-
     6.5 + *
     6.6 + * This file is a part of LEMON, a generic C++ optimization library
     6.7 + *
     6.8 + * Copyright (C) 2003-2006
     6.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
    6.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
    6.11 + *
    6.12 + * Permission to use, modify and distribute this software is granted
    6.13 + * provided that this copyright notice appears in all copies. For
    6.14 + * precise terms see the accompanying LICENSE file.
    6.15 + *
    6.16 + * This software is provided "AS IS" with no warranty of any kind,
    6.17 + * express or implied, and with no claim as to its suitability for any
    6.18 + * purpose.
    6.19 + *
    6.20 + */
    6.21 +
    6.22 +#ifndef LEMON_RANDOM_H
    6.23 +#define LEMON_RANDOM_H
    6.24 +
    6.25 +#include<iostream>
    6.26 +#include<fstream>
    6.27 +#include<string>
    6.28 +
    6.29 +#include <lemon/dim2.h>
    6.30 +
    6.31 +///\ingroup misc
    6.32 +///\file
    6.33 +///\brief Measure a Distribution
    6.34 +///
    6.35 +///\todo Needs lot more docs
    6.36 +///
    6.37 +
    6.38 +
    6.39 +namespace lemon {
    6.40 +
    6.41 +  ///Measure a distribution
    6.42 +  class DistLog
    6.43 +  {
    6.44 +    std::vector<int> _dist;
    6.45 +    double _lo,_up;
    6.46 +    int _count;
    6.47 +    bool _cut;
    6.48 +  public:
    6.49 +    ///\e
    6.50 +    Dist(double lo,double up,int gr,bool cut=true) 
    6.51 +      : _dist(gr,0),_lo(lo),_up(up),_count(0),_cut(cut) {}
    6.52 +    ///\e
    6.53 +    void operator()(double v)
    6.54 +    {
    6.55 +      if(_cut) {
    6.56 +	if(_lo<=v && v<_up)
    6.57 +	  _dist[int((v-_lo)/(_up-_lo)*_dist.size())]++;
    6.58 +      }
    6.59 +      else {
    6.60 +	_dist[std::max(0,std::min(int(_dist.size())-1,
    6.61 +				  int((v-_lo)/(_up-_lo)*_dist.size())
    6.62 +				  ))]++;
    6.63 +      }
    6.64 +      _count++;
    6.65 +    }
    6.66 +    ///\e
    6.67 +    void dump(std::ostream& os=std::cout)
    6.68 +    {
    6.69 +      for(int i=0;i<_dist.size();i++)
    6.70 +	os << _lo+(i+0.5)*(_up-_lo)/_dist.size() << ' '
    6.71 +	   << double(_dist[i])/_count << std::endl;
    6.72 +    }
    6.73 +    ///\e
    6.74 +    void dump(const std::string& file_name)
    6.75 +    {
    6.76 +      dump(std::ofstream(file_name.c_str()));
    6.77 +    }
    6.78 +  };
    6.79 +  
    6.80 +  ///Measure a two dimensional distribution
    6.81 +  class DistLog2
    6.82 +  {
    6.83 +  public:
    6.84 +    typedef dim2::Point<double> Point;
    6.85 +  private:
    6.86 +    std::vector<int> _dist;
    6.87 +    int _gr;
    6.88 +    Point _lo,_up;
    6.89 +    int _count;
    6.90 +    bool _cut;
    6.91 +  public:  
    6.92 +    ///\e
    6.93 +    Dist2(Point a,Point b,int gr,bool cut=true) :
    6.94 +      _dist(gr*gr,0),_gr(gr),
    6.95 +      _lo(a),_up(b),_count(0),_cut(cut) {}
    6.96 +    ///\e
    6.97 +    Dist2(double lox,double upx,double loy,double upy,int gr,bool cut=true) :
    6.98 +      _dist(gr*gr,0),_gr(gr),
    6.99 +      _lo(Point(lox,loy)),_up(Point(upx,upy)),_count(0),_cut(cut) {}
   6.100 +    ///\e
   6.101 +    void operator()(Point v)
   6.102 +    {
   6.103 +      if(_cut)
   6.104 +	{
   6.105 +	  if(v.x>=_lo.x && v.x<_up.x && v.y>=_lo.y && v.y<_up.y)
   6.106 +	    _dist[int((v.x-_lo.x)/(_up.x-_lo.x)*_gr)*_gr+
   6.107 +		  int((v.y-_lo.y)/(_up.y-_lo.y)*_gr)]++;
   6.108 +	}
   6.109 +      else {
   6.110 +	_dist[std::max(0,std::min(_gr-1,
   6.111 +				  int((v.x-_lo.x)/(_up.x-_lo.x)*_gr)
   6.112 +				  ))*_gr+
   6.113 +	      std::max(0,std::min(_gr-1,
   6.114 +				  int((v.y-_lo.y)/(_up.y-_lo.y)*_gr)
   6.115 +				  ))
   6.116 +	      ]++;
   6.117 +      }
   6.118 +      _count++;
   6.119 +    }
   6.120 +    ///\e
   6.121 +    void dump(std::ostream& os=std::cout)
   6.122 +    {
   6.123 +      for(int i=0;i<_gr;i++)
   6.124 +	{
   6.125 +	  for(int j=0;j<_gr;j++)
   6.126 +	    os << _lo.x+(i+0.5)*(_up.x-_lo.x)/_gr << ' '
   6.127 +	       << _lo.y+(j+0.5)*(_up.y-_lo.y)/_gr << ' '
   6.128 +	       << double(_dist[i*_gr+j])/_count << std::endl;
   6.129 +	  os << std::endl;
   6.130 +	}
   6.131 +    }
   6.132 +    ///\e
   6.133 +    void dump(const std::string& file_name)
   6.134 +    {
   6.135 +      dump(std::ofstream(file_name.c_str()));
   6.136 +    }
   6.137 +  };
   6.138 +}
   6.139 +
   6.140 +#endif