Skip to content

Commit 8bf340d

Browse files
authored
fix: improved clarity in RequiredError (#1029)
Hello. Thank you for this tool. I found a few small typos in the RequiredError class. This is an error message I received: ``` Requires at most 2 options be used and 3were given from [filename,--get-size,--get-size2] ``` This PR changes the error message to ``` Requires at most 2 options be used but 3 were given from [filename,--get-size,--get-size2] ``` Happy to make changes as needed! ## Source ```cpp std::string filename; bool get_size = false; CLI::App app{"Program to read a file specified as command-line argument"}; app.add_option("filename", filename, "File to read")->required(); app.add_flag("--get-size", get_size, "Print the size of the file"); app.add_flag("--get-size2", get_size, "Print the size of the file2"); app.require_option(1, 2); // Enforce only one flag can be input CLI11_PARSE(app, argc, argv); ``` ## Further Idea Also, another idea I had was to remove required options from this error message. In this example, "filename" is required, so a better error message would be ``` Requires at most 1 options be used but 2 were given from [--get-size,--get-size2] ``` I'm happy to look into this if you feel it would be valuable
1 parent 2fa609a commit 8bf340d

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

Diff for: examples/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ add_test(NAME option_groups_missing COMMAND option_groups)
9999
set_property(TEST option_groups_missing PROPERTY PASS_REGULAR_EXPRESSION "Exactly 1 option from"
100100
"is required")
101101
add_test(NAME option_groups_extra COMMAND option_groups --csv --binary)
102-
set_property(TEST option_groups_extra PROPERTY PASS_REGULAR_EXPRESSION "and 2 were given")
102+
set_property(TEST option_groups_extra PROPERTY PASS_REGULAR_EXPRESSION "but 2 were given")
103103
add_test(NAME option_groups_extra2 COMMAND option_groups --csv --address "192.168.1.1" -o
104104
"test.out")
105105
set_property(TEST option_groups_extra2 PROPERTY PASS_REGULAR_EXPRESSION "at most 1")

Diff for: include/CLI/Error.hpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -239,22 +239,22 @@ class RequiredError : public ParseError {
239239
if((min_option == 1) && (max_option == 1) && (used == 0))
240240
return RequiredError("Exactly 1 option from [" + option_list + "]");
241241
if((min_option == 1) && (max_option == 1) && (used > 1)) {
242-
return {"Exactly 1 option from [" + option_list + "] is required and " + std::to_string(used) +
242+
return {"Exactly 1 option from [" + option_list + "] is required but " + std::to_string(used) +
243243
" were given",
244244
ExitCodes::RequiredError};
245245
}
246246
if((min_option == 1) && (used == 0))
247247
return RequiredError("At least 1 option from [" + option_list + "]");
248248
if(used < min_option) {
249-
return {"Requires at least " + std::to_string(min_option) + " options used and only " +
250-
std::to_string(used) + "were given from [" + option_list + "]",
249+
return {"Requires at least " + std::to_string(min_option) + " options used but only " +
250+
std::to_string(used) + " were given from [" + option_list + "]",
251251
ExitCodes::RequiredError};
252252
}
253253
if(max_option == 1)
254254
return {"Requires at most 1 options be given from [" + option_list + "]", ExitCodes::RequiredError};
255255

256-
return {"Requires at most " + std::to_string(max_option) + " options be used and " + std::to_string(used) +
257-
"were given from [" + option_list + "]",
256+
return {"Requires at most " + std::to_string(max_option) + " options be used but " + std::to_string(used) +
257+
" were given from [" + option_list + "]",
258258
ExitCodes::RequiredError};
259259
}
260260
};

0 commit comments

Comments
 (0)