Skip to content

Commit

Permalink
Merge pull request #360 from rouault/too_few_arguments
Browse files Browse the repository at this point in the history
Add argument name after 'Too few arguments' error
  • Loading branch information
p-ranav authored May 27, 2024
2 parents eab1d75 + 84b7b46 commit 9893754
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
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

0 comments on commit 9893754

Please sign in to comment.