Use StringLiteral and constexpr for options/switches
MODIFY
include/vcpkg/vcpkgcmdarguments.h
include/vcpkg/vcpkgcmdarguments.h
#include <vcpkg/base/cstringview.h>
#include <vcpkg/base/optional.h>
#include <vcpkg/base/span.h>
+#include <vcpkg/base/stringliteral.h>
#include <memory>
#include <unordered_map>
@@ -21,14 +22,24 @@ namespace vcpkg
struct CommandSwitch
{
- std::string name;
- CStringView short_help_text;
+ constexpr CommandSwitch(const StringLiteral& name, const StringLiteral& short_help_text)
+ : name(name), short_help_text(short_help_text)
+ {
+ }
+
+ StringLiteral name;
+ StringLiteral short_help_text;
};
struct CommandSetting
{
- std::string name;
- CStringView short_help_text;
+ constexpr CommandSetting(const StringLiteral& name, const StringLiteral& short_help_text)
+ : name(name), short_help_text(short_help_text)
+ {
+ }
+
+ StringLiteral name;
+ StringLiteral short_help_text;
};
struct CommandOptionsStructure

MODIFY
src/vcpkg/build.cpp
src/vcpkg/build.cpp
#include <vcpkg/base/chrono.h>
#include <vcpkg/base/enums.h>
#include <vcpkg/base/optional.h>
+#include <vcpkg/base/stringliteral.h>
#include <vcpkg/base/system.h>
#include <vcpkg/build.h>
#include <vcpkg/commands.h>
@@ -26,7 +27,7 @@ namespace vcpkg::Build::Command
using Dependencies::InstallPlanAction;
using Dependencies::InstallPlanType;
- static const std::string OPTION_CHECKS_ONLY = "--checks-only";
+ static constexpr StringLiteral OPTION_CHECKS_ONLY = "--checks-only";
void perform_and_exit_ex(const FullPackageSpec& full_spec,
const fs::path& port_dir,
@@ -97,7 +98,7 @@ namespace vcpkg::Build::Command
Checks::exit_success(VCPKG_LINE_INFO);
}
- static const std::array<CommandSwitch, 1> BUILD_SWITCHES = {{
+ static constexpr std::array<CommandSwitch, 1> BUILD_SWITCHES = {{
{OPTION_CHECKS_ONLY, "Only run checks, do not rebuild package"},
}};

MODIFY
src/vcpkg/commands.autocomplete.cpp
src/vcpkg/commands.autocomplete.cpp
@@ -136,8 +136,8 @@ namespace vcpkg::Commands::Autocomplete
const bool is_option = Strings::case_insensitive_ascii_starts_with(prefix, "-");
if (is_option)
{
- results =
- Util::fmap(command.structure.options.switches, [](const CommandSwitch& s) { return s.name; });
+ results = Util::fmap(command.structure.options.switches,
+ [](const CommandSwitch& s) -> std::string { return s.name; });
auto settings = Util::fmap(command.structure.options.settings, [](auto&& s) { return s.name; });
results.insert(results.end(), settings.begin(), settings.end());
@@ -145,8 +145,10 @@ namespace vcpkg::Commands::Autocomplete
else
{
if (command.structure.valid_arguments != nullptr)
+ {
results = command.structure.valid_arguments(paths);
}
+ }
Util::unstable_keep_if(results, [&](const std::string& s) {
return Strings::case_insensitive_ascii_starts_with(s, prefix);

MODIFY
src/vcpkg/commands.ci.cpp
src/vcpkg/commands.ci.cpp
#include "pch.h"
#include <vcpkg/base/files.h>
+#include <vcpkg/base/stringliteral.h>
#include <vcpkg/base/system.h>
#include <vcpkg/base/util.h>
#include <vcpkg/build.h>
@@ -62,10 +63,10 @@ namespace vcpkg::Commands::CI
Install::InstallSummary summary;
};
- static const std::string OPTION_EXCLUDE = "--exclude";
- static const std::string OPTION_XUNIT = "--x-xunit";
+ static constexpr StringLiteral OPTION_EXCLUDE = "--exclude";
+ static constexpr StringLiteral OPTION_XUNIT = "--x-xunit";
- static const std::array<CommandSetting, 2> CI_SETTINGS = {{
+ static constexpr std::array<CommandSetting, 2> CI_SETTINGS = {{
{OPTION_EXCLUDE, "Comma separated list of ports to skip"},
{OPTION_XUNIT, "File to output results in XUnit format (internal)"},
}};

MODIFY
src/vcpkg/commands.edit.cpp
src/vcpkg/commands.edit.cpp
@@ -32,7 +32,7 @@ namespace vcpkg::Commands::Edit
return output;
}
- static const std::string OPTION_BUILDTREES = "--buildtrees";
+ static constexpr StringLiteral OPTION_BUILDTREES = "--buildtrees";
static std::vector<std::string> valid_arguments(const VcpkgPaths& paths)
{
@@ -42,7 +42,7 @@ namespace vcpkg::Commands::Edit
[](auto&& pgh) -> std::string { return pgh->core_paragraph->name; });
}
- static const std::array<CommandSwitch, 1> EDIT_SWITCHES = {{
+ static constexpr std::array<CommandSwitch, 1> EDIT_SWITCHES = {{
{OPTION_BUILDTREES, "Open editor into the port-specific buildtree subfolder"},
}};

MODIFY
src/vcpkg/commands.list.cpp
src/vcpkg/commands.list.cpp
namespace vcpkg::Commands::List
{
- static const std::string OPTION_FULLDESC = "--x-full-desc"; // TODO: This should find a better home, eventually
+ static constexpr StringLiteral OPTION_FULLDESC =
+ "--x-full-desc"; // TODO: This should find a better home, eventually
static void do_print(const StatusParagraph& pgh, bool full_desc)
{
@@ -24,7 +25,7 @@ namespace vcpkg::Commands::List
}
}
- static const std::array<CommandSwitch, 1> LIST_SWITCHES = {{
+ static constexpr std::array<CommandSwitch, 1> LIST_SWITCHES = {{
{OPTION_FULLDESC, "Do not truncate long text"},
}};

MODIFY
src/vcpkg/commands.search.cpp
src/vcpkg/commands.search.cpp
namespace vcpkg::Commands::Search
{
- static const std::string OPTION_GRAPH = "--graph"; // TODO: This should find a better home, eventually
- static const std::string OPTION_FULLDESC = "--x-full-desc"; // TODO: This should find a better home, eventually
+ static constexpr StringLiteral OPTION_GRAPH = "--graph"; // TODO: This should find a better home, eventually
+ static constexpr StringLiteral OPTION_FULLDESC =
+ "--x-full-desc"; // TODO: This should find a better home, eventually
static std::string replace_dashes_with_underscore(const std::string& input)
{
@@ -79,7 +80,7 @@ namespace vcpkg::Commands::Search
}
}
- static std::array<CommandSwitch, 2> SEARCH_SWITCHES = {{
+ static constexpr std::array<CommandSwitch, 2> SEARCH_SWITCHES = {{
{OPTION_GRAPH, "Open editor into the port-specific buildtree subfolder"},
{OPTION_FULLDESC, "Do not truncate long text"},
}};

MODIFY
src/vcpkg/commands.upgrade.cpp
src/vcpkg/commands.upgrade.cpp
@@ -15,10 +15,10 @@ namespace vcpkg::Commands::Upgrade
using Install::KeepGoing;
using Install::to_keep_going;
- static const std::string OPTION_NO_DRY_RUN = "--no-dry-run";
- static const std::string OPTION_KEEP_GOING = "--keep-going";
+ static constexpr StringLiteral OPTION_NO_DRY_RUN = "--no-dry-run";
+ static constexpr StringLiteral OPTION_KEEP_GOING = "--keep-going";
- static const std::array<CommandSwitch, 2> INSTALL_SWITCHES = {{
+ static constexpr std::array<CommandSwitch, 2> INSTALL_SWITCHES = {{
{OPTION_NO_DRY_RUN, "Actually upgrade"},
{OPTION_KEEP_GOING, "Continue installing packages on failure"},
}};

MODIFY
src/vcpkg/export.cpp
src/vcpkg/export.cpp
#include "pch.h"
+#include <vcpkg/base/stringliteral.h>
#include <vcpkg/base/system.h>
#include <vcpkg/base/util.h>
#include <vcpkg/commands.h>
@@ -260,22 +261,22 @@ namespace vcpkg::Export
std::vector<PackageSpec> specs;
};
- static const std::string OPTION_OUTPUT = "--output";
- static const std::string OPTION_DRY_RUN = "--dry-run";
- static const std::string OPTION_RAW = "--raw";
- static const std::string OPTION_NUGET = "--nuget";
- static const std::string OPTION_IFW = "--ifw";
- static const std::string OPTION_ZIP = "--zip";
- static const std::string OPTION_SEVEN_ZIP = "--7zip";
- static const std::string OPTION_NUGET_ID = "--nuget-id";
- static const std::string OPTION_NUGET_VERSION = "--nuget-version";
- static const std::string OPTION_IFW_REPOSITORY_URL = "--ifw-repository-url";
- static const std::string OPTION_IFW_PACKAGES_DIR_PATH = "--ifw-packages-directory-path";
- static const std::string OPTION_IFW_REPOSITORY_DIR_PATH = "--ifw-repository-directory-path";
- static const std::string OPTION_IFW_CONFIG_FILE_PATH = "--ifw-configuration-file-path";
- static const std::string OPTION_IFW_INSTALLER_FILE_PATH = "--ifw-installer-file-path";
-
- static const std::array<CommandSwitch, 6> EXPORT_SWITCHES = {{
+ static constexpr StringLiteral OPTION_OUTPUT = "--output";
+ static constexpr StringLiteral OPTION_DRY_RUN = "--dry-run";
+ static constexpr StringLiteral OPTION_RAW = "--raw";
+ static constexpr StringLiteral OPTION_NUGET = "--nuget";
+ static constexpr StringLiteral OPTION_IFW = "--ifw";
+ static constexpr StringLiteral OPTION_ZIP = "--zip";
+ static constexpr StringLiteral OPTION_SEVEN_ZIP = "--7zip";
+ static constexpr StringLiteral OPTION_NUGET_ID = "--nuget-id";
+ static constexpr StringLiteral OPTION_NUGET_VERSION = "--nuget-version";
+ static constexpr StringLiteral OPTION_IFW_REPOSITORY_URL = "--ifw-repository-url";
+ static constexpr StringLiteral OPTION_IFW_PACKAGES_DIR_PATH = "--ifw-packages-directory-path";
+ static constexpr StringLiteral OPTION_IFW_REPOSITORY_DIR_PATH = "--ifw-repository-directory-path";
+ static constexpr StringLiteral OPTION_IFW_CONFIG_FILE_PATH = "--ifw-configuration-file-path";
+ static constexpr StringLiteral OPTION_IFW_INSTALLER_FILE_PATH = "--ifw-installer-file-path";
+
+ static constexpr std::array<CommandSwitch, 6> EXPORT_SWITCHES = {{
{OPTION_DRY_RUN, "Do not actually export"},
{OPTION_RAW, "Export to an uncompressed directory"},
{OPTION_NUGET, "Export a NuGet package"},
@@ -284,12 +285,9 @@ namespace vcpkg::Export
{OPTION_SEVEN_ZIP, "Export to a 7zip (.7z) file"},
}};
- static const std::string EXPORT_SETTINGS_NUGET_ID_HELP_TEXT =
- Strings::format("Specify the id for the exported NuGet package (overrides %s)", OPTION_OUTPUT);
-
- static const std::array<CommandSetting, 8> EXPORT_SETTINGS = {{
+ static constexpr std::array<CommandSetting, 8> EXPORT_SETTINGS = {{
{OPTION_OUTPUT, "Specify the output name (used to construct filename)"},
- {OPTION_NUGET_ID, EXPORT_SETTINGS_NUGET_ID_HELP_TEXT},
+ {OPTION_NUGET_ID, "Specify the id for the exported NuGet package (overrides --output)"},
{OPTION_NUGET_VERSION, "Specify the version for the exported NuGet package"},
{OPTION_IFW_REPOSITORY_URL, "Specify the remote repository URL for the online installer"},
{OPTION_IFW_PACKAGES_DIR_PATH, "Specify the temporary directory path for the repacked packages"},

MODIFY
src/vcpkg/install.cpp
src/vcpkg/install.cpp
@@ -413,21 +413,21 @@ namespace vcpkg::Install
return InstallSummary{std::move(results), timer.to_string()};
}
- static const std::string OPTION_DRY_RUN = "--dry-run";
- static const std::string OPTION_USE_HEAD_VERSION = "--head";
- static const std::string OPTION_NO_DOWNLOADS = "--no-downloads";
- static const std::string OPTION_RECURSE = "--recurse";
- static const std::string OPTION_KEEP_GOING = "--keep-going";
- static const std::string OPTION_XUNIT = "--x-xunit";
-
- static const std::array<CommandSwitch, 5> INSTALL_SWITCHES = {{
+ static constexpr StringLiteral OPTION_DRY_RUN = "--dry-run";
+ static constexpr StringLiteral OPTION_USE_HEAD_VERSION = "--head";
+ static constexpr StringLiteral OPTION_NO_DOWNLOADS = "--no-downloads";
+ static constexpr StringLiteral OPTION_RECURSE = "--recurse";
+ static constexpr StringLiteral OPTION_KEEP_GOING = "--keep-going";
+ static constexpr StringLiteral OPTION_XUNIT = "--x-xunit";
+
+ static constexpr std::array<CommandSwitch, 5> INSTALL_SWITCHES = {{
{OPTION_DRY_RUN, "Do not actually build or install"},
{OPTION_USE_HEAD_VERSION, "Install the libraries on the command line using the latest upstream sources"},
{OPTION_NO_DOWNLOADS, "Do not download new sources"},
{OPTION_RECURSE, "Allow removal of packages as part of installation"},
{OPTION_KEEP_GOING, "Continue installing packages on failure"},
}};
- static const std::array<CommandSetting, 1> INSTALL_SETTINGS = {{
+ static constexpr std::array<CommandSetting, 1> INSTALL_SETTINGS = {{
{OPTION_XUNIT, "File to output results in XUnit format (Internal use)"},
}};

MODIFY
src/vcpkg/remove.cpp
src/vcpkg/remove.cpp
@@ -164,13 +164,13 @@ namespace vcpkg::Remove
}
}
- static const std::string OPTION_PURGE = "--purge";
- static const std::string OPTION_NO_PURGE = "--no-purge";
- static const std::string OPTION_RECURSE = "--recurse";
- static const std::string OPTION_DRY_RUN = "--dry-run";
- static const std::string OPTION_OUTDATED = "--outdated";
+ static constexpr StringLiteral OPTION_PURGE = "--purge";
+ static constexpr StringLiteral OPTION_NO_PURGE = "--no-purge";
+ static constexpr StringLiteral OPTION_RECURSE = "--recurse";
+ static constexpr StringLiteral OPTION_DRY_RUN = "--dry-run";
+ static constexpr StringLiteral OPTION_OUTDATED = "--outdated";
- static const std::array<CommandSwitch, 5> SWITCHES = {{
+ static constexpr std::array<CommandSwitch, 5> SWITCHES = {{
{OPTION_PURGE, "Remove the cached copy of the package (default)"},
{OPTION_NO_PURGE, "Do not remove the cached copy of the package"},
{OPTION_RECURSE, "Allow removal of packages not explicitly specified on the command line"},

Use StringLiteral and constexpr for options/switches