3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2008
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, 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");
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,
136 ArgParser &ArgParser::refOption(const std::string &name,
137 const std::string &help,
138 double &ref, bool obl)
150 ArgParser &ArgParser::refOption(const std::string &name,
151 const std::string &help,
167 ArgParser &ArgParser::refOption(const std::string &name,
168 const std::string &help,
169 std::string &ref, bool obl)
181 ArgParser &ArgParser::funcOption(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 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;
209 ArgParser &ArgParser::onlyOneGroup(const std::string &group)
211 GroupData &g=_groups[group];
216 ArgParser &ArgParser::synonym(const std::string &syn,
217 const std::string &opt)
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+"'");
228 o->second.has_syn=true;
232 ArgParser &ArgParser::mandatoryGroup(const std::string &group)
234 GroupData &g=_groups[group];
239 ArgParser &ArgParser::other(const std::string &name,
240 const std::string &help)
242 _others_help.push_back(OtherArg(name,help));
246 void ArgParser::show(std::ostream &os,Opts::iterator i)
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) {
268 void ArgParser::show(std::ostream &os,Groups::iterator i)
270 GroupData::Opts::iterator o=i->second.opts.begin();
271 while(o!=i->second.opts.end()) {
272 show(os,_opts.find(*o));
274 if(o!=i->second.opts.end()) os<<'|';
278 void ArgParser::showHelp(Opts::iterator i)
280 if(i->second.help.size()==0||i->second.syn) return;
283 std::cerr << std::endl;
284 std::cerr << " " << i->second.help << std::endl;
286 void ArgParser::showHelp(std::vector<ArgParser::OtherArg>::iterator i)
288 if(i->help.size()==0) return;
289 std::cerr << " " << i->name << std::endl
290 << " " << i->help << std::endl;
293 void ArgParser::shortHelp()
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;
302 if(!g->second.mandatory) cstr << '[';
304 if(!g->second.mandatory) cstr << ']';
305 if(pos+cstr.str().size()>LINE_LEN) {
306 std::cerr << std::endl << indent;
309 std::cerr << cstr.str();
310 pos+=cstr.str().size();
312 for(Opts::iterator i=_opts.begin();i!=_opts.end();++i)
313 if(!i->second.ingroup&&!i->second.syn) {
314 std::ostringstream cstr;
316 if(!i->second.mandatory) cstr << '[';
318 if(!i->second.mandatory) cstr << ']';
319 if(pos+cstr.str().size()>LINE_LEN) {
320 std::cerr << std::endl << indent;
323 std::cerr << cstr.str();
324 pos+=cstr.str().size();
326 for(std::vector<OtherArg>::iterator i=_others_help.begin();
327 i!=_others_help.end();++i)
329 std::ostringstream cstr;
330 cstr << ' ' << i->name;
332 if(pos+cstr.str().size()>LINE_LEN) {
333 std::cerr << std::endl << indent;
336 std::cerr << cstr.str();
337 pos+=cstr.str().size();
339 std::cerr << std::endl;
342 void ArgParser::showHelp()
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);
353 void ArgParser::unknownOpt(std::string arg)
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";
361 void ArgParser::requiresValue(std::string arg, OptType t)
363 std::cerr << "Argument '" << arg << "' requires a";
366 std::cerr << " string";
369 std::cerr << "n integer";
372 std::cerr << " floating point";
377 std::cerr << " value\n\n";
382 void ArgParser::checkMandatories()
385 for(Opts::iterator i=_opts.begin();i!=_opts.end();++i)
386 if(i->second.mandatory&&!i->second.set)
389 std::cerr << _command_name
390 << ": The following mandatory arguments are missing.\n";
394 for(Groups::iterator i=_groups.begin();i!=_groups.end();++i)
395 if(i->second.mandatory||i->second.only_one)
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";
405 for(GroupData::Opts::iterator o=i->second.opts.begin();
406 o!=i->second.opts.end();++o)
407 showHelp(_opts.find(*o));
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";
413 for(GroupData::Opts::iterator o=i->second.opts.begin();
414 o!=i->second.opts.end();++o)
415 showHelp(_opts.find(*o));
419 std::cerr << "\nType '" << _command_name <<
420 " --help' to obtain a short summary on the usage.\n\n";
425 ArgParser &ArgParser::parse()
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);
433 Opts::iterator i = _opts.find(arg.substr(1));
434 if(i==_opts.end()) unknownOpt(arg);
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);
442 std::string val(_argv[ar]);
443 std::istringstream vals(val);
457 if(p.type!=STRING&&(!vals||!vals.eof()))
458 requiresValue(arg, p.type);