00001 #ifndef QPID_COMMONOPTIONS_H
00002 #define QPID_COMMONOPTIONS_H
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "qpid/Exception.h"
00026 #include <boost/program_options.hpp>
00027 #include <boost/format.hpp>
00028 #include <sstream>
00029 #include <iterator>
00030 #include <algorithm>
00031
00032 namespace qpid {
00033 namespace po=boost::program_options;
00034
00036 std::string prettyArg(const std::string&, const std::string&);
00037
00039 template <class T>
00040 class OptionValue : public po::typed_value<T> {
00041 public:
00042 OptionValue(T& value, const std::string& arg)
00043 : po::typed_value<T>(&value), argName(arg) {}
00044 std::string name() const { return argName; }
00045
00046 private:
00047 std::string argName;
00048 };
00049
00050
00057 template<class T>
00058 po::value_semantic* optValue(T& value, const char* name) {
00059 std::string valstr(boost::lexical_cast<std::string>(value));
00060 return new OptionValue<T>(value, prettyArg(name, valstr));
00061 }
00062
00066 template <class T>
00067 po::value_semantic* optValue(std::vector<T>& value, const char* name) {
00068 using namespace std;
00069 ostringstream os;
00070 copy(value.begin(), value.end(), ostream_iterator<T>(os, " "));
00071 string val=os.str();
00072 if (!val.empty())
00073 val.erase(val.end()-1);
00074 return (new OptionValue<vector<T> >(value, prettyArg(name, val)));
00075 }
00076
00078 inline po::value_semantic* optValue(bool& value) { return po::bool_switch(&value); }
00079
00116 struct Options : public po::options_description {
00117 struct Exception : public qpid::Exception {
00118 Exception(const std::string& msg) : qpid::Exception(msg) {}
00119 };
00120
00121 Options(const std::string& name=std::string());
00122
00123 boost::program_options::options_description_easy_init addOptions() {
00124 return add_options();
00125 }
00126
00132 void parse(int argc, char** argv,
00133 const std::string& configfile=std::string(),
00134 bool allowUnknown = false);
00135 };
00136
00140 struct CommonOptions : public Options {
00141 CommonOptions(const std::string& name=std::string(),
00142 const std::string& configfile=std::string());
00143 bool help;
00144 bool version;
00145 std::string config;
00146 };
00147
00148 }
00149
00150 #endif