// @requires-reflection #include #include #include #include #include namespace arg = Void::Argparse; namespace { struct Opts { [[= arg::Short{ 'v'}]][[= arg::Help{"verbose output"}]] bool verbose = false; [[= arg::Help{"output path"}]] std::string out = "a.out"; std::optional config; [[= arg::Positional{}]] std::string input = ""; }; } // namespace // @tag reflection Void::Testing::Test argparse_reflective_parse() { const char* argv[] = {"prog", "-v", "--out=x.bin", "foo.txt"}; const auto o = arg::parse(4, argv, "prog"); Void::Testing::expect(o.has_value()); Void::Testing::expect_eq(o->verbose, true); Void::Testing::expect_eq(o->out, std::string{"x.bin"}); Void::Testing::expect_eq(o->input, std::string{"foo.txt"}); Void::Testing::expect(!o->config.has_value()); } // @tag reflection Void::Testing::Test argparse_reflective_defaults() { const char* argv[] = {"prog", "bar.txt"}; const auto o = arg::parse(2, argv, "prog"); Void::Testing::expect(o.has_value()); Void::Testing::expect_eq(o->verbose, false); // flag default 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()); }