/usr/share/cruisecontrol-bin-2.6.1/projects/qpid-trunk/cpp/src/qpid/Options.h

00001 #ifndef QPID_COMMONOPTIONS_H
00002 #define QPID_COMMONOPTIONS_H
00003 
00004 /*
00005  *
00006  * Licensed to the Apache Software Foundation (ASF) under one
00007  * or more contributor license agreements.  See the NOTICE file
00008  * distributed with this work for additional information
00009  * regarding copyright ownership.  The ASF licenses this file
00010  * to you under the Apache License, Version 2.0 (the
00011  * "License"); you may not use this file except in compliance
00012  * with the License.  You may obtain a copy of the License at
00013  *
00014  *   http://www.apache.org/licenses/LICENSE-2.0
00015  *
00016  * Unless required by applicable law or agreed to in writing,
00017  * software distributed under the License is distributed on an
00018  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
00019  * KIND, either express or implied.  See the License for the
00020  * specific language governing permissions and limitations
00021  * under the License.
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); // Remove trailing " "
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 } // namespace qpid
00149 
00150 #endif  

Generated on Thu Apr 10 11:08:18 2008 for Qpid by  doxygen 1.4.7