Skip to content

Commit

Permalink
Merge pull request #348 from elpaso/store_into-set-of-string-int
Browse files Browse the repository at this point in the history
Add Argument::store_into(std::set<int||string> &var) method
  • Loading branch information
p-ranav authored May 1, 2024
2 parents ac4c2c2 + b85a0a4 commit 805e235
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
29 changes: 29 additions & 0 deletions include/argparse/argparse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ SOFTWARE.
#include <algorithm>
#include <any>
#include <array>
#include <set>
#include <charconv>
#include <cstdlib>
#include <functional>
Expand Down Expand Up @@ -755,6 +756,34 @@ class Argument {
return *this;
}

auto &store_into(std::set<std::string> &var) {
if (m_default_value.has_value()) {
var = std::any_cast<std::set<std::string>>(m_default_value);
}
action([this, &var](const std::string &s) {
if (!m_is_used) {
var.clear();
}
m_is_used = true;
var.insert(s);
});
return *this;
}

auto &store_into(std::set<int> &var) {
if (m_default_value.has_value()) {
var = std::any_cast<std::set<int>>(m_default_value);
}
action([this, &var](const std::string &s) {
if (!m_is_used) {
var.clear();
}
m_is_used = true;
var.insert(details::parse_number<int, details::radix_10>()(s));
});
return *this;
}

auto &append() {
m_is_repeatable = true;
return *this;
Expand Down
47 changes: 47 additions & 0 deletions test/test_store_into.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,50 @@ TEST_CASE("Test store_into(vector of int), default value, multi valued, specifie
program.parse_args({"./test.exe", "--intvector-opt", "3", "4"});
REQUIRE(res == std::vector<int>{3, 4});
}

TEST_CASE("Test store_into(set of int), default value, multi valued, specified" *
test_suite("store_into")) {

{
argparse::ArgumentParser program("test");
std::set<int> res;
program.add_argument("--intset-opt").nargs(2).default_value(
std::set<int>{1, 2}).store_into(res);

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

{
argparse::ArgumentParser program("test");
std::set<int> res;
program.add_argument("--intset-opt").nargs(2).default_value(
std::set<int>{1, 2}).store_into(res);
program.parse_args({"./test.exe"});
REQUIRE(res == std::set<int>{1, 2});
}
}

TEST_CASE("Test store_into(set of string), default value, multi valued, specified" *
test_suite("store_into")) {

{
argparse::ArgumentParser program("test");
std::set<std::string> res;
program.add_argument("--stringset-opt").nargs(2).default_value(
std::set<std::string>{"1", "2"}).store_into(res);

program.parse_args({"./test.exe", "--stringset-opt", "3", "4"});
REQUIRE(res == std::set<std::string>{"3", "4"});
}

{
argparse::ArgumentParser program("test");
std::set<std::string> res;
program.add_argument("--stringset-opt").nargs(2).default_value(
std::set<std::string>{"1", "2"}).store_into(res);
program.parse_args({"./test.exe"});
REQUIRE(res == std::set<std::string>{"1", "2"});
}
}

0 comments on commit 805e235

Please sign in to comment.