diff --git a/include/argparse/argparse.hpp b/include/argparse/argparse.hpp index f0dd85b..f319424 100644 --- a/include/argparse/argparse.hpp +++ b/include/argparse/argparse.hpp @@ -698,9 +698,10 @@ public: return *this; } - auto &store_into(int &var) { + template ::value>::type * = nullptr> + auto &store_into(T &var) { if (m_default_value.has_value()) { - var = std::any_cast(m_default_value); + var = std::any_cast(m_default_value); } action([&var](const auto &s) { var = details::parse_number()(s); diff --git a/test/test_store_into.cpp b/test/test_store_into.cpp index 7072c3f..1f580ba 100644 --- a/test/test_store_into.cpp +++ b/test/test_store_into.cpp @@ -27,6 +27,8 @@ TEST_CASE("Test store_into(bool), flag specified" * REQUIRE(flag == true); } +// int cases + TEST_CASE("Test store_into(int), no default value, non specified" * test_suite("store_into")) { argparse::ArgumentParser program("test"); @@ -57,6 +59,40 @@ TEST_CASE("Test store_into(int), default value, specified" * REQUIRE(res == 5); } +// integral cases + +TEST_CASE("Test store_into(uint8_t), no default value, non specified" * + test_suite("store_into")) { + argparse::ArgumentParser program("test"); + uint8_t res = 55; + program.add_argument("--int-opt").store_into(res); + + program.parse_args({"./test.exe"}); + REQUIRE(res == 55); +} + +TEST_CASE("Test store_into(uint8_t), default value, non specified" * + test_suite("store_into")) { + argparse::ArgumentParser program("test"); + uint8_t res = 55; + program.add_argument("--int-opt").default_value((uint8_t)3).store_into(res); + + program.parse_args({"./test.exe"}); + REQUIRE(res == 3); +} + +TEST_CASE("Test store_into(uint8_t), default value, specified" * + test_suite("store_into")) { + argparse::ArgumentParser program("test"); + uint8_t res = -1; + program.add_argument("--int-opt").default_value((uint8_t)3).store_into(res); + + program.parse_args({"./test.exe", "--int-opt", "5"}); + REQUIRE(res == 5); +} + +// Double cases + TEST_CASE("Test store_into(double), no default value, non specified" * test_suite("store_into")) { argparse::ArgumentParser program("test");