Apache Qpid - AMQP Messaging for Java JMS, C++, Python, Ruby, and .NET Apache Qpid Documentation
Options.h
Go to the documentation of this file.
1 #ifndef QPID_COMMONOPTIONS_H
2 #define QPID_COMMONOPTIONS_H
3 
4 /*
5  *
6  * Licensed to the Apache Software Foundation (ASF) under one
7  * or more contributor license agreements. See the NOTICE file
8  * distributed with this work for additional information
9  * regarding copyright ownership. The ASF licenses this file
10  * to you under the Apache License, Version 2.0 (the
11  * "License"); you may not use this file except in compliance
12  * with the License. You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing,
17  * software distributed under the License is distributed on an
18  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19  * KIND, either express or implied. See the License for the
20  * specific language governing permissions and limitations
21  * under the License.
22  *
23  */
24 
25 #include "qpid/Exception.h"
26 
27 // Disable warnings triggered by boost.
28 #ifdef _MSC_VER
29 # pragma warning(push)
30 # pragma warning(disable : 4251 4275)
31 #endif
32 
33 #include <boost/program_options.hpp>
34 #include <boost/format.hpp>
35 
36 #ifdef _MSC_VER
37 # pragma warning(pop)
38 #endif
39 
40 #include <sstream>
41 #include <iterator>
42 #include <algorithm>
43 #include <string>
45 
46 namespace qpid {
47 namespace po=boost::program_options;
48 
49 
50 
52 QPID_COMMON_EXTERN std::string prettyArg(const std::string&, const std::string&);
53 
55 template <class T>
56 class OptionValue : public po::typed_value<T> {
57  public:
58  OptionValue(T& value, const std::string& arg)
59  : po::typed_value<T>(&value), argName(arg) {}
60  std::string name() const { return argName; }
61 
62  private:
63  std::string argName;
64 };
65 
66 
73 template<class T>
74 po::value_semantic* optValue(T& value, const char* name) {
75  std::string valstr(boost::lexical_cast<std::string>(value));
76  return new OptionValue<T>(value, prettyArg(name, valstr));
77 }
78 
82 template <class T>
83 po::value_semantic* optValue(std::vector<T>& value, const char* name) {
84  std::ostringstream os;
85  std::copy(value.begin(), value.end(), std::ostream_iterator<T>(os, " "));
86  std::string val=os.str();
87  if (!val.empty())
88  val.erase(val.end()-1); // Remove trailing " "
89  return (new OptionValue<std::vector<T> >(value, prettyArg(name, val)));
90 }
91 
93 inline po::value_semantic* optValue(bool& value) {
94 #if (BOOST_VERSION >= 103500)
95  return (new OptionValue<bool>(value, ""))->implicit_value(true);
96 #else
97  return po::bool_switch(&value);
98 #endif
99 }
100 
101 inline po::value_semantic* pure_switch(bool& value) {
102  return po::bool_switch(&value);
103 }
104 
145 struct Options : public po::options_description {
146 
147  struct Exception : public qpid::Exception {
148  Exception(const std::string& msg) : qpid::Exception(msg) {}
149  };
150 
151  QPID_COMMON_EXTERN Options(const std::string& name=std::string());
152 
158  QPID_COMMON_EXTERN void parse(int argc, char const* const* argv,
159  const std::string& configfile=std::string(),
160  bool allowUnknown = false);
161 
165  QPID_COMMON_EXTERN bool findArg(int argc, char const* const* argv,
166  const std::string& theArg);
167 
168  boost::program_options::options_description_easy_init addOptions() {
169  return add_options();
170  }
171 };
172 
173 
174 
178 struct CommonOptions : public Options {
179  QPID_COMMON_EXTERN CommonOptions(const std::string& name=std::string(),
180  const std::string& configfile=std::string(),
181  const std::string& clientConfigFile=std::string());
182  bool help;
183  bool version;
184  std::string config;
185  std::string clientConfig;
186 };
187 
188 
189 
190 
191 } // namespace qpid
192 
193 #endif
po::value_semantic * pure_switch(bool &value)
Definition: Options.h:101
std::string config
Definition: Options.h:184
CommonOptions(const std::string &name=std::string(), const std::string &configfile=std::string(), const std::string &clientConfigFile=std::string())
std::string name() const
Definition: Options.h:60
void parse(int argc, char const *const *argv, const std::string &configfile=std::string(), bool allowUnknown=false)
Parses options from argc/argv, environment variables and config file.
po::value_semantic * optValue(T &value, const char *name)
Create an option value.
Definition: Options.h:74
bool findArg(int argc, char const *const *argv, const std::string &theArg)
Tests for presence of argc/argv switch.
Exception(const std::string &msg)
Definition: Options.h:148
std::string prettyArg(const std::string &, const std::string &)
boost::program_options::options_description_easy_init addOptions()
Definition: Options.h:168
Standard options for configuration.
Definition: Options.h:178
std::string clientConfig
Definition: Options.h:185
Options(const std::string &name=std::string())
Base class for options.
Definition: Options.h:145
#define QPID_COMMON_EXTERN
OptionValue(T &value, const std::string &arg)
Definition: Options.h:58
Base class for Qpid runtime exceptions.
Definition: Exception.h:39

Qpid C++ API Reference
Generated on Wed May 28 2014 for Qpid C++ Client API by doxygen 1.8.5