libpnicore
configuration/configuration.hpp
1 //
2 // (c) Copyright 2012 DESY, Eugen Wintersberger <eugen.wintersberger@desy.de>
3 //
4 // This file is part of libpnicore.
5 //
6 // libpnicore is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // libpnicore is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with libpnicore. If not, see <http://www.gnu.org/licenses/>.
18 //
19 // ============================================================================
20 //
21 // Created on: Dec 27, 2012
22 // Author: Eugen Wintersberger <eugen.wintersberger@desy.de>
23 //
24 
25 #pragma once
26 
27 #include <iostream>
28 #include <boost/program_options.hpp>
29 #include <boost/filesystem.hpp>
30 #include <boost/tokenizer.hpp>
31 #include "../types.hpp"
32 #include "../error/exceptions.hpp"
33 #include "config_option.hpp"
34 #include "config_argument.hpp"
35 
36 //set an abriviation for the namespace
37 namespace popts = boost::program_options;
38 
39 namespace pni{
40 namespace core{
41 
50  {
51  private:
53  popts::variables_map _vmap;
54 
56  popts::options_description _visible_opts;
57 
59  popts::options_description _hidden_opts;
60 
62  popts::positional_options_description _oargs;
63 
74  template<typename T>
75  string default_value_string(const T &v)
76  {
77  return boost::lexical_cast<string>(v);
78  }
79 
93  template<typename T>
94  string default_value_string(const std::vector<T> &v)
95  {
96  if(v.empty())
98  "Default value container is empty!");
99 
100  return boost::lexical_cast<string>(v[0]);
101  }
102  protected:
112  std::ostream &print(std::ostream &o) const;
113  public:
114  //===================constructors and destructor====================
116  configuration();
117 
119  configuration(const configuration &c) = delete;
120 
123 
124  //------------------------------------------------------------------
127 
128  //=====================public member functions======================
134  const popts::options_description &visible_options() const
135  {
136  return _visible_opts;
137  }
138 
139  //-----------------------------------------------------------------
145  const popts::options_description &hidden_options() const
146  {
147  return _hidden_opts;
148  }
149 
150  //-----------------------------------------------------------------
158  const popts::positional_options_description &arguments() const
159  {
160  return _oargs;
161  }
162 
163  //-----------------------------------------------------------------
172  const popts::variables_map &map() const
173  {
174  return _vmap;
175  }
176 
177  //-----------------------------------------------------------------
187  bool has_option(const string &name) const;
188 
189  //------------------------------------------------------------------
204  template<typename T> T value(const string &name) const
205  {
206  if(has_option(name)) return _vmap[name].as<T>();
207  {
209  "Program option ["+name+"] not passed or "
210  "inappropriate value!");
211  throw error;
212  }
213  }
214 
215  //------------------------------------------------------------------
219  template<typename T>
220  void add_option(const config_option<T> &opt,bool visible=true)
221  {
222  typedef boost::shared_ptr<popts::option_description> option_sptr;
223  //assemble the name of the option
224  string oname = opt.long_name();
225 
226  if(!opt.short_name().empty())
227  oname += ","+opt.short_name();
228 
229  //assemble the sematnic value
230  auto value =
231  popts::value<T>(const_cast<T*>(opt.external_reference()));
232  if(opt.has_default())
233  value->default_value(opt.default_value(),
235 
236  option_sptr option_ptr (new popts::option_description(oname.c_str(),
237  value,opt.description().c_str()));
238 
239  //finally we cann add the option
240  if(visible)
241  _visible_opts.add(option_ptr);
242  else
243  _hidden_opts.add(option_ptr);
244 
245  }
246 
247  //------------------------------------------------------------------
257  void add_option(const config_option<bool> &opt,bool visible=true);
258 
259  //-----------------------------------------------------------------
266  template<typename T>
268  {
269  typedef boost::shared_ptr<popts::option_description> option_sptr;
270  //assemble the name of the option
271  string oname = arg.long_name();
272 
273  //assemble the sematnic value
274  auto value =
275  popts::value<T>(const_cast<T*>(arg.external_reference()));
276 
277  option_sptr option_ptr (new popts::option_description(oname.c_str(),
278  value,arg.description().c_str()));
279 
280  //finally we cann add the option
281  _hidden_opts.add(option_ptr);
282 
283  _oargs.add(arg.long_name().c_str(),arg.position());
284  }
285 
286 
288  friend std::ostream &operator<<(std::ostream &o,const configuration &c);
289 
290  };
291 
292 //end of namespace
293 }
294 }
class describing a program option
Definition: config_option.hpp:47
popts::options_description _hidden_opts
options hidden in the help output
Definition: configuration/configuration.hpp:59
string default_value_string(const std::vector< T > &v)
get default value string
Definition: configuration/configuration.hpp:94
string default_value_string(const T &v)
get default value string
Definition: configuration/configuration.hpp:75
const popts::options_description & visible_options() const
get visible options
Definition: configuration/configuration.hpp:134
T value(const string &name) const
return the value of an option
Definition: configuration/configuration.hpp:204
popts::positional_options_description _oargs
positional arguments description
Definition: configuration/configuration.hpp:62
const popts::positional_options_description & arguments() const
get arguments
Definition: configuration/configuration.hpp:158
bool has_option(const string &name) const
check if option is there
popts::options_description _visible_opts
options visible in the help
Definition: configuration/configuration.hpp:56
#define EXCEPTION_RECORD
macro creating an instance of ExceptionRecord
Definition: exceptions.hpp:48
class describing a program argument
Definition: config_argument.hpp:50
friend std::ostream & operator<<(std::ostream &o, const configuration &c)
output operator
string long_name() const
get long name
Definition: config_option.hpp:105
configuration class
Definition: configuration/configuration.hpp:49
configuration()
default constructor
bool has_default() const
check if option has a default value
Definition: config_option.hpp:125
std::ostream & print(std::ostream &o) const
print options
index error
Definition: exceptions.hpp:437
Definition: add_op.hpp:29
void add_option(const config_option< T > &opt, bool visible=true)
add a program option
Definition: configuration/configuration.hpp:220
string short_name() const
get short name
Definition: config_option.hpp:109
const popts::variables_map & map() const
get variables map
Definition: configuration/configuration.hpp:172
const T * external_reference() const
get external refernce
Definition: config_option.hpp:121
int position() const
get short name
Definition: config_argument.hpp:89
command line option error
Definition: exceptions.hpp:766
popts::variables_map _vmap
stores the program options
Definition: configuration/configuration.hpp:53
T default_value() const
get default value
Definition: config_option.hpp:117
void add_argument(const config_argument< T > &arg)
add argument
Definition: configuration/configuration.hpp:267
type erasure for POD data
Definition: value.hpp:46
const popts::options_description & hidden_options() const
get hidden options
Definition: configuration/configuration.hpp:145
string description() const
get description
Definition: config_option.hpp:113
~configuration()
destructor
Definition: configuration/configuration.hpp:126