Skip to content

Commit

Permalink
Add a better error message for unlimited argument commands. (#1515)
Browse files Browse the repository at this point in the history
Before / after:

```console
PS D:\vcpkg\test> ..\vcpkg.exe new --application
PS D:\vcpkg\test> ..\vcpkg.exe add rapidjson
error: the command 'add' requires between 2 and 18446744073709551615 arguments, inclusive, but 1 were provided
[...]

PS D:\vcpkg\test> D:\vcpkg-tool\out\build\Win-x64-Debug-WithArtifacts\vcpkg.exe add rapidjson
error: the command 'add' requires at least 2 arguments, but 1 were provided
```
  • Loading branch information
BillyONeal authored Oct 18, 2024
1 parent 67931f1 commit dc69ae8
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
4 changes: 4 additions & 0 deletions include/vcpkg/base/message-data.inc.h
Original file line number Diff line number Diff line change
Expand Up @@ -2148,6 +2148,10 @@ DECLARE_MESSAGE(NonRangeArgs,
"{actual} is an integer",
"the command '{command_name}' requires between {lower} and {upper} arguments, inclusive, but {actual} "
"were provided")
DECLARE_MESSAGE(NonRangeArgsGreater,
(msg::command_name, msg::lower, msg::actual),
"{actual} is an integer",
"the command '{command_name}' requires at least {lower} arguments, but {actual} were provided")
DECLARE_MESSAGE(NonZeroOrOneRemainingArgs,
(msg::command_name),
"",
Expand Down
2 changes: 2 additions & 0 deletions locales/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1191,6 +1191,8 @@
"_NonOneRemainingArgs.comment": "An example of {command_name} is install.",
"NonRangeArgs": "the command '{command_name}' requires between {lower} and {upper} arguments, inclusive, but {actual} were provided",
"_NonRangeArgs.comment": "{actual} is an integer An example of {command_name} is install. An example of {lower} is 42. An example of {upper} is 42.",
"NonRangeArgsGreater": "the command '{command_name}' requires at least {lower} arguments, but {actual} were provided",
"_NonRangeArgsGreater.comment": "{actual} is an integer An example of {command_name} is install. An example of {lower} is 42.",
"NonZeroOrOneRemainingArgs": "the command '{command_name}' requires zero or one arguments",
"_NonZeroOrOneRemainingArgs.comment": "An example of {command_name} is install.",
"NonZeroRemainingArgs": "the command '{command_name}' does not accept any additional arguments",
Expand Down
9 changes: 9 additions & 0 deletions src/vcpkg-test/cmd-parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,15 @@ TEST_CASE ("Consume remaining args", "[cmd_parser]")
CHECK(uut.get_errors() == expected_errors);
CHECK(uut.get_remaining_args().empty());
}

{
CmdParser uut{std::vector<std::string>{"first-arg", "second-arg"}};
CHECK(uut.consume_remaining_args("command", 3, SIZE_MAX) == std::vector<std::string>{});
const auto expected_errors =
localized({"error: the command 'command' requires at least 3 arguments, but 2 were provided"});
CHECK(uut.get_errors() == expected_errors);
CHECK(uut.get_remaining_args().empty());
}
}

TEST_CASE ("delistify_conjoined_value", "[cmd_parser]")
Expand Down
21 changes: 16 additions & 5 deletions src/vcpkg/base/cmd-parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -964,11 +964,22 @@ namespace vcpkg
bool error = consume_remaining_args_impl(results);
if (max_arity < results.size() || results.size() < min_arity)
{
errors.emplace_back(msg::format_error(msgNonRangeArgs,
msg::command_name = command_name,
msg::lower = min_arity,
msg::upper = max_arity,
msg::actual = results.size()));
if (max_arity == SIZE_MAX)
{
errors.emplace_back(msg::format_error(msgNonRangeArgsGreater,
msg::command_name = command_name,
msg::lower = min_arity,
msg::actual = results.size()));
}
else
{
errors.emplace_back(msg::format_error(msgNonRangeArgs,
msg::command_name = command_name,
msg::lower = min_arity,
msg::upper = max_arity,
msg::actual = results.size()));
}

for (std::size_t idx = max_arity; idx < results.size(); ++idx)
{
errors.emplace_back(msg::format_error(msgUnexpectedArgument, msg::option = results[idx]));
Expand Down

0 comments on commit dc69ae8

Please sign in to comment.