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 option("-help","Print a short help message",_showHelp,this);
32 synonym("help","-help");
37 ArgParser &ArgParser::option(const std::string &name,
38 const std::string &help,
50 ArgParser &ArgParser::option(const std::string &name,
51 const std::string &help,
52 double &value, bool obl)
63 ArgParser &ArgParser::option(const std::string &name,
64 const std::string &help,
65 bool &value, bool obl)
79 ArgParser &ArgParser::option(const std::string &name,
80 const std::string &help,
81 std::string &value, bool obl)
92 ArgParser &ArgParser::option(const std::string &name,
93 const std::string &help,
94 void (*func)(void *),void *data)
105 ArgParser &ArgParser::optionGroup(const std::string &group,
106 const std::string &opt)
108 Opts::iterator i = _opts.find(opt);
109 if(i==_opts.end()) exit(3); ///\todo throw exception instead
110 else if(i->second.ingroup) exit(3); ///\todo throw exception instead
112 GroupData &g=_groups[group];
113 g.opts.push_back(opt);
114 i->second.ingroup=true;
119 ArgParser &ArgParser::onlyOneGroup(const std::string &group)
121 GroupData &g=_groups[group];
126 ArgParser &ArgParser::synonym(const std::string &syn,
127 const std::string &opt)
129 Opts::iterator o = _opts.find(opt);
130 Opts::iterator s = _opts.find(syn);
131 if(o==_opts.end()||s!=_opts.end())
132 exit(3); ///\todo throw exception instead
139 o->second.has_syn=true;
144 ArgParser &ArgParser::mandatoryGroup(const std::string &group)
146 GroupData &g=_groups[group];
151 ArgParser &ArgParser::other(const std::string &name,
152 const std::string &help)
154 _others_help.push_back(OtherArg(name,help));
158 void ArgParser::show(std::ostream &os,Opts::iterator i)
160 os << "-" << i->first;
161 if(i->second.has_syn)
162 for(Opts::iterator j=_opts.begin();j!=_opts.end();++j)
163 if(j->second.syn&&j->second.help==i->first)
164 os << "|-" << j->first;
165 switch(i->second.type) {
180 void ArgParser::show(std::ostream &os,Groups::iterator i)
182 GroupData::Opts::iterator o=i->second.opts.begin();
183 while(o!=i->second.opts.end()) {
184 show(os,_opts.find(*o));
186 if(o!=i->second.opts.end()) os<<'|';
190 void ArgParser::showHelp(Opts::iterator i)
192 if(i->second.help.size()==0||i->second.syn) return;
195 std::cerr << std::endl;
196 std::cerr << " " << i->second.help << std::endl;
198 void ArgParser::showHelp(std::vector<ArgParser::OtherArg>::iterator i)
200 if(i->help.size()==0) return;
201 std::cerr << " " << i->name << std::endl
202 << " " << i->help << std::endl;
205 void ArgParser::shortHelp()
207 const unsigned int LINE_LEN=77;
208 const std::string indent(" ");
209 std::cerr << "Usage:\n " << _command_name;
210 int pos=_command_name.size()+2;
211 for(Groups::iterator g=_groups.begin();g!=_groups.end();++g) {
212 std::ostringstream cstr;
214 if(!g->second.mandatory) cstr << '[';
216 if(!g->second.mandatory) cstr << ']';
217 if(pos+cstr.str().size()>LINE_LEN) {
218 std::cerr << std::endl << indent;
221 std::cerr << cstr.str();
222 pos+=cstr.str().size();
224 for(Opts::iterator i=_opts.begin();i!=_opts.end();++i)
225 if(!i->second.ingroup&&!i->second.syn) {
226 std::ostringstream cstr;
228 if(!i->second.mandatory) cstr << '[';
230 if(!i->second.mandatory) cstr << ']';
231 if(pos+cstr.str().size()>LINE_LEN) {
232 std::cerr << std::endl << indent;
235 std::cerr << cstr.str();
236 pos+=cstr.str().size();
238 for(std::vector<OtherArg>::iterator i=_others_help.begin();
239 i!=_others_help.end();++i)
241 std::ostringstream cstr;
242 cstr << ' ' << i->name;
244 if(pos+cstr.str().size()>LINE_LEN) {
245 std::cerr << std::endl << indent;
248 std::cerr << cstr.str();
249 pos+=cstr.str().size();
251 std::cerr << std::endl;
254 void ArgParser::showHelp()
257 std::cerr << "Where:\n";
258 for(std::vector<OtherArg>::iterator i=_others_help.begin();
259 i!=_others_help.end();++i) showHelp(i);
260 for(Opts::iterator i=_opts.begin();i!=_opts.end();++i) showHelp(i);
265 void ArgParser::unknownOpt(std::string arg)
267 std::cerr << "\nUnknown option: " << arg << "\n";
268 std::cerr << "\nType '" << _command_name <<
269 " --help' to obtain a short summary on the usage.\n\n";
273 void ArgParser::requiresValue(std::string arg, OptType t)
275 std::cerr << "Argument '" << arg << "' requires a";
278 std::cerr << " string";
281 std::cerr << "n integer";
284 std::cerr << " floating point";
289 std::cerr << " value\n\n";
294 void ArgParser::checkMandatories()
297 for(Opts::iterator i=_opts.begin();i!=_opts.end();++i)
298 if(i->second.mandatory&&!i->second.set)
301 std::cerr << _command_name
302 << ": The following mandatory arguments are missing.\n";
306 for(Groups::iterator i=_groups.begin();i!=_groups.end();++i)
307 if(i->second.mandatory||i->second.only_one)
310 for(GroupData::Opts::iterator o=i->second.opts.begin();
311 o!=i->second.opts.end();++o)
312 if(_opts.find(*o)->second.set) ++set;
313 if(i->second.mandatory&&!set) {
314 std::cerr << _command_name
315 << ": At least one of the following arguments is mandatory.\n";
317 for(GroupData::Opts::iterator o=i->second.opts.begin();
318 o!=i->second.opts.end();++o)
319 showHelp(_opts.find(*o));
321 if(i->second.only_one&&set>1) {
322 std::cerr << _command_name
323 << ": At most one of the following arguments can be given.\n";
325 for(GroupData::Opts::iterator o=i->second.opts.begin();
326 o!=i->second.opts.end();++o)
327 showHelp(_opts.find(*o));
331 std::cerr << "\nType '" << _command_name <<
332 " --help' to obtain a short summary on the usage.\n\n";
337 ArgParser &ArgParser::parse()
339 for(int ar=1; ar<_argc; ++ar) {
340 std::string arg(_argv[ar]);
341 if (arg[0] != '-' || arg.size() == 1) {
342 _file_args.push_back(arg);
345 Opts::iterator i = _opts.find(arg.substr(1));
346 if(i==_opts.end()) unknownOpt(arg);
348 if(i->second.syn) i=_opts.find(i->second.help);
349 ParData &p(i->second);
350 if (p.type==BOOL) *p.bool_p=true;
351 else if (p.type==FUNC) p.func_p.p(p.func_p.data);
352 else if(++ar==_argc) requiresValue(arg, p.type);
354 std::string val(_argv[ar]);
355 std::istringstream vals(val);
369 if(p.type!=STRING&&(!vals||!vals.eof()))
370 requiresValue(arg, p.type);