Added mutex args to copy constructor, changed to ordered set for data structure

This commit is contained in:
Pranav Srinivas Kumar 2023-11-04 15:19:10 -05:00
parent 39988ec62d
commit eea95c0e3a
2 changed files with 36 additions and 7 deletions

View File

@ -46,13 +46,13 @@ SOFTWARE.
#include <map> #include <map>
#include <numeric> #include <numeric>
#include <optional> #include <optional>
#include <set>
#include <sstream> #include <sstream>
#include <stdexcept> #include <stdexcept>
#include <string> #include <string>
#include <string_view> #include <string_view>
#include <tuple> #include <tuple>
#include <type_traits> #include <type_traits>
#include <unordered_set>
#include <utility> #include <utility>
#include <variant> #include <variant>
#include <vector> #include <vector>
@ -1419,6 +1419,19 @@ public:
m_subparser_map.insert_or_assign(it->get().m_program_name, it); m_subparser_map.insert_or_assign(it->get().m_program_name, it);
m_subparser_used.insert_or_assign(it->get().m_program_name, false); 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; ~ArgumentParser() = default;
@ -1485,7 +1498,7 @@ public:
private: private:
ArgumentParser &m_parent; ArgumentParser &m_parent;
bool m_required{false}; bool m_required{false};
std::unordered_set<Argument *> m_elements{}; std::set<Argument *> m_elements{};
}; };
MutuallyExclusiveGroup &add_mutually_exclusive_group(bool required = false) { MutuallyExclusiveGroup &add_mutually_exclusive_group(bool required = false) {
@ -2087,9 +2100,7 @@ private:
std::list<std::reference_wrapper<ArgumentParser>> m_subparsers; std::list<std::reference_wrapper<ArgumentParser>> m_subparsers;
std::map<std::string_view, argument_parser_it> m_subparser_map; std::map<std::string_view, argument_parser_it> m_subparser_map;
std::map<std::string_view, bool> m_subparser_used; std::map<std::string_view, bool> m_subparser_used;
std::vector<MutuallyExclusiveGroup> std::vector<MutuallyExclusiveGroup> m_mutually_exclusive_groups;
m_mutually_exclusive_groups; /// TODO: Add this to the copy/move
/// constructors
}; };
} // namespace argparse } // namespace argparse

View File

@ -7,7 +7,8 @@ import argparse;
using doctest::test_suite; 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"); argparse::ArgumentParser program("test");
auto &group = program.add_mutually_exclusive_group(); auto &group = program.add_mutually_exclusive_group();
@ -16,6 +17,23 @@ TEST_CASE("User-supplied argument" * test_suite("is_used")) {
REQUIRE_THROWS_WITH_AS( REQUIRE_THROWS_WITH_AS(
program.parse_args({"test", "--first", "1", "--second", "2"}), 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); std::runtime_error);
} }