From 546628c5b9b3ebe7c0829d00a9c6978a4a5ae471 Mon Sep 17 00:00:00 2001 From: "Aleksei (Faumaray) Selikhov" Date: Fri, 4 Dec 2026 01:24:18 +0300 Subject: [PATCH] Argparse: derive optional and repeated numeric/enum fields --- include/Void/Configuration/Argparse.hpp | 73 ++++++++++++++++++- .../Configuration/ArgparseReflect.cpp | 27 +++++++ 2 files changed, 97 insertions(+), 3 deletions(-) diff --git a/include/Void/Configuration/Argparse.hpp b/include/Void/Configuration/Argparse.hpp index 45ecd27..7fb6b0d 100644 --- a/include/Void/Configuration/Argparse.hpp +++ b/include/Void/Configuration/Argparse.hpp @@ -257,7 +257,9 @@ namespace Void::Argparse // strict validation after a successful parse. struct NumericSink { std::string text{}; + std::vector texts{}; std::string option{}; + bool many{false}; std::function assign{}; }; // Heap-owned so the parser's bound string pointer stays valid when the @@ -284,9 +286,67 @@ namespace Void::Argparse } else if constexpr (std::same_as) { parser.flag(lng, sht, desc, out.[:m:]); } else if constexpr (Void::Meta::is_optional_v) { - parser.option(lng, sht, desc, out.[:m:]); + using Inner = typename MT::value_type; + if constexpr (std::same_as) { + parser.option(lng, sht, desc, out.[:m:]); + } else { + auto sink = std::make_unique(); + sink->option = lng; + sink->assign = [target = &out.[:m:]](std::string_view text) { + if (text.empty()) { + target->reset(); + return true; + } + Inner value{}; + if constexpr (std::is_enum_v) { + const auto parsed = Void::Meta::enum_parse(text); + if (!parsed) { + return false; + } + *target = *parsed; + return true; + } else { + const auto result = std::from_chars( + text.data(), text.data() + text.size(), value); + if (result.ec != std::errc{} || + result.ptr != text.data() + text.size()) { + return false; + } + *target = value; + return true; + } + }; + parser.option_default(lng, sht, desc, sink->text, ""); + numeric_sinks.push_back(std::move(sink)); + } } else if constexpr (std::same_as>) { parser.option_many(lng, sht, desc, out.[:m:]); + } else if constexpr (Void::Meta::is_vector_v) { + using Inner = typename MT::value_type; + auto sink = std::make_unique(); + sink->option = lng; + sink->many = true; + sink->assign = [target = &out.[:m:]](std::string_view text) { + Inner value{}; + if constexpr (std::is_enum_v) { + const auto parsed = Void::Meta::enum_parse(text); + if (!parsed) { + return false; + } + value = *parsed; + } else { + const auto result = std::from_chars( + text.data(), text.data() + text.size(), value); + if (result.ec != std::errc{} || + result.ptr != text.data() + text.size()) { + return false; + } + } + target->push_back(value); + return true; + }; + parser.option_many(lng, sht, desc, sink->texts); + numeric_sinks.push_back(std::move(sink)); } else if constexpr (std::same_as) { const std::string def = out.[:m:]; parser.option_default(lng, sht, desc, out.[:m:], def); @@ -338,8 +398,15 @@ namespace Void::Argparse return std::unexpected{r.error()}; } for (const auto& sink : numeric_sinks) { - if (!sink->assign(sink->text)) { - return std::unexpected{"invalid numeric value '" + sink->text + + if (sink->many) { + for (const auto& text : sink->texts) { + if (!sink->assign(text)) { + return std::unexpected{"invalid value '" + text + + "' for " + sink->option}; + } + } + } else if (!sink->assign(sink->text)) { + return std::unexpected{"invalid value '" + sink->text + "' for " + sink->option}; } } diff --git a/tests/standalone/Configuration/ArgparseReflect.cpp b/tests/standalone/Configuration/ArgparseReflect.cpp index 7a2acc5..d3877cd 100644 --- a/tests/standalone/Configuration/ArgparseReflect.cpp +++ b/tests/standalone/Configuration/ArgparseReflect.cpp @@ -69,6 +69,33 @@ Void::Testing::Test argparse_reflect_numeric_fields() Void::Testing::expect(!bad.has_value()); } +Void::Testing::Test argparse_reflect_optional_and_vector_numerics() +{ + struct Options { + std::optional port; + std::vector ids; + }; + + const char* absent[] = {"app"}; + auto defaults = Void::Argparse::parse(1, absent); + Void::Testing::require(defaults.has_value()); + Void::Testing::expect(!defaults->port.has_value()); + Void::Testing::expect(defaults->ids.empty()); + + const char* argv[] = {"app", "--port", "8080", "--ids", "1", "--ids", "2", + "--ids", "3"}; + auto parsed = Void::Argparse::parse(9, argv); + Void::Testing::require(parsed.has_value()); + Void::Testing::require(parsed->port.has_value()); + Void::Testing::expect_eq(*parsed->port, 8080); + Void::Testing::expect_eq(parsed->ids.size(), std::size_t{3}); + Void::Testing::expect_eq(parsed->ids[2], 3); + + const char* bad[] = {"app", "--ids", "nope"}; + auto rejected = Void::Argparse::parse(3, bad); + Void::Testing::expect(!rejected.has_value()); +} + Void::Testing::Test argparse_reflect_enum_fields() { enum class Color { Red, Green, Blue };