3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2007
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
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.
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
19 #include <lemon/arg_parser.h>
23 void ArgParser::_showHelp(void *p)
25 (static_cast<ArgParser*>(p))->showHelp();
29 ArgParser::ArgParser(int argc, char **argv) :_argc(argc), _argv(argv),
30 _command_name(argv[0]) {
31 refOption("-help","Print a short help message",_showHelp,this);
32 synonym("help","-help");
37 ArgParser::~ArgParser()
39 for(Opts::iterator i=_opts.begin();i!=_opts.end();++i)
40 if(i->second.self_delete)
41 switch(i->second.type) {
43 delete i->second.bool_p;
46 delete i->second.string_p;
49 delete i->second.double_p;
52 delete i->second.int_p;
62 ArgParser &ArgParser::intOption(const std::string &name,
63 const std::string &help,
67 p.int_p=new int(value);
77 ArgParser &ArgParser::doubleOption(const std::string &name,
78 const std::string &help,
79 double value, bool obl)
82 p.double_p=new double(value);
91 ArgParser &ArgParser::boolOption(const std::string &name,
92 const std::string &help,
96 p.bool_p=new bool(value);
108 ArgParser &ArgParser::stringOption(const std::string &name,
109 const std::string &help,
110 std::string value, bool obl)
113 p.string_p=new std::string(value);
122 ArgParser &ArgParser::refOption(const std::string &name,
123 const std::string &help,
124 int &value, bool obl)
136 ArgParser &ArgParser::refOption(const std::string &name,
137 const std::string &help,
138 double &value, bool obl)
150 ArgParser &ArgParser::refOption(const std::string &name,
151 const std::string &help,
152 bool &value, bool obl)
167 ArgParser &ArgParser::refOption(const std::string &name,
168 const std::string &help,
169 std::string &value, bool obl)
181 ArgParser &ArgParser::refOption(const std::string &name,
182 const std::string &help,
183 void (*func)(void *),void *data)
196 ArgParser &ArgParser::optionGroup(const std::string &group,
197 const std::string &opt)
199 Opts::iterator i = _opts.find(opt);
200 if(i==_opts.end()) exit(3); ///\todo throw exception instead
201 else if(i->second.ingroup) exit(3); ///\todo throw exception instead
203 GroupData &g=_groups[group];
204 g.opts.push_back(opt);
205 i->second.ingroup=true;
210 ArgParser &ArgParser::onlyOneGroup(const std::string &group)
212 GroupData &g=_groups[group];
217 ArgParser &ArgParser::synonym(const std::string &syn,
218 const std::string &opt)
220 Opts::iterator o = _opts.find(opt);
221 Opts::iterator s = _opts.find(syn);
222 if(o==_opts.end()||s!=_opts.end())
223 exit(3); ///\todo throw exception instead
230 o->second.has_syn=true;
235 ArgParser &ArgParser::mandatoryGroup(const std::string &group)
237 GroupData &g=_groups[group];
242 ArgParser &ArgParser::other(const std::string &name,
243 const std::string &help)
245 _others_help.push_back(OtherArg(name,help));
249 void ArgParser::show(std::ostream &os,Opts::iterator i)
251 os << "-" << i->first;
252 if(i->second.has_syn)
253 for(Opts::iterator j=_opts.begin();j!=_opts.end();++j)
254 if(j->second.syn&&j->second.help==i->first)
255 os << "|-" << j->first;
256 switch(i->second.type) {
271 void ArgParser::show(std::ostream &os,Groups::iterator i)
273 GroupData::Opts::iterator o=i->second.opts.begin();
274 while(o!=i->second.opts.end()) {
275 show(os,_opts.find(*o));
277 if(o!=i->second.opts.end()) os<<'|';
281 void ArgParser::showHelp(Opts::iterator i)
283 if(i->second.help.size()==0||i->second.syn) return;
286 std::cerr << std::endl;
287 std::cerr << " " << i->second.help << std::endl;
289 void ArgParser::showHelp(std::vector<ArgParser::OtherArg>::iterator i)
291 if(i->help.size()==0) return;
292 std::cerr << " " << i->name << std::endl
293 << " " << i->help << std::endl;
296 void ArgParser::shortHelp()
298 const unsigned int LINE_LEN=77;
299 const std::string indent(" ");
300 std::cerr << "Usage:\n " << _command_name;
301 int pos=_command_name.size()+2;
302 for(Groups::iterator g=_groups.begin();g!=_groups.end();++g) {
303 std::ostringstream cstr;
305 if(!g->second.mandatory) cstr << '[';
307 if(!g->second.mandatory) cstr << ']';
308 if(pos+cstr.str().size()>LINE_LEN) {
309 std::cerr << std::endl << indent;
312 std::cerr << cstr.str();
313 pos+=cstr.str().size();
315 for(Opts::iterator i=_opts.begin();i!=_opts.end();++i)
316 if(!i->second.ingroup&&!i->second.syn) {
317 std::ostringstream cstr;
319 if(!i->second.mandatory) cstr << '[';
321 if(!i->second.mandatory) cstr << ']';
322 if(pos+cstr.str().size()>LINE_LEN) {
323 std::cerr << std::endl << indent;
326 std::cerr << cstr.str();
327 pos+=cstr.str().size();
329 for(std::vector<OtherArg>::iterator i=_others_help.begin();
330 i!=_others_help.end();++i)
332 std::ostringstream cstr;
333 cstr << ' ' << i->name;
335 if(pos+cstr.str().size()>LINE_LEN) {
336 std::cerr << std::endl << indent;
339 std::cerr << cstr.str();
340 pos+=cstr.str().size();
342 std::cerr << std::endl;
345 void ArgParser::showHelp()
348 std::cerr << "Where:\n";
349 for(std::vector<OtherArg>::iterator i=_others_help.begin();
350 i!=_others_help.end();++i) showHelp(i);
351 for(Opts::iterator i=_opts.begin();i!=_opts.end();++i) showHelp(i);
356 void ArgParser::unknownOpt(std::string arg)
358 std::cerr << "\nUnknown option: " << arg << "\n";
359 std::cerr << "\nType '" << _command_name <<
360 " --help' to obtain a short summary on the usage.\n\n";
364 void ArgParser::requiresValue(std::string arg, OptType t)
366 std::cerr << "Argument '" << arg << "' requires a";
369 std::cerr << " string";
372 std::cerr << "n integer";
375 std::cerr << " floating point";
380 std::cerr << " value\n\n";
385 void ArgParser::checkMandatories()
388 for(Opts::iterator i=_opts.begin();i!=_opts.end();++i)
389 if(i->second.mandatory&&!i->second.set)
392 std::cerr << _command_name
393 << ": The following mandatory arguments are missing.\n";
397 for(Groups::iterator i=_groups.begin();i!=_groups.end();++i)
398 if(i->second.mandatory||i->second.only_one)
401 for(GroupData::Opts::iterator o=i->second.opts.begin();
402 o!=i->second.opts.end();++o)
403 if(_opts.find(*o)->second.set) ++set;
404 if(i->second.mandatory&&!set) {
405 std::cerr << _command_name
406 << ": At least one of the following arguments is mandatory.\n";
408 for(GroupData::Opts::iterator o=i->second.opts.begin();
409 o!=i->second.opts.end();++o)
410 showHelp(_opts.find(*o));
412 if(i->second.only_one&&set>1) {
413 std::cerr << _command_name
414 << ": At most one of the following arguments can be given.\n";
416 for(GroupData::Opts::iterator o=i->second.opts.begin();
417 o!=i->second.opts.end();++o)
418 showHelp(_opts.find(*o));
422 std::cerr << "\nType '" << _command_name <<
423 " --help' to obtain a short summary on the usage.\n\n";
428 ArgParser &ArgParser::parse()
430 for(int ar=1; ar<_argc; ++ar) {
431 std::string arg(_argv[ar]);
432 if (arg[0] != '-' || arg.size() == 1) {
433 _file_args.push_back(arg);
436 Opts::iterator i = _opts.find(arg.substr(1));
437 if(i==_opts.end()) unknownOpt(arg);
439 if(i->second.syn) i=_opts.find(i->second.help);
440 ParData &p(i->second);
441 if (p.type==BOOL) *p.bool_p=true;
442 else if (p.type==FUNC) p.func_p.p(p.func_p.data);
443 else if(++ar==_argc) requiresValue(arg, p.type);
445 std::string val(_argv[ar]);
446 std::istringstream vals(val);
460 if(p.type!=STRING&&(!vals||!vals.eof()))
461 requiresValue(arg, p.type);