Skip to content

Commit

Permalink
remove duplication of configuration arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
Phil Tooley committed Apr 30, 2019
1 parent 9625917 commit 88a5fb9
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 22 deletions.
20 changes: 8 additions & 12 deletions src/baseconfiguration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,24 @@ const std::string ConfigurationBase::k_extension_token = "%ext%";
const std::string ConfigurationBase::k_outer_token = "%s%";
const std::string ConfigurationBase::k_inner_token = "%i%";

const config_map ConfigurationBase::default_config = {{"verbose", "false"},
const config_map ConfigurationBase::arg_options = {
{"registered", "registered.xdmf"}, {"map", "map.xdmf"}, {"lambda", "auto"}, {"mask", ""},
{"registered_h5_path", "/registered"}, {"map_h5_path", "/map"}, {"lambda_mult", "1.0"},
{"with_memory", "true"}, {"save_intermediate_frames", "false"},
{"intermediate_template", "%name%-intermediate-%s%-%i%%ext%"},
{"intermediate_map_template", "%name%-intermediate-map-%s%-%i%%ext%"},
{"intermediate_directory", "intermediates"}, {"max_iterations", "100"}};

const config_map ConfigurationBase::bool_options = {
{"verbose", "false"}, {"with_memory", "true"}, {"save_intermediate_frames", "false"}};

const std::vector<std::string> ConfigurationBase::required_options = {
"fixed", "moved", "nodespacing"};

const std::vector<std::string> ConfigurationBase::arg_options = {"fixed", "moved", "mask",
"nodespacing", "registered", "map", "lambda", "lambda_mult", "intermediate_template",
"intermediate_directory", "max_iterations", "registered_h5_path", "map_h5_path"};

const std::vector<std::string> ConfigurationBase::bool_options = {
"verbose", "with_memory", "save_intermediate_frames"};

ConfigurationBase::ConfigurationBase(const int &argc, char const *const *argv)
: config(default_config), arguments(argv + 1, argv + argc),
invocation_name(get_invocation_name(argv[0]))
: arguments(argv + 1, argv + argc), invocation_name(get_invocation_name(argv[0]))
{
config.insert(arg_options.cbegin(), arg_options.cend());
config.insert(bool_options.cbegin(), bool_options.cend());
}

void ConfigurationBase::validate_config()
Expand All @@ -59,7 +55,7 @@ void ConfigurationBase::validate_config()
missing.push_back(req_it);
}
}
if (missing.size() > 0)
if (missing.empty())
{
std::ostringstream errmsg;
errmsg << "Missing required argument(s) \"";
Expand Down
5 changes: 2 additions & 3 deletions src/baseconfiguration.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,9 @@ class ConfigurationBase {
std::vector<std::string> arguments;
std::string invocation_name;

static const config_map default_config;
static const std::vector<std::string> required_options;
static const std::vector<std::string> arg_options;
static const std::vector<std::string> bool_options;
static const config_map arg_options;
static const config_map bool_options;
};

#endif // CONFIGURATION_HPP
8 changes: 4 additions & 4 deletions src/iniconfiguration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ void IniConfig::parse_arguments()
std::exit(0);
}

if (vm.count("help") || !vm.count("config-file"))
if (vm.count("help") > 0 || vm.count("config-file") != 1 )
{
// TODO improve to iterate over options
std::cout << usage() << std::endl << cmdline_visible << std::endl;
Expand Down Expand Up @@ -112,11 +112,11 @@ void IniConfig::read_config_file(const std::string &config_path)
for (const auto &it : config_data)
{
std::string key = ba::to_lower_copy(it.first);
if (std::find(arg_options.cbegin(), arg_options.cend(), key) != arg_options.cend())
if (arg_options.find(key) != arg_options.cend())
{
config[key] = it.second.data();
}
else if (std::find(bool_options.cbegin(), bool_options.cend(), key) != bool_options.cend())
else if (bool_options.find(key) != bool_options.cend())
{
config[key] = boost::to_lower_copy(it.second.data());
}
Expand All @@ -126,7 +126,7 @@ void IniConfig::read_config_file(const std::string &config_path)
}
}

if (unknowns.size() > 0)
if (!unknowns.empty())
{
std::ostringstream unkss;
std::copy(unknowns.begin(), unknowns.end(), std::ostream_iterator<std::string>(unkss, ", "));
Expand Down
5 changes: 2 additions & 3 deletions src/shirtemulation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ void ShirtConfig::parse_arguments()
}

// now check option taking arguments
if (std::find(arg_options.cbegin(), arg_options.cend(), arg_lower) != arg_options.cend())
if (arg_options.find(arg_lower) != arg_options.cend())
{
// try to get associated option
if (args_it == arguments.cend())
Expand All @@ -95,8 +95,7 @@ void ShirtConfig::parse_arguments()
// finally insert arg-val pair into the argument map
config[arg_lower] = optval;
}
else if (
std::find(bool_options.cbegin(), bool_options.cend(), arg_lower) != bool_options.cend())
else if (bool_options.find(arg_lower) != bool_options.cend())
{
// insert arg-val pair into argument map
config[arg_lower] = "true";
Expand Down

0 comments on commit 88a5fb9

Please sign in to comment.