diff --git a/README.md b/README.md index 2196ce9b..a5e07281 100644 --- a/README.md +++ b/README.md @@ -324,8 +324,9 @@ It is possible to bind arguments to a variable storing their value, as an alternative to explicitly calling ``program.get(arg_name)`` or ``program[arg_name]`` This is currently implementeted for variables of type ``bool`` (this also -implicitly calls ``flag()``), ``int``, ``double``, ``std::string`` and -``std::vector``. If the argument is not specified in the command +implicitly calls ``flag()``), ``int``, ``double``, ``std::string``, +``std::vector`` and ``std::vector``. +If the argument is not specified in the command line, the default value (if set) is set into the variable. ```cpp @@ -346,6 +347,12 @@ program.add_argument("--strvar-repeated").append().store_into(strvar_repeated); std::vector strvar_multi_valued; program.add_argument("--strvar-multi-valued").nargs(2).store_into(strvar_multi_valued); + +std::vector intvar_repeated; +program.add_argument("--intvar-repeated").append().store_into(intvar_repeated); + +std::vector intvar_multi_valued; +program.add_argument("--intvar-multi-valued").nargs(2).store_into(intvar_multi_valued); ``` ### Negative Numbers diff --git a/include/argparse/argparse.hpp b/include/argparse/argparse.hpp index ba546e33..3baba884 100644 --- a/include/argparse/argparse.hpp +++ b/include/argparse/argparse.hpp @@ -740,6 +740,20 @@ class Argument { return *this; } + auto &store_into(std::vector &var) { + if (m_default_value.has_value()) { + var = std::any_cast>(m_default_value); + } + action([this, &var](const std::string &s) { + if (!m_is_used) { + var.clear(); + } + m_is_used = true; + var.push_back(details::parse_number()(s)); + }); + return *this; + } + auto &append() { m_is_repeatable = true; return *this; diff --git a/test/test_store_into.cpp b/test/test_store_into.cpp index 4723b842..7072c3f2 100644 --- a/test/test_store_into.cpp +++ b/test/test_store_into.cpp @@ -159,3 +159,46 @@ TEST_CASE("Test store_into(vector of string), default value, multi valued, speci program.parse_args({"./test.exe", "--strvector-opt", "foo", "bar"}); REQUIRE(res == std::vector{"foo", "bar"}); } + +TEST_CASE("Test store_into(vector of int), no default value, non specified" * + test_suite("store_into")) { + argparse::ArgumentParser program("test"); + std::vector res; + program.add_argument("--intvector-opt").append().store_into(res); + + program.parse_args({"./test.exe"}); + REQUIRE(res == std::vector{}); +} + +TEST_CASE("Test store_into(vector of int), default value, non specified" * + test_suite("store_into")) { + argparse::ArgumentParser program("test"); + std::vector res; + program.add_argument("--intvector-opt").append().default_value( + std::vector{1, 2}).store_into(res); + + program.parse_args({"./test.exe"}); + REQUIRE(res == std::vector{1, 2}); +} + +TEST_CASE("Test store_into(vector of int), default value, specified" * + test_suite("store_into")) { + argparse::ArgumentParser program("test"); + std::vector res; + program.add_argument("--intvector-opt").append().default_value( + std::vector{1, 2}).store_into(res); + + program.parse_args({"./test.exe", "--intvector-opt", "3", "--intvector-opt", "4"}); + REQUIRE(res == std::vector{3, 4}); +} + +TEST_CASE("Test store_into(vector of int), default value, multi valued, specified" * + test_suite("store_into")) { + argparse::ArgumentParser program("test"); + std::vector res; + program.add_argument("--intvector-opt").nargs(2).default_value( + std::vector{1, 2}).store_into(res); + + program.parse_args({"./test.exe", "--intvector-opt", "3", "4"}); + REQUIRE(res == std::vector{3, 4}); +}