Skip to content

Commit

Permalink
Merge pull request #343 from rouault/store_into_vector_int
Browse files Browse the repository at this point in the history
Add Argument::store_into(std::vector<int> &var) method
  • Loading branch information
p-ranav authored Apr 2, 2024
2 parents a1c41c5 + 2936725 commit 1c48205
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
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});
}

0 comments on commit 1c48205

Please sign in to comment.