25 #ifndef GOBY_MIDDLEWARE_APPLICATION_CONFIGURATION_READER_H
26 #define GOBY_MIDDLEWARE_APPLICATION_CONFIGURATION_READER_H
32 #include <boost/lexical_cast/bad_lexical_cast.hpp>
33 #include <boost/program_options/options_description.hpp>
34 #include <boost/program_options/value_semantic.hpp>
35 #include <boost/program_options/variables_map.hpp>
36 #include <google/protobuf/descriptor.h>
80 std::string* application_name, std::string* binary_name,
81 boost::program_options::options_description* od_all,
82 boost::program_options::variables_map* var_map,
83 bool check_required_configuration =
true);
90 const std::string& binary);
94 boost::program_options::options_description>& od_map,
95 const google::protobuf::Descriptor* desc,
96 std::map<std::string, std::string>& environmental_var_map);
107 std::vector<PositionalOption>& positional_options);
111 const std::string& full_name,
112 const boost::program_options::variable_value& value,
113 bool overwrite_if_exists);
117 const std::string& indent =
"",
124 MAX_CHAR_PER_LINE = 66
132 build_description(
const google::protobuf::Descriptor* desc, std::ostream& human_desc,
133 const std::string& indent =
"",
bool use_color =
true,
138 build_description_field(
const google::protobuf::FieldDescriptor* desc, std::ostream& human_desc,
139 const std::string& indent,
bool use_color,
142 static void wrap_description(std::string* description,
int num_blanks);
144 static std::string label(
const google::protobuf::FieldDescriptor* field_desc);
146 static std::string word_wrap(std::string s,
unsigned width,
const std::string& delim);
148 template <
typename T>
149 static void set_single_option(boost::program_options::options_description& po_desc,
150 const google::protobuf::FieldDescriptor* field_desc,
151 const T& default_value,
const std::string& name,
152 const std::string& description)
154 if (!field_desc->is_repeated())
156 if (field_desc->has_default_value())
158 po_desc.add_options()(
159 name.c_str(), boost::program_options::value<T>()->default_value(default_value),
160 description.c_str());
164 po_desc.add_options()(name.c_str(), boost::program_options::value<T>(),
165 description.c_str());
170 if (field_desc->has_default_value())
172 po_desc.add_options()(
174 boost::program_options::value<std::vector<T>>()
175 ->default_value(std::vector<T>(1, default_value),
176 goby::util::as<std::string>(default_value))
178 description.c_str());
182 po_desc.add_options()(name.c_str(),
183 boost::program_options::value<std::vector<T>>()->composing(),
184 description.c_str());
191 static void set_single_option(boost::program_options::options_description& po_desc,
192 const google::protobuf::FieldDescriptor* field_desc,
193 bool default_value,
const std::string& name,
194 const std::string& description)
196 if (!field_desc->is_repeated() && field_desc->has_default_value() && default_value ==
false)
198 po_desc.add_options()(
199 name.c_str(), boost::program_options::bool_switch()->default_value(default_value),
200 description.c_str());
204 set_single_option<bool>(po_desc, field_desc, default_value, name, description);
208 static void write_usage(
const std::string& binary,
209 const std::vector<PositionalOption>& positional_options,
212 (*out) <<
"Usage: " <<
binary <<
" ";
213 for (
const auto& po : positional_options)
217 (*out) <<
"<" << po.name;
218 if (po.position_max_count > 1)
219 (*out) <<
"(" << po.position_max_count <<
")";
220 else if (po.position_max_count == -1)
227 (*out) <<
"[options]\n";