44 lines
1.3 KiB
C++
44 lines
1.3 KiB
C++
// @requires-reflection
|
|
#include <Void/Configuration/Argparse.hpp>
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
|
|
#include <optional>
|
|
#include <string>
|
|
|
|
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<std::string> 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<Opts>(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<Opts>(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"});
|
|
}
|