owlps/owlps-positioning/userinterface.cc

129 lines
2.6 KiB
C++

#include "userinterface.hh"
#include "configuration.hh"
#include <iostream>
#include <fstream>
#include <boost/program_options.hpp>
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 ;
cli_options = new po::options_description("General options") ;
file_options = new po::options_description("Parameters") ;
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<string>(),
"Alternative configuration file")
;
}
void UserInterface::fill_file_options()
{
file_options->add_options()
("server.port,l", po::value<int>()
->default_value(DEFAULT_LISTENING_PORT),
"Server listening port")
;
}
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::get_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()) ;
}