Go to the documentation of this file.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
00027
00028 #ifdef _MSC_VER
00029 # pragma warning(push)
00030 # pragma warning(disable : 4251 4275)
00031 #endif
00032
00033 #include <boost/program_options.hpp>
00034 #include <boost/format.hpp>
00035
00036 #ifdef _MSC_VER
00037 # pragma warning(pop)
00038 #endif
00039
00040 #include <sstream>
00041 #include <iterator>
00042 #include <algorithm>
00043 #include <string>
00044 #include "qpid/CommonImportExport.h"
00045
00046 namespace qpid {
00047 namespace po=boost::program_options;
00048
00049
00050
00052 QPID_COMMON_EXTERN std::string prettyArg(const std::string&, const std::string&);
00053
00055 template <class T>
00056 class OptionValue : public po::typed_value<T> {
00057 public:
00058 OptionValue(T& value, const std::string& arg)
00059 : po::typed_value<T>(&value), argName(arg) {}
00060 std::string name() const { return argName; }
00061
00062 private:
00063 std::string argName;
00064 };
00065
00066
00073 template<class T>
00074 po::value_semantic* optValue(T& value, const char* name) {
00075 std::string valstr(boost::lexical_cast<std::string>(value));
00076 return new OptionValue<T>(value, prettyArg(name, valstr));
00077 }
00078
00082 template <class T>
00083 po::value_semantic* optValue(std::vector<T>& value, const char* name) {
00084 std::ostringstream os;
00085 std::copy(value.begin(), value.end(), std::ostream_iterator<T>(os, " "));
00086 std::string val=os.str();
00087 if (!val.empty())
00088 val.erase(val.end()-1);
00089 return (new OptionValue<std::vector<T> >(value, prettyArg(name, val)));
00090 }
00091
00093 inline po::value_semantic* optValue(bool& value) {
00094 #if (BOOST_VERSION >= 103500)
00095 return (new OptionValue<bool>(value, ""))->implicit_value(true);
00096 #else
00097 return po::bool_switch(&value);
00098 #endif
00099 }
00100
00101 inline po::value_semantic* pure_switch(bool& value) {
00102 return po::bool_switch(&value);
00103 }
00104
00145 struct Options : public po::options_description {
00146
00147 struct Exception : public qpid::Exception {
00148 Exception(const std::string& msg) : qpid::Exception(msg) {}
00149 };
00150
00151 QPID_COMMON_EXTERN Options(const std::string& name=std::string());
00152
00158 QPID_COMMON_EXTERN void parse(int argc, char const* const* argv,
00159 const std::string& configfile=std::string(),
00160 bool allowUnknown = false);
00161
00165 QPID_COMMON_EXTERN bool findArg(int argc, char const* const* argv,
00166 const std::string& theArg);
00167
00168 boost::program_options::options_description_easy_init addOptions() {
00169 return add_options();
00170 }
00171 };
00172
00173
00174
00178 struct CommonOptions : public Options {
00179 QPID_COMMON_EXTERN CommonOptions(const std::string& name=std::string(),
00180 const std::string& configfile=std::string(),
00181 const std::string& clientConfigFile=std::string());
00182 bool help;
00183 bool version;
00184 std::string config;
00185 std::string clientConfig;
00186 };
00187
00188
00189
00190
00191 }
00192
00193 #endif