Add general command line parsing code into Config::cretateFromArgs() method
This commit is contained in:
parent
712c55a9f4
commit
060510d695
10 changed files with 108 additions and 363 deletions
|
@ -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 <config_file.cfg>\nAllowed options");
|
||||
desc.add_options()
|
||||
("help", "help")
|
||||
("host,h", boost::program_options::value<std::string>(&host)->default_value(""), "Host to connect to")
|
||||
("port,p", boost::program_options::value<int>(&port)->default_value(10000), "Port to connect to")
|
||||
("config", boost::program_options::value<std::string>(&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;
|
||||
}
|
||||
|
|
|
@ -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 <config_file.cfg>\nAllowed options");
|
||||
desc.add_options()
|
||||
("help", "help")
|
||||
("host,h", boost::program_options::value<std::string>(&host)->default_value(""), "Host to connect to")
|
||||
("port,p", boost::program_options::value<int>(&port)->default_value(10000), "Port to connect to")
|
||||
("config", boost::program_options::value<std::string>(&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();
|
||||
|
|
|
@ -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 <config_file.cfg>\nAllowed options");
|
||||
desc.add_options()
|
||||
("help", "help")
|
||||
("host,h", boost::program_options::value<std::string>(&host)->default_value(""), "Host to connect to")
|
||||
("port,p", boost::program_options::value<int>(&port)->default_value(10000), "Port to connect to")
|
||||
("config", boost::program_options::value<std::string>(&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<Config>(argc, argv);
|
||||
if (!config->load(vm["config"].as<std::string>())) {
|
||||
std::cerr << "Can't load configuration file.\n";
|
||||
return 1;
|
||||
}
|
||||
config = boost::shared_ptr<Config>(cfg);
|
||||
|
||||
Logging::initBackendLogging(config.get());
|
||||
initPurple();
|
||||
|
|
|
@ -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 <config_file.cfg>\nAllowed options");
|
||||
desc.add_options()
|
||||
("help", "help")
|
||||
("host,h", boost::program_options::value<std::string>(&host)->default_value(""), "Host to connect to")
|
||||
("port,p", boost::program_options::value<int>(&port)->default_value(10000), "Port to connect to")
|
||||
("config", boost::program_options::value<std::string>(&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;
|
||||
}
|
||||
|
||||
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;
|
||||
|
|
|
@ -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 <config_file.cfg>\nAllowed options");
|
||||
desc.add_options()
|
||||
("help", "help")
|
||||
("host,h", boost::program_options::value<std::string>(&host)->default_value(""), "Host to connect to")
|
||||
("port,p", boost::program_options::value<int>(&port)->default_value(10000), "Port to connect to")
|
||||
("config", boost::program_options::value<std::string>(&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::cout << "Can't open " << argv[1] << " configuration file.\n";
|
||||
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);
|
||||
|
|
|
@ -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 <config_file.cfg>\nAllowed options");
|
||||
desc.add_options()
|
||||
("help", "help")
|
||||
("host,h", boost::program_options::value<std::string>(&host)->default_value(""), "Host to connect to")
|
||||
("port,p", boost::program_options::value<int>(&port)->default_value(10000), "Port to connect to")
|
||||
("config", boost::program_options::value<std::string>(&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";
|
||||
return 1;
|
||||
}
|
||||
|
||||
Logging::initBackendLogging(&config);
|
||||
|
||||
std::string error;
|
||||
StorageBackend *storageBackend = StorageBackend::createBackend(&config, error);
|
||||
Config *cfg = Config::createFromArgs(argc, argv, error, host, port);
|
||||
if (cfg == NULL) {
|
||||
std::cerr << error;
|
||||
return 1;
|
||||
}
|
||||
|
||||
Logging::initBackendLogging(cfg);
|
||||
|
||||
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;
|
||||
|
|
|
@ -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 <config_file.cfg>\nAllowed options");
|
||||
desc.add_options()
|
||||
("help", "help")
|
||||
("host,h", boost::program_options::value<std::string>(&host)->default_value(""), "Host to connect to")
|
||||
("port,p", boost::program_options::value<int>(&port)->default_value(10000), "Port to connect to")
|
||||
("config", boost::program_options::value<std::string>(&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;
|
||||
}
|
||||
|
||||
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;
|
||||
|
|
|
@ -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 <config_file.cfg>\nAllowed options");
|
||||
desc.add_options()
|
||||
("help", "help")
|
||||
("host,h", boost::program_options::value<std::string>(&host)->default_value(""), "Host to connect to")
|
||||
("port,p", boost::program_options::value<int>(&port)->default_value(10000), "Port to connect to")
|
||||
("config", boost::program_options::value<std::string>(&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;
|
||||
}
|
||||
|
||||
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;
|
||||
|
|
|
@ -106,6 +106,8 @@ class Config {
|
|||
|
||||
/// This signal is emitted when config is loaded/reloaded.
|
||||
boost::signal<void ()> onConfigReloaded;
|
||||
|
||||
static Config *createFromArgs(int argc, char **argv, std::string &error, std::string &host, int &port);
|
||||
|
||||
private:
|
||||
int m_argc;
|
||||
|
|
|
@ -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 <config_file.cfg>\nAllowed options");
|
||||
desc.add_options()
|
||||
("help", "help")
|
||||
("host,h", boost::program_options::value<std::string>(&host)->default_value(""), "Host to connect to")
|
||||
("port,p", boost::program_options::value<int>(&port)->default_value(10000), "Port to connect to")
|
||||
("config", boost::program_options::value<std::string>(&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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue