Checking if i < argc on edge case. New tests added

This commit is contained in:
Pranav Srinivas Kumar 2019-04-01 20:06:54 -04:00
parent 2149ab40b9
commit e5c3630eba
2 changed files with 32 additions and 3 deletions

View File

@ -478,11 +478,13 @@ class ArgumentParser {
tNumArgs = tArgumentObject->mNumArgs; tNumArgs = tArgumentObject->mNumArgs;
} }
std::vector<std::string> tArgumentsForRecursiveParsing = { "", "-" + tArgument }; std::vector<std::string> tArgumentsForRecursiveParsing = { "", "-" + tArgument };
while (tNumArgs > 0) { while (tNumArgs > 0 && i < argc) {
i += 1; i += 1;
if (i < argc) {
tArgumentsForRecursiveParsing.push_back(argv[i]); tArgumentsForRecursiveParsing.push_back(argv[i]);
tNumArgs -= 1; tNumArgs -= 1;
} }
}
parse_args_internal(tArgumentsForRecursiveParsing); parse_args_internal(tArgumentsForRecursiveParsing);
} }
} }

View File

@ -123,3 +123,30 @@ TEST_CASE("Parse out-of-order compound arguments", "[compound_arguments]") {
auto b = program.get<bool>("-b"); // true auto b = program.get<bool>("-b"); // true
auto c = program.get<std::vector<float>>("-c"); // {3.14f, 2.718f} 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});
}