-#include <iostream>
-
#include "configuration.h"
using namespace std;
-using namespace boost::program_options;
+
+//-----------------------------------------------------------------------------
+// Configuration
+//-----------------------------------------------------------------------------
Configuration::Configuration() :
- limit_to_notify( 0 )
+ config_file_name( "" ),
+ limit_to_notify( 0 ),
+ MIN_LIMIT_TO_NOTIFY( 0 ),
+ MAX_LIMIT_TO_NOTIFY( 50 )
{
}
{
}
-bool Configuration::read( variables_map vm )
+string Configuration::get_config_file_name() const
{
- if ( vm.count( "limit-to-notify" ) )
- {
- limit_to_notify = vm[ "limit-to-notify" ].as<int> ();
- cout << "limit-to-notify:" << limit_to_notify << endl;
- }
+ return config_file_name;
+}
- return true;
+void Configuration::set_config_file_name( std::string config_file_name )
+{
+ this->config_file_name = config_file_name;
}
uint32_t Configuration::get_limit_to_notify() const
-#include <iostream>
#include <fstream>
+#include <iostream>
#include <iterator>
#include "configurationreader.h"
using namespace std;
using namespace boost::program_options;
+//-----------------------------------------------------------------------------
+// ConfigurationReader
+//-----------------------------------------------------------------------------
+
ConfigurationReader::ConfigurationReader() :
configuration(),
- default_config_file_name( "pingcheck.cfg" )
+ VERSION_CMD_STR( "version" ),
+ VERSION_CMD_DESC( "Print the version string and exit." ),
+ HELP_CMD_STR( "help" ),
+ HELP_CMD_DESC( "Print this help and exit." ),
+ DEFAULT_CONFIG_FILE_NAME( "pingcheck.cfg" ),
+ CONFIG_FILE_CMD_STR( "config-file" ),
+ CONFIG_FILE_CMD_DESC( "Name of the configuration file." ),
+ DEFAULT_LIMIT_TO_NOTIFY( 4 ),
+ LIMIT_TO_NOTIFY_CMD_STR( "limit-to-notify" ),
+ LIMIT_TO_NOTIFY_CMD_DESC( "Limit of host that have to be down in order to notify." ),
+ HOST_CMD_STR( "host" ),
+ HOST_CMD_DESC( "Host address" )
{
}
{
}
-// A helper function to simplify the main part.
-template<class T>
- ostream& operator<<( ostream& os, const vector<T>& v )
- {
- copy( v.begin(), v.end(), ostream_iterator<T> ( cout, " " ) );
- return os;
- }
-
options_description ConfigurationReader::get_generic_options() const
{
options_description options( "Generic options" );
options.add_options()
- ( "version,v", "Print the version string and exit." )
- ( "help,h", "Print this help and exit." )
- ( "config-file", value<string>()->default_value( default_config_file_name ), "Name of the configuration file." );
+ ( VERSION_CMD_STR.c_str(), VERSION_CMD_DESC.c_str() )
+ ( HELP_CMD_STR.c_str(), HELP_CMD_DESC.c_str() )
+ ( CONFIG_FILE_CMD_STR.c_str(), value<string>()->default_value( DEFAULT_CONFIG_FILE_NAME ), CONFIG_FILE_CMD_DESC.c_str() );
return options;
}
{
options_description options( "Configuration" );
options.add_options()
- ( "limit-to-notify", value<int>()->default_value( 4 ), "Limit of host that have to be down in order to notify." )
- ( "host", value< vector<string> >(), "Host address" );
+ ( LIMIT_TO_NOTIFY_CMD_STR.c_str(), value<int>()->default_value( DEFAULT_LIMIT_TO_NOTIFY ), LIMIT_TO_NOTIFY_CMD_DESC.c_str() )
+ ( HOST_CMD_STR.c_str(), value< vector<string> >(), HOST_CMD_DESC.c_str() );
return options;
}
visible.add( generic ).add( config );
positional_options_description p;
- p.add( "host", -1 );
+ p.add( HOST_CMD_STR.c_str(), -1 );
store( command_line_parser( argc, argv ).
options( cmdline_options ).
positional( p ).run(), vm );
notify( vm );
- if ( vm.count( "help" ) )
+ if ( vm.count( HELP_CMD_STR ) )
{
cout << visible << endl;
return false;
}
- if ( vm.count( "version" ) )
+ if ( vm.count( VERSION_CMD_STR ) )
{
- cout << "pingcheck version " << VERSION_STRING << endl;
+ cout << PROJECT_NAME << " version " << VERSION_STRING << endl;
return false;
}
+
}
catch ( exception& e )
{
bool ConfigurationReader::parse_configuration_file( variables_map& vm )
{
- string config_file_name;
- if ( vm.count( "config-file" ) )
- {
- config_file_name = vm[ "config-file" ].as<string> ();
- }
+ string config_file_name = "";
+ if ( fill_configuration( vm ) )
+ config_file_name = configuration.get_config_file_name();
return parse_configuration_file( config_file_name, vm );
}
return true;
}
-bool ConfigurationReader::read( const int argc, char* argv[] )
+bool ConfigurationReader::parse(
+ const int argc,
+ char* argv[]
+)
{
variables_map vm;
bool command_line_parsed = parse_command_line( argc, argv, vm );
bool parser_success = command_line_parsed && configuration_file_parsed;
if (parser_success)
{
- return configuration.read( vm );
+ return fill_configuration( vm );
}
- else
+
+ return false;
+}
+
+bool ConfigurationReader::fill_configuration( const variables_map& vm )
+{
+ if ( vm.count( CONFIG_FILE_CMD_STR ) )
{
- return false;
+ string config_file_name = vm[ CONFIG_FILE_CMD_STR ].as<string> ();
+ configuration.set_config_file_name( config_file_name );
+
+ cout << CONFIG_FILE_CMD_STR << "=" << config_file_name << endl;
}
+
+ if ( vm.count( LIMIT_TO_NOTIFY_CMD_STR ) )
+ {
+ uint32_t limit_to_notify = vm[ LIMIT_TO_NOTIFY_CMD_STR ].as<int> ();
+ configuration.set_limit_to_notify( limit_to_notify );
+
+ cout << LIMIT_TO_NOTIFY_CMD_STR << "=" << limit_to_notify << endl;
+ }
+
+ return true;
}
-Configuration ConfigurationReader::getConfiguration() const
+Configuration ConfigurationReader::get_configuration() const
{
return configuration;
}
#include "configuration.h"
+//-----------------------------------------------------------------------------
+// ConfigurationReader
+//-----------------------------------------------------------------------------
+
class ConfigurationReader
{
public:
ConfigurationReader();
virtual ~ConfigurationReader();
- bool read( const int argc, char* argv[] );
- Configuration getConfiguration() const;
+ bool parse(
+ const int argc,
+ char* argv[]
+ );
+ Configuration get_configuration() const;
private:
boost::program_options::options_description get_generic_options() const;
boost::program_options::options_description get_configuration_options() const;
+
bool parse_command_line(
const int argc,
char* argv[],
const std::string& config_file_name,
boost::program_options::variables_map& vm
);
+ bool fill_configuration( const boost::program_options::variables_map& vm );
private:
Configuration configuration;
- const std::string default_config_file_name;
+
+ const std::string VERSION_CMD_STR;
+ const std::string VERSION_CMD_DESC;
+ const std::string HELP_CMD_STR;
+ const std::string HELP_CMD_DESC;
+ const std::string DEFAULT_CONFIG_FILE_NAME;
+ const std::string CONFIG_FILE_CMD_STR;
+ const std::string CONFIG_FILE_CMD_DESC;
+ const uint32_t DEFAULT_LIMIT_TO_NOTIFY;
+ const std::string LIMIT_TO_NOTIFY_CMD_STR;
+ const std::string LIMIT_TO_NOTIFY_CMD_DESC;
+ const std::string HOST_CMD_STR;
+ const std::string HOST_CMD_DESC;
+
};
#endif /* CONFIGURATIONREADER_H */