Skip to content

Commit

Permalink
Fix default list processing
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Taylor C. Richberger committed Jan 11, 2019
1 parent 2eb0f5f commit f68b7e1
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
55 changes: 55 additions & 0 deletions args.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
40 changes: 40 additions & 0 deletions test.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,46 @@ TEST_CASE("Argument flag lists work as expected", "[args]")
REQUIRE((args::get(foo) == std::vector<int>{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<int> foo(parser, "FOO", "test flag", {'f', "foo"}, {9, 7, 5});
parser.ParseArgs(std::vector<std::string>());
REQUIRE((args::get(foo) == std::vector<int>{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<int> foo(parser, "FOO", "test flag", {'f', "foo"}, {9, 7, 5});
parser.ParseArgs(std::vector<std::string>{"--foo=7", "-f2", "-f", "9", "--foo", "42"});
REQUIRE((args::get(foo) == std::vector<int>{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<int> foo(parser, "FOO", "test flag");
parser.ParseArgs(std::vector<std::string>{"7", "2", "9", "42"});
REQUIRE((args::get(foo) == std::vector<int>{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<int> foo(parser, "FOO", "test flag", {9, 7, 5});
parser.ParseArgs(std::vector<std::string>());
REQUIRE((args::get(foo) == std::vector<int>{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<int> foo(parser, "FOO", "test flag", {9, 7, 5});
parser.ParseArgs(std::vector<std::string>{"7", "2", "9", "42"});
REQUIRE((args::get(foo) == std::vector<int>{7, 2, 9, 42}));
}

#include <unordered_set>

TEST_CASE("Argument flag lists work with sets", "[args]")
Expand Down

0 comments on commit f68b7e1

Please sign in to comment.