change parameter via CLI options
This commit is contained in:
57
utils/io.cpp
57
utils/io.cpp
@ -7,6 +7,7 @@
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <complex>
|
||||
#include <vector>
|
||||
#include <iomanip>
|
||||
#include <unordered_map>
|
||||
@ -16,29 +17,56 @@
|
||||
|
||||
|
||||
|
||||
Arguments parse_args(const int argc, char **argv) {
|
||||
Arguments parse_args(const int argc, char* argv[]) {
|
||||
if (argc < 3) {
|
||||
throw std::runtime_error("Not enough arguments: missing parameter file");
|
||||
}
|
||||
|
||||
Arguments args;
|
||||
|
||||
for (int i=1; i<argc; i++) {
|
||||
if (std::string arg = argv[i]; arg[0] == '-') {
|
||||
if (arg == "--ste") {
|
||||
args.ste = true;
|
||||
} else if (arg == "--spectrum") {
|
||||
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;
|
||||
if (it->at(0) == '-') {
|
||||
|
||||
if (*it == "--spectrum") {
|
||||
args.spectrum = true;
|
||||
} else {
|
||||
throw std::runtime_error("Unrecognized option: " + arg);
|
||||
std::cout << "spectrum: " << args.spectrum << std::endl;
|
||||
continue;
|
||||
}
|
||||
} else if (args.parameter_file.empty()) {
|
||||
args.parameter_file = arg;
|
||||
} else if (args.motion_type.empty()) {
|
||||
args.motion_type = arg;
|
||||
} else {
|
||||
throw std::invalid_argument("Too many options for parameter file");
|
||||
|
||||
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;
|
||||
continue;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
throw std::invalid_argument("too many positional arguments");
|
||||
}
|
||||
|
||||
if (args.parameter_file.empty()) {
|
||||
@ -48,7 +76,6 @@ Arguments parse_args(const int argc, char **argv) {
|
||||
if (args.motion_type.empty()) {
|
||||
throw std::invalid_argument("Missing motion model");
|
||||
}
|
||||
|
||||
return args;
|
||||
}
|
||||
|
||||
|
@ -16,7 +16,7 @@ struct Arguments {
|
||||
std::unordered_map<std::string, double> optional;
|
||||
};
|
||||
|
||||
Arguments parse_args(int argc, char **argv);
|
||||
Arguments parse_args(int argc, char* argv[]);
|
||||
|
||||
std::unordered_map<std::string, double> read_parameter(const std::filesystem::path&);
|
||||
|
||||
|
Reference in New Issue
Block a user