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

Add Argument::store_into(std::vector<int> &var) method #343

Merged
merged 1 commit into from
Apr 2, 2024
Merged
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
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,9 @@ It is possible to bind arguments to a variable storing their value, as an
alternative to explicitly calling ``program.get<T>(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<std::string>``. If the argument is not specified in the command
implicitly calls ``flag()``), ``int``, ``double``, ``std::string``,
``std::vector<std::string>`` and ``std::vector<int>``.
If the argument is not specified in the command
line, the default value (if set) is set into the variable.

```cpp
Expand All @@ -346,6 +347,12 @@ program.add_argument("--strvar-repeated").append().store_into(strvar_repeated);

std::vector<std::string> strvar_multi_valued;
program.add_argument("--strvar-multi-valued").nargs(2).store_into(strvar_multi_valued);

std::vector<int> intvar_repeated;
program.add_argument("--intvar-repeated").append().store_into(intvar_repeated);

std::vector<int> intvar_multi_valued;
program.add_argument("--intvar-multi-valued").nargs(2).store_into(intvar_multi_valued);
```

### Negative Numbers
Expand Down
14 changes: 14 additions & 0 deletions include/argparse/argparse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,20 @@ class Argument {
return *this;
}

auto &store_into(std::vector<int> &var) {
if (m_default_value.has_value()) {
var = std::any_cast<std::vector<int>>(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<int, details::radix_10>()(s));
});
return *this;
}

auto &append() {
m_is_repeatable = true;
return *this;
Expand Down
43 changes: 43 additions & 0 deletions test/test_store_into.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string>{"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<int> res;
program.add_argument("--intvector-opt").append().store_into(res);

program.parse_args({"./test.exe"});
REQUIRE(res == std::vector<int>{});
}

TEST_CASE("Test store_into(vector of int), default value, non specified" *
test_suite("store_into")) {
argparse::ArgumentParser program("test");
std::vector<int> res;
program.add_argument("--intvector-opt").append().default_value(
std::vector<int>{1, 2}).store_into(res);

program.parse_args({"./test.exe"});
REQUIRE(res == std::vector<int>{1, 2});
}

TEST_CASE("Test store_into(vector of int), default value, specified" *
test_suite("store_into")) {
argparse::ArgumentParser program("test");
std::vector<int> res;
program.add_argument("--intvector-opt").append().default_value(
std::vector<int>{1, 2}).store_into(res);

program.parse_args({"./test.exe", "--intvector-opt", "3", "--intvector-opt", "4"});
REQUIRE(res == std::vector<int>{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<int> res;
program.add_argument("--intvector-opt").nargs(2).default_value(
std::vector<int>{1, 2}).store_into(res);

program.parse_args({"./test.exe", "--intvector-opt", "3", "4"});
REQUIRE(res == std::vector<int>{3, 4});
}
Loading