Skip to content

Commit ba462db

Browse files
committed
Add a better error message for unlimited argument commands.
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 ```
1 parent 7869ef3 commit ba462db

File tree

4 files changed

+31
-5
lines changed

4 files changed

+31
-5
lines changed

include/vcpkg/base/message-data.inc.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2138,6 +2138,10 @@ DECLARE_MESSAGE(NonRangeArgs,
21382138
"{actual} is an integer",
21392139
"the command '{command_name}' requires between {lower} and {upper} arguments, inclusive, but {actual} "
21402140
"were provided")
2141+
DECLARE_MESSAGE(NonRangeArgsGreater,
2142+
(msg::command_name, msg::lower, msg::actual),
2143+
"{actual} is an integer",
2144+
"the command '{command_name}' requires at least {lower} arguments, but {actual} were provided")
21412145
DECLARE_MESSAGE(NonZeroOrOneRemainingArgs,
21422146
(msg::command_name),
21432147
"",

locales/messages.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,6 +1187,8 @@
11871187
"_NonOneRemainingArgs.comment": "An example of {command_name} is install.",
11881188
"NonRangeArgs": "the command '{command_name}' requires between {lower} and {upper} arguments, inclusive, but {actual} were provided",
11891189
"_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.",
1190+
"NonRangeArgsGreater": "the command '{command_name}' requires at least {lower} arguments, but {actual} were provided",
1191+
"_NonRangeArgsGreater.comment": "{actual} is an integer An example of {command_name} is install. An example of {lower} is 42.",
11901192
"NonZeroOrOneRemainingArgs": "the command '{command_name}' requires zero or one arguments",
11911193
"_NonZeroOrOneRemainingArgs.comment": "An example of {command_name} is install.",
11921194
"NonZeroRemainingArgs": "the command '{command_name}' does not accept any additional arguments",

src/vcpkg-test/cmd-parser.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -891,6 +891,15 @@ TEST_CASE ("Consume remaining args", "[cmd_parser]")
891891
CHECK(uut.get_errors() == expected_errors);
892892
CHECK(uut.get_remaining_args().empty());
893893
}
894+
895+
{
896+
CmdParser uut{std::vector<std::string>{"first-arg", "second-arg"}};
897+
CHECK(uut.consume_remaining_args("command", 3, SIZE_MAX) == std::vector<std::string>{});
898+
const auto expected_errors =
899+
localized({"error: the command 'command' requires at least 3 arguments, but 2 were provided"});
900+
CHECK(uut.get_errors() == expected_errors);
901+
CHECK(uut.get_remaining_args().empty());
902+
}
894903
}
895904

896905
TEST_CASE ("delistify_conjoined_value", "[cmd_parser]")

src/vcpkg/base/cmd-parser.cpp

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -964,11 +964,22 @@ namespace vcpkg
964964
bool error = consume_remaining_args_impl(results);
965965
if (max_arity < results.size() || results.size() < min_arity)
966966
{
967-
errors.emplace_back(msg::format_error(msgNonRangeArgs,
968-
msg::command_name = command_name,
969-
msg::lower = min_arity,
970-
msg::upper = max_arity,
971-
msg::actual = results.size()));
967+
if (max_arity == SIZE_MAX)
968+
{
969+
errors.emplace_back(msg::format_error(msgNonRangeArgsGreater,
970+
msg::command_name = command_name,
971+
msg::lower = min_arity,
972+
msg::actual = results.size()));
973+
}
974+
else
975+
{
976+
errors.emplace_back(msg::format_error(msgNonRangeArgs,
977+
msg::command_name = command_name,
978+
msg::lower = min_arity,
979+
msg::upper = max_arity,
980+
msg::actual = results.size()));
981+
}
982+
972983
for (std::size_t idx = max_arity; idx < results.size(); ++idx)
973984
{
974985
errors.emplace_back(msg::format_error(msgUnexpectedArgument, msg::option = results[idx]));

0 commit comments

Comments
 (0)