diff --git a/include/argparse/argparse.hpp b/include/argparse/argparse.hpp index 118e2145..b1faa28e 100644 --- a/include/argparse/argparse.hpp +++ b/include/argparse/argparse.hpp @@ -36,6 +36,7 @@ SOFTWARE. #include #include #include +#include #include #include #include @@ -755,6 +756,34 @@ class Argument { return *this; } + auto &store_into(std::set &var) { + if (m_default_value.has_value()) { + var = std::any_cast>(m_default_value); + } + action([this, &var](const std::string &s) { + if (!m_is_used) { + var.clear(); + } + m_is_used = true; + var.insert(s); + }); + return *this; + } + + auto &store_into(std::set &var) { + if (m_default_value.has_value()) { + var = std::any_cast>(m_default_value); + } + action([this, &var](const std::string &s) { + if (!m_is_used) { + var.clear(); + } + m_is_used = true; + var.insert(details::parse_number()(s)); + }); + return *this; + } + auto &append() { m_is_repeatable = true; return *this; diff --git a/test/test_store_into.cpp b/test/test_store_into.cpp index c9b7a40d..9717fab8 100644 --- a/test/test_store_into.cpp +++ b/test/test_store_into.cpp @@ -239,3 +239,50 @@ TEST_CASE("Test store_into(vector of int), default value, multi valued, specifie program.parse_args({"./test.exe", "--intvector-opt", "3", "4"}); REQUIRE(res == std::vector{3, 4}); } + +TEST_CASE("Test store_into(set of int), default value, multi valued, specified" * + test_suite("store_into")) { + + { + argparse::ArgumentParser program("test"); + std::set res; + program.add_argument("--intset-opt").nargs(2).default_value( + std::set{1, 2}).store_into(res); + + program.parse_args({"./test.exe", "--intset-opt", "3", "4"}); + REQUIRE(res == std::set{3, 4}); + } + + { + argparse::ArgumentParser program("test"); + std::set res; + program.add_argument("--intset-opt").nargs(2).default_value( + std::set{1, 2}).store_into(res); + program.parse_args({"./test.exe"}); + REQUIRE(res == std::set{1, 2}); + } +} + +TEST_CASE("Test store_into(set of string), default value, multi valued, specified" * + test_suite("store_into")) { + + { + argparse::ArgumentParser program("test"); + std::set res; + program.add_argument("--stringset-opt").nargs(2).default_value( + std::set{"1", "2"}).store_into(res); + + program.parse_args({"./test.exe", "--stringset-opt", "3", "4"}); + REQUIRE(res == std::set{"3", "4"}); + } + + { + argparse::ArgumentParser program("test"); + std::set res; + program.add_argument("--stringset-opt").nargs(2).default_value( + std::set{"1", "2"}).store_into(res); + program.parse_args({"./test.exe"}); + REQUIRE(res == std::set{"1", "2"}); + } +} +