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