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 <sean.robinson@scottsdalecc.edu>
This commit is contained in:
Sean Robinson 2021-07-22 06:45:44 -07:00
parent 97993666ab
commit 1c2fd8726d
2 changed files with 3 additions and 3 deletions

View File

@ -759,7 +759,7 @@ private:
if (mDefaultValue.has_value()) {
return std::any_cast<T>(mDefaultValue);
}
throw std::logic_error("No value provided");
throw std::logic_error("No value provided for '" + mNames.back() + "'.");
}
/*

View File

@ -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);
}