From 1c2fd8726d7ecc700aad0bf9317386301dfb90cd 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::get As the user did not include the argument, the longest name for the unused argument is in the last position of mNames. This is an API change that may affect programs trying to match the specific "No value provided" message. The new error message appends the argument that caused the error. A solution which works with both versions is to look for "No value provided" at the beginning of the error message. - if (err.what() == "No value provided") + if (std:string(err.what()).rfind("No value provided", 0) == 0) Signed-off-by: Sean Robinson --- include/argparse/argparse.hpp | 2 +- test/test_get.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/include/argparse/argparse.hpp b/include/argparse/argparse.hpp index cdbcd19..c7719fe 100644 --- a/include/argparse/argparse.hpp +++ b/include/argparse/argparse.hpp @@ -759,7 +759,7 @@ private: if (mDefaultValue.has_value()) { return std::any_cast(mDefaultValue); } - throw std::logic_error("No value provided"); + throw std::logic_error("No value provided for '" + mNames.back() + "'."); } /* diff --git a/test/test_get.cpp b/test/test_get.cpp index d54e0f3..35d9464 100644 --- a/test/test_get.cpp +++ b/test/test_get.cpp @@ -15,7 +15,7 @@ TEST_CASE("Missing argument" * test_suite("ArgumentParser::get")) { program.add_argument("-s", "--stuff"); REQUIRE_NOTHROW(program.parse_args({ "test" })); REQUIRE_THROWS_WITH_AS(program.get("--stuff"), - "No value provided", + "No value provided for '--stuff'.", std::logic_error); } @@ -24,6 +24,6 @@ TEST_CASE("Implicit argument" * test_suite("ArgumentParser::get")) { program.add_argument("-s", "--stuff").nargs(1); REQUIRE_NOTHROW(program.parse_args({ "test" })); REQUIRE_THROWS_WITH_AS(program.get("--stuff"), - "No value provided", + "No value provided for '--stuff'.", std::logic_error); }