move program options for routed/simpleclient into common header
This commit is contained in:
+206
-5
@@ -24,16 +24,25 @@ Custom validators for use with boost::program_options.
|
||||
#ifndef PROGAM_OPTIONS_H
|
||||
#define PROGAM_OPTIONS_H
|
||||
|
||||
#include "GitDescription.h"
|
||||
#include "OSRMException.h"
|
||||
#include "SimpleLogger.h"
|
||||
|
||||
#include <boost/any.hpp>
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <boost/program_options.hpp>
|
||||
#include <boost/regex.hpp>
|
||||
#include <boost/unordered_map.hpp>
|
||||
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
typedef boost::unordered_map<
|
||||
const std::string,
|
||||
boost::filesystem::path
|
||||
> ServerPaths;
|
||||
|
||||
namespace boost {
|
||||
namespace filesystem {
|
||||
// Validator for boost::filesystem::path, that verifies that the file
|
||||
@@ -58,14 +67,206 @@ namespace boost {
|
||||
}
|
||||
}
|
||||
|
||||
//support old capitalized option names by downcasing them with a regex replace
|
||||
//read from file and store in a stringstream that can be passed to boost::program_options
|
||||
inline void PrepareConfigFile(const boost::filesystem::path& path, std::string& output ) {
|
||||
std::ifstream config_stream(path.c_str());
|
||||
std::string input_str( (std::istreambuf_iterator<char>(config_stream)), std::istreambuf_iterator<char>() );
|
||||
// support old capitalized option names by downcasing them with a regex replace
|
||||
// read from file and store in a stringstream that can be passed to
|
||||
// boost::program_options
|
||||
inline void PrepareConfigFile(
|
||||
const boost::filesystem::path& path,
|
||||
std::string& output
|
||||
) {
|
||||
std::ifstream config_stream(path.string().c_str());
|
||||
std::string input_str(
|
||||
(std::istreambuf_iterator<char>(config_stream)),
|
||||
std::istreambuf_iterator<char>()
|
||||
);
|
||||
boost::regex regex( "^([^=]*)" ); //match from start of line to '='
|
||||
std::string format( "\\L$1\\E" ); //replace with downcased substring
|
||||
output = boost::regex_replace( input_str, regex, format );
|
||||
}
|
||||
|
||||
|
||||
// generate boost::program_options object for the routing part
|
||||
bool GenerateServerProgramOptions(
|
||||
const int argc,
|
||||
const char * argv[],
|
||||
ServerPaths & paths,
|
||||
std::string & ip_address,
|
||||
int & ip_port,
|
||||
int & requested_num_threads
|
||||
) {
|
||||
|
||||
// declare a group of options that will be allowed only on command line
|
||||
boost::program_options::options_description generic_options("Options");
|
||||
generic_options.add_options()
|
||||
("version,v", "Show version")
|
||||
("help,h", "Show this help message")
|
||||
(
|
||||
"config,c",
|
||||
boost::program_options::value<boost::filesystem::path>(
|
||||
&paths["config"]
|
||||
)->default_value("server.ini"),
|
||||
"Path to a configuration file"
|
||||
);
|
||||
|
||||
// declare a group of options that will be allowed both on command line
|
||||
// as well as in a config file
|
||||
boost::program_options::options_description config_options("Configuration");
|
||||
config_options.add_options()
|
||||
(
|
||||
"hsgrdata",
|
||||
boost::program_options::value<boost::filesystem::path>(&paths["hsgrdata"]),
|
||||
".hsgr file"
|
||||
)
|
||||
(
|
||||
"nodesdata",
|
||||
boost::program_options::value<boost::filesystem::path>(&paths["nodesdata"]),
|
||||
".nodes file"
|
||||
)
|
||||
(
|
||||
"edgesdata",
|
||||
boost::program_options::value<boost::filesystem::path>(&paths["edgesdata"]),
|
||||
".edges file")
|
||||
(
|
||||
"ramindex",
|
||||
boost::program_options::value<boost::filesystem::path>(&paths["ramindex"]),
|
||||
".ramIndex file")
|
||||
(
|
||||
"fileindex",
|
||||
boost::program_options::value<boost::filesystem::path>(&paths["fileindex"]),
|
||||
"File index file")
|
||||
(
|
||||
"namesdata",
|
||||
boost::program_options::value<boost::filesystem::path>(&paths["namesdata"]),
|
||||
".names file")
|
||||
(
|
||||
"timestamp",
|
||||
boost::program_options::value<boost::filesystem::path>(&paths["timestamp"]),
|
||||
".timestamp file")
|
||||
(
|
||||
"ip,i",
|
||||
boost::program_options::value<std::string>(&ip_address)->default_value("0.0.0.0"),
|
||||
"IP address"
|
||||
)
|
||||
(
|
||||
"port,p",
|
||||
boost::program_options::value<int>(&ip_port)->default_value(5000),
|
||||
"TCP/IP port"
|
||||
)
|
||||
(
|
||||
"threads,t",
|
||||
boost::program_options::value<int>(&requested_num_threads)->default_value(8),
|
||||
"Number of threads to use"
|
||||
);
|
||||
|
||||
// hidden options, will be allowed both on command line and in config
|
||||
// file, but will not be shown to the user
|
||||
boost::program_options::options_description hidden_options("Hidden options");
|
||||
hidden_options.add_options()
|
||||
(
|
||||
"base,b",
|
||||
boost::program_options::value<boost::filesystem::path>(&paths["base"]),
|
||||
"base path to .osrm file"
|
||||
);
|
||||
|
||||
// positional option
|
||||
boost::program_options::positional_options_description positional_options;
|
||||
positional_options.add("base", 1);
|
||||
|
||||
// combine above options for parsing
|
||||
boost::program_options::options_description cmdline_options;
|
||||
cmdline_options.add(generic_options).add(config_options).add(hidden_options);
|
||||
|
||||
boost::program_options::options_description config_file_options;
|
||||
config_file_options.add(config_options).add(hidden_options);
|
||||
|
||||
boost::program_options::options_description visible_options(
|
||||
boost::filesystem::basename(argv[0]) + " <base.osrm> [<options>]"
|
||||
);
|
||||
visible_options.add(generic_options).add(config_options);
|
||||
|
||||
// parse command line options
|
||||
boost::program_options::variables_map option_variables;
|
||||
boost::program_options::store(
|
||||
boost::program_options::command_line_parser(argc, argv).options(cmdline_options).positional(positional_options).run(),
|
||||
option_variables
|
||||
);
|
||||
|
||||
if(option_variables.count("version")) {
|
||||
SimpleLogger().Write() << g_GIT_DESCRIPTION;
|
||||
return false;
|
||||
}
|
||||
|
||||
if(option_variables.count("help")) {
|
||||
SimpleLogger().Write() << visible_options;
|
||||
return false;
|
||||
}
|
||||
|
||||
boost::program_options::notify(option_variables);
|
||||
|
||||
// parse config file
|
||||
if(boost::filesystem::is_regular_file(paths["config"])) {
|
||||
SimpleLogger().Write() <<
|
||||
"Reading options from: " << paths["config"].c_str();
|
||||
std::string config_str;
|
||||
PrepareConfigFile( paths["config"], config_str );
|
||||
std::stringstream config_stream( config_str );
|
||||
boost::program_options::store(parse_config_file(config_stream, config_file_options), option_variables);
|
||||
boost::program_options::notify(option_variables);
|
||||
}
|
||||
|
||||
if(!option_variables.count("hsgrdata")) {
|
||||
if(!option_variables.count("base")) {
|
||||
throw OSRMException("hsgrdata (or base) must be specified");
|
||||
}
|
||||
paths["hsgrdata"] = std::string( paths["base"].c_str()) + ".hsgr";
|
||||
}
|
||||
|
||||
if(!option_variables.count("nodesdata")) {
|
||||
if(!option_variables.count("base")) {
|
||||
throw OSRMException("nodesdata (or base) must be specified");
|
||||
}
|
||||
paths["nodesdata"] = std::string( paths["base"].c_str()) + ".nodes";
|
||||
}
|
||||
|
||||
if(!option_variables.count("edgesdata")) {
|
||||
if(!option_variables.count("base")) {
|
||||
throw OSRMException("edgesdata (or base) must be specified");
|
||||
}
|
||||
paths["edgesdata"] = std::string( paths["base"].c_str()) + ".edges";
|
||||
}
|
||||
|
||||
if(!option_variables.count("ramindex")) {
|
||||
if(!option_variables.count("base")) {
|
||||
throw OSRMException("ramindex (or base) must be specified");
|
||||
}
|
||||
paths["ramindex"] = std::string( paths["base"].c_str()) + ".ramIndex";
|
||||
}
|
||||
|
||||
if(!option_variables.count("fileindex")) {
|
||||
if(!option_variables.count("base")) {
|
||||
throw OSRMException("fileindex (or base) must be specified");
|
||||
}
|
||||
paths["fileindex"] = std::string( paths["base"].c_str()) + ".fileIndex";
|
||||
}
|
||||
|
||||
if(!option_variables.count("namesdata")) {
|
||||
if(!option_variables.count("base")) {
|
||||
throw OSRMException("namesdata (or base) must be specified");
|
||||
}
|
||||
paths["namesdata"] = std::string( paths["base"].c_str()) + ".names";
|
||||
}
|
||||
|
||||
if(!option_variables.count("timestamp")) {
|
||||
if(!option_variables.count("base")) {
|
||||
throw OSRMException("timestamp (or base) must be specified");
|
||||
}
|
||||
paths["timestamp"] = std::string( paths["base"].c_str()) + ".timestamp";
|
||||
}
|
||||
|
||||
if(1 > requested_num_threads) {
|
||||
throw OSRMException("Number of threads must be a positive number");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
#endif /* PROGRAM_OPTIONS_H */
|
||||
|
||||
Reference in New Issue
Block a user