Skip to content

Commit f68b7e1

Browse files
author
Taylor C. Richberger
committed
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.
1 parent 2eb0f5f commit f68b7e1

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

args.hxx

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3506,6 +3506,17 @@ namespace args
35063506
FlagBase::Reset();
35073507
values = defaultValues;
35083508
}
3509+
3510+
virtual FlagBase *Match(const EitherFlag &arg) override
3511+
{
3512+
const bool wasMatched = Matched();
3513+
auto me = FlagBase::Match(arg);
3514+
if (me && !wasMatched)
3515+
{
3516+
values.clear();
3517+
}
3518+
return me;
3519+
}
35093520
};
35103521

35113522
/** An argument-accepting flag class that pushes the found values into a list
@@ -3583,6 +3594,17 @@ namespace args
35833594
values = defaultValues;
35843595
}
35853596

3597+
virtual FlagBase *Match(const EitherFlag &arg) override
3598+
{
3599+
const bool wasMatched = Matched();
3600+
auto me = FlagBase::Match(arg);
3601+
if (me && !wasMatched)
3602+
{
3603+
values.clear();
3604+
}
3605+
return me;
3606+
}
3607+
35863608
iterator begin() noexcept
35873609
{
35883610
return values.begin();
@@ -3799,6 +3821,17 @@ namespace args
37993821
values = defaultValues;
38003822
}
38013823

3824+
virtual FlagBase *Match(const EitherFlag &arg) override
3825+
{
3826+
const bool wasMatched = Matched();
3827+
auto me = FlagBase::Match(arg);
3828+
if (me && !wasMatched)
3829+
{
3830+
values.clear();
3831+
}
3832+
return me;
3833+
}
3834+
38023835
iterator begin() noexcept
38033836
{
38043837
return values.begin();
@@ -3960,6 +3993,17 @@ namespace args
39603993
values = defaultValues;
39613994
}
39623995

3996+
virtual PositionalBase *GetNextPositional() override
3997+
{
3998+
const bool wasMatched = Matched();
3999+
auto me = PositionalBase::GetNextPositional();
4000+
if (me && !wasMatched)
4001+
{
4002+
values.clear();
4003+
}
4004+
return me;
4005+
}
4006+
39634007
iterator begin() noexcept
39644008
{
39654009
return values.begin();
@@ -4170,6 +4214,17 @@ namespace args
41704214
values = defaultValues;
41714215
}
41724216

4217+
virtual PositionalBase *GetNextPositional() override
4218+
{
4219+
const bool wasMatched = Matched();
4220+
auto me = PositionalBase::GetNextPositional();
4221+
if (me && !wasMatched)
4222+
{
4223+
values.clear();
4224+
}
4225+
return me;
4226+
}
4227+
41734228
iterator begin() noexcept
41744229
{
41754230
return values.begin();

test.cxx

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,46 @@ TEST_CASE("Argument flag lists work as expected", "[args]")
137137
REQUIRE((args::get(foo) == std::vector<int>{7, 2, 9, 42}));
138138
}
139139

140+
TEST_CASE("Argument flag lists use default values", "[args]")
141+
{
142+
args::ArgumentParser parser("This is a test program.", "This goes after the options.");
143+
args::ValueFlagList<int> foo(parser, "FOO", "test flag", {'f', "foo"}, {9, 7, 5});
144+
parser.ParseArgs(std::vector<std::string>());
145+
REQUIRE((args::get(foo) == std::vector<int>{9, 7, 5}));
146+
}
147+
148+
TEST_CASE("Argument flag lists replace default values", "[args]")
149+
{
150+
args::ArgumentParser parser("This is a test program.", "This goes after the options.");
151+
args::ValueFlagList<int> foo(parser, "FOO", "test flag", {'f', "foo"}, {9, 7, 5});
152+
parser.ParseArgs(std::vector<std::string>{"--foo=7", "-f2", "-f", "9", "--foo", "42"});
153+
REQUIRE((args::get(foo) == std::vector<int>{7, 2, 9, 42}));
154+
}
155+
156+
TEST_CASE("Positional lists work as expected", "[args]")
157+
{
158+
args::ArgumentParser parser("This is a test program.", "This goes after the options.");
159+
args::PositionalList<int> foo(parser, "FOO", "test flag");
160+
parser.ParseArgs(std::vector<std::string>{"7", "2", "9", "42"});
161+
REQUIRE((args::get(foo) == std::vector<int>{7, 2, 9, 42}));
162+
}
163+
164+
TEST_CASE("Positional lists use default values", "[args]")
165+
{
166+
args::ArgumentParser parser("This is a test program.", "This goes after the options.");
167+
args::PositionalList<int> foo(parser, "FOO", "test flag", {9, 7, 5});
168+
parser.ParseArgs(std::vector<std::string>());
169+
REQUIRE((args::get(foo) == std::vector<int>{9, 7, 5}));
170+
}
171+
172+
TEST_CASE("Positional lists replace default values", "[args]")
173+
{
174+
args::ArgumentParser parser("This is a test program.", "This goes after the options.");
175+
args::PositionalList<int> foo(parser, "FOO", "test flag", {9, 7, 5});
176+
parser.ParseArgs(std::vector<std::string>{"7", "2", "9", "42"});
177+
REQUIRE((args::get(foo) == std::vector<int>{7, 2, 9, 42}));
178+
}
179+
140180
#include <unordered_set>
141181

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

0 commit comments

Comments
 (0)