Skip to content

Commit 8b0a96c

Browse files
committed
Restructure argument parser
1 parent 236c4aa commit 8b0a96c

3 files changed

Lines changed: 30 additions & 34 deletions

File tree

argument_parser.cpp

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,14 @@ namespace partdiff {
6464
usage();
6565
exit(EXIT_SUCCESS);
6666
}
67-
for (std::size_t i = 0; i <= to_underlying(argument_index::termination); i++) {
67+
for (std::size_t i = 0; i <= to_underlying(argument_index::term_dummy); i++) {
6868
parse_param(i, args[i]);
6969
}
7070
if (this->options.termination == termination_condition::accuracy) {
71-
parse_param(argument_index::term_accuracy, args[5]);
71+
parse_param(argument_index::term_accuracy, options.acc_iter);
7272
this->options.term_iteration = iteration_bounds.upper;
7373
} else {
74-
parse_param(argument_index::term_iteration, args[5]);
74+
parse_param(argument_index::term_iteration, options.acc_iter);
7575
this->options.term_accuracy = 0.0;
7676
}
7777
}
@@ -106,52 +106,44 @@ namespace partdiff {
106106
};
107107

108108
auto &number = this->options.number;
109-
this->add_argument("num", number, std::format("number of threads ({:d})", num_bounds), num_bounds);
109+
this->add_argument("num", number, std::make_optional(num_bounds),
110+
std::format("number of threads ({:d})", num_bounds));
110111

111112
auto &method = this->options.method;
112-
this->add_argument("method", method,
113-
std::format("calculation method ({:d})\n{}", method_bounds, display_enum(method_bounds)),
114-
method_bounds);
113+
this->add_argument("method", method, std::make_optional(method_bounds),
114+
std::format("calculation method ({:d})\n{}", method_bounds, display_enum(method_bounds)));
115115

116116
auto &interlines = this->options.interlines;
117-
this->add_argument("lines", interlines,
117+
this->add_argument("lines", interlines, std::make_optional(lines_bounds),
118118
std::format("number of interlines ({1:d})\n"
119119
"{0}matrixsize = (interlines * 8) + 9",
120-
indent, lines_bounds),
121-
lines_bounds);
120+
indent, lines_bounds));
122121

123122
auto &pert_func = this->options.pert_func;
124-
this->add_argument("func", pert_func,
125-
std::format("perturbation function ({:d})\n{}", func_bounds, display_enum(func_bounds)),
126-
func_bounds);
123+
this->add_argument("func", pert_func, std::make_optional(func_bounds),
124+
std::format("perturbation function ({:d})\n{}", func_bounds, display_enum(func_bounds)));
127125

128126
auto &termination = this->options.termination;
129-
this->add_argument("term", termination,
130-
std::format("termination condition ({:d})\n{}", term_bounds, display_enum(term_bounds)),
131-
term_bounds);
127+
this->add_argument("term", termination, std::make_optional(term_bounds),
128+
std::format("termination condition ({:d})\n{}", term_bounds, display_enum(term_bounds)));
132129

133-
this->add_argument("acc/iter", std::format("depending on term:\n"
134-
"{0}accuracy: {1:.0e}\n"
135-
"{0}iterations: {2:d}\n",
136-
indent, accuracy_bounds, iteration_bounds));
130+
auto &acc_iter = this->options.acc_iter;
131+
this->add_argument("acc/iter", acc_iter, std::optional<bounds_t<std::string>>{std::nullopt},
132+
std::format("depending on term:\n"
133+
"{0}accuracy: {1:.0e}\n"
134+
"{0}iterations: {2:d}\n",
135+
indent, accuracy_bounds, iteration_bounds));
137136

138137
auto &term_accuracy = this->options.term_accuracy;
139-
this->add_argument("acc", term_accuracy, std::nullopt, accuracy_bounds);
138+
this->add_argument("acc", term_accuracy, std::make_optional(accuracy_bounds), std::nullopt);
140139

141140
auto &term_iteration = this->options.term_iteration;
142-
this->add_argument("iter", term_iteration, std::nullopt, iteration_bounds);
143-
}
144-
145-
void argument_parser::add_argument(std::string name, std::optional<std::string> description) {
146-
argument_description arg_desc;
147-
arg_desc.name = name;
148-
arg_desc.description = description;
149-
this->argument_descriptions.push_back(arg_desc);
141+
this->add_argument("iter", term_iteration, std::make_optional(iteration_bounds), std::nullopt);
150142
}
151143

152144
template <class T>
153-
void argument_parser::add_argument(std::string name, T &target, std::optional<std::string> description,
154-
bounds_t<T> bounds) {
145+
void argument_parser::add_argument(std::string name, T &target, std::optional<bounds_t<T>> bounds,
146+
std::optional<std::string> description) {
155147
argument_description arg_desc;
156148
arg_desc.name = name;
157149
arg_desc.target = std::reference_wrapper<T>(target);
@@ -166,7 +158,10 @@ namespace partdiff {
166158
} else {
167159
valid_input = static_cast<bool>(iss >> target_ref);
168160
}
169-
valid_input &= bounds.contains(target_ref);
161+
if (bounds.has_value()) {
162+
valid_input &= bounds.value().contains(target_ref);
163+
}
164+
170165
return valid_input;
171166
};
172167
arg_desc.description = description;

argument_parser.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ namespace partdiff {
7979
void ask_param(argument_index index);
8080
void fill_argument_descriptions();
8181
template <class T>
82-
void add_argument(std::string name, T &target, std::optional<std::string> description, bounds_t<T> bounds);
83-
void add_argument(std::string name, std::optional<std::string> description);
82+
void add_argument(std::string name, T &target, std::optional<bounds_t<T>> bounds,
83+
std::optional<std::string> description);
8484
};
8585

8686
} // namespace partdiff

calculation_options.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace partdiff {
1111
calculation_method method;
1212
perturbation_function pert_func;
1313
termination_condition termination;
14+
std::string acc_iter;
1415
uint64_t term_iteration;
1516
double term_accuracy;
1617
};

0 commit comments

Comments
 (0)