From f68b7e186cd2a020cbddfe3194c1d8ddfeeb1013 Mon Sep 17 00:00:00 2001 From: "Taylor C. Richberger" Date: Fri, 11 Jan 2019 11:35:07 -0700 Subject: [PATCH] Fix default list processing The previous release had set up default arguments to add to the existing default list. Now making a match will clear the list and replace it. --- args.hxx | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ test.cxx | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) diff --git a/args.hxx b/args.hxx index 1eb8da8..bbc521d 100644 --- a/args.hxx +++ b/args.hxx @@ -3506,6 +3506,17 @@ namespace args FlagBase::Reset(); values = defaultValues; } + + virtual FlagBase *Match(const EitherFlag &arg) override + { + const bool wasMatched = Matched(); + auto me = FlagBase::Match(arg); + if (me && !wasMatched) + { + values.clear(); + } + return me; + } }; /** An argument-accepting flag class that pushes the found values into a list @@ -3583,6 +3594,17 @@ namespace args values = defaultValues; } + virtual FlagBase *Match(const EitherFlag &arg) override + { + const bool wasMatched = Matched(); + auto me = FlagBase::Match(arg); + if (me && !wasMatched) + { + values.clear(); + } + return me; + } + iterator begin() noexcept { return values.begin(); @@ -3799,6 +3821,17 @@ namespace args values = defaultValues; } + virtual FlagBase *Match(const EitherFlag &arg) override + { + const bool wasMatched = Matched(); + auto me = FlagBase::Match(arg); + if (me && !wasMatched) + { + values.clear(); + } + return me; + } + iterator begin() noexcept { return values.begin(); @@ -3960,6 +3993,17 @@ namespace args values = defaultValues; } + virtual PositionalBase *GetNextPositional() override + { + const bool wasMatched = Matched(); + auto me = PositionalBase::GetNextPositional(); + if (me && !wasMatched) + { + values.clear(); + } + return me; + } + iterator begin() noexcept { return values.begin(); @@ -4170,6 +4214,17 @@ namespace args values = defaultValues; } + virtual PositionalBase *GetNextPositional() override + { + const bool wasMatched = Matched(); + auto me = PositionalBase::GetNextPositional(); + if (me && !wasMatched) + { + values.clear(); + } + return me; + } + iterator begin() noexcept { return values.begin(); diff --git a/test.cxx b/test.cxx index 13cd4d0..b46007d 100644 --- a/test.cxx +++ b/test.cxx @@ -137,6 +137,46 @@ TEST_CASE("Argument flag lists work as expected", "[args]") REQUIRE((args::get(foo) == std::vector{7, 2, 9, 42})); } +TEST_CASE("Argument flag lists use default values", "[args]") +{ + args::ArgumentParser parser("This is a test program.", "This goes after the options."); + args::ValueFlagList foo(parser, "FOO", "test flag", {'f', "foo"}, {9, 7, 5}); + parser.ParseArgs(std::vector()); + REQUIRE((args::get(foo) == std::vector{9, 7, 5})); +} + +TEST_CASE("Argument flag lists replace default values", "[args]") +{ + args::ArgumentParser parser("This is a test program.", "This goes after the options."); + args::ValueFlagList foo(parser, "FOO", "test flag", {'f', "foo"}, {9, 7, 5}); + parser.ParseArgs(std::vector{"--foo=7", "-f2", "-f", "9", "--foo", "42"}); + REQUIRE((args::get(foo) == std::vector{7, 2, 9, 42})); +} + +TEST_CASE("Positional lists work as expected", "[args]") +{ + args::ArgumentParser parser("This is a test program.", "This goes after the options."); + args::PositionalList foo(parser, "FOO", "test flag"); + parser.ParseArgs(std::vector{"7", "2", "9", "42"}); + REQUIRE((args::get(foo) == std::vector{7, 2, 9, 42})); +} + +TEST_CASE("Positional lists use default values", "[args]") +{ + args::ArgumentParser parser("This is a test program.", "This goes after the options."); + args::PositionalList foo(parser, "FOO", "test flag", {9, 7, 5}); + parser.ParseArgs(std::vector()); + REQUIRE((args::get(foo) == std::vector{9, 7, 5})); +} + +TEST_CASE("Positional lists replace default values", "[args]") +{ + args::ArgumentParser parser("This is a test program.", "This goes after the options."); + args::PositionalList foo(parser, "FOO", "test flag", {9, 7, 5}); + parser.ParseArgs(std::vector{"7", "2", "9", "42"}); + REQUIRE((args::get(foo) == std::vector{7, 2, 9, 42})); +} + #include TEST_CASE("Argument flag lists work with sets", "[args]")