mirror of
https://github.com/KeqingMoe/argparse.git
synced 2025-07-03 22:54:39 +00:00
Add Argument::store_into(std::set<int||string> &var) method
This commit is contained in:
parent
ac4c2c2d96
commit
b85a0a414d
@ -36,6 +36,7 @@ SOFTWARE.
|
|||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <any>
|
#include <any>
|
||||||
#include <array>
|
#include <array>
|
||||||
|
#include <set>
|
||||||
#include <charconv>
|
#include <charconv>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
@ -755,6 +756,34 @@ public:
|
|||||||
return *this;
|
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() {
|
auto &append() {
|
||||||
m_is_repeatable = true;
|
m_is_repeatable = true;
|
||||||
return *this;
|
return *this;
|
||||||
|
@ -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"});
|
program.parse_args({"./test.exe", "--intvector-opt", "3", "4"});
|
||||||
REQUIRE(res == std::vector<int>{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"});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user