RAUL 0.7.0
|
00001 /* This file is part of Raul. 00002 * Copyright (C) 2009 David Robillard <http://drobilla.net> 00003 * 00004 * Raul is free software; you can redistribute it and/or modify it under the 00005 * terms of the GNU General Public License as published by the Free Software 00006 * Foundation; either version 2 of the License, or (at your option) any later 00007 * version. 00008 * 00009 * Raul is distributed in the hope that it will be useful, but WITHOUT ANY 00010 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 00011 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details. 00012 * 00013 * You should have received a copy of the GNU General Public License along 00014 * with this program; if not, write to the Free Software Foundation, Inc., 00015 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00016 */ 00017 00018 #ifndef RAUL_CONFIGURATION_HPP 00019 #define RAUL_CONFIGURATION_HPP 00020 00021 #include <map> 00022 #include <string> 00023 #include <list> 00024 #include <ostream> 00025 #include <exception> 00026 #include "raul/Atom.hpp" 00027 00028 namespace Raul { 00029 00034 class Configuration { 00035 public: 00036 Configuration(const std::string& shortdesc, const std::string& desc) 00037 : _shortdesc(shortdesc) 00038 , _desc(desc) 00039 , _max_name_length(0) 00040 {} 00041 00042 Configuration& add( 00043 const std::string& name, 00044 char letter, 00045 const std::string& desc, 00046 const Atom::Type type, 00047 const Atom& value); 00048 00049 void print_usage(const std::string& program, std::ostream& os); 00050 00051 struct CommandLineError : public std::exception { 00052 CommandLineError(const std::string& m) : msg(m) {} 00053 ~CommandLineError() throw() {} 00054 const char* what() const throw() { return msg.c_str(); } 00055 std::string msg; 00056 }; 00057 00058 void parse(int argc, char** argv) throw (CommandLineError); 00059 00060 void print(std::ostream& os, const std::string mime_type="text/plain") const; 00061 00062 const Raul::Atom& option(const std::string& long_name); 00063 00064 private: 00065 struct Option { 00066 public: 00067 Option(const std::string& n, char l, const std::string& d, 00068 const Atom::Type type, const Raul::Atom& def) 00069 : name(n), letter(l), desc(d), type(type), default_value(def), value(def) 00070 {} 00071 00072 std::string name; 00073 char letter; 00074 std::string desc; 00075 Atom::Type type; 00076 Raul::Atom default_value; 00077 Raul::Atom value; 00078 }; 00079 00080 struct OptionNameOrder { 00081 inline bool operator()(const Option& a, const Option& b) { 00082 return a.name < b.name; 00083 } 00084 }; 00085 00086 typedef std::map<std::string, Option> Options; 00087 typedef std::map<char, std::string> ShortNames; 00088 typedef std::list<std::string> Files; 00089 00090 int set_value_from_string(Configuration::Option& option, const std::string& value) 00091 throw (Configuration::CommandLineError); 00092 00093 const std::string _shortdesc; 00094 const std::string _desc; 00095 Options _options; 00096 ShortNames _short_names; 00097 Files _files; 00098 size_t _max_name_length; 00099 }; 00100 00101 } // namespace Raul 00102 00103 #endif // RAUL_CONFIGURATION_HPP 00104