mirror of
https://github.com/KeqingMoe/argparse.git
synced 2025-07-04 07:04:39 +00:00
Added an example that uses a reference catching in action lambda
This commit is contained in:
parent
953f7d9462
commit
28e084dfd8
@ -51,3 +51,31 @@ TEST_CASE("Parse list of arguments with default values", "[vector]") {
|
||||
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<std::string> 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<std::vector<std::string>>("--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");
|
||||
}
|
Loading…
Reference in New Issue
Block a user