commandline.hh
Go to the documentation of this file.
00001 #ifndef UTILMM_COMMANDLINE_HH
00002 #define UTILMM_COMMANDLINE_HH
00003 
00004 #include <string>
00005 #include <vector>
00006 #include <list>
00007 // #include <getopt.h>
00008 #include <iosfwd>
00009 
00010 namespace utilmm
00011 {
00012     class config_set;
00013    
00014     class bad_syntax : public std::exception
00015     {
00016     public:
00017         ~bad_syntax() throw() {}
00018 
00019         std::string source, error;
00020         bad_syntax(std::string const& source_, std::string const& error_ = "")
00021             : source(source_), error(error_) {}
00022         char const* what() const throw() { return error.c_str(); }
00023     };
00024 
00025     class commandline_error : public std::exception
00026     {
00027     public:
00028         ~commandline_error() throw() {}
00029         std::string error;
00030         commandline_error(std::string const& error_)
00031             : error(error_) {}
00032         char const* what() const throw() { return error.c_str(); }
00033     };
00034 
00039     class cmdline_option
00040     {
00041     public:
00047         enum ArgumentType
00048         {
00049             None = 0,         
00050             Optional = 1,     
00051 
00052             IntArgument = 2,    
00053             BoolArgument = 4,   
00054             StringArgument = 8,  
00055 
00056             DefaultValue = 16   
00057         };
00058 
00059     public:
00064         cmdline_option(const std::string& description);
00065         ~cmdline_option();
00066 
00067         bool        isMultiple() const;
00068         bool        isRequired() const;
00069         std::string getConfigKey() const;
00070         std::string getLong()   const;
00071         std::string getShort()  const;
00072         std::string getHelp()   const;
00073 
00074         int getArgumentFlags() const;
00075 
00076         bool hasArgument() const;
00077         bool isArgumentOptional() const;
00078 
00079         bool hasDefaultValue() const;
00080         std::string getDefaultValue() const;
00081 
00085         bool checkArgument(const std::string& value) const;
00086 
00087     private:
00088         bool m_multiple;
00089         bool m_required;
00090         std::string m_config, m_long, m_short, m_help;
00091 
00092         int  m_argument_flags;
00093         std::string m_default;
00094     };
00095 
00165     class command_line
00166     {
00167     private:
00168         typedef std::vector<cmdline_option> Options;
00169 
00170     public:
00174         command_line(const char* options[]);
00175 
00177         command_line(const std::list<std::string>& description);
00178 
00179         ~command_line();
00180 
00188         void parse(int argc, char const* const argv[], config_set& config);
00189 
00195         std::list<std::string> remaining() const;
00196 
00198         void setBanner(std::string const& banner);
00199     
00201         void usage(std::ostream& out) const;
00202 
00203     private:
00204         void add_argument(config_set& config, cmdline_option const& optdesc, std::string const& value);
00205         int option_match(config_set& config, cmdline_option const& opt, int argc, char const* const* argv, int i);
00206 
00207         std::string m_banner;
00208         Options    m_options;
00209         std::list<std::string> m_remaining;
00210     };
00211 
00212     std::ostream& operator << (std::ostream& stream, utilmm::command_line const& cmdline)
00213     { 
00214         cmdline.usage(stream);
00215         return stream;
00216     }
00217 }
00218 
00219 #endif
00220 


utilmm
Author(s): Sylvain Joyeux/sylvain.joyeux@m4x.org
autogenerated on Mon Oct 6 2014 03:17:01