diff --git a/include/argparse/argparse.hpp b/include/argparse/argparse.hpp index a66c8ed..d4aec0e 100644 --- a/include/argparse/argparse.hpp +++ b/include/argparse/argparse.hpp @@ -46,13 +46,13 @@ SOFTWARE. #include #include #include +#include #include #include #include #include #include #include -#include #include #include #include @@ -1419,6 +1419,19 @@ public: m_subparser_map.insert_or_assign(it->get().m_program_name, it); m_subparser_used.insert_or_assign(it->get().m_program_name, false); } + + for (auto &g : other.m_mutually_exclusive_groups) { + MutuallyExclusiveGroup group(*this, g.m_required); + for (auto &arg : g.m_elements) { + // Find argument in argument map and add reference to it + // in new group + // argument_it = other.m_argument_map.find("name") + auto first_name = arg->m_names[0]; + auto it = m_argument_map.find(first_name); + group.m_elements.insert(&(*it->second)); + } + m_mutually_exclusive_groups.push_back(std::move(group)); + } } ~ArgumentParser() = default; @@ -1485,7 +1498,7 @@ public: private: ArgumentParser &m_parent; bool m_required{false}; - std::unordered_set m_elements{}; + std::set m_elements{}; }; MutuallyExclusiveGroup &add_mutually_exclusive_group(bool required = false) { @@ -2087,9 +2100,7 @@ private: std::list> m_subparsers; std::map m_subparser_map; std::map m_subparser_used; - std::vector - m_mutually_exclusive_groups; /// TODO: Add this to the copy/move - /// constructors + std::vector m_mutually_exclusive_groups; }; } // namespace argparse diff --git a/test/test_mutually_exclusive_group.cpp b/test/test_mutually_exclusive_group.cpp index e83e4f7..493f58a 100644 --- a/test/test_mutually_exclusive_group.cpp +++ b/test/test_mutually_exclusive_group.cpp @@ -7,7 +7,8 @@ import argparse; using doctest::test_suite; -TEST_CASE("User-supplied argument" * test_suite("is_used")) { +TEST_CASE("Create mutually exclusive group with 2 arguments" * + test_suite("mutex_args")) { argparse::ArgumentParser program("test"); auto &group = program.add_mutually_exclusive_group(); @@ -16,6 +17,23 @@ TEST_CASE("User-supplied argument" * test_suite("is_used")) { REQUIRE_THROWS_WITH_AS( program.parse_args({"test", "--first", "1", "--second", "2"}), - "Argument '--first VAR' not allowed with '--second VAR'", + "Argument '--second VAR' not allowed with '--first VAR'", + std::runtime_error); +} + +TEST_CASE( + "Create mutually exclusive group with 2 arguments, then copy the parser" * + test_suite("mutex_args")) { + argparse::ArgumentParser program("test"); + + auto &group = program.add_mutually_exclusive_group(); + group.add_argument("--first"); + group.add_argument("--second"); + + auto program_copy(program); + + REQUIRE_THROWS_WITH_AS( + program_copy.parse_args({"test", "--first", "1", "--second", "2"}), + "Argument '--second VAR' not allowed with '--first VAR'", std::runtime_error); } \ No newline at end of file