implemented command-line and config-file options
This commit is contained in:
parent
c904e08ded
commit
bd4cbb6872
4 changed files with 105 additions and 1 deletions
|
@ -1,7 +1,28 @@
|
|||
#include "settings.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
int main(int, char **)
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
try
|
||||
{
|
||||
Settings settings(argc, argv);
|
||||
|
||||
std::cout << "token: " << settings.token << std::endl;
|
||||
std::cout << "users: " << std::flush;
|
||||
for (const auto &user : settings.users)
|
||||
{
|
||||
std::cout << user << " ";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
catch (std::runtime_error &e)
|
||||
{
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
std::cout << "BeezleBot running" << std::endl;
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
|
|
|
@ -2,8 +2,16 @@ cmake_minimum_required(VERSION 3.5)
|
|||
|
||||
project(BeezleBot LANGUAGES CXX)
|
||||
|
||||
find_package(Boost 1.60.0 COMPONENTS program_options REQUIRED)
|
||||
|
||||
add_executable(${PROJECT_NAME}
|
||||
BeezleBot.cpp
|
||||
settings.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(${PROJECT_NAME}
|
||||
PRIVATE
|
||||
Boost::program_options
|
||||
)
|
||||
|
||||
target_compile_features(${PROJECT_NAME}
|
||||
|
|
62
BeezleBot/settings.cpp
Normal file
62
BeezleBot/settings.cpp
Normal file
|
@ -0,0 +1,62 @@
|
|||
#include "settings.h"
|
||||
|
||||
#include <boost/program_options.hpp>
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <stdexcept>
|
||||
|
||||
Settings::Settings(int argc, char **argv)
|
||||
{
|
||||
namespace po = boost::program_options;
|
||||
|
||||
po::options_description commandlineOptions("Allowed options");
|
||||
commandlineOptions.add_options()
|
||||
("help,h", "show this help message")
|
||||
("config-file,f", po::value<std::string>(), "read options from config file")
|
||||
("token,t", po::value<std::string>(&token)->required(), "Telegram bot token")
|
||||
("users,u", po::value<std::vector<std::string>>(&users)->multitoken()->required(), "allowed Telegram users")
|
||||
;
|
||||
|
||||
po::variables_map configuredOptions;
|
||||
po::store(po::command_line_parser(argc, argv).options(commandlineOptions).run(),
|
||||
configuredOptions);
|
||||
|
||||
if (configuredOptions.find("help") != configuredOptions.cend())
|
||||
{
|
||||
std::cout << commandlineOptions << std::endl;
|
||||
throw std::runtime_error("invalid options");
|
||||
}
|
||||
|
||||
if (configuredOptions.find("config-file") != configuredOptions.cend())
|
||||
{
|
||||
std::ifstream configFile(configuredOptions["config-file"].as<std::string>());
|
||||
|
||||
po::options_description fileOptions;
|
||||
fileOptions.add_options()
|
||||
("token", po::value<std::string>(&token))
|
||||
("users", po::value<std::vector<std::string>>(&users)->multitoken())
|
||||
;
|
||||
|
||||
try
|
||||
{
|
||||
po::store(po::parse_config_file(configFile, fileOptions), configuredOptions);
|
||||
}
|
||||
catch (po::unknown_option &e)
|
||||
{
|
||||
std::cout << "Error: " << e.what() << std::endl;
|
||||
throw std::runtime_error("invalid options");
|
||||
}
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
po::notify(configuredOptions);
|
||||
}
|
||||
catch (po::required_option &e)
|
||||
{
|
||||
std::cout << "Error: " << e.what() << std::endl;
|
||||
std::cout << commandlineOptions << std::endl;
|
||||
throw std::runtime_error("invalid options");
|
||||
}
|
||||
}
|
13
BeezleBot/settings.h
Normal file
13
BeezleBot/settings.h
Normal file
|
@ -0,0 +1,13 @@
|
|||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
struct Settings
|
||||
{
|
||||
std::string token;
|
||||
std::vector<std::string> users;
|
||||
|
||||
Settings(int argc, char **argv);
|
||||
};
|
||||
|
Loading…
Reference in a new issue