Skip to content

Commit

Permalink
Don't format options table like errors (microsoft#1306)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Billy Robert O'Neal III <[email protected]>
  • Loading branch information
Thomas1664 and BillyONeal authored Jan 11, 2024
1 parent 2aa3a71 commit b1d882f
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 24 deletions.
36 changes: 36 additions & 0 deletions azure-pipelines/end-to-end-tests-dir/cli.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,42 @@ Throw-IfNotFailed
Run-Vcpkg -TestArgs ($commonArgs + @("install", "vcpkg-hello-world-1", "--fast")) # --fast is not a switch
Throw-IfNotFailed

# Test switching stdout vs. stderr for help topics and errors
[string]$out = Run-VcpkgAndCaptureOutput -TestArgs @('help')
Throw-IfFailed
if (-Not ($out.StartsWith('usage: vcpkg <command> [--switches] [--options=values] [arguments] @response_file')))
{
throw 'Bad help output'
}

$out = Run-VcpkgAndCaptureOutput -TestArgs @('help', 'help')
Throw-IfFailed
if (-Not ($out.StartsWith('Synopsis: Displays specific help topic')))
{
throw 'Bad help help output'
}

$out = Run-VcpkgAndCaptureStdErr -TestArgs @('help', 'not-a-topic')
Throw-IfNotFailed
if (-Not ($out.StartsWith('error: unknown topic not-a-topic')))
{
throw 'Bad help not-a-topic output'
}

$out = Run-VcpkgAndCaptureStdErr -TestArgs @('not-a-command')
Throw-IfNotFailed
if (-Not ($out.StartsWith('error: invalid command: not-a-command')))
{
throw 'Bad not-a-command output'
}

$out = Run-VcpkgAndCaptureStdErr -TestArgs @('install', '--not-a-switch')
Throw-IfNotFailed
if (-Not ($out.StartsWith('error: unexpected switch: --not-a-switch')))
{
throw 'Bad install --not-a-switch output'
}

if ($IsWindows) {
$warningText = 'In the September 2023 release'

Expand Down
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 include/vcpkg/vcpkgcmdarguments.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ namespace vcpkg
LocalizedString get_example_text() const;
};

void print_usage(const CommandMetadata& command_metadata);
LocalizedString usage_for_command(const CommandMetadata& command_metadata);

struct FeatureFlagSettings
{
Expand Down Expand Up @@ -402,7 +402,7 @@ namespace vcpkg

Optional<StringLiteral> m_detected_ci_environment;

friend void print_usage(const CommandMetadata& command_metadata);
friend LocalizedString usage_for_command(const CommandMetadata& command_metadata);
CmdParser parser;
};
}
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 @@ -1003,14 +1004,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 @@ -157,7 +157,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 @@ -213,7 +213,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
17 changes: 9 additions & 8 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 All @@ -181,14 +182,14 @@ namespace vcpkg
{
if (Strings::case_insensitive_ascii_equals(command_metadata->name, topic))
{
print_usage(*command_metadata);
msg::print(usage_for_command(*command_metadata));
get_global_metrics_collector().track_string(StringMetric::CommandContext, command_metadata->name);
Checks::exit_success(VCPKG_LINE_INFO);
}
}

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
4 changes: 2 additions & 2 deletions src/vcpkg/commands.install.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1085,7 +1085,7 @@ namespace vcpkg
if (failure)
{
msg::println(msgUsingManifestAt, msg::path = p->path);
print_usage(CommandInstallMetadataManifest);
msg::print(usage_for_command(CommandInstallMetadataManifest));
Checks::exit_fail(VCPKG_LINE_INFO);
}

Expand All @@ -1111,7 +1111,7 @@ namespace vcpkg
}
if (failure)
{
print_usage(CommandInstallMetadataClassic);
msg::write_unlocalized_text_to_stderr(Color::none, usage_for_command(CommandInstallMetadataClassic));
Checks::exit_fail(VCPKG_LINE_INFO);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/vcpkg/vcpkgcmdarguments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ namespace vcpkg
return result;
}

void print_usage(const CommandMetadata& command_metadata)
LocalizedString usage_for_command(const CommandMetadata& command_metadata)
{
auto with_common_options = VcpkgCmdArguments::create_from_arg_sequence(nullptr, nullptr);
ParsedArguments throwaway;
Expand Down Expand Up @@ -506,7 +506,7 @@ namespace vcpkg

with_common_options.parser.append_options_table(result);
result.append_raw('\n');
msg::write_unlocalized_text_to_stderr(Color::error, result);
return result;
}

static void from_env(const std::function<Optional<std::string>(ZStringView)>& f,
Expand Down

0 comments on commit b1d882f

Please sign in to comment.