Skip to content

Commit

Permalink
Improve thrown message in case of invalid argument.
Browse files Browse the repository at this point in the history
Now message contains information which argument is the source of error.
It's easier to spot typo/understand which part of more complex command
is the source of problem.
  • Loading branch information
MaciejPatro committed Nov 5, 2021
1 parent b9583b4 commit 87afaba
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 5 deletions.
4 changes: 2 additions & 2 deletions include/argparse/argparse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1089,11 +1089,11 @@ class ArgumentParser {
auto tArgument = tIterator2->second;
it = tArgument->consume(it, end, tIterator2->first);
} else {
throw std::runtime_error("Unknown argument");
throw std::runtime_error("Unknown argument: " + tCurrentArgument);
}
}
} else {
throw std::runtime_error("Unknown argument");
throw std::runtime_error("Unknown argument: " + tCurrentArgument);
}
}
mIsParsed = true;
Expand Down
3 changes: 2 additions & 1 deletion test/test_invalid_arguments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@ TEST_CASE("Parse unknown optional argument" *
.scan<'u', unsigned long long>()
.help("memory in MB to give the VMM when loading");

REQUIRE_THROWS(bfm.parse_args({ "./test.exe", "-om" }));
REQUIRE_THROWS_WITH_AS(bfm.parse_args({"./test.exe", "-om"}),
"Unknown argument: -om", std::runtime_error);
}
4 changes: 2 additions & 2 deletions test/test_version.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ TEST_CASE("Users can print version and exit" * test_suite("version")
TEST_CASE("Users can disable default -v/--version" * test_suite("version")) {
argparse::ArgumentParser program("test", "1.0",
argparse::default_arguments::help);
REQUIRE_THROWS_AS(program.parse_args({"test", "--version"}),
std::runtime_error);
REQUIRE_THROWS_WITH_AS(program.parse_args({"test", "--version"}),
"Unknown argument: --version", std::runtime_error);
}

TEST_CASE("Users can replace default -v/--version" * test_suite("version")) {
Expand Down

0 comments on commit 87afaba

Please sign in to comment.