Skip to content

Commit

Permalink
Print errors to stderr
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas1664 committed Dec 22, 2023
1 parent b1e45c2 commit 21e9c58
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 17 deletions.
2 changes: 1 addition & 1 deletion include/vcpkg/commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ namespace vcpkg

std::vector<const CommandMetadata*> get_all_commands_metadata();

void print_zero_args_usage();
std::string get_zero_args_usage();
void print_full_command_list();
}
4 changes: 2 additions & 2 deletions src/vcpkg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ namespace
LocalizedString::from_raw(ErrorPrefix)
.append(msgVcpkgInvalidCommand, msg::command_name = args.get_command())
.append_raw('\n'));
print_zero_args_usage();
msg::write_unlocalized_text_to_stderr(Color::none, get_zero_args_usage());
Checks::exit_fail(VCPKG_LINE_INFO);
}

Expand Down Expand Up @@ -99,7 +99,7 @@ namespace

if (args.get_command().empty())
{
print_zero_args_usage();
msg::write_unlocalized_text_to_stderr(Color::none, get_zero_args_usage());
Checks::exit_fail(VCPKG_LINE_INFO);
}

Expand Down
8 changes: 3 additions & 5 deletions src/vcpkg/base/cmd-parser.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <vcpkg/base/cmd-parser.h>
#include <vcpkg/base/files.h>
#include <vcpkg/base/message_sinks.h>
#include <vcpkg/base/strings.h>

#include <stdint.h>
Expand Down Expand Up @@ -1010,14 +1011,11 @@ namespace vcpkg
return;
}

for (auto&& error : errors)
{
msg::write_unlocalized_text(Color::error, error.append_raw("\n"));
}
msg::write_unlocalized_text_to_stderr(Color::error, Strings::join("\n", errors).append("\n"));

example.append_raw('\n');
append_options_table(example);
msg::println(Color::none, example);
stderr_sink.println(Color::none, example);
Checks::exit_with_code(VCPKG_LINE_INFO, 1);
}
}
4 changes: 2 additions & 2 deletions src/vcpkg/commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ namespace vcpkg
}
}

void print_zero_args_usage()
std::string get_zero_args_usage()
{
HelpTableFormatter table;
table.example(msg::format(msgVcpkgUsage));
Expand Down Expand Up @@ -212,7 +212,7 @@ namespace vcpkg
table.example(msg::format(msgHelpExampleCommand));

table.m_str.push_back('\n');
msg::write_unlocalized_text_to_stderr(Color::none, table.m_str);
return table.m_str;
}

void print_full_command_list()
Expand Down
15 changes: 8 additions & 7 deletions src/vcpkg/commands.help.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <vcpkg/base/cmd-parser.h>
#include <vcpkg/base/message_sinks.h>
#include <vcpkg/base/strings.h>
#include <vcpkg/base/util.h>

Expand All @@ -20,7 +21,7 @@ namespace
void (*print)(const VcpkgPaths&);
};

void help_topics(const VcpkgPaths&);
LocalizedString help_topics();

void help_topic_versioning(const VcpkgPaths&)
{
Expand Down Expand Up @@ -70,12 +71,12 @@ namespace
{"assetcaching", [](const VcpkgPaths&) { msg::println(format_help_topic_asset_caching()); }},
{"binarycaching", [](const VcpkgPaths&) { msg::println(format_help_topic_binary_caching()); }},
{"commands", [](const VcpkgPaths&) { print_full_command_list(); }},
{"topics", help_topics},
{"topics", [](const VcpkgPaths&) { msg::print(help_topics()); }},
{"triplet", [](const VcpkgPaths& paths) { help_topic_valid_triplet(paths.get_triplet_db()); }},
{"versioning", help_topic_versioning},
};

void help_topics(const VcpkgPaths&)
LocalizedString help_topics()
{
std::vector<LocalizedString> all_topic_names;
for (auto&& topic : topics)
Expand All @@ -94,7 +95,7 @@ namespace
result.append(msgAvailableHelpTopics);
result.append_floating_list(1, all_topic_names);
result.append_raw('\n');
msg::print(result);
return result;
}

} // unnamed namespace
Expand Down Expand Up @@ -155,7 +156,7 @@ namespace vcpkg

if (parsed.command_arguments.empty())
{
print_zero_args_usage();
msg::write_unlocalized_text_to_stdout(Color::none, get_zero_args_usage());
Checks::exit_success(VCPKG_LINE_INFO);
}
const auto& topic = parsed.command_arguments[0];
Expand Down Expand Up @@ -187,8 +188,8 @@ namespace vcpkg
}
}

msg::println_error(msgUnknownTopic, msg::value = topic);
help_topics(paths);
stderr_sink.println_error(msgUnknownTopic, msg::value = topic);
stderr_sink.print(help_topics());
get_global_metrics_collector().track_string(StringMetric::CommandContext, "unknown");
Checks::exit_fail(VCPKG_LINE_INFO);
}
Expand Down

0 comments on commit 21e9c58

Please sign in to comment.