lemon/arg_parser.h
changeset 204 77d56a21c3ab
parent 108 889d0c289d19
child 209 765619b7cbb2
equal deleted inserted replaced
6:dfe22f56f005 7:110b85270adf
   116 		    const std::string &help,
   116 		    const std::string &help,
   117 		    void (*func)(void *),void *data);
   117 		    void (*func)(void *),void *data);
   118     
   118     
   119   public:
   119   public:
   120 
   120 
   121     ///\e
   121     ///Constructor
   122     ArgParser(int argc, const char **argv);
   122     ArgParser(int argc, const char **argv);
   123 
   123 
   124     ~ArgParser();
   124     ~ArgParser();
   125 
   125 
       
   126     ///\name Options
       
   127     ///
       
   128 
       
   129     ///@{
       
   130 
   126     ///Add a new integer type option
   131     ///Add a new integer type option
   127 
   132 
       
   133     ///Add a new integer type option.
   128     ///\param name The name of the option. The leading '-' must be omitted.
   134     ///\param name The name of the option. The leading '-' must be omitted.
   129     ///\param help A help string.
   135     ///\param help A help string.
   130     ///\param value A default value for the option.
   136     ///\param value A default value for the option.
   131     ///\param obl Indicate if the option is mandatory.
   137     ///\param obl Indicate if the option is mandatory.
   132     ArgParser &intOption(const std::string &name,
   138     ArgParser &intOption(const std::string &name,
   133 		    const std::string &help,
   139 		    const std::string &help,
   134 		    int value=0, bool obl=false);
   140 		    int value=0, bool obl=false);
   135 
   141 
   136     ///Add a new floating point type option
   142     ///Add a new floating point type option
   137 
   143 
       
   144     ///Add a new floating point type option.
   138     ///\param name The name of the option. The leading '-' must be omitted.
   145     ///\param name The name of the option. The leading '-' must be omitted.
   139     ///\param help A help string.
   146     ///\param help A help string.
   140     ///\param value A default value for the option.
   147     ///\param value A default value for the option.
   141     ///\param obl Indicate if the option is mandatory.
   148     ///\param obl Indicate if the option is mandatory.
   142     ArgParser &doubleOption(const std::string &name,
   149     ArgParser &doubleOption(const std::string &name,
   143 		      const std::string &help,
   150 		      const std::string &help,
   144 		      double value=0, bool obl=false);
   151 		      double value=0, bool obl=false);
   145 
   152 
   146     ///Add a new bool type option
   153     ///Add a new bool type option
   147 
   154 
       
   155     ///Add a new bool type option.
   148     ///\param name The name of the option. The leading '-' must be omitted.
   156     ///\param name The name of the option. The leading '-' must be omitted.
   149     ///\param help A help string.
   157     ///\param help A help string.
   150     ///\param value A default value for the option.
   158     ///\param value A default value for the option.
   151     ///\param obl Indicate if the option is mandatory.
   159     ///\param obl Indicate if the option is mandatory.
   152     ///\note A mandatory bool obtion is of very little use.
   160     ///\note A mandatory bool obtion is of very little use.
   154 		      const std::string &help,
   162 		      const std::string &help,
   155 		      bool value=false, bool obl=false);
   163 		      bool value=false, bool obl=false);
   156 
   164 
   157     ///Add a new string type option
   165     ///Add a new string type option
   158 
   166 
       
   167     ///Add a new string type option.
   159     ///\param name The name of the option. The leading '-' must be omitted.
   168     ///\param name The name of the option. The leading '-' must be omitted.
   160     ///\param help A help string.
   169     ///\param help A help string.
   161     ///\param value A default value for the option.
   170     ///\param value A default value for the option.
   162     ///\param obl Indicate if the option is mandatory.
   171     ///\param obl Indicate if the option is mandatory.
   163     ArgParser &stringOption(const std::string &name,
   172     ArgParser &stringOption(const std::string &name,
   164 		      const std::string &help,
   173 		      const std::string &help,
   165 		      std::string value="", bool obl=false);
   174 		      std::string value="", bool obl=false);
   166 
   175 
   167     ///\name Options with external storage
   176     ///Give help string for non-parsed arguments.
       
   177 
       
   178     ///With this function you can give help string for non-parsed arguments.
       
   179     ///The parameter \c name will be printed in the short usage line, while
       
   180     ///\c help gives a more detailed description.
       
   181     ArgParser &other(const std::string &name,
       
   182 		     const std::string &help="");
       
   183     
       
   184     ///@}
       
   185 
       
   186     ///\name Options with External Storage
   168     ///Using this functions, the value of the option will be directly written
   187     ///Using this functions, the value of the option will be directly written
   169     ///into a variable once the option appears in the command line.
   188     ///into a variable once the option appears in the command line.
   170 
   189 
   171     ///@{
   190     ///@{
   172 
   191 
   173     ///Add a new integer type option with a storage reference
   192     ///Add a new integer type option with a storage reference
   174 
   193 
       
   194     ///Add a new integer type option with a storage reference.
   175     ///\param name The name of the option. The leading '-' must be omitted.
   195     ///\param name The name of the option. The leading '-' must be omitted.
   176     ///\param help A help string.
   196     ///\param help A help string.
   177     ///\param obl Indicate if the option is mandatory.
   197     ///\param obl Indicate if the option is mandatory.
   178     ///\retval ref The value of the argument will be written to this variable.
   198     ///\retval ref The value of the argument will be written to this variable.
   179     ArgParser &refOption(const std::string &name,
   199     ArgParser &refOption(const std::string &name,
   180 		    const std::string &help,
   200 		    const std::string &help,
   181 		    int &ref, bool obl=false);
   201 		    int &ref, bool obl=false);
   182 
   202 
   183     ///Add a new floating type option with a storage reference
   203     ///Add a new floating type option with a storage reference
   184 
   204 
       
   205     ///Add a new floating type option with a storage reference.
   185     ///\param name The name of the option. The leading '-' must be omitted.
   206     ///\param name The name of the option. The leading '-' must be omitted.
   186     ///\param help A help string.
   207     ///\param help A help string.
   187     ///\param obl Indicate if the option is mandatory.
   208     ///\param obl Indicate if the option is mandatory.
   188     ///\retval ref The value of the argument will be written to this variable.
   209     ///\retval ref The value of the argument will be written to this variable.
   189     ArgParser &refOption(const std::string &name,
   210     ArgParser &refOption(const std::string &name,
   190 		      const std::string &help,
   211 		      const std::string &help,
   191 		      double &ref, bool obl=false);
   212 		      double &ref, bool obl=false);
   192 
   213 
   193     ///Add a new bool type option with a storage reference
   214     ///Add a new bool type option with a storage reference
   194 
   215 
       
   216     ///Add a new bool type option with a storage reference.
   195     ///\param name The name of the option. The leading '-' must be omitted.
   217     ///\param name The name of the option. The leading '-' must be omitted.
   196     ///\param help A help string.
   218     ///\param help A help string.
   197     ///\param obl Indicate if the option is mandatory.
   219     ///\param obl Indicate if the option is mandatory.
   198     ///\retval ref The value of the argument will be written to this variable.
   220     ///\retval ref The value of the argument will be written to this variable.
   199     ///\note A mandatory bool obtion is of very little use.
   221     ///\note A mandatory bool obtion is of very little use.
   201 		      const std::string &help,
   223 		      const std::string &help,
   202 		      bool &ref, bool obl=false);
   224 		      bool &ref, bool obl=false);
   203 
   225 
   204     ///Add a new string type option with a storage reference
   226     ///Add a new string type option with a storage reference
   205 
   227 
       
   228     ///Add a new string type option with a storage reference.
   206     ///\param name The name of the option. The leading '-' must be omitted.
   229     ///\param name The name of the option. The leading '-' must be omitted.
   207     ///\param help A help string.
   230     ///\param help A help string.
   208     ///\param obl Indicate if the option is mandatory.
   231     ///\param obl Indicate if the option is mandatory.
   209     ///\retval ref The value of the argument will be written to this variable.
   232     ///\retval ref The value of the argument will be written to this variable.
   210     ArgParser &refOption(const std::string &name,
   233     ArgParser &refOption(const std::string &name,
   216     ///\name Option Groups and Synonyms
   239     ///\name Option Groups and Synonyms
   217     ///
   240     ///
   218     
   241     
   219     ///@{
   242     ///@{
   220 
   243 
   221     ///Boundle some options into a group
   244     ///Bundle some options into a group
   222 
   245 
   223     /// You can group some option by calling this function repeatedly for each
   246     /// You can group some option by calling this function repeatedly for each
   224     /// option to be grouped with the same groupname.
   247     /// option to be grouped with the same groupname.
   225     ///\param group The group name.
   248     ///\param group The group name.
   226     ///\param opt The option name.
   249     ///\param opt The option name.
   228 			   const std::string &opt);
   251 			   const std::string &opt);
   229 
   252 
   230     ///Make the members of a group exclusive
   253     ///Make the members of a group exclusive
   231 
   254 
   232     ///If you call this function for a group, than at most one of them can be
   255     ///If you call this function for a group, than at most one of them can be
   233     ///given at the same time
   256     ///given at the same time.
   234     ArgParser &onlyOneGroup(const std::string &group);
   257     ArgParser &onlyOneGroup(const std::string &group);
   235   
   258   
   236     ///Make a group mandatory
   259     ///Make a group mandatory
   237 
   260 
   238     ///Using this function, at least one of the members of \c group
   261     ///Using this function, at least one of the members of \c group
   245     ///option \c opt.
   268     ///option \c opt.
   246     ArgParser &synonym(const std::string &syn,
   269     ArgParser &synonym(const std::string &syn,
   247 			   const std::string &opt);
   270 			   const std::string &opt);
   248     
   271     
   249     ///@}
   272     ///@}
   250 
       
   251     ///Give help string for non-parsed arguments.
       
   252 
       
   253     ///With this function you can give help string for non-parsed arguments.
       
   254     ///The parameter \c name will be printed in the short usage line, while
       
   255     ///\c help gives a more detailed description.
       
   256     ArgParser &other(const std::string &name,
       
   257 		     const std::string &help="");
       
   258     
       
   259     ///Give back the non-option type arguments.
       
   260 
       
   261     ///Give back a reference to a vector consisting of the program arguments
       
   262     ///not starting with a '-' character.
       
   263     std::vector<std::string> &files() { return _file_args; }
       
   264 
       
   265     ///Give back the command name (the 0th argument)
       
   266     const std::string &commandName() { return _command_name; }
       
   267 
   273 
   268     void show(std::ostream &os,Opts::iterator i);
   274     void show(std::ostream &os,Opts::iterator i);
   269     void show(std::ostream &os,Groups::iterator i);
   275     void show(std::ostream &os,Groups::iterator i);
   270     void showHelp(Opts::iterator i);
   276     void showHelp(Opts::iterator i);
   271     void showHelp(std::vector<OtherArg>::iterator i);
   277     void showHelp(std::vector<OtherArg>::iterator i);
   284     ArgParser &run() 
   290     ArgParser &run() 
   285     {
   291     {
   286       return parse();
   292       return parse();
   287     }
   293     }
   288     
   294     
       
   295     ///Give back the command name (the 0th argument)
       
   296     const std::string &commandName() { return _command_name; }
       
   297 
   289     ///Check if an opion has been given to the command.
   298     ///Check if an opion has been given to the command.
   290     bool given(std::string op) 
   299     bool given(std::string op) 
   291     {
   300     {
   292       Opts::iterator i = _opts.find(op);
   301       Opts::iterator i = _opts.find(op);
   293       return i!=_opts.end()?i->second.set:false;
   302       return i!=_opts.end()?i->second.set:false;
   358     ///\sa RefType
   367     ///\sa RefType
   359     RefType operator[](const std::string &n)
   368     RefType operator[](const std::string &n)
   360     {
   369     {
   361       return RefType(*this, n);
   370       return RefType(*this, n);
   362     }    
   371     }    
       
   372 
       
   373     ///Give back the non-option type arguments.
       
   374 
       
   375     ///Give back a reference to a vector consisting of the program arguments
       
   376     ///not starting with a '-' character.
       
   377     std::vector<std::string> &files() { return _file_args; }
   363  
   378  
   364   };
   379   };
   365 }
   380 }
   366 
   381 
   367     
   382 #endif // LEMON_ARG_PARSER
   368 
       
   369 #endif // LEMON_MAIN_PARAMS