lemon/arg_parser.h
changeset 222 f9a18c21dba8
parent 209 765619b7cbb2
child 261 1c2ac7deb5d8
equal deleted inserted replaced
8:fe448abed4f5 9:7444e0b219e5
   269     ArgParser &synonym(const std::string &syn,
   269     ArgParser &synonym(const std::string &syn,
   270                            const std::string &opt);
   270                            const std::string &opt);
   271 
   271 
   272     ///@}
   272     ///@}
   273 
   273 
   274     void show(std::ostream &os,Opts::iterator i);
   274   private:
   275     void show(std::ostream &os,Groups::iterator i);
   275     void show(std::ostream &os,Opts::const_iterator i) const;
   276     void showHelp(Opts::iterator i);
   276     void show(std::ostream &os,Groups::const_iterator i) const;
   277     void showHelp(std::vector<OtherArg>::iterator i);
   277     void showHelp(Opts::const_iterator i) const;
   278     void shortHelp();
   278     void showHelp(std::vector<OtherArg>::const_iterator i) const;
   279     void showHelp();
   279 
   280 
   280     void unknownOpt(std::string arg) const;
   281     void unknownOpt(std::string arg);
   281 
   282 
   282     void requiresValue(std::string arg, OptType t) const;
   283     void requiresValue(std::string arg, OptType t);
   283     void checkMandatories() const;
   284     void checkMandatories();
   284 
       
   285     void shortHelp() const;
       
   286     void showHelp() const;
       
   287   public:
   285 
   288 
   286     ///Start the parsing process
   289     ///Start the parsing process
   287     ArgParser &parse();
   290     ArgParser &parse();
   288 
   291 
   289     /// Synonym for parse()
   292     /// Synonym for parse()
   291     {
   294     {
   292       return parse();
   295       return parse();
   293     }
   296     }
   294 
   297 
   295     ///Give back the command name (the 0th argument)
   298     ///Give back the command name (the 0th argument)
   296     const std::string &commandName() { return _command_name; }
   299     const std::string &commandName() const { return _command_name; }
   297 
   300 
   298     ///Check if an opion has been given to the command.
   301     ///Check if an opion has been given to the command.
   299     bool given(std::string op)
   302     bool given(std::string op) const
   300     {
   303     {
   301       Opts::iterator i = _opts.find(op);
   304       Opts::const_iterator i = _opts.find(op);
   302       return i!=_opts.end()?i->second.set:false;
   305       return i!=_opts.end()?i->second.set:false;
   303     }
   306     }
   304 
   307 
   305 
   308 
   306     ///Magic type for operator[]
   309     ///Magic type for operator[]
   309     ///It automatically converts to \c int, \c double, \c bool or
   312     ///It automatically converts to \c int, \c double, \c bool or
   310     ///\c std::string if the type of the option matches, otherwise it
   313     ///\c std::string if the type of the option matches, otherwise it
   311     ///throws an exception (i.e. it performs runtime type checking).
   314     ///throws an exception (i.e. it performs runtime type checking).
   312     class RefType
   315     class RefType
   313     {
   316     {
   314       ArgParser &_parser;
   317       const ArgParser &_parser;
   315       std::string _name;
   318       std::string _name;
   316     public:
   319     public:
   317       ///\e
   320       ///\e
   318       RefType(ArgParser &p,const std::string &n) :_parser(p),_name(n) {}
   321       RefType(const ArgParser &p,const std::string &n) :_parser(p),_name(n) {}
   319       ///\e
   322       ///\e
   320       operator bool()
   323       operator bool()
   321       {
   324       {
   322         Opts::iterator i = _parser._opts.find(_name);
   325         Opts::const_iterator i = _parser._opts.find(_name);
   323         LEMON_ASSERT(i!=_parser._opts.end(),
   326         LEMON_ASSERT(i!=_parser._opts.end(),
   324                      std::string()+"Unkown option: '"+_name+"'");
   327                      std::string()+"Unkown option: '"+_name+"'");
   325         LEMON_ASSERT(i->second.type==ArgParser::BOOL,
   328         LEMON_ASSERT(i->second.type==ArgParser::BOOL,
   326                      std::string()+"'"+_name+"' is a bool option");
   329                      std::string()+"'"+_name+"' is a bool option");
   327         return *(i->second.bool_p);
   330         return *(i->second.bool_p);
   328       }
   331       }
   329       ///\e
   332       ///\e
   330       operator std::string()
   333       operator std::string()
   331       {
   334       {
   332         Opts::iterator i = _parser._opts.find(_name);
   335         Opts::const_iterator i = _parser._opts.find(_name);
   333         LEMON_ASSERT(i!=_parser._opts.end(),
   336         LEMON_ASSERT(i!=_parser._opts.end(),
   334                      std::string()+"Unkown option: '"+_name+"'");
   337                      std::string()+"Unkown option: '"+_name+"'");
   335         LEMON_ASSERT(i->second.type==ArgParser::STRING,
   338         LEMON_ASSERT(i->second.type==ArgParser::STRING,
   336                      std::string()+"'"+_name+"' is a string option");
   339                      std::string()+"'"+_name+"' is a string option");
   337         return *(i->second.string_p);
   340         return *(i->second.string_p);
   338       }
   341       }
   339       ///\e
   342       ///\e
   340       operator double()
   343       operator double()
   341       {
   344       {
   342         Opts::iterator i = _parser._opts.find(_name);
   345         Opts::const_iterator i = _parser._opts.find(_name);
   343         LEMON_ASSERT(i!=_parser._opts.end(),
   346         LEMON_ASSERT(i!=_parser._opts.end(),
   344                      std::string()+"Unkown option: '"+_name+"'");
   347                      std::string()+"Unkown option: '"+_name+"'");
   345         LEMON_ASSERT(i->second.type==ArgParser::DOUBLE ||
   348         LEMON_ASSERT(i->second.type==ArgParser::DOUBLE ||
   346                      i->second.type==ArgParser::INTEGER,
   349                      i->second.type==ArgParser::INTEGER,
   347                      std::string()+"'"+_name+"' is a floating point option");
   350                      std::string()+"'"+_name+"' is a floating point option");
   349           *(i->second.double_p) : *(i->second.int_p);
   352           *(i->second.double_p) : *(i->second.int_p);
   350       }
   353       }
   351       ///\e
   354       ///\e
   352       operator int()
   355       operator int()
   353       {
   356       {
   354         Opts::iterator i = _parser._opts.find(_name);
   357         Opts::const_iterator i = _parser._opts.find(_name);
   355         LEMON_ASSERT(i!=_parser._opts.end(),
   358         LEMON_ASSERT(i!=_parser._opts.end(),
   356                      std::string()+"Unkown option: '"+_name+"'");
   359                      std::string()+"Unkown option: '"+_name+"'");
   357         LEMON_ASSERT(i->second.type==ArgParser::INTEGER,
   360         LEMON_ASSERT(i->second.type==ArgParser::INTEGER,
   358                      std::string()+"'"+_name+"' is an integer option");
   361                      std::string()+"'"+_name+"' is an integer option");
   359         return *(i->second.int_p);
   362         return *(i->second.int_p);
   363 
   366 
   364     ///Give back the value of an option
   367     ///Give back the value of an option
   365 
   368 
   366     ///Give back the value of an option.
   369     ///Give back the value of an option.
   367     ///\sa RefType
   370     ///\sa RefType
   368     RefType operator[](const std::string &n)
   371     RefType operator[](const std::string &n) const
   369     {
   372     {
   370       return RefType(*this, n);
   373       return RefType(*this, n);
   371     }
   374     }
   372 
   375 
   373     ///Give back the non-option type arguments.
   376     ///Give back the non-option type arguments.
   374 
   377 
   375     ///Give back a reference to a vector consisting of the program arguments
   378     ///Give back a reference to a vector consisting of the program arguments
   376     ///not starting with a '-' character.
   379     ///not starting with a '-' character.
   377     std::vector<std::string> &files() { return _file_args; }
   380     const std::vector<std::string> &files() const { return _file_args; }
   378 
   381 
   379   };
   382   };
   380 }
   383 }
   381 
   384 
   382 #endif // LEMON_ARG_PARSER
   385 #endif // LEMON_ARG_PARSER