From 3ea4c791374554327954bf9160da08ff3fca312e Mon Sep 17 00:00:00 2001 From: Pranav Srinivas Kumar Date: Sat, 4 May 2019 11:49:30 -0400 Subject: [PATCH] Fixes core dump on optional arg (issue #10) --- include/argparse.hpp | 29 ++++++++++++++++--------- test/main.cpp | 3 ++- test/test_invalid_arguments.hpp | 38 +++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 11 deletions(-) create mode 100644 test/test_invalid_arguments.hpp diff --git a/include/argparse.hpp b/include/argparse.hpp index f755bbe0..930fcc54 100644 --- a/include/argparse.hpp +++ b/include/argparse.hpp @@ -518,7 +518,8 @@ class ArgumentParser { print_help(); exit(0); } - std::map>::iterator tIterator = mArgumentMap.find(argv[i]); + std::map>::iterator tIterator = + mArgumentMap.find(argv[i]); if (tIterator != mArgumentMap.end()) { // Start parsing optional argument auto tArgument = tIterator->second; @@ -568,16 +569,24 @@ class ArgumentParser { if (tIterator != mArgumentMap.end()) { auto tArgumentObject = tIterator->second; tNumArgs = tArgumentObject->mNumArgs; + std::vector 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 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 { diff --git a/test/main.cpp b/test/main.cpp index 98c2408d..99ae60d4 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -7,4 +7,5 @@ #include #include #include -#include \ No newline at end of file +#include +#include diff --git a/test/test_invalid_arguments.hpp b/test/test_invalid_arguments.hpp new file mode 100644 index 00000000..f466be8e --- /dev/null +++ b/test/test_invalid_arguments.hpp @@ -0,0 +1,38 @@ +#pragma once +#include +#include + +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" }); +}