diff --git a/include/argparse.hpp b/include/argparse.hpp index 4ac2468..faf1d74 100644 --- a/include/argparse.hpp +++ b/include/argparse.hpp @@ -1919,6 +1919,12 @@ namespace argparse } else { + if (m_options.mutually_exclusive_group != nullptr + && m_options.required) + { + throw option_error("mutually exclusive arguments must be optional"); + } + m_arguments.emplace_back(OptionalArgument(std::move(m_options))); } } diff --git a/test/unittest/test_argument_parser.cpp b/test/unittest/test_argument_parser.cpp index e8a9062..edce5c4 100644 --- a/test/unittest/test_argument_parser.cpp +++ b/test/unittest/test_argument_parser.cpp @@ -375,6 +375,14 @@ TEST_CASE("Adding an optional argument to a mutually exclusive group does not re CHECK_NOTHROW(group.add_argument("-o")); } +TEST_CASE("Adding a required optional argument to a mutually exclusive group results in error") +{ + auto parser = argparse::ArgumentParser(); + auto group = parser.add_mutually_exclusive_group(); + + CHECK_THROWS_WITH_AS(group.add_argument("-o").required(true), "mutually exclusive arguments must be optional", argparse::option_error); +} + TEST_CASE("Test combining Handle values") { CHECK((argparse::Handle::none | argparse::Handle::errors) == argparse::Handle::errors);