Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add argument name after 'Too few arguments' error #360

Merged
merged 1 commit into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion include/argparse/argparse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -997,7 +997,8 @@ class Argument {
std::bind(is_optional, std::placeholders::_1, m_prefix_chars));
dist = static_cast<std::size_t>(std::distance(start, end));
if (dist < num_args_min) {
throw std::runtime_error("Too few arguments");
throw std::runtime_error("Too few arguments for '" +
std::string(m_used_name) + "'.");
}
}

Expand Down
9 changes: 9 additions & 0 deletions test/test_parse_args.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ TEST_CASE("Missing argument" * test_suite("parse_args")) {
std::runtime_error);
}

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

TEST_CASE("Parse a string argument with value" * test_suite("parse_args")) {
argparse::ArgumentParser program("test");
program.add_argument("--config");
Expand Down
Loading