1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/lemon/arg_parser.cc Mon Mar 17 11:03:35 2008 +0000
1.3 @@ -0,0 +1,465 @@
1.4 +/* -*- C++ -*-
1.5 + *
1.6 + * This file is a part of LEMON, a generic C++ optimization library
1.7 + *
1.8 + * Copyright (C) 2003-2008
1.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
1.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
1.11 + *
1.12 + * Permission to use, modify and distribute this software is granted
1.13 + * provided that this copyright notice appears in all copies. For
1.14 + * precise terms see the accompanying LICENSE file.
1.15 + *
1.16 + * This software is provided "AS IS" with no warranty of any kind,
1.17 + * express or implied, and with no claim as to its suitability for any
1.18 + * purpose.
1.19 + *
1.20 + */
1.21 +
1.22 +#include <lemon/arg_parser.h>
1.23 +
1.24 +namespace lemon {
1.25 +
1.26 + void ArgParser::_showHelp(void *p)
1.27 + {
1.28 + (static_cast<ArgParser*>(p))->showHelp();
1.29 + exit(1);
1.30 + }
1.31 +
1.32 + ArgParser::ArgParser(int argc, const char **argv) :_argc(argc), _argv(argv),
1.33 + _command_name(argv[0]) {
1.34 + funcOption("-help","Print a short help message",_showHelp,this);
1.35 + synonym("help","-help");
1.36 + synonym("h","-help");
1.37 +
1.38 + }
1.39 +
1.40 + ArgParser::~ArgParser()
1.41 + {
1.42 + for(Opts::iterator i=_opts.begin();i!=_opts.end();++i)
1.43 + if(i->second.self_delete)
1.44 + switch(i->second.type) {
1.45 + case BOOL:
1.46 + delete i->second.bool_p;
1.47 + break;
1.48 + case STRING:
1.49 + delete i->second.string_p;
1.50 + break;
1.51 + case DOUBLE:
1.52 + delete i->second.double_p;
1.53 + break;
1.54 + case INTEGER:
1.55 + delete i->second.int_p;
1.56 + break;
1.57 + case UNKNOWN:
1.58 + break;
1.59 + case FUNC:
1.60 + break;
1.61 + }
1.62 + }
1.63 +
1.64 +
1.65 + ArgParser &ArgParser::intOption(const std::string &name,
1.66 + const std::string &help,
1.67 + int value, bool obl)
1.68 + {
1.69 + ParData p;
1.70 + p.int_p=new int(value);
1.71 + p.self_delete=true;
1.72 + p.help=help;
1.73 + p.type=INTEGER;
1.74 + p.mandatory=obl;
1.75 + _opts[name]=p;
1.76 + return *this;
1.77 + }
1.78 +
1.79 + ArgParser &ArgParser::doubleOption(const std::string &name,
1.80 + const std::string &help,
1.81 + double value, bool obl)
1.82 + {
1.83 + ParData p;
1.84 + p.double_p=new double(value);
1.85 + p.self_delete=true;
1.86 + p.help=help;
1.87 + p.type=DOUBLE;
1.88 + p.mandatory=obl;
1.89 + _opts[name]=p;
1.90 + return *this;
1.91 + }
1.92 +
1.93 + ArgParser &ArgParser::boolOption(const std::string &name,
1.94 + const std::string &help,
1.95 + bool value, bool obl)
1.96 + {
1.97 + ParData p;
1.98 + p.bool_p=new bool(value);
1.99 + p.self_delete=true;
1.100 + p.help=help;
1.101 + p.type=BOOL;
1.102 + p.mandatory=obl;
1.103 + _opts[name]=p;
1.104 + return *this;
1.105 + }
1.106 +
1.107 + ArgParser &ArgParser::stringOption(const std::string &name,
1.108 + const std::string &help,
1.109 + std::string value, bool obl)
1.110 + {
1.111 + ParData p;
1.112 + p.string_p=new std::string(value);
1.113 + p.self_delete=true;
1.114 + p.help=help;
1.115 + p.type=STRING;
1.116 + p.mandatory=obl;
1.117 + _opts[name]=p;
1.118 + return *this;
1.119 + }
1.120 +
1.121 + ArgParser &ArgParser::refOption(const std::string &name,
1.122 + const std::string &help,
1.123 + int &ref, bool obl)
1.124 + {
1.125 + ParData p;
1.126 + p.int_p=&ref;
1.127 + p.self_delete=false;
1.128 + p.help=help;
1.129 + p.type=INTEGER;
1.130 + p.mandatory=obl;
1.131 + _opts[name]=p;
1.132 + return *this;
1.133 + }
1.134 +
1.135 + ArgParser &ArgParser::refOption(const std::string &name,
1.136 + const std::string &help,
1.137 + double &ref, bool obl)
1.138 + {
1.139 + ParData p;
1.140 + p.double_p=&ref;
1.141 + p.self_delete=false;
1.142 + p.help=help;
1.143 + p.type=DOUBLE;
1.144 + p.mandatory=obl;
1.145 + _opts[name]=p;
1.146 + return *this;
1.147 + }
1.148 +
1.149 + ArgParser &ArgParser::refOption(const std::string &name,
1.150 + const std::string &help,
1.151 + bool &ref, bool obl)
1.152 + {
1.153 + ParData p;
1.154 + p.bool_p=&ref;
1.155 + p.self_delete=false;
1.156 + p.help=help;
1.157 + p.type=BOOL;
1.158 + p.mandatory=obl;
1.159 + _opts[name]=p;
1.160 +
1.161 + ref = false;
1.162 +
1.163 + return *this;
1.164 + }
1.165 +
1.166 + ArgParser &ArgParser::refOption(const std::string &name,
1.167 + const std::string &help,
1.168 + std::string &ref, bool obl)
1.169 + {
1.170 + ParData p;
1.171 + p.string_p=&ref;
1.172 + p.self_delete=false;
1.173 + p.help=help;
1.174 + p.type=STRING;
1.175 + p.mandatory=obl;
1.176 + _opts[name]=p;
1.177 + return *this;
1.178 + }
1.179 +
1.180 + ArgParser &ArgParser::funcOption(const std::string &name,
1.181 + const std::string &help,
1.182 + void (*func)(void *),void *data)
1.183 + {
1.184 + ParData p;
1.185 + p.func_p.p=func;
1.186 + p.func_p.data=data;
1.187 + p.self_delete=false;
1.188 + p.help=help;
1.189 + p.type=FUNC;
1.190 + p.mandatory=false;
1.191 + _opts[name]=p;
1.192 + return *this;
1.193 + }
1.194 +
1.195 + ArgParser &ArgParser::optionGroup(const std::string &group,
1.196 + const std::string &opt)
1.197 + {
1.198 + Opts::iterator i = _opts.find(opt);
1.199 + LEMON_ASSERT(i!=_opts.end(), "Unknown option: '"+opt+"'");
1.200 + LEMON_ASSERT(!(i->second.ingroup),
1.201 + "Option already in option group: '"+opt+"'");
1.202 + GroupData &g=_groups[group];
1.203 + g.opts.push_back(opt);
1.204 + i->second.ingroup=true;
1.205 + return *this;
1.206 + }
1.207 +
1.208 + ArgParser &ArgParser::onlyOneGroup(const std::string &group)
1.209 + {
1.210 + GroupData &g=_groups[group];
1.211 + g.only_one=true;
1.212 + return *this;
1.213 + }
1.214 +
1.215 + ArgParser &ArgParser::synonym(const std::string &syn,
1.216 + const std::string &opt)
1.217 + {
1.218 + Opts::iterator o = _opts.find(opt);
1.219 + Opts::iterator s = _opts.find(syn);
1.220 + LEMON_ASSERT(o!=_opts.end(), "Unknown option: '"+opt+"'");
1.221 + LEMON_ASSERT(s==_opts.end(), "Option already used: '"+syn+"'");
1.222 + ParData p;
1.223 + p.help=opt;
1.224 + p.mandatory=false;
1.225 + p.syn=true;
1.226 + _opts[syn]=p;
1.227 + o->second.has_syn=true;
1.228 + return *this;
1.229 + }
1.230 +
1.231 + ArgParser &ArgParser::mandatoryGroup(const std::string &group)
1.232 + {
1.233 + GroupData &g=_groups[group];
1.234 + g.mandatory=true;
1.235 + return *this;
1.236 + }
1.237 +
1.238 + ArgParser &ArgParser::other(const std::string &name,
1.239 + const std::string &help)
1.240 + {
1.241 + _others_help.push_back(OtherArg(name,help));
1.242 + return *this;
1.243 + }
1.244 +
1.245 + void ArgParser::show(std::ostream &os,Opts::iterator i)
1.246 + {
1.247 + os << "-" << i->first;
1.248 + if(i->second.has_syn)
1.249 + for(Opts::iterator j=_opts.begin();j!=_opts.end();++j)
1.250 + if(j->second.syn&&j->second.help==i->first)
1.251 + os << "|-" << j->first;
1.252 + switch(i->second.type) {
1.253 + case STRING:
1.254 + os << " str";
1.255 + break;
1.256 + case INTEGER:
1.257 + os << " int";
1.258 + break;
1.259 + case DOUBLE:
1.260 + os << " num";
1.261 + break;
1.262 + default:
1.263 + break;
1.264 + }
1.265 + }
1.266 +
1.267 + void ArgParser::show(std::ostream &os,Groups::iterator i)
1.268 + {
1.269 + GroupData::Opts::iterator o=i->second.opts.begin();
1.270 + while(o!=i->second.opts.end()) {
1.271 + show(os,_opts.find(*o));
1.272 + ++o;
1.273 + if(o!=i->second.opts.end()) os<<'|';
1.274 + }
1.275 + }
1.276 +
1.277 + void ArgParser::showHelp(Opts::iterator i)
1.278 + {
1.279 + if(i->second.help.size()==0||i->second.syn) return;
1.280 + std::cerr << " ";
1.281 + show(std::cerr,i);
1.282 + std::cerr << std::endl;
1.283 + std::cerr << " " << i->second.help << std::endl;
1.284 + }
1.285 + void ArgParser::showHelp(std::vector<ArgParser::OtherArg>::iterator i)
1.286 + {
1.287 + if(i->help.size()==0) return;
1.288 + std::cerr << " " << i->name << std::endl
1.289 + << " " << i->help << std::endl;
1.290 + }
1.291 +
1.292 + void ArgParser::shortHelp()
1.293 + {
1.294 + const unsigned int LINE_LEN=77;
1.295 + const std::string indent(" ");
1.296 + std::cerr << "Usage:\n " << _command_name;
1.297 + int pos=_command_name.size()+2;
1.298 + for(Groups::iterator g=_groups.begin();g!=_groups.end();++g) {
1.299 + std::ostringstream cstr;
1.300 + cstr << ' ';
1.301 + if(!g->second.mandatory) cstr << '[';
1.302 + show(cstr,g);
1.303 + if(!g->second.mandatory) cstr << ']';
1.304 + if(pos+cstr.str().size()>LINE_LEN) {
1.305 + std::cerr << std::endl << indent;
1.306 + pos=indent.size();
1.307 + }
1.308 + std::cerr << cstr.str();
1.309 + pos+=cstr.str().size();
1.310 + }
1.311 + for(Opts::iterator i=_opts.begin();i!=_opts.end();++i)
1.312 + if(!i->second.ingroup&&!i->second.syn) {
1.313 + std::ostringstream cstr;
1.314 + cstr << ' ';
1.315 + if(!i->second.mandatory) cstr << '[';
1.316 + show(cstr,i);
1.317 + if(!i->second.mandatory) cstr << ']';
1.318 + if(pos+cstr.str().size()>LINE_LEN) {
1.319 + std::cerr << std::endl << indent;
1.320 + pos=indent.size();
1.321 + }
1.322 + std::cerr << cstr.str();
1.323 + pos+=cstr.str().size();
1.324 + }
1.325 + for(std::vector<OtherArg>::iterator i=_others_help.begin();
1.326 + i!=_others_help.end();++i)
1.327 + {
1.328 + std::ostringstream cstr;
1.329 + cstr << ' ' << i->name;
1.330 +
1.331 + if(pos+cstr.str().size()>LINE_LEN) {
1.332 + std::cerr << std::endl << indent;
1.333 + pos=indent.size();
1.334 + }
1.335 + std::cerr << cstr.str();
1.336 + pos+=cstr.str().size();
1.337 + }
1.338 + std::cerr << std::endl;
1.339 + }
1.340 +
1.341 + void ArgParser::showHelp()
1.342 + {
1.343 + shortHelp();
1.344 + std::cerr << "Where:\n";
1.345 + for(std::vector<OtherArg>::iterator i=_others_help.begin();
1.346 + i!=_others_help.end();++i) showHelp(i);
1.347 + for(Opts::iterator i=_opts.begin();i!=_opts.end();++i) showHelp(i);
1.348 + exit(1);
1.349 + }
1.350 +
1.351 +
1.352 + void ArgParser::unknownOpt(std::string arg)
1.353 + {
1.354 + std::cerr << "\nUnknown option: " << arg << "\n";
1.355 + std::cerr << "\nType '" << _command_name <<
1.356 + " --help' to obtain a short summary on the usage.\n\n";
1.357 + exit(1);
1.358 + }
1.359 +
1.360 + void ArgParser::requiresValue(std::string arg, OptType t)
1.361 + {
1.362 + std::cerr << "Argument '" << arg << "' requires a";
1.363 + switch(t) {
1.364 + case STRING:
1.365 + std::cerr << " string";
1.366 + break;
1.367 + case INTEGER:
1.368 + std::cerr << "n integer";
1.369 + break;
1.370 + case DOUBLE:
1.371 + std::cerr << " floating point";
1.372 + break;
1.373 + default:
1.374 + break;
1.375 + }
1.376 + std::cerr << " value\n\n";
1.377 + showHelp();
1.378 + }
1.379 +
1.380 +
1.381 + void ArgParser::checkMandatories()
1.382 + {
1.383 + bool ok=true;
1.384 + for(Opts::iterator i=_opts.begin();i!=_opts.end();++i)
1.385 + if(i->second.mandatory&&!i->second.set)
1.386 + {
1.387 + if(ok)
1.388 + std::cerr << _command_name
1.389 + << ": The following mandatory arguments are missing.\n";
1.390 + ok=false;
1.391 + showHelp(i);
1.392 + }
1.393 + for(Groups::iterator i=_groups.begin();i!=_groups.end();++i)
1.394 + if(i->second.mandatory||i->second.only_one)
1.395 + {
1.396 + int set=0;
1.397 + for(GroupData::Opts::iterator o=i->second.opts.begin();
1.398 + o!=i->second.opts.end();++o)
1.399 + if(_opts.find(*o)->second.set) ++set;
1.400 + if(i->second.mandatory&&!set) {
1.401 + std::cerr << _command_name
1.402 + << ": At least one of the following arguments is mandatory.\n";
1.403 + ok=false;
1.404 + for(GroupData::Opts::iterator o=i->second.opts.begin();
1.405 + o!=i->second.opts.end();++o)
1.406 + showHelp(_opts.find(*o));
1.407 + }
1.408 + if(i->second.only_one&&set>1) {
1.409 + std::cerr << _command_name
1.410 + << ": At most one of the following arguments can be given.\n";
1.411 + ok=false;
1.412 + for(GroupData::Opts::iterator o=i->second.opts.begin();
1.413 + o!=i->second.opts.end();++o)
1.414 + showHelp(_opts.find(*o));
1.415 + }
1.416 + }
1.417 + if(!ok) {
1.418 + std::cerr << "\nType '" << _command_name <<
1.419 + " --help' to obtain a short summary on the usage.\n\n";
1.420 + exit(1);
1.421 + }
1.422 + }
1.423 +
1.424 + ArgParser &ArgParser::parse()
1.425 + {
1.426 + for(int ar=1; ar<_argc; ++ar) {
1.427 + std::string arg(_argv[ar]);
1.428 + if (arg[0] != '-' || arg.size() == 1) {
1.429 + _file_args.push_back(arg);
1.430 + }
1.431 + else {
1.432 + Opts::iterator i = _opts.find(arg.substr(1));
1.433 + if(i==_opts.end()) unknownOpt(arg);
1.434 + else {
1.435 + if(i->second.syn) i=_opts.find(i->second.help);
1.436 + ParData &p(i->second);
1.437 + if (p.type==BOOL) *p.bool_p=true;
1.438 + else if (p.type==FUNC) p.func_p.p(p.func_p.data);
1.439 + else if(++ar==_argc) requiresValue(arg, p.type);
1.440 + else {
1.441 + std::string val(_argv[ar]);
1.442 + std::istringstream vals(val);
1.443 + switch(p.type) {
1.444 + case STRING:
1.445 + *p.string_p=val;
1.446 + break;
1.447 + case INTEGER:
1.448 + vals >> *p.int_p;
1.449 + break;
1.450 + case DOUBLE:
1.451 + vals >> *p.double_p;
1.452 + break;
1.453 + default:
1.454 + break;
1.455 + }
1.456 + if(p.type!=STRING&&(!vals||!vals.eof()))
1.457 + requiresValue(arg, p.type);
1.458 + }
1.459 + p.set = true;
1.460 + }
1.461 + }
1.462 + }
1.463 + checkMandatories();
1.464 +
1.465 + return *this;
1.466 + }
1.467 +
1.468 +}