mirror of
https://github.com/KeqingMoe/argparse.git
synced 2025-07-03 14:44:40 +00:00
Merge pull request #395 from ndevenish/float
store_into: Accept float (by accepting generic floating point)
This commit is contained in:
commit
d924b84eba
@ -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;
|
||||||
|
@ -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");
|
||||||
|
Loading…
Reference in New Issue
Block a user