From 54fb3c21a4bb76cb2b6df5137194c198c54f32f2 Mon Sep 17 00:00:00 2001 From: Matthew Abruzzo Date: Thu, 15 Aug 2024 13:10:05 -0400 Subject: [PATCH] some light refactoring. --- src/grcli/handle_gr_parameters.h | 41 +++++++------------------------- 1 file changed, 9 insertions(+), 32 deletions(-) diff --git a/src/grcli/handle_gr_parameters.h b/src/grcli/handle_gr_parameters.h index 166f0b51..2ce7d94e 100644 --- a/src/grcli/handle_gr_parameters.h +++ b/src/grcli/handle_gr_parameters.h @@ -25,8 +25,7 @@ using value_variant = std::variant; /// receives. If the order within argv, this becomes invalidated. Likewise /// this would also become invalidated, if argv were somehow deallocated... struct CliParamSpec{ - const char* const * begin; - const char* const * end; + std::vector args; private: /// internal type used to hold the parsed key-value pair @@ -55,8 +54,8 @@ struct CliParamSpec{ /// arguments (std::string_view key, value_variant value) template void for_each(PairFn f) const { - for(const char* const* ptr = this->begin; ptr != end; ++ptr) { - KVPair kv_pair = this->parse_param_(*ptr); + for(const char* arg : args) { + KVPair kv_pair = this->parse_param_(arg); f(kv_pair.key, kv_pair.val); } } @@ -73,35 +72,13 @@ struct CliParamSpec{ inline bool try_parse_cli_paramspec(const char* leading_arg, CliParser& parser, std::optional& param_spec) { - if (std::string_view(leading_arg) != "--par-start") { - return false; - } else if (param_spec.has_value()) { - std::fprintf(stderr, "%s isn't allowed to be provided more than once\n", - leading_arg); - std::exit(1); - } - - const std::string_view sentinel("--par-stop"); - if ((not parser.has_next()) || (parser.peek() == sentinel)) { - fprintf(stderr, "the \"%s\" flag doesn't start a group of parameters\n", - leading_arg); - std::exit(1); - - } else { - char * const * begin = parser.next_argv_ptr(); - char * const * latest = begin; - while (sentinel != *latest) { - if (!parser.has_next()) { - latest++; - break; - } - latest = parser.next_argv_ptr(); - } + args::EscapedArgsSpec arg_spec{'P', "--par-start", "--par-stop"}; - CliParamSpec tmp{begin, latest}; - param_spec = std::make_optional(std::move(tmp)); - return true; - } + auto fn = [¶m_spec](const char* arg) -> void { + if (!param_spec.has_value()) { param_spec = CliParamSpec{}; } + param_spec.value().args.push_back(arg); + }; + return arg_spec.try_parse(leading_arg, parser, fn); } /// Class that holds the full configuration of the Grackle-Solver