From 28e084dfd8bf63f2026f1fee422aa2d525798233 Mon Sep 17 00:00:00 2001 From: Pranav Srinivas Kumar Date: Sun, 31 Mar 2019 15:15:56 -0400 Subject: [PATCH] Added an example that uses a reference catching in action lambda --- tests/test_container_arguments.hpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tests/test_container_arguments.hpp b/tests/test_container_arguments.hpp index fc6429b..a2ee85a 100644 --- a/tests/test_container_arguments.hpp +++ b/tests/test_container_arguments.hpp @@ -50,4 +50,32 @@ TEST_CASE("Parse list of arguments with default values", "[vector]") { REQUIRE(argparse::get_from_list(inputs, 2) == 3); REQUIRE(argparse::get_from_list(inputs, 3) == 4); REQUIRE(argparse::get_from_list(inputs, 4) == 5); +} + +TEST_CASE("Parse list of arguments and save in an object", "[vector]") { + + struct ConfigManager { + std::vector files; + void add_file(const std::string& file) { + files.push_back(file); + } + }; + + ConfigManager config_manager; + + argparse::ArgumentParser program("test"); + program.add_argument("--input_files") + .nargs(2) + .action([&](const std::string& value) { config_manager.add_file(value); return value; }); + + program.parse_args({ "test", "--input_files", "config.xml", "system.json" }); + + auto file_args = program.get>("--input_files"); + REQUIRE(file_args.size() == 2); + REQUIRE(file_args[0] == "config.xml"); + REQUIRE(file_args[1] == "system.json"); + + REQUIRE(config_manager.files.size() == 2); + REQUIRE(config_manager.files[0] == "config.xml"); + REQUIRE(config_manager.files[1] == "system.json"); } \ No newline at end of file