Skip to content

Commit 6bf5adf

Browse files
authored
[vcpkg] Clean up command switch code (microsoft#12351)
* [vcpkg] Clean up command switch code Make it more similar to the non-command switch code * format * fix the tests * reformat * format * wip * support x- for command options * fix autocomplete * format
1 parent 5a4e2c0 commit 6bf5adf

18 files changed

+195
-188
lines changed

toolsrc/include/vcpkg/vcpkgcmdarguments.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ namespace vcpkg
182182
void track_feature_flag_metrics() const;
183183

184184
private:
185-
std::unordered_map<std::string, Optional<std::vector<std::string>>> optional_command_arguments;
185+
std::unordered_set<std::string> command_switches;
186+
std::unordered_map<std::string, std::vector<std::string>> command_options;
186187
};
187188
}

toolsrc/src/vcpkg-test/arguments.cpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -77,33 +77,33 @@ TEST_CASE ("VcpkgCmdArguments from argument sequence with valued options", "[arg
7777
{
7878
SECTION ("case 1")
7979
{
80-
std::array<CommandSetting, 1> settings = {{{"--a", ""}}};
80+
std::array<CommandSetting, 1> settings = {{{"a", ""}}};
8181
CommandStructure cmdstruct = {"", 0, SIZE_MAX, {{}, settings}, nullptr};
8282

8383
std::vector<std::string> t = {"--a=b", "command", "argument"};
8484
auto v = VcpkgCmdArguments::create_from_arg_sequence(t.data(), t.data() + t.size());
8585
auto opts = v.parse_arguments(cmdstruct);
8686

87-
REQUIRE(opts.settings["--a"] == "b");
87+
REQUIRE(opts.settings["a"] == "b");
8888
REQUIRE(v.command_arguments.size() == 1);
8989
REQUIRE(v.command_arguments[0] == "argument");
9090
REQUIRE(v.command == "command");
9191
}
9292

9393
SECTION ("case 2")
9494
{
95-
std::array<CommandSwitch, 2> switches = {{{"--a", ""}, {"--c", ""}}};
96-
std::array<CommandSetting, 2> settings = {{{"--b", ""}, {"--d", ""}}};
95+
std::array<CommandSwitch, 2> switches = {{{"a", ""}, {"c", ""}}};
96+
std::array<CommandSetting, 2> settings = {{{"b", ""}, {"d", ""}}};
9797
CommandStructure cmdstruct = {"", 0, SIZE_MAX, {switches, settings}, nullptr};
9898

9999
std::vector<std::string> t = {"--a", "--b=c"};
100100
auto v = VcpkgCmdArguments::create_from_arg_sequence(t.data(), t.data() + t.size());
101101
auto opts = v.parse_arguments(cmdstruct);
102102

103-
REQUIRE(opts.settings["--b"] == "c");
104-
REQUIRE(opts.settings.find("--d") == opts.settings.end());
105-
REQUIRE(opts.switches.find("--a") != opts.switches.end());
106-
REQUIRE(opts.settings.find("--c") == opts.settings.end());
103+
REQUIRE(opts.settings["b"] == "c");
104+
REQUIRE(opts.settings.find("d") == opts.settings.end());
105+
REQUIRE(opts.switches.find("a") != opts.switches.end());
106+
REQUIRE(opts.settings.find("c") == opts.settings.end());
107107
REQUIRE(v.command_arguments.size() == 0);
108108
}
109109
}

toolsrc/src/vcpkg/commands.autocomplete.cpp

+9-3
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,17 @@ namespace vcpkg::Commands::Autocomplete
138138
const bool is_option = Strings::case_insensitive_ascii_starts_with(prefix, "-");
139139
if (is_option)
140140
{
141-
results = Util::fmap(command.structure.options.switches,
142-
[](const CommandSwitch& s) -> std::string { return s.name.to_string(); });
141+
results = Util::fmap(command.structure.options.switches, [](const CommandSwitch& s) -> std::string {
142+
return Strings::format("--%s", s.name.to_string());
143+
});
143144

144-
auto settings = Util::fmap(command.structure.options.settings, [](auto&& s) { return s.name; });
145+
auto settings = Util::fmap(command.structure.options.settings,
146+
[](auto&& s) { return Strings::format("--%s", s.name); });
145147
results.insert(results.end(), settings.begin(), settings.end());
148+
149+
auto multisettings = Util::fmap(command.structure.options.multisettings,
150+
[](auto&& s) { return Strings::format("--%s", s.name); });
151+
results.insert(results.end(), multisettings.begin(), multisettings.end());
146152
}
147153
else
148154
{

toolsrc/src/vcpkg/commands.ci.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,11 @@ namespace vcpkg::Commands::CI
8282
Install::InstallSummary summary;
8383
};
8484

85-
static constexpr StringLiteral OPTION_DRY_RUN = "--dry-run";
86-
static constexpr StringLiteral OPTION_EXCLUDE = "--exclude";
87-
static constexpr StringLiteral OPTION_FAILURE_LOGS = "--failure-logs";
88-
static constexpr StringLiteral OPTION_XUNIT = "--x-xunit";
89-
static constexpr StringLiteral OPTION_RANDOMIZE = "--x-randomize";
85+
static constexpr StringLiteral OPTION_DRY_RUN = "dry-run";
86+
static constexpr StringLiteral OPTION_EXCLUDE = "exclude";
87+
static constexpr StringLiteral OPTION_FAILURE_LOGS = "failure-logs";
88+
static constexpr StringLiteral OPTION_XUNIT = "x-xunit";
89+
static constexpr StringLiteral OPTION_RANDOMIZE = "x-randomize";
9090

9191
static constexpr std::array<CommandSetting, 3> CI_SETTINGS = {
9292
{{OPTION_EXCLUDE, "Comma separated list of ports to skip"},

toolsrc/src/vcpkg/commands.contact.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace vcpkg::Commands::Contact
1616
return S_EMAIL;
1717
}
1818

19-
static constexpr StringLiteral OPTION_SURVEY = "--survey";
19+
static constexpr StringLiteral OPTION_SURVEY = "survey";
2020

2121
static constexpr std::array<CommandSwitch, 1> SWITCHES = {{
2222
{OPTION_SURVEY, "Launch default browser to the current vcpkg survey"},

toolsrc/src/vcpkg/commands.dependinfo.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ namespace vcpkg::Commands::DependInfo
2121
{
2222
namespace
2323
{
24-
constexpr StringLiteral OPTION_DOT = "--dot";
25-
constexpr StringLiteral OPTION_DGML = "--dgml";
26-
constexpr StringLiteral OPTION_SHOW_DEPTH = "--show-depth";
27-
constexpr StringLiteral OPTION_MAX_RECURSE = "--max-recurse";
28-
constexpr StringLiteral OPTION_SORT = "--sort";
24+
constexpr StringLiteral OPTION_DOT = "dot";
25+
constexpr StringLiteral OPTION_DGML = "dgml";
26+
constexpr StringLiteral OPTION_SHOW_DEPTH = "show-depth";
27+
constexpr StringLiteral OPTION_MAX_RECURSE = "max-recurse";
28+
constexpr StringLiteral OPTION_SORT = "sort";
2929

3030
constexpr int NO_RECURSE_LIMIT_VALUE = -1;
3131

toolsrc/src/vcpkg/commands.edit.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ namespace
8080

8181
namespace vcpkg::Commands::Edit
8282
{
83-
static constexpr StringLiteral OPTION_BUILDTREES = "--buildtrees";
83+
static constexpr StringLiteral OPTION_BUILDTREES = "buildtrees";
8484

85-
static constexpr StringLiteral OPTION_ALL = "--all";
85+
static constexpr StringLiteral OPTION_ALL = "all";
8686

8787
static std::vector<std::string> valid_arguments(const VcpkgPaths& paths)
8888
{

toolsrc/src/vcpkg/commands.env.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010

1111
namespace vcpkg::Commands::Env
1212
{
13-
static constexpr StringLiteral OPTION_BIN = "--bin";
14-
static constexpr StringLiteral OPTION_INCLUDE = "--include";
15-
static constexpr StringLiteral OPTION_DEBUG_BIN = "--debug-bin";
16-
static constexpr StringLiteral OPTION_TOOLS = "--tools";
17-
static constexpr StringLiteral OPTION_PYTHON = "--python";
13+
static constexpr StringLiteral OPTION_BIN = "bin";
14+
static constexpr StringLiteral OPTION_INCLUDE = "include";
15+
static constexpr StringLiteral OPTION_DEBUG_BIN = "debug-bin";
16+
static constexpr StringLiteral OPTION_TOOLS = "tools";
17+
static constexpr StringLiteral OPTION_PYTHON = "python";
1818

1919
static constexpr std::array<CommandSwitch, 5> SWITCHES = {{
2020
{OPTION_BIN, "Add installed bin/ to PATH"},

toolsrc/src/vcpkg/commands.format-manifest.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace vcpkg::Commands::FormatManifest
1212
{
13-
static constexpr StringLiteral OPTION_ALL = "--all";
13+
static constexpr StringLiteral OPTION_ALL = "all";
1414

1515
const CommandSwitch FORMAT_SWITCHES[] = {{OPTION_ALL, "Format all ports' manifest files."}};
1616

toolsrc/src/vcpkg/commands.list.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99

1010
namespace vcpkg::Commands::List
1111
{
12-
static constexpr StringLiteral OPTION_FULLDESC =
13-
"--x-full-desc"; // TODO: This should find a better home, eventually
12+
static constexpr StringLiteral OPTION_FULLDESC = "x-full-desc"; // TODO: This should find a better home, eventually
1413

1514
static void do_print(const StatusParagraph& pgh, const bool full_desc)
1615
{

toolsrc/src/vcpkg/commands.search.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ using vcpkg::PortFileProvider::PathsPortFileProvider;
1515

1616
namespace vcpkg::Commands::Search
1717
{
18-
static constexpr StringLiteral OPTION_FULLDESC =
19-
"--x-full-desc"; // TODO: This should find a better home, eventually
18+
static constexpr StringLiteral OPTION_FULLDESC = "x-full-desc"; // TODO: This should find a better home, eventually
2019

2120
static void do_print(const SourceParagraph& source_paragraph, bool full_desc)
2221
{

toolsrc/src/vcpkg/commands.setinstalled.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

1515
namespace vcpkg::Commands::SetInstalled
1616
{
17-
static constexpr StringLiteral OPTION_DRY_RUN = "--dry-run";
18-
static constexpr StringLiteral OPTION_WRITE_PACKAGES_CONFIG = "--x-write-nuget-packages-config";
17+
static constexpr StringLiteral OPTION_DRY_RUN = "dry-run";
18+
static constexpr StringLiteral OPTION_WRITE_PACKAGES_CONFIG = "x-write-nuget-packages-config";
1919

2020
static constexpr CommandSwitch INSTALL_SWITCHES[] = {
2121
{OPTION_DRY_RUN, "Do not actually build or install"},

toolsrc/src/vcpkg/commands.upgrade.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ namespace vcpkg::Commands::Upgrade
1919
using Install::KeepGoing;
2020
using Install::to_keep_going;
2121

22-
static constexpr StringLiteral OPTION_NO_DRY_RUN = "--no-dry-run";
23-
static constexpr StringLiteral OPTION_KEEP_GOING = "--keep-going";
22+
static constexpr StringLiteral OPTION_NO_DRY_RUN = "no-dry-run";
23+
static constexpr StringLiteral OPTION_KEEP_GOING = "keep-going";
2424

2525
static constexpr std::array<CommandSwitch, 2> INSTALL_SWITCHES = {{
2626
{OPTION_NO_DRY_RUN, "Actually upgrade"},

toolsrc/src/vcpkg/export.chocolatey.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ Get-Content $whereToInstallCache | Foreach-Object {
114114
}
115115
116116
$installedDir = Join-Path $whereToInstall 'installed'
117-
Get-Content $listFile | Foreach-Object {
117+
Get-Content $listFile | Foreach-Object {
118118
$fileToRemove = Join-Path $installedDir $_
119119
if (Test-Path $fileToRemove -PathType Leaf) {
120120
Remove-Item $fileToRemove

toolsrc/src/vcpkg/export.cpp

+27-27
Original file line numberDiff line numberDiff line change
@@ -272,33 +272,33 @@ namespace vcpkg::Export
272272
std::vector<PackageSpec> specs;
273273
};
274274

275-
static constexpr StringLiteral OPTION_OUTPUT = "--output";
276-
static constexpr StringLiteral OPTION_DRY_RUN = "--dry-run";
277-
static constexpr StringLiteral OPTION_RAW = "--raw";
278-
static constexpr StringLiteral OPTION_NUGET = "--nuget";
279-
static constexpr StringLiteral OPTION_IFW = "--ifw";
280-
static constexpr StringLiteral OPTION_ZIP = "--zip";
281-
static constexpr StringLiteral OPTION_SEVEN_ZIP = "--7zip";
282-
static constexpr StringLiteral OPTION_NUGET_ID = "--nuget-id";
283-
static constexpr StringLiteral OPTION_NUGET_VERSION = "--nuget-version";
284-
static constexpr StringLiteral OPTION_IFW_REPOSITORY_URL = "--ifw-repository-url";
285-
static constexpr StringLiteral OPTION_IFW_PACKAGES_DIR_PATH = "--ifw-packages-directory-path";
286-
static constexpr StringLiteral OPTION_IFW_REPOSITORY_DIR_PATH = "--ifw-repository-directory-path";
287-
static constexpr StringLiteral OPTION_IFW_CONFIG_FILE_PATH = "--ifw-configuration-file-path";
288-
static constexpr StringLiteral OPTION_IFW_INSTALLER_FILE_PATH = "--ifw-installer-file-path";
289-
static constexpr StringLiteral OPTION_CHOCOLATEY = "--x-chocolatey";
290-
static constexpr StringLiteral OPTION_CHOCOLATEY_MAINTAINER = "--x-maintainer";
291-
static constexpr StringLiteral OPTION_CHOCOLATEY_VERSION_SUFFIX = "--x-version-suffix";
292-
static constexpr StringLiteral OPTION_ALL_INSTALLED = "--x-all-installed";
293-
294-
static constexpr StringLiteral OPTION_PREFAB = "--prefab";
295-
static constexpr StringLiteral OPTION_PREFAB_GROUP_ID = "--prefab-group-id";
296-
static constexpr StringLiteral OPTION_PREFAB_ARTIFACT_ID = "--prefab-artifact-id";
297-
static constexpr StringLiteral OPTION_PREFAB_VERSION = "--prefab-version";
298-
static constexpr StringLiteral OPTION_PREFAB_SDK_MIN_VERSION = "--prefab-min-sdk";
299-
static constexpr StringLiteral OPTION_PREFAB_SDK_TARGET_VERSION = "--prefab-target-sdk";
300-
static constexpr StringLiteral OPTION_PREFAB_ENABLE_MAVEN = "--prefab-maven";
301-
static constexpr StringLiteral OPTION_PREFAB_ENABLE_DEBUG = "--prefab-debug";
275+
static constexpr StringLiteral OPTION_OUTPUT = "output";
276+
static constexpr StringLiteral OPTION_DRY_RUN = "dry-run";
277+
static constexpr StringLiteral OPTION_RAW = "raw";
278+
static constexpr StringLiteral OPTION_NUGET = "nuget";
279+
static constexpr StringLiteral OPTION_IFW = "ifw";
280+
static constexpr StringLiteral OPTION_ZIP = "zip";
281+
static constexpr StringLiteral OPTION_SEVEN_ZIP = "7zip";
282+
static constexpr StringLiteral OPTION_NUGET_ID = "nuget-id";
283+
static constexpr StringLiteral OPTION_NUGET_VERSION = "nuget-version";
284+
static constexpr StringLiteral OPTION_IFW_REPOSITORY_URL = "ifw-repository-url";
285+
static constexpr StringLiteral OPTION_IFW_PACKAGES_DIR_PATH = "ifw-packages-directory-path";
286+
static constexpr StringLiteral OPTION_IFW_REPOSITORY_DIR_PATH = "ifw-repository-directory-path";
287+
static constexpr StringLiteral OPTION_IFW_CONFIG_FILE_PATH = "ifw-configuration-file-path";
288+
static constexpr StringLiteral OPTION_IFW_INSTALLER_FILE_PATH = "ifw-installer-file-path";
289+
static constexpr StringLiteral OPTION_CHOCOLATEY = "x-chocolatey";
290+
static constexpr StringLiteral OPTION_CHOCOLATEY_MAINTAINER = "x-maintainer";
291+
static constexpr StringLiteral OPTION_CHOCOLATEY_VERSION_SUFFIX = "x-version-suffix";
292+
static constexpr StringLiteral OPTION_ALL_INSTALLED = "x-all-installed";
293+
294+
static constexpr StringLiteral OPTION_PREFAB = "prefab";
295+
static constexpr StringLiteral OPTION_PREFAB_GROUP_ID = "prefab-group-id";
296+
static constexpr StringLiteral OPTION_PREFAB_ARTIFACT_ID = "prefab-artifact-id";
297+
static constexpr StringLiteral OPTION_PREFAB_VERSION = "prefab-version";
298+
static constexpr StringLiteral OPTION_PREFAB_SDK_MIN_VERSION = "prefab-min-sdk";
299+
static constexpr StringLiteral OPTION_PREFAB_SDK_TARGET_VERSION = "prefab-target-sdk";
300+
static constexpr StringLiteral OPTION_PREFAB_ENABLE_MAVEN = "prefab-maven";
301+
static constexpr StringLiteral OPTION_PREFAB_ENABLE_DEBUG = "prefab-debug";
302302

303303
static constexpr std::array<CommandSwitch, 11> EXPORT_SWITCHES = {{
304304
{OPTION_DRY_RUN, "Do not actually export"},

toolsrc/src/vcpkg/install.cpp

+11-11
Original file line numberDiff line numberDiff line change
@@ -501,17 +501,17 @@ namespace vcpkg::Install
501501
return InstallSummary{std::move(results), timer.to_string()};
502502
}
503503

504-
static constexpr StringLiteral OPTION_DRY_RUN = "--dry-run";
505-
static constexpr StringLiteral OPTION_USE_HEAD_VERSION = "--head";
506-
static constexpr StringLiteral OPTION_NO_DOWNLOADS = "--no-downloads";
507-
static constexpr StringLiteral OPTION_ONLY_DOWNLOADS = "--only-downloads";
508-
static constexpr StringLiteral OPTION_RECURSE = "--recurse";
509-
static constexpr StringLiteral OPTION_KEEP_GOING = "--keep-going";
510-
static constexpr StringLiteral OPTION_EDITABLE = "--editable";
511-
static constexpr StringLiteral OPTION_XUNIT = "--x-xunit";
512-
static constexpr StringLiteral OPTION_USE_ARIA2 = "--x-use-aria2";
513-
static constexpr StringLiteral OPTION_CLEAN_AFTER_BUILD = "--clean-after-build";
514-
static constexpr StringLiteral OPTION_WRITE_PACKAGES_CONFIG = "--x-write-nuget-packages-config";
504+
static constexpr StringLiteral OPTION_DRY_RUN = "dry-run";
505+
static constexpr StringLiteral OPTION_USE_HEAD_VERSION = "head";
506+
static constexpr StringLiteral OPTION_NO_DOWNLOADS = "no-downloads";
507+
static constexpr StringLiteral OPTION_ONLY_DOWNLOADS = "only-downloads";
508+
static constexpr StringLiteral OPTION_RECURSE = "recurse";
509+
static constexpr StringLiteral OPTION_KEEP_GOING = "keep-going";
510+
static constexpr StringLiteral OPTION_EDITABLE = "editable";
511+
static constexpr StringLiteral OPTION_XUNIT = "x-xunit";
512+
static constexpr StringLiteral OPTION_USE_ARIA2 = "x-use-aria2";
513+
static constexpr StringLiteral OPTION_CLEAN_AFTER_BUILD = "clean-after-build";
514+
static constexpr StringLiteral OPTION_WRITE_PACKAGES_CONFIG = "x-write-nuget-packages-config";
515515

516516
static constexpr std::array<CommandSwitch, 9> INSTALL_SWITCHES = {{
517517
{OPTION_DRY_RUN, "Do not actually build or install"},

toolsrc/src/vcpkg/remove.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,11 @@ namespace vcpkg::Remove
184184
}
185185
}
186186

187-
static constexpr StringLiteral OPTION_PURGE = "--purge";
188-
static constexpr StringLiteral OPTION_NO_PURGE = "--no-purge";
189-
static constexpr StringLiteral OPTION_RECURSE = "--recurse";
190-
static constexpr StringLiteral OPTION_DRY_RUN = "--dry-run";
191-
static constexpr StringLiteral OPTION_OUTDATED = "--outdated";
187+
static constexpr StringLiteral OPTION_PURGE = "purge";
188+
static constexpr StringLiteral OPTION_NO_PURGE = "no-purge";
189+
static constexpr StringLiteral OPTION_RECURSE = "recurse";
190+
static constexpr StringLiteral OPTION_DRY_RUN = "dry-run";
191+
static constexpr StringLiteral OPTION_OUTDATED = "outdated";
192192

193193
static constexpr std::array<CommandSwitch, 5> SWITCHES = {{
194194
{OPTION_PURGE, "Remove the cached copy of the package (default)"},

0 commit comments

Comments
 (0)