Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion args.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -1715,7 +1715,24 @@ namespace args
children.emplace_back(&child);

if(child.IsFlag()) {
#ifndef ARGS_NOEXCEPT
// Detection runs from the child's own constructor, so a
// duplicate throws before that constructor completes and the
// child's storage is released while the stack unwinds. Undo
// the registration first, or a caller that catches the error
// leaves this group holding a pointer to a dead object.
try
{
SignalDetectDuplicates();
}
catch (...)
{
children.pop_back();
throw;
}
#else
SignalDetectDuplicates();
#endif
}
}

Expand Down Expand Up @@ -3392,8 +3409,11 @@ namespace args

void AddCompletion(CompletionFlag &completionFlag)
{
completion = &completionFlag;
// Only take the pointer once registration has succeeded: Add()
// throws on a duplicate flag, and the flag it was handed is
// gone by the time that error reaches the caller.
Add(completionFlag);
completion = &completionFlag;
}

/** The program name for help generation
Expand Down
1 change: 1 addition & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ test_names = [
'count_flag',
'custom_types',
'default_values',
'duplicate_flag_rollback',
'extra_positionals',
'get_assignable',
'get_program_line',
Expand Down
74 changes: 74 additions & 0 deletions test/duplicate_flag_rollback.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/* Copyright (c) Taylor Richberger <taylor@axfive.net>
* This code is released under the license described in the LICENSE file
*/

#include "test_common.hxx"

#include <args.hxx>

#include "test_helpers.hxx"

void testFailedFlagIsNotRegistered()
{
args::ArgumentParser parser("This is a test program.");
args::Flag flag_a(parser, "aone", "test flag", {'a', "aone"});

const auto children = parser.Children().size();
test::require_throws_as<args::ParseError>([&]{
args::Flag flag_b(parser, "atwo", "test flag", {'a', "atwo"});
});
test::require(parser.Children().size() == children);
}

void testFailedFlagInGroupIsNotRegistered()
{
args::ArgumentParser parser("This is a test program.");
args::Group group(parser, "This is a test group.", args::Group::Validators::DontCare);
args::Flag flag_a(group, "aone", "test flag", {'a', "aone"});

const auto children = group.Children().size();
test::require_throws_as<args::ParseError>([&]{
args::Flag flag_b(group, "atwo", "test flag", {'a', "atwo"});
});
test::require(group.Children().size() == children);
}

void testParserStillUsableAfterCaughtDuplicate()
{
args::ArgumentParser parser("This is a test program.");
args::Flag flag_a(parser, "aone", "test flag", {'a', "aone"});
args::Positional<std::string> pos(parser, "pos", "test positional");

test::require_throws_as<args::ParseError>([&]{
args::Flag flag_b(parser, "atwo", "test flag", {'a', "atwo"});
});

test::require_nothrow([&]{ parser.ParseArgs(std::vector<std::string>{"-a", "value"}); });
test::require(bool(flag_a));
test::require(args::get(pos) == "value");
test::require_nothrow([&]{ (void)parser.Help(); });
}

void testFailedCompletionFlagIsNotRegistered()
{
args::ArgumentParser parser("This is a test program.");
args::Flag flag_a(parser, "aone", "test flag", {'a', "complete"});

const auto children = parser.Children().size();
test::require_throws_as<args::ParseError>([&]{
args::CompletionFlag completion(parser, {"complete"});
});
test::require(parser.Children().size() == children);

// The parser must not be left pointing at the completion flag it rejected.
test::require_nothrow([&]{ parser.ParseArgs(std::vector<std::string>{"--complete"}); });
test::require(bool(flag_a));
}

int main()
{
testFailedFlagIsNotRegistered();
testFailedFlagInGroupIsNotRegistered();
testParserStillUsableAfterCaughtDuplicate();
testFailedCompletionFlagIsNotRegistered();
}