diff --git a/lemon/arg_parser.cc b/lemon/arg_parser.cc --- a/lemon/arg_parser.cc +++ b/lemon/arg_parser.cc @@ -239,11 +239,11 @@ return *this; } - void ArgParser::show(std::ostream &os,Opts::iterator i) + void ArgParser::show(std::ostream &os,Opts::const_iterator i) const { os << "-" << i->first; if(i->second.has_syn) - for(Opts::iterator j=_opts.begin();j!=_opts.end();++j) + for(Opts::const_iterator j=_opts.begin();j!=_opts.end();++j) if(j->second.syn&&j->second.help==i->first) os << "|-" << j->first; switch(i->second.type) { @@ -261,9 +261,9 @@ } } - void ArgParser::show(std::ostream &os,Groups::iterator i) + void ArgParser::show(std::ostream &os,Groups::const_iterator i) const { - GroupData::Opts::iterator o=i->second.opts.begin(); + GroupData::Opts::const_iterator o=i->second.opts.begin(); while(o!=i->second.opts.end()) { show(os,_opts.find(*o)); ++o; @@ -271,7 +271,7 @@ } } - void ArgParser::showHelp(Opts::iterator i) + void ArgParser::showHelp(Opts::const_iterator i) const { if(i->second.help.size()==0||i->second.syn) return; std::cerr << " "; @@ -279,20 +279,21 @@ std::cerr << std::endl; std::cerr << " " << i->second.help << std::endl; } - void ArgParser::showHelp(std::vector::iterator i) + void ArgParser::showHelp(std::vector::const_iterator i) + const { if(i->help.size()==0) return; std::cerr << " " << i->name << std::endl << " " << i->help << std::endl; } - void ArgParser::shortHelp() + 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::iterator g=_groups.begin();g!=_groups.end();++g) { + for(Groups::const_iterator g=_groups.begin();g!=_groups.end();++g) { std::ostringstream cstr; cstr << ' '; if(!g->second.mandatory) cstr << '['; @@ -305,7 +306,7 @@ std::cerr << cstr.str(); pos+=cstr.str().size(); } - for(Opts::iterator i=_opts.begin();i!=_opts.end();++i) + for(Opts::const_iterator i=_opts.begin();i!=_opts.end();++i) if(!i->second.ingroup&&!i->second.syn) { std::ostringstream cstr; cstr << ' '; @@ -319,7 +320,7 @@ std::cerr << cstr.str(); pos+=cstr.str().size(); } - for(std::vector::iterator i=_others_help.begin(); + for(std::vector::const_iterator i=_others_help.begin(); i!=_others_help.end();++i) { std::ostringstream cstr; @@ -335,18 +336,18 @@ std::cerr << std::endl; } - void ArgParser::showHelp() + void ArgParser::showHelp() const { shortHelp(); std::cerr << "Where:\n"; - for(std::vector::iterator i=_others_help.begin(); + for(std::vector::const_iterator i=_others_help.begin(); i!=_others_help.end();++i) showHelp(i); - for(Opts::iterator i=_opts.begin();i!=_opts.end();++i) showHelp(i); + for(Opts::const_iterator i=_opts.begin();i!=_opts.end();++i) showHelp(i); exit(1); } - void ArgParser::unknownOpt(std::string arg) + void ArgParser::unknownOpt(std::string arg) const { std::cerr << "\nUnknown option: " << arg << "\n"; std::cerr << "\nType '" << _command_name << @@ -354,7 +355,7 @@ exit(1); } - void ArgParser::requiresValue(std::string arg, OptType t) + void ArgParser::requiresValue(std::string arg, OptType t) const { std::cerr << "Argument '" << arg << "' requires a"; switch(t) { @@ -375,10 +376,10 @@ } - void ArgParser::checkMandatories() + void ArgParser::checkMandatories() const { bool ok=true; - for(Opts::iterator i=_opts.begin();i!=_opts.end();++i) + for(Opts::const_iterator i=_opts.begin();i!=_opts.end();++i) if(i->second.mandatory&&!i->second.set) { if(ok) @@ -387,18 +388,18 @@ ok=false; showHelp(i); } - for(Groups::iterator i=_groups.begin();i!=_groups.end();++i) + for(Groups::const_iterator i=_groups.begin();i!=_groups.end();++i) if(i->second.mandatory||i->second.only_one) { int set=0; - for(GroupData::Opts::iterator o=i->second.opts.begin(); + 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"; ok=false; - for(GroupData::Opts::iterator o=i->second.opts.begin(); + for(GroupData::Opts::const_iterator o=i->second.opts.begin(); o!=i->second.opts.end();++o) showHelp(_opts.find(*o)); } @@ -406,7 +407,7 @@ std::cerr << _command_name << ": At most one of the following arguments can be given.\n"; ok=false; - for(GroupData::Opts::iterator o=i->second.opts.begin(); + for(GroupData::Opts::const_iterator o=i->second.opts.begin(); o!=i->second.opts.end();++o) showHelp(_opts.find(*o)); } diff --git a/lemon/arg_parser.h b/lemon/arg_parser.h --- a/lemon/arg_parser.h +++ b/lemon/arg_parser.h @@ -271,17 +271,20 @@ ///@} - void show(std::ostream &os,Opts::iterator i); - void show(std::ostream &os,Groups::iterator i); - void showHelp(Opts::iterator i); - void showHelp(std::vector::iterator i); - void shortHelp(); - void showHelp(); + private: + void show(std::ostream &os,Opts::const_iterator i) const; + void show(std::ostream &os,Groups::const_iterator i) const; + void showHelp(Opts::const_iterator i) const; + void showHelp(std::vector::const_iterator i) const; - void unknownOpt(std::string arg); + void unknownOpt(std::string arg) const; - void requiresValue(std::string arg, OptType t); - void checkMandatories(); + void requiresValue(std::string arg, OptType t) const; + void checkMandatories() const; + + void shortHelp() const; + void showHelp() const; + public: ///Start the parsing process ArgParser &parse(); @@ -293,12 +296,12 @@ } ///Give back the command name (the 0th argument) - const std::string &commandName() { return _command_name; } + const std::string &commandName() const { return _command_name; } ///Check if an opion has been given to the command. - bool given(std::string op) + bool given(std::string op) const { - Opts::iterator i = _opts.find(op); + Opts::const_iterator i = _opts.find(op); return i!=_opts.end()?i->second.set:false; } @@ -311,15 +314,15 @@ ///throws an exception (i.e. it performs runtime type checking). class RefType { - ArgParser &_parser; + const ArgParser &_parser; std::string _name; public: ///\e - RefType(ArgParser &p,const std::string &n) :_parser(p),_name(n) {} + RefType(const ArgParser &p,const std::string &n) :_parser(p),_name(n) {} ///\e operator bool() { - Opts::iterator i = _parser._opts.find(_name); + Opts::const_iterator i = _parser._opts.find(_name); LEMON_ASSERT(i!=_parser._opts.end(), std::string()+"Unkown option: '"+_name+"'"); LEMON_ASSERT(i->second.type==ArgParser::BOOL, @@ -329,7 +332,7 @@ ///\e operator std::string() { - Opts::iterator i = _parser._opts.find(_name); + Opts::const_iterator i = _parser._opts.find(_name); LEMON_ASSERT(i!=_parser._opts.end(), std::string()+"Unkown option: '"+_name+"'"); LEMON_ASSERT(i->second.type==ArgParser::STRING, @@ -339,7 +342,7 @@ ///\e operator double() { - Opts::iterator i = _parser._opts.find(_name); + Opts::const_iterator i = _parser._opts.find(_name); LEMON_ASSERT(i!=_parser._opts.end(), std::string()+"Unkown option: '"+_name+"'"); LEMON_ASSERT(i->second.type==ArgParser::DOUBLE || @@ -351,7 +354,7 @@ ///\e operator int() { - Opts::iterator i = _parser._opts.find(_name); + Opts::const_iterator i = _parser._opts.find(_name); LEMON_ASSERT(i!=_parser._opts.end(), std::string()+"Unkown option: '"+_name+"'"); LEMON_ASSERT(i->second.type==ArgParser::INTEGER, @@ -365,7 +368,7 @@ ///Give back the value of an option. ///\sa RefType - RefType operator[](const std::string &n) + RefType operator[](const std::string &n) const { return RefType(*this, n); } @@ -374,7 +377,7 @@ ///Give back a reference to a vector consisting of the program arguments ///not starting with a '-' character. - std::vector &files() { return _file_args; } + const std::vector &files() const { return _file_args; } }; }