Merge pull request #395 from ndevenish/float

store_into: Accept float (by accepting generic floating point)
This commit is contained in:
Pranav 2025-01-26 18:27:21 -05:00 committed by GitHub
commit d924b84eba
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 36 additions and 3 deletions

View File

@ -717,12 +717,13 @@ public:
return *this; return *this;
} }
auto &store_into(double &var) { template <typename T, typename std::enable_if<std::is_floating_point<T>::value>::type * = nullptr>
auto &store_into(T &var) {
if (m_default_value.has_value()) { if (m_default_value.has_value()) {
var = std::any_cast<double>(m_default_value); var = std::any_cast<T>(m_default_value);
} }
action([&var](const auto &s) { action([&var](const auto &s) {
var = details::parse_number<double, details::chars_format::general>()(s); var = details::parse_number<T, details::chars_format::general>()(s);
return var; return var;
}); });
return *this; return *this;

View File

@ -124,6 +124,38 @@ TEST_CASE("Test store_into(double), default value, specified" *
REQUIRE(res == 5.5); REQUIRE(res == 5.5);
} }
// Float cases
TEST_CASE("Test store_into(float), no default value, non specified" *
test_suite("store_into")) {
argparse::ArgumentParser program("test");
float res = -1.0f;
program.add_argument("--float-opt").store_into(res);
program.parse_args({"./test.exe"});
REQUIRE(res == -1.0f);
}
TEST_CASE("Test store_into(float), default value, non specified" *
test_suite("store_into")) {
argparse::ArgumentParser program("test");
float res = -1.0f;
program.add_argument("--float-opt").default_value(3.5f).store_into(res);
program.parse_args({"./test.exe"});
REQUIRE(res == 3.5f);
}
TEST_CASE("Test store_into(float), default value, specified" *
test_suite("store_into")) {
argparse::ArgumentParser program("test");
float res = -1.0f;
program.add_argument("--float-opt").default_value(3.5f).store_into(res);
program.parse_args({"./test.exe", "--float-opt", "5.5"});
REQUIRE(res == 5.5f);
}
TEST_CASE("Test store_into(string), no default value, non specified" * TEST_CASE("Test store_into(string), no default value, non specified" *
test_suite("store_into")) { test_suite("store_into")) {
argparse::ArgumentParser program("test"); argparse::ArgumentParser program("test");