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 #include <string>
00032
00033
00034 namespace qpid {
00035 namespace po=boost::program_options;
00036
00037
00038
00040 std::string prettyArg(const std::string&, const std::string&);
00041
00043 template <class T>
00044 class OptionValue : public po::typed_value<T> {
00045 public:
00046 OptionValue(T& value, const std::string& arg)
00047 : po::typed_value<T>(&value), argName(arg) {}
00048 std::string name() const { return argName; }
00049
00050 private:
00051 std::string argName;
00052 };
00053
00054
00061 template<class T>
00062 po::value_semantic* optValue(T& value, const char* name) {
00063 std::string valstr(boost::lexical_cast<std::string>(value));
00064 return new OptionValue<T>(value, prettyArg(name, valstr));
00065 }
00066
00070 template <class T>
00071 po::value_semantic* optValue(std::vector<T>& value, const char* name) {
00072 using namespace std;
00073 ostringstream os;
00074 copy(value.begin(), value.end(), ostream_iterator<T>(os, " "));
00075 string val=os.str();
00076 if (!val.empty())
00077 val.erase(val.end()-1);
00078 return (new OptionValue<vector<T> >(value, prettyArg(name, val)));
00079 }
00080
00082 inline po::value_semantic* optValue(bool& value) { return po::bool_switch(&value); }
00083
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157 #if ( BOOST_VERSION == 103200 )
00158 struct Options;
00159
00160
00161 struct
00162 options_description_less_easy_init
00163 : public po::options_description_easy_init
00164 {
00165 options_description_less_easy_init ( Options * my_owner,
00166 po::options_description * my_parents_owner
00167 )
00168 : po::options_description_easy_init(my_parents_owner)
00169 {
00170 owner = my_owner;
00171 }
00172
00173
00174 options_description_less_easy_init&
00175 operator()(char const * name,
00176 char const * description);
00177
00178
00179 options_description_less_easy_init&
00180 operator()(char const * name,
00181 const po::value_semantic* s);
00182
00183
00184 options_description_less_easy_init&
00185 operator()(const char* name,
00186 const po::value_semantic* s,
00187 const char* description);
00188
00189
00190 Options * owner;
00191 };
00192 #endif
00193
00194
00195
00196
00197
00198
00199 struct Options : public po::options_description {
00200
00201 struct Exception : public qpid::Exception {
00202 Exception(const std::string& msg) : qpid::Exception(msg) {}
00203 };
00204
00205 Options(const std::string& name=std::string());
00206
00212 void parse(int argc, char** argv,
00213 const std::string& configfile=std::string(),
00214 bool allowUnknown = false);
00215
00216
00217 #if ( BOOST_VERSION == 103200 )
00218 options_description_less_easy_init m_less_easy;
00219
00220 options_description_less_easy_init addOptions() {
00221 return m_less_easy;
00222 }
00223
00224 bool
00225 is_registered_option ( std::string s );
00226
00227 void
00228 register_names ( std::string s );
00229
00230 static std::vector<std::string> long_names;
00231 static std::vector<std::string> short_names;
00232 #else
00233 boost::program_options::options_description_easy_init addOptions() {
00234 return add_options();
00235 }
00236 #endif
00237 };
00238
00239
00240
00244 struct CommonOptions : public Options {
00245 CommonOptions(const std::string& name=std::string(),
00246 const std::string& configfile=std::string());
00247 bool help;
00248 bool version;
00249 std::string config;
00250 };
00251
00252
00253
00254
00255 }
00256
00257 #endif