CMake improvements.
- documentation generation with Doxygen
- installation support
1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
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);
76 ArgParser &ArgParser::doubleOption(const std::string &name,
77 const std::string &help,
78 double value, bool obl)
81 p.double_p=new double(value);
90 ArgParser &ArgParser::boolOption(const std::string &name,
91 const std::string &help,
95 p.bool_p=new bool(value);
104 ArgParser &ArgParser::stringOption(const std::string &name,
105 const std::string &help,
106 std::string value, bool obl)
109 p.string_p=new std::string(value);
118 ArgParser &ArgParser::refOption(const std::string &name,
119 const std::string &help,
132 ArgParser &ArgParser::refOption(const std::string &name,
133 const std::string &help,
134 double &ref, bool obl)
146 ArgParser &ArgParser::refOption(const std::string &name,
147 const std::string &help,
163 ArgParser &ArgParser::refOption(const std::string &name,
164 const std::string &help,
165 std::string &ref, bool obl)
177 ArgParser &ArgParser::funcOption(const std::string &name,
178 const std::string &help,
179 void (*func)(void *),void *data)
192 ArgParser &ArgParser::optionGroup(const std::string &group,
193 const std::string &opt)
195 Opts::iterator i = _opts.find(opt);
196 LEMON_ASSERT(i!=_opts.end(), "Unknown option: '"+opt+"'");
197 LEMON_ASSERT(!(i->second.ingroup),
198 "Option already in option group: '"+opt+"'");
199 GroupData &g=_groups[group];
200 g.opts.push_back(opt);
201 i->second.ingroup=true;
205 ArgParser &ArgParser::onlyOneGroup(const std::string &group)
207 GroupData &g=_groups[group];
212 ArgParser &ArgParser::synonym(const std::string &syn,
213 const std::string &opt)
215 Opts::iterator o = _opts.find(opt);
216 Opts::iterator s = _opts.find(syn);
217 LEMON_ASSERT(o!=_opts.end(), "Unknown option: '"+opt+"'");
218 LEMON_ASSERT(s==_opts.end(), "Option already used: '"+syn+"'");
224 o->second.has_syn=true;
228 ArgParser &ArgParser::mandatoryGroup(const std::string &group)
230 GroupData &g=_groups[group];
235 ArgParser &ArgParser::other(const std::string &name,
236 const std::string &help)
238 _others_help.push_back(OtherArg(name,help));
242 void ArgParser::show(std::ostream &os,Opts::const_iterator i) const
244 os << "-" << i->first;
245 if(i->second.has_syn)
246 for(Opts::const_iterator j=_opts.begin();j!=_opts.end();++j)
247 if(j->second.syn&&j->second.help==i->first)
248 os << "|-" << j->first;
249 switch(i->second.type) {
264 void ArgParser::show(std::ostream &os,Groups::const_iterator i) const
266 GroupData::Opts::const_iterator o=i->second.opts.begin();
267 while(o!=i->second.opts.end()) {
268 show(os,_opts.find(*o));
270 if(o!=i->second.opts.end()) os<<'|';
274 void ArgParser::showHelp(Opts::const_iterator i) const
276 if(i->second.help.size()==0||i->second.syn) return;
279 std::cerr << std::endl;
280 std::cerr << " " << i->second.help << std::endl;
282 void ArgParser::showHelp(std::vector<ArgParser::OtherArg>::const_iterator i)
285 if(i->help.size()==0) return;
286 std::cerr << " " << i->name << std::endl
287 << " " << i->help << std::endl;
290 void ArgParser::shortHelp() const
292 const unsigned int LINE_LEN=77;
293 const std::string indent(" ");
294 std::cerr << "Usage:\n " << _command_name;
295 int pos=_command_name.size()+2;
296 for(Groups::const_iterator g=_groups.begin();g!=_groups.end();++g) {
297 std::ostringstream cstr;
299 if(!g->second.mandatory) cstr << '[';
301 if(!g->second.mandatory) cstr << ']';
302 if(pos+cstr.str().size()>LINE_LEN) {
303 std::cerr << std::endl << indent;
306 std::cerr << cstr.str();
307 pos+=cstr.str().size();
309 for(Opts::const_iterator i=_opts.begin();i!=_opts.end();++i)
310 if(!i->second.ingroup&&!i->second.syn) {
311 std::ostringstream cstr;
313 if(!i->second.mandatory) cstr << '[';
315 if(!i->second.mandatory) cstr << ']';
316 if(pos+cstr.str().size()>LINE_LEN) {
317 std::cerr << std::endl << indent;
320 std::cerr << cstr.str();
321 pos+=cstr.str().size();
323 for(std::vector<OtherArg>::const_iterator i=_others_help.begin();
324 i!=_others_help.end();++i)
326 std::ostringstream cstr;
327 cstr << ' ' << i->name;
329 if(pos+cstr.str().size()>LINE_LEN) {
330 std::cerr << std::endl << indent;
333 std::cerr << cstr.str();
334 pos+=cstr.str().size();
336 std::cerr << std::endl;
339 void ArgParser::showHelp() const
342 std::cerr << "Where:\n";
343 for(std::vector<OtherArg>::const_iterator i=_others_help.begin();
344 i!=_others_help.end();++i) showHelp(i);
345 for(Opts::const_iterator i=_opts.begin();i!=_opts.end();++i) showHelp(i);
350 void ArgParser::unknownOpt(std::string arg) const
352 std::cerr << "\nUnknown option: " << arg << "\n";
353 std::cerr << "\nType '" << _command_name <<
354 " --help' to obtain a short summary on the usage.\n\n";
358 void ArgParser::requiresValue(std::string arg, OptType t) const
360 std::cerr << "Argument '" << arg << "' requires a";
363 std::cerr << " string";
366 std::cerr << "n integer";
369 std::cerr << " floating point";
374 std::cerr << " value\n\n";
379 void ArgParser::checkMandatories() const
382 for(Opts::const_iterator i=_opts.begin();i!=_opts.end();++i)
383 if(i->second.mandatory&&!i->second.set)
386 std::cerr << _command_name
387 << ": The following mandatory arguments are missing.\n";
391 for(Groups::const_iterator i=_groups.begin();i!=_groups.end();++i)
392 if(i->second.mandatory||i->second.only_one)
395 for(GroupData::Opts::const_iterator o=i->second.opts.begin();
396 o!=i->second.opts.end();++o)
397 if(_opts.find(*o)->second.set) ++set;
398 if(i->second.mandatory&&!set) {
399 std::cerr << _command_name <<
400 ": At least one of the following arguments is mandatory.\n";
402 for(GroupData::Opts::const_iterator o=i->second.opts.begin();
403 o!=i->second.opts.end();++o)
404 showHelp(_opts.find(*o));
406 if(i->second.only_one&&set>1) {
407 std::cerr << _command_name <<
408 ": At most one of the following arguments can be given.\n";
410 for(GroupData::Opts::const_iterator o=i->second.opts.begin();
411 o!=i->second.opts.end();++o)
412 showHelp(_opts.find(*o));
416 std::cerr << "\nType '" << _command_name <<
417 " --help' to obtain a short summary on the usage.\n\n";
422 ArgParser &ArgParser::parse()
424 for(int ar=1; ar<_argc; ++ar) {
425 std::string arg(_argv[ar]);
426 if (arg[0] != '-' || arg.size() == 1) {
427 _file_args.push_back(arg);
430 Opts::iterator i = _opts.find(arg.substr(1));
431 if(i==_opts.end()) unknownOpt(arg);
433 if(i->second.syn) i=_opts.find(i->second.help);
434 ParData &p(i->second);
435 if (p.type==BOOL) *p.bool_p=true;
436 else if (p.type==FUNC) p.func_p.p(p.func_p.data);
437 else if(++ar==_argc) requiresValue(arg, p.type);
439 std::string val(_argv[ar]);
440 std::istringstream vals(val);
454 if(p.type!=STRING&&(!vals||!vals.eof()))
455 requiresValue(arg, p.type);