Demonstrate passing through arguments with x-forward test function
ADD
include/vcpkg/commands.zforward.h
+#pragma once
+
+#include <vcpkg/commands.interface.h>
+
+namespace vcpkg::Commands::Z_Forward
+{
+ void perform_and_exit(const VcpkgCmdArguments& args, const VcpkgPaths& paths);
+
+ struct ForwardCommand : PathsCommand
+ {
+ void perform_and_exit(const VcpkgCmdArguments& args, const VcpkgPaths& paths) const override;
+ };
+}
\ No newline at end of file

MODIFY
include/vcpkg/vcpkgcmdarguments.h
include/vcpkg/vcpkgcmdarguments.h
#pragma once
+#include <vcpkg/base/fwd/system.process.h>
+
#include <vcpkg/fwd/vcpkgcmdarguments.h>
#include <vcpkg/fwd/vcpkgpaths.h>
@@ -245,6 +247,8 @@ namespace vcpkg
Optional<std::string> asset_sources_template() const;
+ void add_forwarded_arguments(Command& cmd) const;
+
private:
void imbue_from_environment_impl(std::function<Optional<std::string>(ZStringView)> get_env);
@@ -253,5 +257,7 @@ namespace vcpkg
std::unordered_set<std::string> command_switches;
std::unordered_map<std::string, std::vector<std::string>> command_options;
+
+ std::vector<std::string> original_arguments;
};
}

MODIFY
src/vcpkg-test/commands.cpp
src/vcpkg-test/commands.cpp
#include <catch2/catch.hpp>
+#include <vcpkg/base/util.h>
+
#include <vcpkg/commands.contact.h>
#include <vcpkg/commands.h>
#include <vcpkg/commands.upload-metrics.h>
#include <stddef.h>
+#include <set>
+
using namespace vcpkg;
-namespace
+TEST_CASE ("list of commands is correct", "[commands]")
{
- template<class CommandListT, size_t ExpectedCount>
- void check_all_commands(const CommandListT& actual_commands, const char* const (&expected_commands)[ExpectedCount])
+ using vcpkg::Util::Sets::contains;
+ std::set<std::string> commands;
+ for (auto command : Commands::get_available_basic_commands())
{
- CHECK(actual_commands.size() == ExpectedCount); // makes sure this test is updated if we add a command
- for (const char* expected_command : expected_commands)
+ CHECK_FALSE(contains(commands, command.name));
+ commands.insert(command.name);
+ }
+ for (auto command : Commands::get_available_paths_commands())
{
- CHECK(Commands::find(StringView{expected_command, strlen(expected_command)}, actual_commands) != nullptr);
+ CHECK_FALSE(contains(commands, command.name));
+ commands.insert(command.name);
}
-
- CHECK(Commands::find("x-never-will-exist", actual_commands) == nullptr);
+ for (auto command : Commands::get_available_triplet_commands())
+ {
+ CHECK_FALSE(contains(commands, command.name));
+ commands.insert(command.name);
}
-} // unnamed namespace
-
-// clang-format tries to wrap the following lists inappropriately
// clang-format off
-TEST_CASE ("get_available_basic_commands works", "[commands]")
-{
- check_all_commands(Commands::get_available_basic_commands(), {
+ std::set<std::string> expected_commands{
"contact",
+ "search",
"version",
"hash",
"x-download",
"x-init-registry",
"x-generate-default-message-map",
-#if defined(_WIN32)
- "x-upload-metrics",
-#endif // defined(_WIN32)
- });
-}
-
-TEST_CASE ("get_available_paths_commands works", "[commands]")
-{
- check_all_commands(Commands::get_available_paths_commands(), {
"/?",
"help",
- "search",
"list",
"integrate",
"owns",
@@ -65,12 +61,6 @@ TEST_CASE ("get_available_paths_commands works", "[commands]")
"x-vsinstances",
"x-ci-verify-versions",
"x-add-version",
- });
-}
-
-TEST_CASE ("get_available_commands_type_a works", "[commands]")
-{
- check_all_commands(Commands::get_available_triplet_commands(), {
"install",
"x-set-installed",
"ci",
@@ -81,8 +71,14 @@ TEST_CASE ("get_available_commands_type_a works", "[commands]")
"build-external",
"export",
"depend-info",
+ "z-forward",
"x-check-support",
- "z-print-config"
- });
-}
+ "z-print-config",
+#if defined(_WIN32)
+ "x-upload-metrics",
+#endif // defined(_WIN32)
+ };
// clang-format on
+
+ CHECK(commands == expected_commands);
+}

MODIFY
src/vcpkg/commands.cpp
src/vcpkg/commands.cpp
#include <vcpkg/commands.version.h>
#include <vcpkg/commands.xdownload.h>
#include <vcpkg/commands.xvsinstances.h>
+#include <vcpkg/commands.zforward.h>
#include <vcpkg/commands.zprintconfig.h>
#include <vcpkg/export.h>
#include <vcpkg/help.h>
@@ -90,6 +91,7 @@ namespace vcpkg::Commands
static const FormatManifest::FormatManifestCommand format_manifest{};
static const CIVerifyVersions::CIVerifyVersionsCommand ci_verify_versions{};
static const AddVersion::AddVersionCommand add_version{};
+ static const Z_Forward::ForwardCommand test_forward{};
static std::vector<PackageNameAndFunction<const PathsCommand*>> t = {
{"/?", &help},
@@ -112,6 +114,7 @@ namespace vcpkg::Commands
{"format-manifest", &format_manifest},
{"x-ci-verify-versions", &ci_verify_versions},
{"x-add-version", &add_version},
+ {"z-forward", &test_forward},
};
return t;
}

ADD
src/vcpkg/commands.zforward.cpp
+#include <vcpkg/base/system.process.h>
+
+#include <vcpkg/commands.zforward.h>
+#include <vcpkg/vcpkgcmdarguments.h>
+
+namespace vcpkg::Commands::Z_Forward
+{
+ void perform_and_exit(const VcpkgCmdArguments& args, const VcpkgPaths&)
+ {
+ Command cmd("insert-executable-name-here");
+ cmd.string_arg("--");
+ args.add_forwarded_arguments(cmd);
+ int status = cmd_execute(cmd);
+ Checks::exit_with_code(VCPKG_LINE_INFO, status);
+ }
+
+ void ForwardCommand::perform_and_exit(const VcpkgCmdArguments& args, const VcpkgPaths& paths) const
+ {
+ Z_Forward::perform_and_exit(args, paths);
+ }
+}

MODIFY
src/vcpkg/vcpkgcmdarguments.cpp
src/vcpkg/vcpkgcmdarguments.cpp
#include <vcpkg/base/json.h>
#include <vcpkg/base/system.debug.h>
#include <vcpkg/base/system.print.h>
+#include <vcpkg/base/system.process.h>
#include <vcpkg/commands.h>
#include <vcpkg/commands.integrate.h>
@@ -235,6 +236,8 @@ namespace vcpkg
VcpkgCmdArguments args;
std::vector<std::string> feature_flags;
+ args.original_arguments.assign(arg_first, arg_last);
+
if (arg_first != arg_last && arg_first + 1 == arg_last && *arg_first == "--version")
{
args.command = "version";
@@ -548,6 +551,14 @@ namespace vcpkg
return output;
}
+ void VcpkgCmdArguments::add_forwarded_arguments(Command& cmd) const
+ {
+ for (auto&& arg : original_arguments)
+ {
+ cmd.string_arg(arg);
+ }
+ }
+
void print_usage()
{
HelpTableFormatter table;

Demonstrate passing through arguments with x-forward test function