Goby3 3.7.0
2026.11.12
Loading...
Searching...
No Matches
configurator.h
Go to the documentation of this file.
1// Copyright 2018-2026:
2// GobySoft, LLC (2013-)
3// Community contributors (see AUTHORS file)
4// File authors:
5// Toby Schneider <toby@gobysoft.org>
6//
7//
8// This file is part of the Goby Underwater Autonomy Project Libraries
9// ("The Goby Libraries").
10//
11// The Goby Libraries are free software: you can redistribute them and/or modify
12// them under the terms of the GNU Lesser General Public License as published by
13// the Free Software Foundation, either version 2.1 of the License, or
14// (at your option) any later version.
15//
16// The Goby Libraries are distributed in the hope that they will be useful,
17// but WITHOUT ANY WARRANTY; without even the implied warranty of
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19// GNU Lesser General Public License for more details.
20//
21// You should have received a copy of the GNU Lesser General Public License
22// along with Goby. If not, see <http://www.gnu.org/licenses/>.
23
24#ifndef GOBY_MIDDLEWARE_APPLICATION_CONFIGURATOR_H
25#define GOBY_MIDDLEWARE_APPLICATION_CONFIGURATOR_H
26
29
30namespace goby
31{
32namespace middleware
33{
38template <typename Config> class ConfiguratorInterface
39{
40 public:
42 const Config& cfg() const { return cfg_; }
43
46 virtual const protobuf::AppConfig& app_configuration() const { return app_configuration_; }
47
51 virtual void validate() const {}
52
55 {
56 std::cerr << "Invalid configuration: " << e.what() << std::endl;
57 }
58
60 virtual std::string str() const = 0;
61
64 virtual bool invalid_command() const { return false; }
65
66 protected:
68 Config& mutable_cfg() { return cfg_; }
69
71 virtual protobuf::AppConfig& mutable_app_configuration() { return app_configuration_; }
72
73 private:
74 Config cfg_;
75 protobuf::AppConfig app_configuration_;
76};
77
81template <typename Config> class ProtobufConfigurator : public ConfiguratorInterface<Config>
82{
83 public:
88 ProtobufConfigurator(int argc, char* argv[]);
89
90 const protobuf::AppConfig& app_configuration() const override { return this->cfg().app(); }
91
92 protected:
93 virtual void validate() const override
94 {
95 middleware::ConfigReader::check_required_cfg(this->cfg(), this->cfg().app().binary());
96 }
97
98 bool invalid_command() const override { return invalid_command_; }
99
100 private:
101 virtual std::string str() const override { return this->cfg().DebugString(); }
102
103 void handle_config_error(middleware::ConfigException& e) const override
104 {
105 if (tool_has_help_action())
106 {
107 std::cerr << "Invalid command: " << e.what() << std::endl;
108 }
109 else
110 {
111 std::cerr << "Invalid configuration: use --help and/or --example_config for more help: "
112 << e.what() << std::endl;
113 }
114 }
115
116 protobuf::AppConfig& mutable_app_configuration() override
117 {
118 return *this->mutable_cfg().mutable_app();
119 }
120
121 void merge_app_base_cfg(protobuf::AppConfig* base_cfg,
122 const boost::program_options::variables_map& var_map);
123
124 bool tool_has_help_action() const
125 {
126 return Config::descriptor()
127 ->options()
128 .GetExtension(goby::msg)
129 .cfg()
130 .tool()
131 .has_help_action();
132 }
133
134 bool invalid_command_{false};
135};
136
137template <typename Config>
139{
140 //
141 // read the configuration
142 //
143 Config& cfg = this->mutable_cfg();
144
145 boost::program_options::variables_map var_map;
146 try
147 {
148 std::string application_name;
149 std::string binary_name;
150
151 // we will check it later in validate()
152 bool check_required_cfg = false;
153 boost::program_options::options_description od{"All options"};
155 argc, argv, &cfg, &application_name, &binary_name, &od, &var_map, check_required_cfg);
156
157 // extra command line parameters for tool mode
158 for (int a = read_argc; a < argc; ++a)
159 cfg.mutable_app()->mutable_tool_cfg()->add_extra_cli_param(argv[a]);
160
161 // overwrite the app.name setting if it isn't set or if -a is set to change the application name. Otherwise, use it
162 if (!cfg.app().has_name() || application_name != binary_name)
163 cfg.mutable_app()->set_name(application_name);
164
165 cfg.mutable_app()->set_binary(binary_name);
166 // incorporate some parts of the AppBaseConfig that are middleware
167 // with gobyd (e.g. Verbosity)
168 merge_app_base_cfg(cfg.mutable_app(), var_map);
169 }
171 {
172 handle_config_error(e);
173
174 // allow default action to take place (usually "help")
175 if (!tool_has_help_action())
176 throw;
177 invalid_command_ = true;
178 }
179
180 // TODO: convert to C++ struct app3 configuration format
181 this->mutable_app_configuration() = *cfg.mutable_app();
182}
183
184template <typename Config>
187 const boost::program_options::variables_map& var_map)
188{
189 if (var_map.count("ncurses"))
190 {
191 base_cfg->mutable_glog_config()->set_show_gui(true);
192 }
193
194 if (var_map.count("verbose"))
195 {
196 switch (var_map["verbose"].as<std::size_t>())
197 {
198 case 0: break;
199 case 1:
202 break;
203 case 2:
206 break;
207 case 3:
210 break;
211 default:
212 case 4:
215 break;
216 }
217 }
218
219 if (var_map.count("glog_file_verbose"))
220 {
221 switch (var_map["glog_file_verbose"].as<std::size_t>())
222 {
223 case 0: break;
224
225 case 1:
228 break;
229 case 2:
232 break;
233 case 3:
236 break;
237 default:
238 case 4:
241 break;
242 }
243 }
244
245 if (var_map.count("glog_file_dir"))
246 {
248 var_map["glog_file_dir"].as<std::string>());
249 }
250}
251
252} // namespace middleware
253} // namespace goby
254
255#endif
indicates a problem with the runtime command line or .cfg file configuration (or –help was given)
static void check_required_cfg(const google::protobuf::Message &message, const std::string &binary)
Checks that all required fields are set (either via the command line or the configuration file) in th...
static int read_cfg(int argc, char *argv[], google::protobuf::Message *message, std::string *application_name, std::string *binary_name, boost::program_options::options_description *od_all, boost::program_options::variables_map *var_map, bool check_required_configuration=true)
Read the configuration into a Protobuf message using the command line parameters.
Defines the interface to a "configurator", a class that can read command line parameters (argc,...
virtual void validate() const
Override to validate the configuration.
Config & mutable_cfg()
Derived classes can modify the configuration as needed in their constructor.
virtual bool invalid_command() const
True if the command line was invalid and a fallback (usually help) is run instead,...
virtual protobuf::AppConfig & mutable_app_configuration()
Derived classes can modify the application configuration as needed in their constructor.
virtual void handle_config_error(middleware::ConfigException &e) const
Override to customize how ConfigException errors are handled.
virtual const protobuf::AppConfig & app_configuration() const
Subset of the configuration used to configure the Application itself.
const Config & cfg() const
The configuration object produced from the command line parameters.
virtual std::string str() const =0
Override to output the configuration object as a string.
Implementation of ConfiguratorInterface for Google Protocol buffers.
const protobuf::AppConfig & app_configuration() const override
Subset of the configuration used to configure the Application itself.
ProtobufConfigurator(int argc, char *argv[])
Constructs a ProtobufConfigurator. Typically passed as a parameter to goby::run.
virtual void validate() const override
Override to validate the configuration.
bool invalid_command() const override
True if the command line was invalid and a fallback (usually help) is run instead,...
::goby::util::protobuf::GLogConfig * mutable_glog_config()
void set_file_dir(ArgT0 &&arg0, ArgT... args)
void set_verbosity(::goby::util::protobuf::GLogConfig_Verbosity value)
static constexpr Verbosity DEBUG1
static constexpr Verbosity VERBOSE
::goby::util::protobuf::GLogConfig_FileLog * mutable_file_log()
static constexpr Verbosity DEBUG2
void set_tty_verbosity(::goby::util::protobuf::GLogConfig_Verbosity value)
static constexpr Verbosity DEBUG3
The global namespace for the Goby project.
extern ::PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< ::PROTOBUF_NAMESPACE_ID::MessageOptions, ::PROTOBUF_NAMESPACE_ID::internal::MessageTypeTraits< ::goby::GobyMessageOptions >, 11, false > msg