mirror of
https://github.com/KeqingMoe/argparse.git
synced 2025-07-04 07:04:39 +00:00
Added some complex unit tests including toggle arguments, compound arguments and positional arguments
This commit is contained in:
parent
b51ff01363
commit
a9b504665f
@ -244,7 +244,7 @@ class ArgumentParser {
|
|||||||
auto tCount = tArgument->mNumArgs - tArgument->mRawValues.size();
|
auto tCount = tArgument->mNumArgs - tArgument->mRawValues.size();
|
||||||
while (tCount > 0) {
|
while (tCount > 0) {
|
||||||
std::map<std::string, std::shared_ptr<Argument>>::iterator tIterator = mArgumentMap.find(argv[i]);
|
std::map<std::string, std::shared_ptr<Argument>>::iterator tIterator = mArgumentMap.find(argv[i]);
|
||||||
if (tIterator != mArgumentMap.end()) {
|
if (tIterator != mArgumentMap.end() || is_optional(argv[i])) {
|
||||||
i = i - 1;
|
i = i - 1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -84,13 +84,67 @@ TEST_CASE("Parse compound toggle arguments with implicit values and nargs", "[pa
|
|||||||
.nargs(2)
|
.nargs(2)
|
||||||
.action([](const std::string& value) { return std::stof(value); });
|
.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();
|
auto arguments = program.get_arguments();
|
||||||
REQUIRE(arguments.size() == 3);
|
REQUIRE(arguments.size() == 4);
|
||||||
REQUIRE(program.get<bool>("-a") == true);
|
REQUIRE(program.get<bool>("-a") == true);
|
||||||
REQUIRE(program.get<bool>("-b") == true);
|
REQUIRE(program.get<bool>("-b") == true);
|
||||||
auto c = program.get<std::vector<float>>("-c");
|
auto c = program.get<std::vector<float>>("-c");
|
||||||
REQUIRE(c.size() == 2);
|
REQUIRE(c.size() == 2);
|
||||||
REQUIRE(c[0] == 3.14f);
|
REQUIRE(c[0] == 3.14f);
|
||||||
REQUIRE(c[1] == 2.718f);
|
REQUIRE(c[1] == 2.718f);
|
||||||
|
auto input_files = program.get<std::vector<std::string>>("--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<bool>("-a") == true);
|
||||||
|
REQUIRE(program.get<bool>("-b") == true);
|
||||||
|
auto c = program.get<std::vector<float>>("-c");
|
||||||
|
REQUIRE(c.size() == 2);
|
||||||
|
REQUIRE(c[0] == 3.14f);
|
||||||
|
REQUIRE(c[1] == 2.718f);
|
||||||
|
auto input_files = program.get<std::vector<std::string>>("--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<std::vector<int>>("numbers");
|
||||||
|
REQUIRE(numbers.size() == 3);
|
||||||
|
REQUIRE(numbers[0] == 1);
|
||||||
|
REQUIRE(numbers[1] == 2);
|
||||||
|
REQUIRE(numbers[2] == 3);
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user