From a9b504665f8891b077d5f2997f1294397855087f Mon Sep 17 00:00:00 2001 From: Pranav Srinivas Kumar Date: Sun, 31 Mar 2019 11:03:25 -0400 Subject: [PATCH] Added some complex unit tests including toggle arguments, compound arguments and positional arguments --- src/argparse.hpp | 2 +- tests/test_implicit_values.hpp | 58 ++++++++++++++++++++++++++++++++-- 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/src/argparse.hpp b/src/argparse.hpp index eed4f55..1ec1565 100644 --- a/src/argparse.hpp +++ b/src/argparse.hpp @@ -244,7 +244,7 @@ class ArgumentParser { auto tCount = tArgument->mNumArgs - tArgument->mRawValues.size(); while (tCount > 0) { std::map>::iterator tIterator = mArgumentMap.find(argv[i]); - if (tIterator != mArgumentMap.end()) { + if (tIterator != mArgumentMap.end() || is_optional(argv[i])) { i = i - 1; break; } diff --git a/tests/test_implicit_values.hpp b/tests/test_implicit_values.hpp index 1c7469a..dffea11 100644 --- a/tests/test_implicit_values.hpp +++ b/tests/test_implicit_values.hpp @@ -84,13 +84,67 @@ TEST_CASE("Parse compound toggle arguments with implicit values and nargs", "[pa .nargs(2) .action([](const std::string& value) { return std::stof(value); }); - program.parse_args({ "./test.exe", "-abc", "3.14", "2.718" }); + program.add_argument("--input_files") + .nargs(3); + + program.parse_args({ "./test.exe", "-abc", "3.14", "2.718", "--input_files", + "a.txt", "b.txt", "c.txt"}); auto arguments = program.get_arguments(); - REQUIRE(arguments.size() == 3); + REQUIRE(arguments.size() == 4); REQUIRE(program.get("-a") == true); REQUIRE(program.get("-b") == true); auto c = program.get>("-c"); REQUIRE(c.size() == 2); REQUIRE(c[0] == 3.14f); REQUIRE(c[1] == 2.718f); + auto input_files = program.get>("--input_files"); + REQUIRE(input_files.size() == 3); + REQUIRE(input_files[0] == "a.txt"); + REQUIRE(input_files[1] == "b.txt"); + REQUIRE(input_files[2] == "c.txt"); +} + +TEST_CASE("Parse compound toggle arguments with implicit values and nargs and other positional arguments", "[parse_args]") { + argparse::ArgumentParser program("test"); + + program.add_argument("numbers") + .nargs(3) + .action([](const std::string& value) { return std::stoi(value); }); + + program.add_argument("-a") + .default_value(false) + .implicit_value(true); + + program.add_argument("-b") + .default_value(false) + .implicit_value(true); + + program.add_argument("-c") + .nargs(2) + .action([](const std::string& value) { return std::stof(value); }); + + program.add_argument("--input_files") + .nargs(3); + + program.parse_args({ "./test.exe", "1", "-abc", "3.14", "2.718", "2", "--input_files", + "a.txt", "b.txt", "c.txt", "3" }); + + auto arguments = program.get_arguments(); + REQUIRE(arguments.size() == 5); + REQUIRE(program.get("-a") == true); + REQUIRE(program.get("-b") == true); + auto c = program.get>("-c"); + REQUIRE(c.size() == 2); + REQUIRE(c[0] == 3.14f); + REQUIRE(c[1] == 2.718f); + auto input_files = program.get>("--input_files"); + REQUIRE(input_files.size() == 3); + REQUIRE(input_files[0] == "a.txt"); + REQUIRE(input_files[1] == "b.txt"); + REQUIRE(input_files[2] == "c.txt"); + auto numbers = program.get>("numbers"); + REQUIRE(numbers.size() == 3); + REQUIRE(numbers[0] == 1); + REQUIRE(numbers[1] == 2); + REQUIRE(numbers[2] == 3); } \ No newline at end of file