mirror of
https://github.com/KeqingMoe/argparse.git
synced 2025-07-04 15:14:39 +00:00
Checking if i < argc on edge case. New tests added
This commit is contained in:
parent
2149ab40b9
commit
e5c3630eba
@ -478,10 +478,12 @@ class ArgumentParser {
|
||||
tNumArgs = tArgumentObject->mNumArgs;
|
||||
}
|
||||
std::vector<std::string> tArgumentsForRecursiveParsing = { "", "-" + tArgument };
|
||||
while (tNumArgs > 0) {
|
||||
while (tNumArgs > 0 && i < argc) {
|
||||
i += 1;
|
||||
tArgumentsForRecursiveParsing.push_back(argv[i]);
|
||||
tNumArgs -= 1;
|
||||
if (i < argc) {
|
||||
tArgumentsForRecursiveParsing.push_back(argv[i]);
|
||||
tNumArgs -= 1;
|
||||
}
|
||||
}
|
||||
parse_args_internal(tArgumentsForRecursiveParsing);
|
||||
}
|
||||
|
@ -123,3 +123,30 @@ TEST_CASE("Parse out-of-order compound arguments", "[compound_arguments]") {
|
||||
auto b = program.get<bool>("-b"); // true
|
||||
auto c = program.get<std::vector<float>>("-c"); // {3.14f, 2.718f}
|
||||
}
|
||||
|
||||
TEST_CASE("Parse out-of-order compound arguments. Second variation", "[compound_arguments]") {
|
||||
argparse::ArgumentParser program("test");
|
||||
|
||||
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)
|
||||
.default_value(std::vector<float>{0.0f, 0.0f})
|
||||
.action([](const std::string& value) { return std::stof(value); });
|
||||
|
||||
program.parse_args({"./main", "-cb"});
|
||||
|
||||
auto a = program.get<bool>("-a");
|
||||
auto b = program.get<bool>("-b");
|
||||
auto c = program.get<std::vector<float>>("-c");
|
||||
|
||||
REQUIRE(a == false);
|
||||
REQUIRE(b == true);
|
||||
REQUIRE(program["-c"] == std::vector<float>{0.0f, 0.0f});
|
||||
}
|
Loading…
Reference in New Issue
Block a user