#include "userinterface.hh" #include "configuration.hh" #include #include #include using namespace std ; namespace po = boost::program_options ; /* *** Default value definitions *** */ #define DEFAULT_CONFIG_FILE_NAME "cfg/owlps-positioning.cfg" #define DEFAULT_LISTENING_PORT 9902 /* *** Constructors *** */ UserInterface::UserInterface(const int argc, char **argv) { cli_argument_count = argc ; cli_argument_values = argv ; config_file_name = DEFAULT_CONFIG_FILE_NAME ; try { cli_options = new po::options_description("General options") ; file_options = new po::options_description("Parameters") ; } catch (bad_alloc e) { throw ; } fill_options() ; parse_options() ; } UserInterface::~UserInterface() { delete cli_options ; delete file_options ; } /* *** Operations *** */ void UserInterface::fill_options() { fill_cli_options() ; fill_file_options() ; // File options are also accepted on the command line cli_options->add(*file_options) ; } void UserInterface::fill_cli_options() { cli_options->add_options() ("help,h", "Print help") ("config-file,f", po::value(), "Alternative configuration file") ; } void UserInterface::fill_file_options() { file_options->add_options() // Server options ("server.port,l", po::value() ->default_value(DEFAULT_LISTENING_PORT), "Server listening port.") // Input options ("input.medium,I", po::value(), "Medium from which requests are read. Allowed: CSV.") ("input.csv-file,C", po::value(), "CSV file to use for input (when input.medium = CSV).") // Log options ("log.medium,L", po::value< vector >()->composing(), "Medium to which the requests will be logged. You can specify \ this option more than once. Allowed: none, CSV. The `none` value \ completely disables logging.") ("log.csv-file", po::value(), "CSV file to use for logging (when log.medium = CSV).") ; } void UserInterface::parse_options() { parse_command_line() ; print_usage_and_exit_if_requested() ; // Was the config file name specified on the command line? if (Configuration::is_configured("config-file")) config_file_name = Configuration::string_value("config-file") ; parse_file() ; po::notify(Configuration::getw_configuration()) ; } void UserInterface::parse_command_line() const { po::store(po::parse_command_line(cli_argument_count, cli_argument_values, *cli_options), Configuration::getw_configuration()) ; } void UserInterface::print_usage_and_exit_if_requested() const { if (Configuration::is_configured("help")) { cout << *cli_options ; exit(0) ; } } void UserInterface::parse_file() const { ifstream config_file(config_file_name.c_str()) ; if (! config_file) { cerr << "Warning! Error opening input configuration file « " << config_file_name << " »! Using command line and default values…" << endl ; return ; } po::store(po::parse_config_file(config_file, *file_options), Configuration::getw_configuration()) ; }