move option parsing to its own function

This commit is contained in:
Dominik Demuth 2024-11-13 08:31:08 +01:00
parent 0c6fd604fc
commit 4b8922ab55
3 changed files with 37 additions and 29 deletions

View File

@ -28,6 +28,13 @@ int main (const int argc, char *argv[]) {
parameter[key] = value; parameter[key] = value;
} }
// print parameter of simulation to inform user
std::cout << "Found parameter\n";
for (const auto& [key, value]: parameter) {
std::cout << key << ": " << std::to_string(value) << "\n";
}
std::cout << std::endl;
std::random_device rd; std::random_device rd;
std::mt19937_64 rng(rd()); std::mt19937_64 rng(rd());

View File

@ -1,6 +1,3 @@
//
// Created by dominik on 8/14/24.
//
#include "io.h" #include "io.h"
#include <sstream> #include <sstream>
@ -16,7 +13,25 @@
#include <filesystem> #include <filesystem>
std::pair<std::string, double> get_optional_parameter(std::vector<std::string>::const_iterator &it) {
std::string stripped_arg;
if (it->size() > 2 && it->at(0) == '-' && it->at(1) == '-') {
stripped_arg = it->substr(2, it->size());
} else if (it->size() > 1 && it->at(0) == '-') {
stripped_arg = it->substr(1, it->size());
}
std::transform(stripped_arg.begin(), stripped_arg.end(), stripped_arg.begin(), [](unsigned char c) { return std::tolower(c); });
const auto stripped_value = std::stod(*(++it), nullptr);
return std::make_pair(stripped_arg, stripped_value);
}
/**
* @brief Read and parse arguments coming from the command line
* @param argc Number of command-line arguments
* @param argv List of command-line arguments
* @return Arguments
*/
Arguments parse_args(const int argc, char* argv[]) { Arguments parse_args(const int argc, char* argv[]) {
if (argc < 3) { if (argc < 3) {
throw std::runtime_error("Not enough arguments: missing parameter file"); throw std::runtime_error("Not enough arguments: missing parameter file");
@ -24,44 +39,37 @@ Arguments parse_args(const int argc, char* argv[]) {
Arguments args; Arguments args;
// convert to vector to use iterator for loop
const std::vector<std::string> input_args(argv + 1, argv + argc); const std::vector<std::string> input_args(argv + 1, argv + argc);
for (auto it = input_args.begin(); it != input_args.end(); ++it) { for (auto it = input_args.begin(); it != input_args.end(); ++it) {
std::cout << *it << std::endl; // check for optional parameter
if (it->at(0) == '-') { if (it->at(0) == '-') {
// only --spectrum and --ste are parameter that are predefined
if (*it == "--spectrum") { if (*it == "--spectrum") {
args.spectrum = true; args.spectrum = true;
std::cout << "spectrum: " << args.spectrum << std::endl;
continue; continue;
} }
if (*it == "--ste") { if (*it == "--ste") {
args.ste = true; args.ste = true;
std::cout << "ste: " << args.ste << std::endl;
continue; continue;
} }
std::string stripped_arg; // all other optional parameter are
if (it->size() > 2 && it->at(0) == '-' && it->at(1) == '-') { auto [option_name, option_value] = get_optional_parameter(it);
stripped_arg = it->substr(2, it->size()); args.optional[option_name] = option_value;
} else if (it->size() > 1 && it->at(0) == '-') {
stripped_arg = it->substr(1, it->size());
}
std::transform(stripped_arg.begin(), stripped_arg.end(), stripped_arg.begin(), [](unsigned char c) { return std::tolower(c); });
const auto stripped_value = std::stod(*(++it), nullptr);
args.optional[stripped_arg] = stripped_value;
continue; continue;
} }
// Two positional parameters are defined: 1. Location of config file; 2. Name of motion model
if (args.parameter_file.empty()) { if (args.parameter_file.empty()) {
std::cout << *it << " is parameter_file" << std::endl;
args.parameter_file = *it; args.parameter_file = *it;
continue; continue;
} }
if (args.motion_type.empty()) { if (args.motion_type.empty()) {
std::cout << *it << " is motion model" << std::endl;
args.motion_type = *it; args.motion_type = *it;
continue; continue;
} }
@ -69,17 +77,16 @@ Arguments parse_args(const int argc, char* argv[]) {
throw std::invalid_argument("too many positional arguments"); throw std::invalid_argument("too many positional arguments");
} }
if (args.parameter_file.empty()) { if (args.parameter_file.empty() || args.motion_type.empty()) {
throw std::invalid_argument("Missing parameter file"); throw std::invalid_argument("Missing argument");
} }
if (args.motion_type.empty()) {
throw std::invalid_argument("Missing motion model");
}
return args; return args;
} }
std::unordered_map<std::string, double> read_parameter(const std::filesystem::path& infile) { std::unordered_map<std::string, double> read_parameter(const std::filesystem::path& infile) {
if (!std::filesystem::exists(infile)) { if (!std::filesystem::exists(infile)) {
std::cerr << "File " << infile << " does not exist" << std::endl; std::cerr << "File " << infile << " does not exist" << std::endl;
@ -110,13 +117,6 @@ std::unordered_map<std::string, double> read_parameter(const std::filesystem::pa
parameter[key] = std::stod(value); parameter[key] = std::stod(value);
} }
// print result
std::cout << "Found parameter\n";
for (const auto& [key, value]: parameter) {
std::cout << " " << key << ": " << std::to_string(value) << "\n";
}
std::cout << std::endl;
return parameter; return parameter;
} }

View File

@ -17,6 +17,7 @@ struct Arguments {
}; };
Arguments parse_args(int argc, char* argv[]); Arguments parse_args(int argc, char* argv[]);
std::pair<std::string, double> get_optional_parameter(std::vector<std::string>::const_iterator &it);
std::unordered_map<std::string, double> read_parameter(const std::filesystem::path&); std::unordered_map<std::string, double> read_parameter(const std::filesystem::path&);