Fixes core dump on optional arg (issue #10)

This commit is contained in:
Pranav Srinivas Kumar 2019-05-04 11:49:30 -04:00
parent 374c70e7b3
commit 3ea4c79137
3 changed files with 59 additions and 11 deletions

View File

@ -518,7 +518,8 @@ class ArgumentParser {
print_help();
exit(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()) {
// Start parsing optional argument
auto tArgument = tIterator->second;
@ -568,7 +569,6 @@ class ArgumentParser {
if (tIterator != mArgumentMap.end()) {
auto tArgumentObject = tIterator->second;
tNumArgs = tArgumentObject->mNumArgs;
}
std::vector<std::string> tArgumentsForRecursiveParsing = { "", "-" + tArgument };
while (tNumArgs > 0 && i < argc) {
i += 1;
@ -579,6 +579,15 @@ class ArgumentParser {
}
parse_args_internal(tArgumentsForRecursiveParsing);
}
else {
if (tArgument.size() > 0 && tArgument[0] == '-')
std::cout << "warning: unrecognized optional argument " << tArgument
<< std::endl;
else
std::cout << "warning: unrecognized optional argument -" << tArgument
<< std::endl;
}
}
}
else {
std::cout << "warning: unrecognized optional argument " << tCompoundArgument << std::endl;

View File

@ -8,3 +8,4 @@
#include <test_actions.hpp>
#include <test_container_arguments.hpp>
#include <test_parent_parsers.hpp>
#include <test_invalid_arguments.hpp>

View File

@ -0,0 +1,38 @@
#pragma once
#include <catch.hpp>
#include <argparse.hpp>
TEST_CASE("Parse unknown optional argument", "[compound_arguments]") {
argparse::ArgumentParser bfm("bfm");
bfm.add_argument("-l","--load")
.help("load a VMM into the kernel");
bfm.add_argument("-x", "--start")
.default_value(false)
.implicit_value(true)
.help("start a previously loaded VMM");
bfm.add_argument("-d", "--dump")
.default_value(false)
.implicit_value(true)
.help("output the contents of the VMM's debug buffer");
bfm.add_argument("-s", "--stop")
.default_value(false)
.implicit_value(true)
.help("stop a previously started VMM");
bfm.add_argument("-u", "--unload")
.default_value(false)
.implicit_value(true)
.help("unload a previously loaded VMM");
bfm.add_argument("-m", "--mem")
.default_value(64ULL)
.action([](const std::string& val) { return std::stoull(val); })
.help("memory in MB to give the VMM when loading");
bfm.parse_args({ "./test.exe", "-om" });
}