I'm using boost::program_options
in one of my applications. With it, I fill almost all of my classes parameters.
Since I need to pass "options" to different classes, I was declaring variable_map
in the global scope and passing it as extern
among class header files.
Something like this (main.cpp):
boost::program_options::variables_map vm;
int main(int argc, char* argv[]) {
using namespace boost::program_options;
try {
options_description desc("Allowed options");
desc.add_options()
("help,h", "produce help message")
("control-port,c", value<uint16_t>()->implicit_value(6016),
"The remote TCP port used for listen to probe control connections")
("data-port", value<uint16_t>()->implicit_value(6026),
"The remote TCP port used for listen to probe data connections")
("db-host", value<std::string>(),
"The host name or address of MySQL database")
("db-port", value<uint16_t>(), "The port of MySQL database"
("db-user", value<std::string>(),
"The username used to connect to MySQL database")
("db-pass", value<std::string>(),
"The password used to connect to MySQL database")
("db-schema", value<std::string>(), "The database scheme used");
...
(DbConnector.hpp)
extern boost::program_options::variables_map vm;
(DbConnector.cpp)
...
DbConnector::DbConnector(const std::string& ownerName_) :
ownerName(ownerName_) {
dbHost = vm["db-host"].as<std::string>();
dbPort = vm["db-port"].as<uint16_t>();
dbUser = vm["db-user"].as<std::string>();
dbPass = vm["db-pass"].as<std::string>();
dbSchema = vm["db-schema"].as<std::string>();
...
And so on, for other classes..
I was wondering if there is a better way to achieve this, maybe avoiding the use of extern
keyword.