Savarese Software Research Corporation
ServiceMainOptions.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2006-2008 Savarese Software Research Corporation
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.savarese.com/software/ApacheLicense-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00022 #ifndef __SSRC_WISP_UTILITY_SERVICE_MAIN_OPTIONS_H
00023 #define __SSRC_WISP_UTILITY_SERVICE_MAIN_OPTIONS_H
00024 
00025 #include <ssrc/wisp/utility/BasicServiceOptions.h>
00026 
00027 #include <fstream>
00028 
00029 __BEGIN_NS_SSRC_WISP_UTILITY
00030 
00031 class ServiceMainOptions : public BasicServiceOptions {
00032   boost::program_options::options_description _custom_description;
00033 
00034 protected:
00035 
00036   ServiceMainOptions() :
00037     BasicServiceOptions(), _custom_description("Wisp Service"),
00038     wisp_version(false)
00039   {
00040     using namespace boost::program_options;
00041 
00042     _custom_description.add_options()
00043       ("config,f", value<std::vector<std::string> >()->composing(),
00044        "Specify a configuration file from which to read additional options.")
00045       ("wisp-version",
00046        "Print Wisp version with which the service was compiled.");
00047 
00048     description.add(_custom_description);
00049   }
00050 
00051   virtual void notify(boost::program_options::variables_map & vm) {
00052     namespace po = boost::program_options;
00053 
00054     if(vm.count("config") > 0) {
00055       std::vector<std::string> config =
00056         vm.find("config")->second.as<std::vector<std::string> >();
00057 
00058       for(std::vector<std::string>::iterator file = config.begin(); file != config.end(); ++file) {
00059         std::ifstream ifs(file->c_str());
00060 
00061         if(ifs.is_open())
00062           po::store(po::parse_config_file(ifs, description), vm);
00063         else
00064           throw std::invalid_argument(*file);
00065       }
00066     }
00067 
00068     BasicServiceOptions::notify(vm);
00069     wisp_version = (vm.count("wisp-version") > 0);
00070   }
00071 
00072 public:
00073   bool wisp_version;
00074 
00075   virtual ~ServiceMainOptions() { }
00076 };
00077 
00078 __END_NS_SSRC_WISP_UTILITY
00079 
00080 #endif

Savarese Software Research Corporation
Copyright © 2006-2010 Savarese Software Research Corporation. All rights reserved.