move option parsing to its own function
This commit is contained in:
parent
0c6fd604fc
commit
4b8922ab55
7
main.cpp
7
main.cpp
@ -28,6 +28,13 @@ int main (const int argc, char *argv[]) {
|
||||
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::mt19937_64 rng(rd());
|
||||
|
||||
|
58
utils/io.cpp
58
utils/io.cpp
@ -1,6 +1,3 @@
|
||||
//
|
||||
// Created by dominik on 8/14/24.
|
||||
//
|
||||
#include "io.h"
|
||||
|
||||
#include <sstream>
|
||||
@ -16,7 +13,25 @@
|
||||
#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[]) {
|
||||
if (argc < 3) {
|
||||
throw std::runtime_error("Not enough arguments: missing parameter file");
|
||||
@ -24,44 +39,37 @@ Arguments parse_args(const int argc, char* argv[]) {
|
||||
|
||||
Arguments args;
|
||||
|
||||
// convert to vector to use iterator for loop
|
||||
const std::vector<std::string> input_args(argv + 1, argv + argc);
|
||||
|
||||
for (auto it = input_args.begin(); it != input_args.end(); ++it) {
|
||||
std::cout << *it << std::endl;
|
||||
// check for optional parameter
|
||||
if (it->at(0) == '-') {
|
||||
|
||||
// only --spectrum and --ste are parameter that are predefined
|
||||
if (*it == "--spectrum") {
|
||||
args.spectrum = true;
|
||||
std::cout << "spectrum: " << args.spectrum << std::endl;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (*it == "--ste") {
|
||||
args.ste = true;
|
||||
std::cout << "ste: " << args.ste << std::endl;
|
||||
continue;
|
||||
}
|
||||
|
||||
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);
|
||||
args.optional[stripped_arg] = stripped_value;
|
||||
// all other optional parameter are
|
||||
auto [option_name, option_value] = get_optional_parameter(it);
|
||||
args.optional[option_name] = option_value;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Two positional parameters are defined: 1. Location of config file; 2. Name of motion model
|
||||
if (args.parameter_file.empty()) {
|
||||
std::cout << *it << " is parameter_file" << std::endl;
|
||||
args.parameter_file = *it;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (args.motion_type.empty()) {
|
||||
std::cout << *it << " is motion model" << std::endl;
|
||||
args.motion_type = *it;
|
||||
continue;
|
||||
}
|
||||
@ -69,17 +77,16 @@ Arguments parse_args(const int argc, char* argv[]) {
|
||||
throw std::invalid_argument("too many positional arguments");
|
||||
}
|
||||
|
||||
if (args.parameter_file.empty()) {
|
||||
throw std::invalid_argument("Missing parameter file");
|
||||
if (args.parameter_file.empty() || args.motion_type.empty()) {
|
||||
throw std::invalid_argument("Missing argument");
|
||||
}
|
||||
|
||||
if (args.motion_type.empty()) {
|
||||
throw std::invalid_argument("Missing motion model");
|
||||
}
|
||||
return args;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
std::unordered_map<std::string, double> read_parameter(const std::filesystem::path& infile) {
|
||||
if (!std::filesystem::exists(infile)) {
|
||||
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);
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
|
@ -17,6 +17,7 @@ struct Arguments {
|
||||
};
|
||||
|
||||
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&);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user