00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 #ifndef RAUL_CONFIGURATION_HPP
00019 #define RAUL_CONFIGURATION_HPP
00020 
00021 #include <exception>
00022 #include <list>
00023 #include <map>
00024 #include <ostream>
00025 #include <string>
00026 
00027 #include "raul/Atom.hpp"
00028 
00029 namespace Raul {
00030 
00035 class Configuration {
00036 public:
00037     Configuration(const std::string& shortdesc, const std::string& desc)
00038         : _shortdesc(shortdesc)
00039         , _desc(desc)
00040         , _max_name_length(0)
00041     {}
00042 
00043     Configuration& add(
00044             const std::string& name,
00045             char               letter,
00046             const std::string& desc,
00047             const Atom::Type   type,
00048             const Atom&        value);
00049 
00050     void print_usage(const std::string& program, std::ostream& os);
00051 
00052     struct CommandLineError : public std::exception {
00053         explicit CommandLineError(const std::string& m) : msg(m) {}
00054         ~CommandLineError() throw() {}
00055         const char* what() const throw() { return msg.c_str(); }
00056         std::string msg;
00057     };
00058 
00059     void parse(int argc, char** argv) throw (CommandLineError);
00060 
00061     void print(std::ostream& os, const std::string mime_type="text/plain") const;
00062 
00063     const Raul::Atom& option(const std::string& long_name);
00064 
00065 private:
00066     struct Option {
00067     public:
00068         Option(const std::string& n, char l, const std::string& d,
00069                const Atom::Type type, const Raul::Atom& def)
00070             : name(n), letter(l), desc(d), type(type), default_value(def), value(def)
00071         {}
00072 
00073         std::string name;
00074         char        letter;
00075         std::string desc;
00076         Atom::Type  type;
00077         Raul::Atom  default_value;
00078         Raul::Atom  value;
00079     };
00080 
00081     struct OptionNameOrder {
00082         inline bool operator()(const Option& a, const Option& b) {
00083             return a.name < b.name;
00084         }
00085     };
00086 
00087     typedef std::map<std::string, Option> Options;
00088     typedef std::map<char, std::string>   ShortNames;
00089     typedef std::list<std::string>        Files;
00090 
00091     int set_value_from_string(Configuration::Option& option, const std::string& value)
00092             throw (Configuration::CommandLineError);
00093 
00094     const std::string _shortdesc;
00095     const std::string _desc;
00096     Options           _options;
00097     ShortNames        _short_names;
00098     Files             _files;
00099     size_t            _max_name_length;
00100 };
00101 
00102 } 
00103 
00104 #endif // RAUL_CONFIGURATION_HPP
00105