1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2010
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::_terminate(ArgParserException::Reason reason) const
27 else throw(ArgParserException(reason));
31 void ArgParser::_showHelp(void *p)
33 (static_cast<ArgParser*>(p))->showHelp();
34 (static_cast<ArgParser*>(p))->_terminate(ArgParserException::HELP);
37 ArgParser::ArgParser(int argc, const char * const *argv)
38 :_argc(argc), _argv(argv), _command_name(argv[0]),
39 _exit_on_problems(true) {
40 funcOption("-help","Print a short help message",_showHelp,this);
41 synonym("help","-help");
45 ArgParser::~ArgParser()
47 for(Opts::iterator i=_opts.begin();i!=_opts.end();++i)
48 if(i->second.self_delete)
49 switch(i->second.type) {
51 delete i->second.bool_p;
54 delete i->second.string_p;
57 delete i->second.double_p;
60 delete i->second.int_p;
70 ArgParser &ArgParser::intOption(const std::string &name,
71 const std::string &help,
75 p.int_p=new int(value);
84 ArgParser &ArgParser::doubleOption(const std::string &name,
85 const std::string &help,
86 double value, bool obl)
89 p.double_p=new double(value);
98 ArgParser &ArgParser::boolOption(const std::string &name,
99 const std::string &help,
100 bool value, bool obl)
103 p.bool_p=new bool(value);
112 ArgParser &ArgParser::stringOption(const std::string &name,
113 const std::string &help,
114 std::string value, bool obl)
117 p.string_p=new std::string(value);
126 ArgParser &ArgParser::refOption(const std::string &name,
127 const std::string &help,
140 ArgParser &ArgParser::refOption(const std::string &name,
141 const std::string &help,
142 double &ref, bool obl)
154 ArgParser &ArgParser::refOption(const std::string &name,
155 const std::string &help,
171 ArgParser &ArgParser::refOption(const std::string &name,
172 const std::string &help,
173 std::string &ref, bool obl)
185 ArgParser &ArgParser::funcOption(const std::string &name,
186 const std::string &help,
187 void (*func)(void *),void *data)
200 ArgParser &ArgParser::optionGroup(const std::string &group,
201 const std::string &opt)
203 Opts::iterator i = _opts.find(opt);
204 LEMON_ASSERT(i!=_opts.end(), "Unknown option: '"+opt+"'");
205 LEMON_ASSERT(!(i->second.ingroup),
206 "Option already in option group: '"+opt+"'");
207 GroupData &g=_groups[group];
208 g.opts.push_back(opt);
209 i->second.ingroup=true;
213 ArgParser &ArgParser::onlyOneGroup(const std::string &group)
215 GroupData &g=_groups[group];
220 ArgParser &ArgParser::synonym(const std::string &syn,
221 const std::string &opt)
223 Opts::iterator o = _opts.find(opt);
224 Opts::iterator s = _opts.find(syn);
225 LEMON_ASSERT(o!=_opts.end(), "Unknown option: '"+opt+"'");
226 LEMON_ASSERT(s==_opts.end(), "Option already used: '"+syn+"'");
232 o->second.has_syn=true;
236 ArgParser &ArgParser::mandatoryGroup(const std::string &group)
238 GroupData &g=_groups[group];
243 ArgParser &ArgParser::other(const std::string &name,
244 const std::string &help)
246 _others_help.push_back(OtherArg(name,help));
250 void ArgParser::show(std::ostream &os,Opts::const_iterator i) const
252 os << "-" << i->first;
253 if(i->second.has_syn)
254 for(Opts::const_iterator j=_opts.begin();j!=_opts.end();++j)
255 if(j->second.syn&&j->second.help==i->first)
256 os << "|-" << j->first;
257 switch(i->second.type) {
272 void ArgParser::show(std::ostream &os,Groups::const_iterator i) const
274 GroupData::Opts::const_iterator o=i->second.opts.begin();
275 while(o!=i->second.opts.end()) {
276 show(os,_opts.find(*o));
278 if(o!=i->second.opts.end()) os<<'|';
282 void ArgParser::showHelp(Opts::const_iterator i) const
284 if(i->second.help.size()==0||i->second.syn) return;
287 std::cerr << std::endl;
288 std::cerr << " " << i->second.help << std::endl;
290 void ArgParser::showHelp(std::vector<ArgParser::OtherArg>::const_iterator i)
293 if(i->help.size()==0) return;
294 std::cerr << " " << i->name << std::endl
295 << " " << i->help << std::endl;
298 void ArgParser::shortHelp() const
300 const unsigned int LINE_LEN=77;
301 const std::string indent(" ");
302 std::cerr << "Usage:\n " << _command_name;
303 int pos=_command_name.size()+2;
304 for(Groups::const_iterator g=_groups.begin();g!=_groups.end();++g) {
305 std::ostringstream cstr;
307 if(!g->second.mandatory) cstr << '[';
309 if(!g->second.mandatory) cstr << ']';
310 if(pos+cstr.str().size()>LINE_LEN) {
311 std::cerr << std::endl << indent;
314 std::cerr << cstr.str();
315 pos+=cstr.str().size();
317 for(Opts::const_iterator i=_opts.begin();i!=_opts.end();++i)
318 if(!i->second.ingroup&&!i->second.syn) {
319 std::ostringstream cstr;
321 if(!i->second.mandatory) cstr << '[';
323 if(!i->second.mandatory) cstr << ']';
324 if(pos+cstr.str().size()>LINE_LEN) {
325 std::cerr << std::endl << indent;
328 std::cerr << cstr.str();
329 pos+=cstr.str().size();
331 for(std::vector<OtherArg>::const_iterator i=_others_help.begin();
332 i!=_others_help.end();++i)
334 std::ostringstream cstr;
335 cstr << ' ' << i->name;
337 if(pos+cstr.str().size()>LINE_LEN) {
338 std::cerr << std::endl << indent;
341 std::cerr << cstr.str();
342 pos+=cstr.str().size();
344 std::cerr << std::endl;
347 void ArgParser::showHelp() const
350 std::cerr << "Where:\n";
351 for(std::vector<OtherArg>::const_iterator i=_others_help.begin();
352 i!=_others_help.end();++i) showHelp(i);
353 for(Opts::const_iterator i=_opts.begin();i!=_opts.end();++i) showHelp(i);
354 _terminate(ArgParserException::HELP);
358 void ArgParser::unknownOpt(std::string arg) const
360 std::cerr << "\nUnknown option: " << arg << "\n";
361 std::cerr << "\nType '" << _command_name <<
362 " --help' to obtain a short summary on the usage.\n\n";
363 _terminate(ArgParserException::UNKNOWN_OPT);
366 void ArgParser::requiresValue(std::string arg, OptType t) const
368 std::cerr << "Argument '" << arg << "' requires a";
371 std::cerr << " string";
374 std::cerr << "n integer";
377 std::cerr << " floating point";
382 std::cerr << " value\n\n";
387 void ArgParser::checkMandatories() const
390 for(Opts::const_iterator i=_opts.begin();i!=_opts.end();++i)
391 if(i->second.mandatory&&!i->second.set)
394 std::cerr << _command_name
395 << ": The following mandatory arguments are missing.\n";
399 for(Groups::const_iterator i=_groups.begin();i!=_groups.end();++i)
400 if(i->second.mandatory||i->second.only_one)
403 for(GroupData::Opts::const_iterator o=i->second.opts.begin();
404 o!=i->second.opts.end();++o)
405 if(_opts.find(*o)->second.set) ++set;
406 if(i->second.mandatory&&!set) {
407 std::cerr << _command_name <<
408 ": At least one of the following arguments is mandatory.\n";
410 for(GroupData::Opts::const_iterator o=i->second.opts.begin();
411 o!=i->second.opts.end();++o)
412 showHelp(_opts.find(*o));
414 if(i->second.only_one&&set>1) {
415 std::cerr << _command_name <<
416 ": At most one of the following arguments can be given.\n";
418 for(GroupData::Opts::const_iterator o=i->second.opts.begin();
419 o!=i->second.opts.end();++o)
420 showHelp(_opts.find(*o));
424 std::cerr << "\nType '" << _command_name <<
425 " --help' to obtain a short summary on the usage.\n\n";
426 _terminate(ArgParserException::INVALID_OPT);
430 ArgParser &ArgParser::parse()
432 for(int ar=1; ar<_argc; ++ar) {
433 std::string arg(_argv[ar]);
434 if (arg[0] != '-' || arg.size() == 1) {
435 _file_args.push_back(arg);
438 Opts::iterator i = _opts.find(arg.substr(1));
439 if(i==_opts.end()) unknownOpt(arg);
441 if(i->second.syn) i=_opts.find(i->second.help);
442 ParData &p(i->second);
443 if (p.type==BOOL) *p.bool_p=true;
444 else if (p.type==FUNC) p.func_p.p(p.func_p.data);
445 else if(++ar==_argc) requiresValue(arg, p.type);
447 std::string val(_argv[ar]);
448 std::istringstream vals(val);
462 if(p.type!=STRING&&(!vals||!vals.eof()))
463 requiresValue(arg, p.type);