Merge pull request #343 from rouault/store_into_vector_int

Add Argument::store_into(std::vector<int> &var) method
This commit is contained in:
Pranav 2024-04-02 19:45:31 -04:00 committed by GitHub
commit 1c4820579c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 66 additions and 2 deletions

View File

@ -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]`` alternative to explicitly calling ``program.get<T>(arg_name)`` or ``program[arg_name]``
This is currently implementeted for variables of type ``bool`` (this also This is currently implementeted for variables of type ``bool`` (this also
implicitly calls ``flag()``), ``int``, ``double``, ``std::string`` and implicitly calls ``flag()``), ``int``, ``double``, ``std::string``,
``std::vector<std::string>``. If the argument is not specified in the command ``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. line, the default value (if set) is set into the variable.
```cpp ```cpp
@ -346,6 +347,12 @@ program.add_argument("--strvar-repeated").append().store_into(strvar_repeated);
std::vector<std::string> strvar_multi_valued; std::vector<std::string> strvar_multi_valued;
program.add_argument("--strvar-multi-valued").nargs(2).store_into(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 ### Negative Numbers

View File

@ -740,6 +740,20 @@ public:
return *this; 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() { auto &append() {
m_is_repeatable = true; m_is_repeatable = true;
return *this; return *this;

View File

@ -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"}); program.parse_args({"./test.exe", "--strvector-opt", "foo", "bar"});
REQUIRE(res == std::vector<std::string>{"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});
}