Argparse: derive numeric option fields (int, unsigned, double) with strict validation

This commit is contained in:
faumaray committed 2026-12-04 01:02:06 +03:00
1 parent 1bd729147f
commit 520849f82e
2 files changed
+63

No files matched your search

@@ -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<Options>(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<Options>(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<Options>(3, bad_argv);
Void::Testing::expect(!bad.has_value());
}