diff --git a/src/argparse.hpp b/src/argparse.hpp index 2638c65..6a62001 100644 --- a/src/argparse.hpp +++ b/src/argparse.hpp @@ -49,6 +49,11 @@ T get_from_list(const std::list& aList, size_t aIndex) { return T(); } +template +T get_from_list(const std::vector& aList, size_t aIndex) { + return aList[aIndex]; +} + struct Argument { std::vector mNames; std::string mHelp; @@ -96,7 +101,7 @@ struct Argument { } template - T get() { + T get() const { if (mValues.size() == 0) { if (mDefaultValue.has_value()) { return std::any_cast(mDefaultValue); @@ -117,7 +122,7 @@ struct Argument { } template - T get_vector() { + T get_vector() const { T tResult; if (mValues.size() == 0) { if (mDefaultValue.has_value()) { @@ -152,7 +157,7 @@ struct Argument { } template - T get_list() { + T get_list() const { T tResult; if (mValues.size() == 0) { if (mDefaultValue.has_value()) { @@ -186,6 +191,50 @@ struct Argument { } } + template + bool operator!=(const T& aRhs) const { + return !(*this == aRhs); + } + + template + typename std::enable_if::value == false && + is_specialization::value == false, bool>::type + operator==(const T& aRhs) const { + return get() == aRhs; + } + + template + typename std::enable_if::value, bool>::type + operator==(const T& aRhs) const { + T tLhs = get_vector(); + if (tLhs.size() != aRhs.size()) + return false; + else { + for (size_t i = 0; i < tLhs.size(); i++) { + auto tValueAtIndex = std::any_cast(tLhs[i]); + if (tValueAtIndex != aRhs[i]) + return false; + } + return true; + } + } + + template + typename std::enable_if::value, bool>::type + operator==(const T& aRhs) const { + T tLhs = get_list(); + if (tLhs.size() != aRhs.size()) + return false; + else { + for (size_t i = 0; i < tLhs.size(); i++) { + auto tValueAtIndex = std::any_cast(get_from_list(tLhs, i)); + if (tValueAtIndex != get_from_list(aRhs, i)) + return false; + } + return true; + } + } + }; class ArgumentParser { @@ -350,13 +399,13 @@ class ArgumentParser { return mArgumentMap; } - std::shared_ptr operator[](const std::string& aArgumentName) { + Argument& operator[](const std::string& aArgumentName) { std::map>::iterator tIterator = mArgumentMap.find(aArgumentName); if (tIterator != mArgumentMap.end()) { - return tIterator->second; + return *(tIterator->second); } else { - return nullptr; + throw std::runtime_error("Argument " + aArgumentName + " not found"); } } diff --git a/tests/test_container_arguments.hpp b/tests/test_container_arguments.hpp index a2ee85a..7ea4928 100644 --- a/tests/test_container_arguments.hpp +++ b/tests/test_container_arguments.hpp @@ -50,6 +50,7 @@ TEST_CASE("Parse list of arguments with default values", "[vector]") { REQUIRE(argparse::get_from_list(inputs, 2) == 3); REQUIRE(argparse::get_from_list(inputs, 3) == 4); REQUIRE(argparse::get_from_list(inputs, 4) == 5); + REQUIRE(program["input"] == std::list{1, 2, 3, 4, 5}); } TEST_CASE("Parse list of arguments and save in an object", "[vector]") { @@ -78,4 +79,5 @@ TEST_CASE("Parse list of arguments and save in an object", "[vector]") { REQUIRE(config_manager.files.size() == 2); REQUIRE(config_manager.files[0] == "config.xml"); REQUIRE(config_manager.files[1] == "system.json"); + REQUIRE(program["--input_files"] == std::vector{"config.xml", "system.json"}); } \ No newline at end of file diff --git a/tests/test_optional_arguments.hpp b/tests/test_optional_arguments.hpp index edd1b64..a89154e 100644 --- a/tests/test_optional_arguments.hpp +++ b/tests/test_optional_arguments.hpp @@ -12,6 +12,7 @@ TEST_CASE("Parse toggle arguments with default value", "[optional_arguments]") { auto arguments = program.get_arguments(); REQUIRE(arguments.size() == 2); REQUIRE(program.get("--verbose") == false); + REQUIRE(program["--verbose"] == false); } TEST_CASE("Parse toggle arguments with implicit value", "[optional_arguments]") { @@ -24,6 +25,8 @@ TEST_CASE("Parse toggle arguments with implicit value", "[optional_arguments]") auto arguments = program.get_arguments(); REQUIRE(arguments.size() == 1); REQUIRE(program.get("--verbose") == true); + REQUIRE(program["--verbose"] == true); + REQUIRE(program["--verbose"] != false); } TEST_CASE("Parse multiple toggle arguments with implicit values", "[optional_arguments]") {