diff --git a/include/Void/Configuration/Argparse.hpp b/include/Void/Configuration/Argparse.hpp index 57ffb55..a229731 100644 --- a/include/Void/Configuration/Argparse.hpp +++ b/include/Void/Configuration/Argparse.hpp @@ -2,6 +2,7 @@ #include +#include #include #include #include @@ -252,6 +253,17 @@ namespace Void::Argparse Opts out{}; Parser parser{std::move(program), std::move(description)}; + // Numeric option sinks: the parser binds text, then converts it with + // strict validation after a successful parse. + struct NumericSink { + std::string text{}; + std::string option{}; + std::function assign{}; + }; + // Heap-owned so the parser's bound string pointer stays valid when the + // collection grows. + std::vector> numeric_sinks; + template for (constexpr auto m : Void::Meta::members_v) { using MT = [:std::meta::type_of(m):]; @@ -278,6 +290,24 @@ namespace Void::Argparse } else if constexpr (std::same_as) { const std::string def = out.[:m:]; parser.option_default(lng, sht, desc, out.[:m:], def); + } else if constexpr (std::is_arithmetic_v) { + auto sink = std::make_unique(); + sink->option = lng; + sink->text = std::to_string(out.[:m:]); + sink->assign = [target = &out.[:m:]](std::string_view text) { + using DT = std::remove_pointer_t; + DT value{}; + 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, sink->text); + numeric_sinks.push_back(std::move(sink)); } // other field types: ignored } @@ -286,6 +316,12 @@ namespace Void::Argparse if (!r) { return std::unexpected{r.error()}; } + for (const auto& sink : numeric_sinks) { + if (!sink->assign(sink->text)) { + return std::unexpected{"invalid numeric value '" + sink->text + + "' for " + sink->option}; + } + } return out; } diff --git a/tests/standalone/Configuration/ArgparseReflect.cpp b/tests/standalone/Configuration/ArgparseReflect.cpp index 079abdc..a9a6c57 100644 --- a/tests/standalone/Configuration/ArgparseReflect.cpp +++ b/tests/standalone/Configuration/ArgparseReflect.cpp @@ -41,3 +41,30 @@ Void::Testing::Test argparse_reflective_defaults() Void::Testing::expect_eq(o->out, std::string{"a.out"}); // option default Void::Testing::expect_eq(o->input, std::string{"bar.txt"}); } + +Void::Testing::Test argparse_reflect_numeric_fields() +{ + struct Options { + int count = 3; + unsigned retries = 1; + double ratio = 0.5; + }; + + const char* default_argv[] = {"app"}; + auto defaults = Void::Argparse::parse(1, default_argv); + Void::Testing::require(defaults.has_value()); + Void::Testing::expect_eq(defaults->count, 3); + Void::Testing::expect_eq(defaults->retries, 1U); + + const char* argv[] = {"app", "--count", "42", "--retries", "7", + "--ratio", "0.25"}; + auto parsed = Void::Argparse::parse(7, argv); + Void::Testing::require(parsed.has_value()); + Void::Testing::expect_eq(parsed->count, 42); + Void::Testing::expect_eq(parsed->retries, 7U); + Void::Testing::expect(parsed->ratio > 0.24 && parsed->ratio < 0.26); + + const char* bad_argv[] = {"app", "--count", "not-a-number"}; + auto bad = Void::Argparse::parse(3, bad_argv); + Void::Testing::expect(!bad.has_value()); +}