[vcpkg] Refactor out implication in option parsing for export
MODIFY
include/Span.h
include/Span.h
#include <array>
#include <cstddef>
+#include <initializer_list>
#include <vector>
template<class T>
@@ -17,6 +18,7 @@ public:
constexpr Span(std::nullptr_t) noexcept : Span() {}
constexpr Span(T* ptr, size_t count) noexcept : m_ptr(ptr), m_count(count) {}
constexpr Span(T* ptr_begin, T* ptr_end) noexcept : m_ptr(ptr_begin), m_count(ptr_end - ptr_begin) {}
+ constexpr Span(std::initializer_list<T> l) noexcept : m_ptr(l.begin()), m_count(l.size()) {}
template<size_t N>
constexpr Span(T (&arr)[N]) noexcept : Span(arr, N)

MODIFY
src/commands_export.cpp
src/commands_export.cpp
@@ -314,37 +314,45 @@ namespace vcpkg::Commands::Export
Checks::exit_fail(VCPKG_LINE_INFO);
}
- ret.maybe_nuget_id = maybe_lookup(options.settings, OPTION_NUGET_ID);
- ret.maybe_nuget_version = maybe_lookup(options.settings, OPTION_NUGET_VERSION);
-
- Checks::check_exit(VCPKG_LINE_INFO, !ret.maybe_nuget_id || ret.nuget, "--nuget-id is only valid with --nuget");
- Checks::check_exit(
- VCPKG_LINE_INFO, !ret.maybe_nuget_version || ret.nuget, "--nuget-version is only valid with --nuget");
-
- ret.ifw_options.maybe_repository_url = maybe_lookup(options.settings, OPTION_IFW_REPOSITORY_URL);
- Checks::check_exit(VCPKG_LINE_INFO,
- !ret.ifw_options.maybe_repository_url || ret.ifw,
- "--ifw-repository-url is only valid with --ifw");
-
- ret.ifw_options.maybe_packages_dir_path = maybe_lookup(options.settings, OPTION_IFW_PACKAGES_DIR_PATH);
- Checks::check_exit(VCPKG_LINE_INFO,
- !ret.ifw_options.maybe_packages_dir_path || ret.ifw,
- "--ifw-packages-directory-path is only valid with --ifw");
-
- ret.ifw_options.maybe_repository_dir_path = maybe_lookup(options.settings, OPTION_IFW_REPOSITORY_DIR_PATH);
+ struct OptionPair
+ {
+ const std::string& name;
+ Optional<std::string>& out_opt;
+ };
+ const auto options_implies =
+ [&](const std::string& main_opt_name, bool main_opt, Span<const OptionPair> implying_opts) {
+ if (main_opt)
+ {
+ for (auto&& opt : implying_opts)
+ opt.out_opt = maybe_lookup(options.settings, opt.name);
+ }
+ else
+ {
+ for (auto&& opt : implying_opts)
Checks::check_exit(VCPKG_LINE_INFO,
- !ret.ifw_options.maybe_repository_dir_path || ret.ifw,
- "--ifw-repository-directory-path is only valid with --ifw");
+ !maybe_lookup(options.settings, opt.name),
+ "%s is only valid with %s",
+ opt.name,
+ main_opt_name);
+ }
+ };
- ret.ifw_options.maybe_config_file_path = maybe_lookup(options.settings, OPTION_IFW_CONFIG_FILE_PATH);
- Checks::check_exit(VCPKG_LINE_INFO,
- !ret.ifw_options.maybe_config_file_path || ret.ifw,
- "--ifw-configuration-file-path is only valid with --ifw");
+ options_implies(OPTION_NUGET,
+ ret.nuget,
+ {
+ {OPTION_NUGET_ID, ret.maybe_nuget_id},
+ {OPTION_NUGET_VERSION, ret.maybe_nuget_version},
+ });
- ret.ifw_options.maybe_installer_file_path = maybe_lookup(options.settings, OPTION_IFW_INSTALLER_FILE_PATH);
- Checks::check_exit(VCPKG_LINE_INFO,
- !ret.ifw_options.maybe_installer_file_path || ret.ifw,
- "--ifw-installer-file-path is only valid with --ifw");
+ options_implies(OPTION_IFW,
+ ret.ifw,
+ {
+ {OPTION_IFW_REPOSITORY_URL, ret.ifw_options.maybe_repository_url},
+ {OPTION_IFW_PACKAGES_DIR_PATH, ret.ifw_options.maybe_packages_dir_path},
+ {OPTION_IFW_REPOSITORY_DIR_PATH, ret.ifw_options.maybe_repository_dir_path},
+ {OPTION_IFW_CONFIG_FILE_PATH, ret.ifw_options.maybe_config_file_path},
+ {OPTION_IFW_INSTALLER_FILE_PATH, ret.ifw_options.maybe_installer_file_path},
+ });
return ret;
}

[vcpkg] Refactor out implication in option parsing for export