Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept integer literals in store_into #363

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions include/argparse/argparse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,14 @@ class Argument {
template <typename T, typename std::enable_if<std::is_integral<T>::value>::type * = nullptr>
auto &store_into(T &var) {
if (m_default_value.has_value()) {
var = std::any_cast<T>(m_default_value);
try
{
var = std::any_cast<T>(m_default_value);
}
catch (...)
{
var = static_cast<T>(std::any_cast<int>(m_default_value));
}
}
action([&var](const auto &s) {
var = details::parse_number<T, details::radix_10>()(s);
Expand All @@ -712,7 +719,14 @@ class Argument {

auto &store_into(double &var) {
if (m_default_value.has_value()) {
var = std::any_cast<double>(m_default_value);
try
{
var = std::any_cast<double>(m_default_value);
}
catch (...)
{
var = std::any_cast<int>(m_default_value);
}
}
action([&var](const auto &s) {
var = details::parse_number<double, details::chars_format::general>()(s);
Expand Down
8 changes: 8 additions & 0 deletions test/test_store_into.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,11 @@ TEST_CASE("Test store_into(set of string), default value, multi valued, specifie
}
}

TEST_CASE("Test store_into other types with integer default" * test_suite("store_into"))
{
argparse::ArgumentParser program("test");
double res = -1;
REQUIRE_NOTHROW(program.add_argument("--double-opt").default_value(3).store_into(res));
uint8_t u = 25;
REQUIRE_NOTHROW(program.add_argument("--uint8_t-opt").default_value(3).store_into(u));
}
Loading