diff --git a/BeezleBot/BeezleBot.cpp b/BeezleBot/BeezleBot.cpp index dc31d5a..3a86265 100644 --- a/BeezleBot/BeezleBot.cpp +++ b/BeezleBot/BeezleBot.cpp @@ -1,7 +1,28 @@ +#include "settings.h" + #include -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; } diff --git a/BeezleBot/CMakeLists.txt b/BeezleBot/CMakeLists.txt index 28537ed..81f0929 100644 --- a/BeezleBot/CMakeLists.txt +++ b/BeezleBot/CMakeLists.txt @@ -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} diff --git a/BeezleBot/settings.cpp b/BeezleBot/settings.cpp new file mode 100644 index 0000000..97e8bfa --- /dev/null +++ b/BeezleBot/settings.cpp @@ -0,0 +1,62 @@ +#include "settings.h" + +#include + +#include +#include +#include + +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(), "read options from config file") + ("token,t", po::value(&token)->required(), "Telegram bot token") + ("users,u", po::value>(&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()); + + po::options_description fileOptions; + fileOptions.add_options() + ("token", po::value(&token)) + ("users", po::value>(&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"); + } +} diff --git a/BeezleBot/settings.h b/BeezleBot/settings.h new file mode 100644 index 0000000..32b3c49 --- /dev/null +++ b/BeezleBot/settings.h @@ -0,0 +1,13 @@ +#pragma once + +#include +#include + +struct Settings +{ + std::string token; + std::vector users; + + Settings(int argc, char **argv); +}; +