Skip to content

Commit

Permalink
Fixes core dump on optional arg (issue #10)
Browse files Browse the repository at this point in the history
  • Loading branch information
p-ranav committed May 4, 2019
1 parent 374c70e commit 3ea4c79
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 11 deletions.
29 changes: 19 additions & 10 deletions include/argparse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -568,16 +569,24 @@ 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;
if (i < argc) {
tArgumentsForRecursiveParsing.push_back(argv[i]);
tNumArgs -= 1;
}
}
parse_args_internal(tArgumentsForRecursiveParsing);
}
std::vector<std::string> tArgumentsForRecursiveParsing = { "", "-" + tArgument };
while (tNumArgs > 0 && i < argc) {
i += 1;
if (i < argc) {
tArgumentsForRecursiveParsing.push_back(argv[i]);
tNumArgs -= 1;
}
}
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 {
Expand Down
3 changes: 2 additions & 1 deletion test/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
#include <test_compound_arguments.hpp>
#include <test_actions.hpp>
#include <test_container_arguments.hpp>
#include <test_parent_parsers.hpp>
#include <test_parent_parsers.hpp>
#include <test_invalid_arguments.hpp>
38 changes: 38 additions & 0 deletions test/test_invalid_arguments.hpp
Original file line number Diff line number Diff line change
@@ -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" });
}

0 comments on commit 3ea4c79

Please sign in to comment.