diff options
author | Pavel Belikov <pavel.fuchs.belikov@gmail.com> | 2017-11-08 22:25:08 +0300 |
---|---|---|
committer | Pavel Belikov <pavel.fuchs.belikov@gmail.com> | 2017-11-08 22:25:08 +0300 |
commit | 2477df98fadc281e55fab806ee9c7f6f47f1e661 (patch) | |
tree | 375c428d618bda0792386054b7943c7763737a6b /test.cxx | |
parent | replace badges with Taywee ones so the repository is properly described by it... (diff) | |
download | args.hxx-2477df98fadc281e55fab806ee9c7f6f47f1e661.tar.xz |
add validation for commands
Diffstat (limited to 'test.cxx')
-rw-r--r-- | test.cxx | 40 |
1 files changed, 40 insertions, 0 deletions
@@ -857,6 +857,46 @@ TEST_CASE("Subparser help works as expected", "[args]") } +TEST_CASE("Subparser validation works as expected", "[args]") +{ + args::ArgumentParser p("parser"); + args::Command a(p, "a", "command a", [](args::Subparser &s) + { + args::ValueFlag<std::string> f(s, "", "", {'f'}, args::Options::Required); + s.Parse(); + }); + + args::Command b(p, "b", "command b"); + args::ValueFlag<std::string> f(b, "", "", {'f'}, args::Options::Required); + + REQUIRE_NOTHROW(p.ParseArgs(std::vector<std::string>{})); + REQUIRE_THROWS_AS(p.ParseArgs(std::vector<std::string>{"a"}), args::RequiredError); + REQUIRE_NOTHROW(p.ParseArgs(std::vector<std::string>{"a", "-f", "F"})); + REQUIRE_THROWS_AS(p.ParseArgs(std::vector<std::string>{"b"}), args::RequiredError); + REQUIRE_NOTHROW(p.ParseArgs(std::vector<std::string>{"b", "-f", "F"})); + + p.RequireCommand(true); + REQUIRE_THROWS_AS(p.ParseArgs(std::vector<std::string>{}), args::ValidationError); + + p.RequireCommand(false); + REQUIRE_NOTHROW(p.ParseArgs(std::vector<std::string>{})); +} + +TEST_CASE("Global options work as expected", "[args]") +{ + args::Group globals; + args::Flag f(globals, "f", "f", {'f'}); + + args::ArgumentParser p("parser"); + args::GlobalOptions g(p, globals); + args::Command a(p, "a", "command a"); + args::Command b(p, "b", "command b"); + + REQUIRE_NOTHROW(p.ParseArgs(std::vector<std::string>{"-f"})); + REQUIRE_NOTHROW(p.ParseArgs(std::vector<std::string>{"a", "-f"})); + REQUIRE_NOTHROW(p.ParseArgs(std::vector<std::string>{"b", "-f"})); +} + #undef ARGS_HXX #define ARGS_TESTNAMESPACE #define ARGS_NOEXCEPT |