mirror of
https://github.com/KeqingMoe/argparse.git
synced 2025-07-04 07:04:39 +00:00
Updated formatting of error message, showing all variations of argument name
This commit is contained in:
parent
78ba5e9828
commit
7657a22001
@ -816,18 +816,18 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
std::string get_names_csv() const {
|
||||
std::string get_names_csv(char separator = ',') const {
|
||||
return std::accumulate(
|
||||
m_names.begin(), m_names.end(), std::string{""},
|
||||
[](const std::string &result, const std::string &name) {
|
||||
return result.empty() ? name : result + ',' + name;
|
||||
[&](const std::string &result, const std::string &name) {
|
||||
return result.empty() ? name : result + separator + name;
|
||||
});
|
||||
}
|
||||
|
||||
std::string get_usage_full() const {
|
||||
std::stringstream usage;
|
||||
|
||||
usage << get_names_csv();
|
||||
usage << get_names_csv('/');
|
||||
const std::string metavar = !m_metavar.empty() ? m_metavar : "VAR";
|
||||
if (m_num_args_range.get_max() > 0) {
|
||||
usage << " " << metavar;
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
argparse::ArgumentParser program;
|
||||
program.add_argument("-a").required();
|
||||
program.add_argument("-b", "--bro").required();
|
||||
program.add_argument("-a", "--number-of-apples");
|
||||
program.add_argument("-b", "--bro");
|
||||
program.parse_args(argc, argv);
|
||||
}
|
Binary file not shown.
@ -35,6 +35,20 @@ TEST_CASE("Missing optional argument name" * test_suite("error_reporting")) {
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("Missing optional argument name (multiple names)" *
|
||||
test_suite("error_reporting")) {
|
||||
argparse::ArgumentParser parser("test");
|
||||
parser.add_argument("-a", "--number-of-apples");
|
||||
parser.add_argument("-b");
|
||||
|
||||
SUBCASE("Bad case 2") {
|
||||
REQUIRE_THROWS_WITH_AS(parser.parse_args({"test", "1", "2"}),
|
||||
"Zero positional arguments expected, did you mean "
|
||||
"-a/--number-of-apples VAR",
|
||||
std::runtime_error);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("Missing optional argument name with other positional arguments" *
|
||||
test_suite("error_reporting")) {
|
||||
argparse::ArgumentParser parser("test");
|
||||
|
Loading…
Reference in New Issue
Block a user