Goby v2
configuration_reader.h
1 // Copyright 2009-2018 Toby Schneider (http://gobysoft.org/index.wt/people/toby)
2 // GobySoft, LLC (2013-)
3 // Massachusetts Institute of Technology (2007-2014)
4 // Community contributors (see AUTHORS file)
5 //
6 //
7 // This file is part of the Goby Underwater Autonomy Project Libraries
8 // ("The Goby Libraries").
9 //
10 // The Goby Libraries are free software: you can redistribute them and/or modify
11 // them under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 2.1 of the License, or
13 // (at your option) any later version.
14 //
15 // The Goby Libraries are distributed in the hope that they will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with Goby. If not, see <http://www.gnu.org/licenses/>.
22 
23 #ifndef CONFIGURATIONREADER20100929H
24 #define CONFIGURATIONREADER20100929H
25 
26 #include <fstream>
27 #include <iostream>
28 #include <sstream>
29 #include <vector>
30 
31 #include <boost/cstdint.hpp>
32 #include <boost/foreach.hpp>
33 #include <boost/program_options.hpp>
34 
35 #include <google/protobuf/descriptor.h>
36 #include <google/protobuf/descriptor.pb.h>
37 #include <google/protobuf/io/zero_copy_stream.h>
38 #include <google/protobuf/io/zero_copy_stream_impl.h>
39 #include <google/protobuf/message.h>
40 #include <google/protobuf/text_format.h>
41 
42 #include "goby/common/exception.h"
43 #include "goby/util/as.h"
44 
45 class AppBaseConfig;
46 
47 namespace goby
48 {
49 namespace common
50 {
53 class ConfigException : public Exception
54 {
55  public:
56  ConfigException(const std::string& s) : Exception(s), error_(true) {}
57 
58  void set_error(bool b) { error_ = b; }
59  bool error() { return error_; }
60 
61  private:
62  bool error_;
63 };
64 
66 {
67  public:
68  static void read_cfg(int argc, char* argv[], google::protobuf::Message* message,
69  std::string* application_name,
70  boost::program_options::options_description* od_all,
71  boost::program_options::variables_map* var_map);
72 
73  static void get_protobuf_program_options(boost::program_options::options_description& po_desc,
74  const google::protobuf::Descriptor* desc);
75 
76  static void set_protobuf_program_option(const boost::program_options::variables_map& vm,
78  const std::string& full_name,
79  const boost::program_options::variable_value& value);
80 
81  static void get_example_cfg_file(google::protobuf::Message* message,
82  std::ostream* human_desc_ss, const std::string& indent = "");
83 
84  private:
85  enum
86  {
87  MAX_CHAR_PER_LINE = 66
88  };
89  enum
90  {
91  MIN_CHAR = 20
92  };
93 
94  static void build_description(const google::protobuf::Descriptor* desc,
95  std::ostream& human_desc, const std::string& indent = "",
96  bool use_color = true);
97 
98  static void build_description_field(const google::protobuf::FieldDescriptor* desc,
99  std::ostream& human_desc, const std::string& indent,
100  bool use_color);
101 
102  static void wrap_description(std::string* description, int num_blanks);
103 
104  static std::string label(const google::protobuf::FieldDescriptor* field_desc);
105 
106  static std::string word_wrap(std::string s, unsigned width, const std::string& delim);
107 
108  template <typename T>
109  static void set_single_option(boost::program_options::options_description& po_desc,
110  const google::protobuf::FieldDescriptor* field_desc,
111  const T& default_value, const std::string& name,
112  const std::string& description)
113  {
114  if (!field_desc->is_repeated())
115  {
116  if (field_desc->has_default_value())
117  {
118  po_desc.add_options()(
119  name.c_str(), boost::program_options::value<T>()->default_value(default_value),
120  description.c_str());
121  }
122  else
123  {
124  po_desc.add_options()(name.c_str(), boost::program_options::value<T>(),
125  description.c_str());
126  }
127  }
128  else
129  {
130  if (field_desc->has_default_value())
131  {
132  po_desc.add_options()(
133  name.c_str(),
134  boost::program_options::value<std::vector<T> >()->default_value(
135  std::vector<T>(1, default_value),
136  goby::util::as<std::string>(default_value)),
137  description.c_str());
138  }
139  else
140  {
141  po_desc.add_options()(name.c_str(),
142  boost::program_options::value<std::vector<T> >(),
143  description.c_str());
144  }
145  }
146  }
147 };
148 } // namespace common
149 } // namespace goby
150 
151 #endif
indicates a problem with the runtime command line or .cfg file configuration (or –help was given) ...
The global namespace for the Goby project.
simple exception class for goby applications
Definition: exception.h:32