lemon/arg_parser.h
changeset 89 e6452a49192c
parent 87 81e138275860
child 90 2981a73d49a3
equal deleted inserted replaced
2:cd95d6317b7e 3:039f3054d745
    28 #include <algorithm>
    28 #include <algorithm>
    29 #include <lemon/error.h>
    29 #include <lemon/error.h>
    30 
    30 
    31 ///\ingroup misc
    31 ///\ingroup misc
    32 ///\file
    32 ///\file
    33 ///\brief A tools to parse command line arguments.
    33 ///\brief A tool to parse command line arguments.
    34 ///
       
    35 ///\author Alpar Juttner
       
    36 
    34 
    37 namespace lemon {
    35 namespace lemon {
    38 
    36 
    39   ///Command line arguments parser
    37   ///Command line arguments parser
    40 
    38 
    41   ///\ingroup misc
    39   ///\ingroup misc
    42   ///Command line arguments parser
    40   ///Command line arguments parser.
    43   ///
    41   ///
       
    42   ///For a complete example see the \ref arg_parser_demo.cc demo file.
    44   class ArgParser {
    43   class ArgParser {
    45     
    44     
    46     static void _showHelp(void *p);
    45     static void _showHelp(void *p);
    47   protected:
    46   protected:
    48     
    47     
   163     ///\param obl Indicate if the option is mandatory.
   162     ///\param obl Indicate if the option is mandatory.
   164     ArgParser &stringOption(const std::string &name,
   163     ArgParser &stringOption(const std::string &name,
   165 		      const std::string &help,
   164 		      const std::string &help,
   166 		      std::string value="", bool obl=false);
   165 		      std::string value="", bool obl=false);
   167 
   166 
   168     ///\name Options with an external strorage.
   167     ///\name Options with external storage
   169     ///Using this functions, the value of the option will be directly written
   168     ///Using this functions, the value of the option will be directly written
   170     ///into a variable once the option appears in the command line.
   169     ///into a variable once the option appears in the command line.
   171 
   170 
   172     ///@{
   171     ///@{
   173 
   172 
   220     ///@{
   219     ///@{
   221 
   220 
   222     ///Boundle some options into a group
   221     ///Boundle some options into a group
   223 
   222 
   224     /// You can group some option by calling this function repeatedly for each
   223     /// You can group some option by calling this function repeatedly for each
   225     /// option to be grupped with the same groupname.
   224     /// option to be grouped with the same groupname.
   226     ///\param group The group name
   225     ///\param group The group name.
   227     ///\param opt The option name
   226     ///\param opt The option name.
   228     ArgParser &optionGroup(const std::string &group,
   227     ArgParser &optionGroup(const std::string &group,
   229 			   const std::string &opt);
   228 			   const std::string &opt);
   230 
   229 
   231     ///Make the members of a group exclusive
   230     ///Make the members of a group exclusive
   232 
   231 
   240     ///must be given.
   239     ///must be given.
   241     ArgParser &mandatoryGroup(const std::string &group);
   240     ArgParser &mandatoryGroup(const std::string &group);
   242     
   241     
   243     ///Create synonym to an option
   242     ///Create synonym to an option
   244 
   243 
   245     ///With this function you can create a sysnonym called \c sys of the
   244     ///With this function you can create a synonym \c syn of the
   246     ///option \c opt.
   245     ///option \c opt.
   247     ArgParser &synonym(const std::string &syn,
   246     ArgParser &synonym(const std::string &syn,
   248 			   const std::string &opt);
   247 			   const std::string &opt);
   249     
   248     
   250     ///@}
   249     ///@}
   251 
   250 
   252     ///Give help string for non-parsed arguments.
   251     ///Give help string for non-parsed arguments.
   253 
   252 
   254     ///With this function you can give help string for non-parsed arguments.
   253     ///With this function you can give help string for non-parsed arguments.
   255     ///the parameter \c name will be printed in the short usage line, while
   254     ///The parameter \c name will be printed in the short usage line, while
   256     ///\c help gives a more detailed description.
   255     ///\c help gives a more detailed description.
   257     ArgParser &other(const std::string &name,
   256     ArgParser &other(const std::string &name,
   258 		     const std::string &help="");
   257 		     const std::string &help="");
   259     
   258     
   260     ///Non option type arguments.
   259     ///Give back the non-option type arguments.
   261 
   260 
   262     ///Gives back a reference to a vector consisting of the program arguments
   261     ///Give back a reference to a vector consisting of the program arguments
   263     ///not starting with a '-' character.
   262     ///not starting with a '-' character.
   264     std::vector<std::string> &files() { return _file_args; }
   263     std::vector<std::string> &files() { return _file_args; }
   265 
   264 
   266     ///Give back the command name (the 0th argument)
   265     ///Give back the command name (the 0th argument)
   267     const std::string &commandName() { return _command_name; }
   266     const std::string &commandName() { return _command_name; }
   296 
   295 
   297 
   296 
   298     ///Magic type for operator[]
   297     ///Magic type for operator[]
   299     
   298     
   300     ///This is the type of the return value of ArgParser::operator[]().
   299     ///This is the type of the return value of ArgParser::operator[]().
   301     ///It automatically converts to int, double, bool or std::string if
   300     ///It automatically converts to \c int, \c double, \c bool or
   302     ///the type of the option matches, otherwise it throws an exception.
   301     ///\c std::string if the type of the option matches, otherwise it
   303     ///(i.e. it performs runtime type checking).
   302     ///throws an exception (i.e. it performs runtime type checking).
   304     class RefType 
   303     class RefType 
   305     {
   304     {
   306       ArgParser &_parser;
   305       ArgParser &_parser;
   307       std::string _name;
   306       std::string _name;
   308     public:
   307     public:
   353 
   352 
   354     };
   353     };
   355 
   354 
   356     ///Give back the value of an option
   355     ///Give back the value of an option
   357     
   356     
   358     ///Give back the value of an option
   357     ///Give back the value of an option.
   359     ///\sa RefType
   358     ///\sa RefType
   360     RefType operator[](const std::string &n)
   359     RefType operator[](const std::string &n)
   361     {
   360     {
   362       return RefType(*this, n);
   361       return RefType(*this, n);
   363     }    
   362     }