From d141b8d2a151d5454cedcb127c352bec93e96b56 Mon Sep 17 00:00:00 2001 From: Andrew Bell Date: Wed, 17 Apr 2024 13:35:51 -0400 Subject: [PATCH 1/4] Add support for general integer types in store_into. --- include/argparse/argparse.hpp | 5 +++-- test/test_store_into.cpp | 36 +++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/include/argparse/argparse.hpp b/include/argparse/argparse.hpp index f0dd85b6..f3194244 100644 --- a/include/argparse/argparse.hpp +++ b/include/argparse/argparse.hpp @@ -698,9 +698,10 @@ class Argument { 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 7072c3f2..1f580bab 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"); From e54e4592867cb9ddb86edd1f18f7f9c401abb170 Mon Sep 17 00:00:00 2001 From: Andrew Bell Date: Wed, 17 Apr 2024 13:42:37 -0400 Subject: [PATCH 2/4] Pass type along to parse_number(). --- include/argparse/argparse.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/argparse/argparse.hpp b/include/argparse/argparse.hpp index f3194244..118e2145 100644 --- a/include/argparse/argparse.hpp +++ b/include/argparse/argparse.hpp @@ -704,7 +704,7 @@ class Argument { var = std::any_cast(m_default_value); } action([&var](const auto &s) { - var = details::parse_number()(s); + var = details::parse_number()(s); }); return *this; } From 29f1d12333ca12884f1b63b811c3ac46b0c6461a Mon Sep 17 00:00:00 2001 From: Andrew Bell Date: Thu, 18 Apr 2024 08:24:57 -0400 Subject: [PATCH 3/4] Add cstdint header. --- test/test_store_into.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/test/test_store_into.cpp b/test/test_store_into.cpp index 1f580bab..c1d28bc0 100644 --- a/test/test_store_into.cpp +++ b/test/test_store_into.cpp @@ -3,6 +3,7 @@ import argparse; #else #include #endif +#include #include using doctest::test_suite; From 7cb70ed6f3130b70122be761786a7154e45b7640 Mon Sep 17 00:00:00 2001 From: Andrew Bell Date: Thu, 18 Apr 2024 18:11:07 -0400 Subject: [PATCH 4/4] Don't store -1 into unsigned value. --- test/test_store_into.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_store_into.cpp b/test/test_store_into.cpp index c1d28bc0..c9b7a40d 100644 --- a/test/test_store_into.cpp +++ b/test/test_store_into.cpp @@ -85,7 +85,7 @@ TEST_CASE("Test store_into(uint8_t), default value, non specified" * TEST_CASE("Test store_into(uint8_t), default value, specified" * test_suite("store_into")) { argparse::ArgumentParser program("test"); - uint8_t res = -1; + uint8_t res = 55; program.add_argument("--int-opt").default_value((uint8_t)3).store_into(res); program.parse_args({"./test.exe", "--int-opt", "5"});