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

View File

@ -472,7 +472,8 @@ public:
} else if (mDefaultValue.has_value()) { } else if (mDefaultValue.has_value()) {
return start; return start;
} else { } else {
throw std::runtime_error("Too few arguments"); throw std::runtime_error("Too few arguments for '" +
std::string(mUsedName) + "'.");
} }
} }

View File

@ -7,7 +7,7 @@ TEST_CASE("Missing argument" * test_suite("parse_args")) {
argparse::ArgumentParser program("test"); argparse::ArgumentParser program("test");
program.add_argument("--config").nargs(1); program.add_argument("--config").nargs(1);
REQUIRE_THROWS_WITH_AS(program.parse_args({ "test", "--config" }), REQUIRE_THROWS_WITH_AS(program.parse_args({ "test", "--config" }),
"Too few arguments", "Too few arguments for '--config'.",
std::runtime_error); std::runtime_error);
} }