From 6344b5dcc7b17adcb44560e7a1d573725510f1ee Mon Sep 17 00:00:00 2001 From: Sean Robinson Date: Thu, 22 Jul 2021 06:45:44 -0700 Subject: [PATCH] Add argument name in exception thrown by Argument::consume Here, the user gave an argument name but failed to provide the required parameters to the argument. Tell the user which argument wants more. This is an API change that may affect programs trying to match the specific "Too few arguments" message. The new error message appends the user-supplied argument that caused the error. A solution which works with both versions is to look for "Too few arguments" at the beginning of the error message. - if (err.what() == "Too few arguments") + if (std:string(err.what()).rfind("Too few arguments", 0) == 0) Signed-off-by: Sean Robinson --- include/argparse/argparse.hpp | 3 ++- test/test_parse_args.cpp | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/include/argparse/argparse.hpp b/include/argparse/argparse.hpp index c7719fe..8829f31 100644 --- a/include/argparse/argparse.hpp +++ b/include/argparse/argparse.hpp @@ -472,7 +472,8 @@ public: } else if (mDefaultValue.has_value()) { return start; } else { - throw std::runtime_error("Too few arguments"); + throw std::runtime_error("Too few arguments for '" + + std::string(mUsedName) + "'."); } } diff --git a/test/test_parse_args.cpp b/test/test_parse_args.cpp index f77d122..146dfff 100644 --- a/test/test_parse_args.cpp +++ b/test/test_parse_args.cpp @@ -7,7 +7,7 @@ TEST_CASE("Missing argument" * test_suite("parse_args")) { argparse::ArgumentParser program("test"); program.add_argument("--config").nargs(1); REQUIRE_THROWS_WITH_AS(program.parse_args({ "test", "--config" }), - "Too few arguments", + "Too few arguments for '--config'.", std::runtime_error); }