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