alpar@2389: /* -*- C++ -*- alpar@2389: * alpar@2391: * This file is a part of LEMON, a generic C++ optimization library alpar@2391: * alpar@2553: * Copyright (C) 2003-2008 alpar@2391: * 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@2390: (static_cast(p))->showHelp(); alpar@2389: exit(1); alpar@2389: } alpar@2389: deba@2410: ArgParser::ArgParser(int argc, const char **argv) :_argc(argc), _argv(argv), deba@2410: _command_name(argv[0]) { alpar@2406: funcOption("-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@2402: ArgParser::~ArgParser() alpar@2402: { alpar@2402: for(Opts::iterator i=_opts.begin();i!=_opts.end();++i) alpar@2402: if(i->second.self_delete) alpar@2402: switch(i->second.type) { alpar@2402: case BOOL: alpar@2402: delete i->second.bool_p; alpar@2402: break; alpar@2402: case STRING: alpar@2402: delete i->second.string_p; alpar@2402: break; alpar@2402: case DOUBLE: alpar@2402: delete i->second.double_p; alpar@2402: break; alpar@2402: case INTEGER: alpar@2402: delete i->second.int_p; alpar@2402: break; alpar@2402: case UNKNOWN: alpar@2402: break; alpar@2402: case FUNC: alpar@2402: break; alpar@2402: } alpar@2402: } alpar@2402: alpar@2402: alpar@2402: ArgParser &ArgParser::intOption(const std::string &name, alpar@2389: const std::string &help, alpar@2402: int value, bool obl) alpar@2389: { alpar@2389: ParData p; alpar@2402: p.int_p=new int(value); alpar@2402: p.self_delete=true; alpar@2389: p.help=help; alpar@2389: p.type=INTEGER; alpar@2389: p.mandatory=obl; alpar@2402: p.self_delete=true; alpar@2389: _opts[name]=p; alpar@2389: return *this; alpar@2389: } alpar@2389: alpar@2402: ArgParser &ArgParser::doubleOption(const std::string &name, alpar@2389: const std::string &help, alpar@2402: double value, bool obl) alpar@2389: { alpar@2389: ParData p; alpar@2402: p.double_p=new double(value); alpar@2402: p.self_delete=true; 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@2402: ArgParser &ArgParser::boolOption(const std::string &name, alpar@2389: const std::string &help, alpar@2402: bool value, bool obl) alpar@2389: { alpar@2389: ParData p; alpar@2402: p.bool_p=new bool(value); alpar@2402: p.self_delete=true; 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@2402: ArgParser &ArgParser::stringOption(const std::string &name, alpar@2389: const std::string &help, alpar@2402: std::string value, bool obl) alpar@2389: { alpar@2389: ParData p; alpar@2402: p.string_p=new std::string(value); alpar@2402: p.self_delete=true; 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@2402: ArgParser &ArgParser::refOption(const std::string &name, alpar@2402: const std::string &help, alpar@2406: int &ref, bool obl) alpar@2402: { alpar@2402: ParData p; alpar@2406: p.int_p=&ref; alpar@2402: p.self_delete=false; alpar@2402: p.help=help; alpar@2402: p.type=INTEGER; alpar@2402: p.mandatory=obl; alpar@2402: _opts[name]=p; alpar@2402: return *this; alpar@2402: } alpar@2402: alpar@2402: ArgParser &ArgParser::refOption(const std::string &name, deba@2410: const std::string &help, deba@2410: double &ref, bool obl) alpar@2402: { alpar@2402: ParData p; alpar@2406: p.double_p=&ref; alpar@2402: p.self_delete=false; alpar@2402: p.help=help; alpar@2402: p.type=DOUBLE; alpar@2402: p.mandatory=obl; alpar@2402: _opts[name]=p; alpar@2402: return *this; alpar@2402: } alpar@2402: alpar@2402: ArgParser &ArgParser::refOption(const std::string &name, deba@2410: const std::string &help, deba@2410: bool &ref, bool obl) alpar@2402: { alpar@2402: ParData p; alpar@2406: p.bool_p=&ref; alpar@2402: p.self_delete=false; alpar@2402: p.help=help; alpar@2402: p.type=BOOL; alpar@2402: p.mandatory=obl; alpar@2402: _opts[name]=p; alpar@2402: alpar@2406: ref = false; alpar@2402: alpar@2402: return *this; alpar@2402: } alpar@2402: alpar@2402: ArgParser &ArgParser::refOption(const std::string &name, alpar@2402: const std::string &help, alpar@2406: std::string &ref, bool obl) alpar@2402: { alpar@2402: ParData p; alpar@2406: p.string_p=&ref; alpar@2402: p.self_delete=false; alpar@2402: p.help=help; alpar@2402: p.type=STRING; alpar@2402: p.mandatory=obl; alpar@2402: _opts[name]=p; alpar@2402: return *this; alpar@2402: } alpar@2402: alpar@2406: ArgParser &ArgParser::funcOption(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@2402: p.self_delete=false; 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@2402: 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); deba@2410: LEMON_ASSERT(i!=_opts.end(), "Unknown option: '"+opt+"'"); deba@2410: LEMON_ASSERT(!(i->second.ingroup), deba@2410: "Option already in option group: '"+opt+"'"); deba@2410: GroupData &g=_groups[group]; deba@2410: g.opts.push_back(opt); deba@2410: i->second.ingroup=true; 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); deba@2410: LEMON_ASSERT(o!=_opts.end(), "Unknown option: '"+opt+"'"); deba@2410: LEMON_ASSERT(s==_opts.end(), "Option already used: '"+syn+"'"); deba@2410: ParData p; deba@2410: p.help=opt; deba@2410: p.mandatory=false; deba@2410: p.syn=true; deba@2410: _opts[syn]=p; deba@2410: o->second.has_syn=true; 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@2402: alpar@2389: }