mirror of
https://github.com/KeqingMoe/argparse.git
synced 2025-07-03 14:44:40 +00:00
commit
ac4c2c2d96
@ -698,12 +698,13 @@ public:
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto &store_into(int &var) {
|
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()) {
|
if (m_default_value.has_value()) {
|
||||||
var = std::any_cast<int>(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<int, details::radix_10>()(s);
|
var = details::parse_number<T, details::radix_10>()(s);
|
||||||
});
|
});
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ import argparse;
|
|||||||
#else
|
#else
|
||||||
#include <argparse/argparse.hpp>
|
#include <argparse/argparse.hpp>
|
||||||
#endif
|
#endif
|
||||||
|
#include <cstdint>
|
||||||
#include <doctest.hpp>
|
#include <doctest.hpp>
|
||||||
|
|
||||||
using doctest::test_suite;
|
using doctest::test_suite;
|
||||||
@ -27,6 +28,8 @@ TEST_CASE("Test store_into(bool), flag specified" *
|
|||||||
REQUIRE(flag == true);
|
REQUIRE(flag == true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// int cases
|
||||||
|
|
||||||
TEST_CASE("Test store_into(int), no default value, non specified" *
|
TEST_CASE("Test store_into(int), no default value, non specified" *
|
||||||
test_suite("store_into")) {
|
test_suite("store_into")) {
|
||||||
argparse::ArgumentParser program("test");
|
argparse::ArgumentParser program("test");
|
||||||
@ -57,6 +60,40 @@ TEST_CASE("Test store_into(int), default value, specified" *
|
|||||||
REQUIRE(res == 5);
|
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 = 55;
|
||||||
|
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_CASE("Test store_into(double), 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