/* -*- mode: C++; indent-tabs-mode: nil; -*-
* This file is a part of LEMON, a generic C++ optimization library.
* Copyright (C) 2003-2009
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
* (Egervary Research Group on Combinatorial Optimization, EGRES).
* Permission to use, modify and distribute this software is granted
* provided that this copyright notice appears in all copies. For
* precise terms see the accompanying LICENSE file.
* This software is provided "AS IS" with no warranty of any kind,
* express or implied, and with no claim as to its suitability for any
#include <lemon/arg_parser.h>
void ArgParser::_showHelp(void *p)
(static_cast<ArgParser*>(p))->showHelp();
ArgParser::ArgParser(int argc, const char * const *argv)
:_argc(argc), _argv(argv), _command_name(argv[0]) {
funcOption("-help","Print a short help message",_showHelp,this);
for(Opts::iterator i=_opts.begin();i!=_opts.end();++i)
if(i->second.self_delete)
delete i->second.string_p;
delete i->second.double_p;
ArgParser &ArgParser::intOption(const std::string &name,
ArgParser &ArgParser::doubleOption(const std::string &name,
p.double_p=new double(value);
ArgParser &ArgParser::boolOption(const std::string &name,
p.bool_p=new bool(value);
ArgParser &ArgParser::stringOption(const std::string &name,
std::string value, bool obl)
p.string_p=new std::string(value);
ArgParser &ArgParser::refOption(const std::string &name,
ArgParser &ArgParser::refOption(const std::string &name,
ArgParser &ArgParser::refOption(const std::string &name,
ArgParser &ArgParser::refOption(const std::string &name,
std::string &ref, bool obl)
ArgParser &ArgParser::funcOption(const std::string &name,
void (*func)(void *),void *data)
ArgParser &ArgParser::optionGroup(const std::string &group,
Opts::iterator i = _opts.find(opt);
LEMON_ASSERT(i!=_opts.end(), "Unknown option: '"+opt+"'");
LEMON_ASSERT(!(i->second.ingroup),
"Option already in option group: '"+opt+"'");
GroupData &g=_groups[group];
ArgParser &ArgParser::onlyOneGroup(const std::string &group)
GroupData &g=_groups[group];
ArgParser &ArgParser::synonym(const std::string &syn,
Opts::iterator o = _opts.find(opt);
Opts::iterator s = _opts.find(syn);
LEMON_ASSERT(o!=_opts.end(), "Unknown option: '"+opt+"'");
LEMON_ASSERT(s==_opts.end(), "Option already used: '"+syn+"'");
ArgParser &ArgParser::mandatoryGroup(const std::string &group)
GroupData &g=_groups[group];
ArgParser &ArgParser::other(const std::string &name,
_others_help.push_back(OtherArg(name,help));
void ArgParser::show(std::ostream &os,Opts::const_iterator i) const
for(Opts::const_iterator j=_opts.begin();j!=_opts.end();++j)
if(j->second.syn&&j->second.help==i->first)
void ArgParser::show(std::ostream &os,Groups::const_iterator i) const
GroupData::Opts::const_iterator o=i->second.opts.begin();
while(o!=i->second.opts.end()) {
if(o!=i->second.opts.end()) os<<'|';
void ArgParser::showHelp(Opts::const_iterator i) const
if(i->second.help.size()==0||i->second.syn) return;
std::cerr << " " << i->second.help << std::endl;
void ArgParser::showHelp(std::vector<ArgParser::OtherArg>::const_iterator i)
if(i->help.size()==0) return;
std::cerr << " " << i->name << std::endl
<< " " << i->help << std::endl;
void ArgParser::shortHelp() const
const unsigned int LINE_LEN=77;
const std::string indent(" ");
std::cerr << "Usage:\n " << _command_name;
int pos=_command_name.size()+2;
for(Groups::const_iterator g=_groups.begin();g!=_groups.end();++g) {
if(!g->second.mandatory) cstr << '[';
if(!g->second.mandatory) cstr << ']';
if(pos+cstr.str().size()>LINE_LEN) {
std::cerr << std::endl << indent;
for(Opts::const_iterator i=_opts.begin();i!=_opts.end();++i)
if(!i->second.ingroup&&!i->second.syn) {
if(!i->second.mandatory) cstr << '[';
if(!i->second.mandatory) cstr << ']';
if(pos+cstr.str().size()>LINE_LEN) {
std::cerr << std::endl << indent;
for(std::vector<OtherArg>::const_iterator i=_others_help.begin();
i!=_others_help.end();++i)
if(pos+cstr.str().size()>LINE_LEN) {
std::cerr << std::endl << indent;
void ArgParser::showHelp() const
for(std::vector<OtherArg>::const_iterator i=_others_help.begin();
i!=_others_help.end();++i) showHelp(i);
for(Opts::const_iterator i=_opts.begin();i!=_opts.end();++i) showHelp(i);
void ArgParser::unknownOpt(std::string arg) const
std::cerr << "\nUnknown option: " << arg << "\n";
std::cerr << "\nType '" << _command_name <<
" --help' to obtain a short summary on the usage.\n\n";
void ArgParser::requiresValue(std::string arg, OptType t) const
std::cerr << "Argument '" << arg << "' requires a";
std::cerr << "n integer";
std::cerr << " floating point";
std::cerr << " value\n\n";
void ArgParser::checkMandatories() const
for(Opts::const_iterator i=_opts.begin();i!=_opts.end();++i)
if(i->second.mandatory&&!i->second.set)
std::cerr << _command_name
<< ": The following mandatory arguments are missing.\n";
for(Groups::const_iterator i=_groups.begin();i!=_groups.end();++i)
if(i->second.mandatory||i->second.only_one)
for(GroupData::Opts::const_iterator o=i->second.opts.begin();
o!=i->second.opts.end();++o)
if(_opts.find(*o)->second.set) ++set;
if(i->second.mandatory&&!set) {
std::cerr << _command_name <<
": At least one of the following arguments is mandatory.\n";
for(GroupData::Opts::const_iterator o=i->second.opts.begin();
o!=i->second.opts.end();++o)
showHelp(_opts.find(*o));
if(i->second.only_one&&set>1) {
std::cerr << _command_name <<
": At most one of the following arguments can be given.\n";
for(GroupData::Opts::const_iterator o=i->second.opts.begin();
o!=i->second.opts.end();++o)
showHelp(_opts.find(*o));
std::cerr << "\nType '" << _command_name <<
" --help' to obtain a short summary on the usage.\n\n";
ArgParser &ArgParser::parse()
for(int ar=1; ar<_argc; ++ar) {
std::string arg(_argv[ar]);
if (arg[0] != '-' || arg.size() == 1) {
_file_args.push_back(arg);
Opts::iterator i = _opts.find(arg.substr(1));
if(i==_opts.end()) unknownOpt(arg);
if(i->second.syn) i=_opts.find(i->second.help);
if (p.type==BOOL) *p.bool_p=true;
else if (p.type==FUNC) p.func_p.p(p.func_p.data);
else if(++ar==_argc) requiresValue(arg, p.type);
std::string val(_argv[ar]);
std::istringstream vals(val);
if(p.type!=STRING&&(!vals||!vals.eof()))
requiresValue(arg, p.type);