1.1 --- a/lemon/arg_parser.cc Sun Jul 13 20:09:47 2008 +0100
1.2 +++ b/lemon/arg_parser.cc Sun Jul 13 22:05:25 2008 +0100
1.3 @@ -239,11 +239,11 @@
1.4 return *this;
1.5 }
1.6
1.7 - void ArgParser::show(std::ostream &os,Opts::iterator i)
1.8 + void ArgParser::show(std::ostream &os,Opts::const_iterator i) const
1.9 {
1.10 os << "-" << i->first;
1.11 if(i->second.has_syn)
1.12 - for(Opts::iterator j=_opts.begin();j!=_opts.end();++j)
1.13 + for(Opts::const_iterator j=_opts.begin();j!=_opts.end();++j)
1.14 if(j->second.syn&&j->second.help==i->first)
1.15 os << "|-" << j->first;
1.16 switch(i->second.type) {
1.17 @@ -261,9 +261,9 @@
1.18 }
1.19 }
1.20
1.21 - void ArgParser::show(std::ostream &os,Groups::iterator i)
1.22 + void ArgParser::show(std::ostream &os,Groups::const_iterator i) const
1.23 {
1.24 - GroupData::Opts::iterator o=i->second.opts.begin();
1.25 + GroupData::Opts::const_iterator o=i->second.opts.begin();
1.26 while(o!=i->second.opts.end()) {
1.27 show(os,_opts.find(*o));
1.28 ++o;
1.29 @@ -271,7 +271,7 @@
1.30 }
1.31 }
1.32
1.33 - void ArgParser::showHelp(Opts::iterator i)
1.34 + void ArgParser::showHelp(Opts::const_iterator i) const
1.35 {
1.36 if(i->second.help.size()==0||i->second.syn) return;
1.37 std::cerr << " ";
1.38 @@ -279,20 +279,21 @@
1.39 std::cerr << std::endl;
1.40 std::cerr << " " << i->second.help << std::endl;
1.41 }
1.42 - void ArgParser::showHelp(std::vector<ArgParser::OtherArg>::iterator i)
1.43 + void ArgParser::showHelp(std::vector<ArgParser::OtherArg>::const_iterator i)
1.44 + const
1.45 {
1.46 if(i->help.size()==0) return;
1.47 std::cerr << " " << i->name << std::endl
1.48 << " " << i->help << std::endl;
1.49 }
1.50
1.51 - void ArgParser::shortHelp()
1.52 + void ArgParser::shortHelp() const
1.53 {
1.54 const unsigned int LINE_LEN=77;
1.55 const std::string indent(" ");
1.56 std::cerr << "Usage:\n " << _command_name;
1.57 int pos=_command_name.size()+2;
1.58 - for(Groups::iterator g=_groups.begin();g!=_groups.end();++g) {
1.59 + for(Groups::const_iterator g=_groups.begin();g!=_groups.end();++g) {
1.60 std::ostringstream cstr;
1.61 cstr << ' ';
1.62 if(!g->second.mandatory) cstr << '[';
1.63 @@ -305,7 +306,7 @@
1.64 std::cerr << cstr.str();
1.65 pos+=cstr.str().size();
1.66 }
1.67 - for(Opts::iterator i=_opts.begin();i!=_opts.end();++i)
1.68 + for(Opts::const_iterator i=_opts.begin();i!=_opts.end();++i)
1.69 if(!i->second.ingroup&&!i->second.syn) {
1.70 std::ostringstream cstr;
1.71 cstr << ' ';
1.72 @@ -319,7 +320,7 @@
1.73 std::cerr << cstr.str();
1.74 pos+=cstr.str().size();
1.75 }
1.76 - for(std::vector<OtherArg>::iterator i=_others_help.begin();
1.77 + for(std::vector<OtherArg>::const_iterator i=_others_help.begin();
1.78 i!=_others_help.end();++i)
1.79 {
1.80 std::ostringstream cstr;
1.81 @@ -335,18 +336,18 @@
1.82 std::cerr << std::endl;
1.83 }
1.84
1.85 - void ArgParser::showHelp()
1.86 + void ArgParser::showHelp() const
1.87 {
1.88 shortHelp();
1.89 std::cerr << "Where:\n";
1.90 - for(std::vector<OtherArg>::iterator i=_others_help.begin();
1.91 + for(std::vector<OtherArg>::const_iterator i=_others_help.begin();
1.92 i!=_others_help.end();++i) showHelp(i);
1.93 - for(Opts::iterator i=_opts.begin();i!=_opts.end();++i) showHelp(i);
1.94 + for(Opts::const_iterator i=_opts.begin();i!=_opts.end();++i) showHelp(i);
1.95 exit(1);
1.96 }
1.97
1.98
1.99 - void ArgParser::unknownOpt(std::string arg)
1.100 + void ArgParser::unknownOpt(std::string arg) const
1.101 {
1.102 std::cerr << "\nUnknown option: " << arg << "\n";
1.103 std::cerr << "\nType '" << _command_name <<
1.104 @@ -354,7 +355,7 @@
1.105 exit(1);
1.106 }
1.107
1.108 - void ArgParser::requiresValue(std::string arg, OptType t)
1.109 + void ArgParser::requiresValue(std::string arg, OptType t) const
1.110 {
1.111 std::cerr << "Argument '" << arg << "' requires a";
1.112 switch(t) {
1.113 @@ -375,10 +376,10 @@
1.114 }
1.115
1.116
1.117 - void ArgParser::checkMandatories()
1.118 + void ArgParser::checkMandatories() const
1.119 {
1.120 bool ok=true;
1.121 - for(Opts::iterator i=_opts.begin();i!=_opts.end();++i)
1.122 + for(Opts::const_iterator i=_opts.begin();i!=_opts.end();++i)
1.123 if(i->second.mandatory&&!i->second.set)
1.124 {
1.125 if(ok)
1.126 @@ -387,18 +388,18 @@
1.127 ok=false;
1.128 showHelp(i);
1.129 }
1.130 - for(Groups::iterator i=_groups.begin();i!=_groups.end();++i)
1.131 + for(Groups::const_iterator i=_groups.begin();i!=_groups.end();++i)
1.132 if(i->second.mandatory||i->second.only_one)
1.133 {
1.134 int set=0;
1.135 - for(GroupData::Opts::iterator o=i->second.opts.begin();
1.136 + for(GroupData::Opts::const_iterator o=i->second.opts.begin();
1.137 o!=i->second.opts.end();++o)
1.138 if(_opts.find(*o)->second.set) ++set;
1.139 if(i->second.mandatory&&!set) {
1.140 std::cerr << _command_name <<
1.141 ": At least one of the following arguments is mandatory.\n";
1.142 ok=false;
1.143 - for(GroupData::Opts::iterator o=i->second.opts.begin();
1.144 + for(GroupData::Opts::const_iterator o=i->second.opts.begin();
1.145 o!=i->second.opts.end();++o)
1.146 showHelp(_opts.find(*o));
1.147 }
1.148 @@ -406,7 +407,7 @@
1.149 std::cerr << _command_name <<
1.150 ": At most one of the following arguments can be given.\n";
1.151 ok=false;
1.152 - for(GroupData::Opts::iterator o=i->second.opts.begin();
1.153 + for(GroupData::Opts::const_iterator o=i->second.opts.begin();
1.154 o!=i->second.opts.end();++o)
1.155 showHelp(_opts.find(*o));
1.156 }
2.1 --- a/lemon/arg_parser.h Sun Jul 13 20:09:47 2008 +0100
2.2 +++ b/lemon/arg_parser.h Sun Jul 13 22:05:25 2008 +0100
2.3 @@ -271,17 +271,20 @@
2.4
2.5 ///@}
2.6
2.7 - void show(std::ostream &os,Opts::iterator i);
2.8 - void show(std::ostream &os,Groups::iterator i);
2.9 - void showHelp(Opts::iterator i);
2.10 - void showHelp(std::vector<OtherArg>::iterator i);
2.11 - void shortHelp();
2.12 - void showHelp();
2.13 + private:
2.14 + void show(std::ostream &os,Opts::const_iterator i) const;
2.15 + void show(std::ostream &os,Groups::const_iterator i) const;
2.16 + void showHelp(Opts::const_iterator i) const;
2.17 + void showHelp(std::vector<OtherArg>::const_iterator i) const;
2.18
2.19 - void unknownOpt(std::string arg);
2.20 + void unknownOpt(std::string arg) const;
2.21
2.22 - void requiresValue(std::string arg, OptType t);
2.23 - void checkMandatories();
2.24 + void requiresValue(std::string arg, OptType t) const;
2.25 + void checkMandatories() const;
2.26 +
2.27 + void shortHelp() const;
2.28 + void showHelp() const;
2.29 + public:
2.30
2.31 ///Start the parsing process
2.32 ArgParser &parse();
2.33 @@ -293,12 +296,12 @@
2.34 }
2.35
2.36 ///Give back the command name (the 0th argument)
2.37 - const std::string &commandName() { return _command_name; }
2.38 + const std::string &commandName() const { return _command_name; }
2.39
2.40 ///Check if an opion has been given to the command.
2.41 - bool given(std::string op)
2.42 + bool given(std::string op) const
2.43 {
2.44 - Opts::iterator i = _opts.find(op);
2.45 + Opts::const_iterator i = _opts.find(op);
2.46 return i!=_opts.end()?i->second.set:false;
2.47 }
2.48
2.49 @@ -311,15 +314,15 @@
2.50 ///throws an exception (i.e. it performs runtime type checking).
2.51 class RefType
2.52 {
2.53 - ArgParser &_parser;
2.54 + const ArgParser &_parser;
2.55 std::string _name;
2.56 public:
2.57 ///\e
2.58 - RefType(ArgParser &p,const std::string &n) :_parser(p),_name(n) {}
2.59 + RefType(const ArgParser &p,const std::string &n) :_parser(p),_name(n) {}
2.60 ///\e
2.61 operator bool()
2.62 {
2.63 - Opts::iterator i = _parser._opts.find(_name);
2.64 + Opts::const_iterator i = _parser._opts.find(_name);
2.65 LEMON_ASSERT(i!=_parser._opts.end(),
2.66 std::string()+"Unkown option: '"+_name+"'");
2.67 LEMON_ASSERT(i->second.type==ArgParser::BOOL,
2.68 @@ -329,7 +332,7 @@
2.69 ///\e
2.70 operator std::string()
2.71 {
2.72 - Opts::iterator i = _parser._opts.find(_name);
2.73 + Opts::const_iterator i = _parser._opts.find(_name);
2.74 LEMON_ASSERT(i!=_parser._opts.end(),
2.75 std::string()+"Unkown option: '"+_name+"'");
2.76 LEMON_ASSERT(i->second.type==ArgParser::STRING,
2.77 @@ -339,7 +342,7 @@
2.78 ///\e
2.79 operator double()
2.80 {
2.81 - Opts::iterator i = _parser._opts.find(_name);
2.82 + Opts::const_iterator i = _parser._opts.find(_name);
2.83 LEMON_ASSERT(i!=_parser._opts.end(),
2.84 std::string()+"Unkown option: '"+_name+"'");
2.85 LEMON_ASSERT(i->second.type==ArgParser::DOUBLE ||
2.86 @@ -351,7 +354,7 @@
2.87 ///\e
2.88 operator int()
2.89 {
2.90 - Opts::iterator i = _parser._opts.find(_name);
2.91 + Opts::const_iterator i = _parser._opts.find(_name);
2.92 LEMON_ASSERT(i!=_parser._opts.end(),
2.93 std::string()+"Unkown option: '"+_name+"'");
2.94 LEMON_ASSERT(i->second.type==ArgParser::INTEGER,
2.95 @@ -365,7 +368,7 @@
2.96
2.97 ///Give back the value of an option.
2.98 ///\sa RefType
2.99 - RefType operator[](const std::string &n)
2.100 + RefType operator[](const std::string &n) const
2.101 {
2.102 return RefType(*this, n);
2.103 }
2.104 @@ -374,7 +377,7 @@
2.105
2.106 ///Give back a reference to a vector consisting of the program arguments
2.107 ///not starting with a '-' character.
2.108 - std::vector<std::string> &files() { return _file_args; }
2.109 + const std::vector<std::string> &files() const { return _file_args; }
2.110
2.111 };
2.112 }