From 060510d6958ef0c86c058a994fa18fc7ec63d625 2012-09-04 14:58:04 From: Jan Kaluza Date: 2012-09-04 14:58:04 Subject: [PATCH] Add general command line parsing code into Config::cretateFromArgs() method --- diff --git a/backends/frotz/main.cpp b/backends/frotz/main.cpp index 1e8b2f6564c5900c56cde02624c21d9f26d5cd68..01f672452acfa00ad37977d2e4fff7516f4342f9 100644 --- a/backends/frotz/main.cpp +++ b/backends/frotz/main.cpp @@ -339,56 +339,19 @@ int main (int argc, char* argv[]) { return -1; } - std::string configFile; - boost::program_options::variables_map vm; - boost::program_options::options_description desc("Usage: spectrum \nAllowed options"); - desc.add_options() - ("help", "help") - ("host,h", boost::program_options::value(&host)->default_value(""), "Host to connect to") - ("port,p", boost::program_options::value(&port)->default_value(10000), "Port to connect to") - ("config", boost::program_options::value(&configFile)->default_value(""), "Config file") - ; - - try - { - boost::program_options::positional_options_description p; - p.add("config", -1); - boost::program_options::store(boost::program_options::command_line_parser(argc, argv). - options(desc).positional(p).allow_unregistered().run(), vm); - boost::program_options::notify(vm); - - if(vm.count("help")) - { - std::cout << desc << "\n"; - return 1; - } - - if(vm.count("config") == 0) { - std::cout << desc << "\n"; - return 1; - } - } - catch (std::runtime_error& e) - { - std::cout << desc << "\n"; - return 1; - } - catch (...) - { - std::cout << desc << "\n"; - return 1; - } - - Config config(argc, argv); - if (!config.load(configFile)) { - std::cerr << "Can't open " << argv[1] << " configuration file.\n"; + std::string error; + Config *cfg = Config::createFromArgs(argc, argv, error, host, port); + if (cfg == NULL) { + std::cerr << error; return 1; } Swift::SimpleEventLoop eventLoop; loop_ = &eventLoop; - np = new FrotzNetworkPlugin(&config, &eventLoop, host, port); + np = new FrotzNetworkPlugin(cfg, &eventLoop, host, port); loop_->run(); + delete cfg; + return 0; } diff --git a/backends/libcommuni/main.cpp b/backends/libcommuni/main.cpp index c059714cabf20e0c10b2f37fce646fa80b04589f..4509b9af50c331305afe5906692f594260293ff5 100644 --- a/backends/libcommuni/main.cpp +++ b/backends/libcommuni/main.cpp @@ -27,63 +27,24 @@ int main (int argc, char* argv[]) { std::string host; int port; - - std::string configFile; - boost::program_options::variables_map vm; - boost::program_options::options_description desc("Usage: spectrum \nAllowed options"); - desc.add_options() - ("help", "help") - ("host,h", boost::program_options::value(&host)->default_value(""), "Host to connect to") - ("port,p", boost::program_options::value(&port)->default_value(10000), "Port to connect to") - ("config", boost::program_options::value(&configFile)->default_value(""), "Config file") - ; - - try - { - boost::program_options::positional_options_description p; - p.add("config", -1); - boost::program_options::store(boost::program_options::command_line_parser(argc, argv). - options(desc).positional(p).allow_unregistered().run(), vm); - boost::program_options::notify(vm); - - if(vm.count("help")) - { - std::cout << desc << "\n"; - return 1; - } - - if(vm.count("config") == 0) { - std::cout << desc << "\n"; - return 1; - } - } - catch (std::runtime_error& e) - { - std::cout << desc << "\n"; - return 1; - } - catch (...) - { - std::cout << desc << "\n"; + std::string error; + Config *cfg = Config::createFromArgs(argc, argv, error, host, port); + if (cfg == NULL) { + std::cerr << error; return 1; } - Config config(argc, argv); - if (!config.load(configFile)) { - std::cerr << "Can't open " << argv[1] << " configuration file.\n"; - return 1; - } QCoreApplication app(argc, argv); - Logging::initBackendLogging(&config); + Logging::initBackendLogging(cfg); Swift::QtEventLoop eventLoop; - if (!CONFIG_HAS_KEY(&config, "service.irc_server")) { - np = new IRCNetworkPlugin(&config, &eventLoop, host, port); + if (!CONFIG_HAS_KEY(cfg, "service.irc_server")) { + np = new IRCNetworkPlugin(cfg, &eventLoop, host, port); } else { - np = new SingleIRCNetworkPlugin(&config, &eventLoop, host, port); + np = new SingleIRCNetworkPlugin(cfg, &eventLoop, host, port); } return app.exec(); diff --git a/backends/libpurple/main.cpp b/backends/libpurple/main.cpp index db76217f517330a216f187523035473de0cfd581..fc4bc886497e9520148bd8cbe76701db47821d2f 100644 --- a/backends/libpurple/main.cpp +++ b/backends/libpurple/main.cpp @@ -1641,51 +1641,14 @@ int main(int argc, char **argv) { } #endif - std::string configFile; - boost::program_options::variables_map vm; - boost::program_options::options_description desc("Usage: spectrum \nAllowed options"); - desc.add_options() - ("help", "help") - ("host,h", boost::program_options::value(&host)->default_value(""), "Host to connect to") - ("port,p", boost::program_options::value(&port)->default_value(10000), "Port to connect to") - ("config", boost::program_options::value(&configFile)->default_value(""), "Config file") - ; - - try - { - boost::program_options::positional_options_description p; - p.add("config", -1); - boost::program_options::store(boost::program_options::command_line_parser(argc, argv). - options(desc).positional(p).allow_unregistered().run(), vm); - boost::program_options::notify(vm); - - if(vm.count("help")) - { - std::cout << desc << "\n"; - return 1; - } - - if(vm.count("config") == 0) { - std::cout << desc << "\n"; - return 1; - } - } - catch (std::runtime_error& e) - { - std::cout << desc << "\n"; - return 1; - } - catch (...) - { - std::cout << desc << "\n"; + std::string error; + Config *cfg = Config::createFromArgs(argc, argv, error, host, port); + if (cfg == NULL) { + std::cerr << error; return 1; } - config = boost::make_shared(argc, argv); - if (!config->load(vm["config"].as())) { - std::cerr << "Can't load configuration file.\n"; - return 1; - } + config = boost::shared_ptr(cfg); Logging::initBackendLogging(config.get()); initPurple(); diff --git a/backends/libyahoo2/main.cpp b/backends/libyahoo2/main.cpp index d793742487c9ae034280cc828a6430b23f166919..9c1338399ed839c210d42bb8d8eac1220e61f0ee 100644 --- a/backends/libyahoo2/main.cpp +++ b/backends/libyahoo2/main.cpp @@ -741,60 +741,21 @@ int main (int argc, char* argv[]) { } #endif - std::string configFile; - boost::program_options::variables_map vm; - boost::program_options::options_description desc("Usage: spectrum \nAllowed options"); - desc.add_options() - ("help", "help") - ("host,h", boost::program_options::value(&host)->default_value(""), "Host to connect to") - ("port,p", boost::program_options::value(&port)->default_value(10000), "Port to connect to") - ("config", boost::program_options::value(&configFile)->default_value(""), "Config file") - ; - - try - { - boost::program_options::positional_options_description p; - p.add("config", -1); - boost::program_options::store(boost::program_options::command_line_parser(argc, argv). - options(desc).positional(p).allow_unregistered().run(), vm); - boost::program_options::notify(vm); - - if(vm.count("help")) - { - std::cout << desc << "\n"; - return 1; - } - - if(vm.count("config") == 0) { - std::cout << desc << "\n"; - return 1; - } - } - catch (std::runtime_error& e) - { - std::cout << desc << "\n"; - return 1; - } - catch (...) - { - std::cout << desc << "\n"; - return 1; - } - - Config config(argc, argv); - if (!config.load(configFile)) { - std::cerr << "Can't open " << argv[1] << " configuration file.\n"; + std::string error; + Config *cfg = Config::createFromArgs(argc, argv, error, host, port); + if (cfg == NULL) { + std::cerr << error; return 1; } - Logging::initBackendLogging(&config); + Logging::initBackendLogging(cfg); register_callbacks(); yahoo_set_log_level(YAHOO_LOG_DEBUG); Swift::SimpleEventLoop eventLoop; loop_ = &eventLoop; - np = new YahooPlugin(&config, &eventLoop, host, port); + np = new YahooPlugin(cfg, &eventLoop, host, port); loop_->run(); return 0; diff --git a/backends/skype/main.cpp b/backends/skype/main.cpp index 180e91824b02f977beb131fa6272c688db637f77..99d77bc271bb4afc6a0dd625e13a45d236839ae9 100644 --- a/backends/skype/main.cpp +++ b/backends/skype/main.cpp @@ -836,54 +836,14 @@ int main(int argc, char **argv) { } #endif - std::string configFile; - boost::program_options::variables_map vm; - boost::program_options::options_description desc("Usage: spectrum \nAllowed options"); - desc.add_options() - ("help", "help") - ("host,h", boost::program_options::value(&host)->default_value(""), "Host to connect to") - ("port,p", boost::program_options::value(&port)->default_value(10000), "Port to connect to") - ("config", boost::program_options::value(&configFile)->default_value(""), "Config file") - ; - - try - { - boost::program_options::positional_options_description p; - p.add("config", -1); - boost::program_options::store(boost::program_options::command_line_parser(argc, argv). - options(desc).positional(p).allow_unregistered().run(), vm); - boost::program_options::notify(vm); - - if(vm.count("help")) - { - std::cout << desc << "\n"; - return 1; - } - - if(vm.count("config") == 0) { - std::cout << desc << "\n"; - return 1; - } - } - catch (std::runtime_error& e) - { - std::cout << desc << "\n"; - return 1; - } - catch (...) - { - std::cout << desc << "\n"; - return 1; - } - - - Config config(argc, argv); - if (!config.load(configFile)) { - std::cout << "Can't open " << argv[1] << " configuration file.\n"; + std::string error; + Config *cfg = Config::createFromArgs(argc, argv, error, host, port); + if (cfg == NULL) { + std::cerr << error; return 1; } - Logging::initBackendLogging(&config); + Logging::initBackendLogging(cfg); g_type_init(); @@ -896,7 +856,7 @@ int main(int argc, char **argv) { channel = g_io_channel_unix_new(m_sock); g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, cond, transportDataReceived, NULL, io_destroy); - np = new SpectrumNetworkPlugin(&config, host, port); + np = new SpectrumNetworkPlugin(cfg, host, port); GMainLoop *m_loop; m_loop = g_main_loop_new(NULL, FALSE); diff --git a/backends/smstools3/main.cpp b/backends/smstools3/main.cpp index 16b172adaa8acca44ff13e4cf97738c0c434070f..31a22b78381250d3cd1d1eb6907cb8249345a726 100644 --- a/backends/smstools3/main.cpp +++ b/backends/smstools3/main.cpp @@ -260,56 +260,16 @@ int main (int argc, char* argv[]) { return -1; } - std::string configFile; - boost::program_options::variables_map vm; - boost::program_options::options_description desc("Usage: spectrum \nAllowed options"); - desc.add_options() - ("help", "help") - ("host,h", boost::program_options::value(&host)->default_value(""), "Host to connect to") - ("port,p", boost::program_options::value(&port)->default_value(10000), "Port to connect to") - ("config", boost::program_options::value(&configFile)->default_value(""), "Config file") - ; - - try - { - boost::program_options::positional_options_description p; - p.add("config", -1); - boost::program_options::store(boost::program_options::command_line_parser(argc, argv). - options(desc).positional(p).allow_unregistered().run(), vm); - boost::program_options::notify(vm); - - if(vm.count("help")) - { - std::cout << desc << "\n"; - return 1; - } - - if(vm.count("config") == 0) { - std::cout << desc << "\n"; - return 1; - } - } - catch (std::runtime_error& e) - { - std::cout << desc << "\n"; - return 1; - } - catch (...) - { - std::cout << desc << "\n"; - return 1; - } - - Config config(argc, argv); - if (!config.load(configFile)) { - std::cerr << "Can't open " << argv[1] << " configuration file.\n"; + std::string error; + Config *cfg = Config::createFromArgs(argc, argv, error, host, port); + if (cfg == NULL) { + std::cerr << error; return 1; } - Logging::initBackendLogging(&config); + Logging::initBackendLogging(cfg); - std::string error; - StorageBackend *storageBackend = StorageBackend::createBackend(&config, error); + StorageBackend *storageBackend = StorageBackend::createBackend(cfg, error); if (storageBackend == NULL) { if (!error.empty()) { std::cerr << error << "\n"; @@ -323,7 +283,7 @@ int main (int argc, char* argv[]) { Swift::SimpleEventLoop eventLoop; loop_ = &eventLoop; - np = new SMSNetworkPlugin(&config, &eventLoop, host, port); + np = new SMSNetworkPlugin(cfg, &eventLoop, host, port); loop_->run(); return 0; diff --git a/backends/swiften/main.cpp b/backends/swiften/main.cpp index 65b3fe4f03c4556852c209b170bca8c54b33790f..d9199aeb3dbfba5abb8ac1ff3cedc42659c59e1d 100644 --- a/backends/swiften/main.cpp +++ b/backends/swiften/main.cpp @@ -264,57 +264,18 @@ int main (int argc, char* argv[]) { } #endif - std::string configFile; - boost::program_options::variables_map vm; - boost::program_options::options_description desc("Usage: spectrum \nAllowed options"); - desc.add_options() - ("help", "help") - ("host,h", boost::program_options::value(&host)->default_value(""), "Host to connect to") - ("port,p", boost::program_options::value(&port)->default_value(10000), "Port to connect to") - ("config", boost::program_options::value(&configFile)->default_value(""), "Config file") - ; - - try - { - boost::program_options::positional_options_description p; - p.add("config", -1); - boost::program_options::store(boost::program_options::command_line_parser(argc, argv). - options(desc).positional(p).allow_unregistered().run(), vm); - boost::program_options::notify(vm); - - if(vm.count("help")) - { - std::cout << desc << "\n"; - return 1; - } - - if(vm.count("config") == 0) { - std::cout << desc << "\n"; - return 1; - } - } - catch (std::runtime_error& e) - { - std::cout << desc << "\n"; - return 1; - } - catch (...) - { - std::cout << desc << "\n"; - return 1; - } - - Config config(argc, argv); - if (!config.load(configFile)) { - std::cerr << "Can't open " << argv[1] << " configuration file.\n"; + std::string error; + Config *cfg = Config::createFromArgs(argc, argv, error, host, port); + if (cfg == NULL) { + std::cerr << error; return 1; } - Logging::initBackendLogging(&config); + Logging::initBackendLogging(cfg); Swift::SimpleEventLoop eventLoop; loop_ = &eventLoop; - np = new SwiftenPlugin(&config, &eventLoop, host, port); + np = new SwiftenPlugin(cfg, &eventLoop, host, port); loop_->run(); return 0; diff --git a/backends/template/main.cpp b/backends/template/main.cpp index 730ece10d395d8e027e54586f471fc98f777a00e..06d2d1ba126a105d96d96c1ef52418be7cd9ba19 100644 --- a/backends/template/main.cpp +++ b/backends/template/main.cpp @@ -113,57 +113,19 @@ int main (int argc, char* argv[]) { return -1; } #endif - std::string configFile; - boost::program_options::variables_map vm; - boost::program_options::options_description desc("Usage: spectrum \nAllowed options"); - desc.add_options() - ("help", "help") - ("host,h", boost::program_options::value(&host)->default_value(""), "Host to connect to") - ("port,p", boost::program_options::value(&port)->default_value(10000), "Port to connect to") - ("config", boost::program_options::value(&configFile)->default_value(""), "Config file") - ; - - try - { - boost::program_options::positional_options_description p; - p.add("config", -1); - boost::program_options::store(boost::program_options::command_line_parser(argc, argv). - options(desc).positional(p).allow_unregistered().run(), vm); - boost::program_options::notify(vm); - - if(vm.count("help")) - { - std::cout << desc << "\n"; - return 1; - } - - if(vm.count("config") == 0) { - std::cout << desc << "\n"; - return 1; - } - } - catch (std::runtime_error& e) - { - std::cout << desc << "\n"; - return 1; - } - catch (...) - { - std::cout << desc << "\n"; - return 1; - } - Config config(argc, argv); - if (!config.load(configFile)) { - std::cerr << "Can't open " << argv[1] << " configuration file.\n"; + std::string error; + Config *cfg = Config::createFromArgs(argc, argv, error, host, port); + if (cfg == NULL) { + std::cerr << error; return 1; } - Logging::initBackendLogging(&config); + Logging::initBackendLogging(cfg); Swift::SimpleEventLoop eventLoop; loop_ = &eventLoop; - np = new TemplatePlugin(&config, &eventLoop, host, port); + np = new TemplatePlugin(cfg, &eventLoop, host, port); loop_->run(); return 0; diff --git a/include/transport/config.h b/include/transport/config.h index dd76bc7be81a0c43f77075b863bb76b75d83ebfc..01c0786b4bfdcf50f760d7b7ec693fb13b98722b 100644 --- a/include/transport/config.h +++ b/include/transport/config.h @@ -106,6 +106,8 @@ class Config { /// This signal is emitted when config is loaded/reloaded. boost::signal onConfigReloaded; + + static Config *createFromArgs(int argc, char **argv, std::string &error, std::string &host, int &port); private: int m_argc; diff --git a/src/config.cpp b/src/config.cpp index a5f670b289469c80cae71a746a4fbfbac92462f6..552c9ff9d233c08dcae79a8b5f1eab721227963b 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -278,4 +278,56 @@ std::string Config::getCommandLineArgs() const { return commandLineArgs.str(); } +Config *Config::createFromArgs(int argc, char **argv, std::string &error, std::string &host, int &port) { + std::ostringstream os; + std::string configFile; + boost::program_options::variables_map vm; + boost::program_options::options_description desc("Usage: spectrum \nAllowed options"); + desc.add_options() + ("help", "help") + ("host,h", boost::program_options::value(&host)->default_value(""), "Host to connect to") + ("port,p", boost::program_options::value(&port)->default_value(10000), "Port to connect to") + ("config", boost::program_options::value(&configFile)->default_value(""), "Config file") + ; + + os << desc; + try + { + boost::program_options::positional_options_description p; + p.add("config", -1); + boost::program_options::store(boost::program_options::command_line_parser(argc, argv). + options(desc).positional(p).allow_unregistered().run(), vm); + boost::program_options::notify(vm); + + if(vm.count("help")) + { + error = os.str(); + return NULL; + } + + if(vm.count("config") == 0) { + error = os.str(); + return NULL; + } + } + catch (std::runtime_error& e) + { + error = os.str(); + return NULL; + } + catch (...) + { + error = os.str(); + return NULL; + } + + Config *config = new Config(argc, argv); + if (!config->load(configFile)) { + error = "Can't open " + configFile + " configuration file.\n"; + delete config; + return NULL; + } + return config; +} + }