Enhance the opt_bool type
MODIFY
include/opt_bool.h
include/opt_bool.h
#pragma once
-namespace vcpkg
+#include <string>
+#include <map>
+
+namespace vcpkg::opt_bool
{
- enum class opt_bool
+ enum class type
{
- UNSPECIFIED,
+ UNSPECIFIED = 0,
ENABLED,
DISABLED
};
+
+ type parse(const std::string& s);
+
+ template <class T>
+ type from_map(const std::map<T, std::string>& map, const T& key)
+ {
+ auto it = map.find(key);
+ if (it == map.cend())
+ {
+ return type::UNSPECIFIED;
+ }
+
+ return parse(*it);
+ }
+}
+
+namespace vcpkg
+{
+ using opt_bool_t = opt_bool::type;
}
\ No newline at end of file

MODIFY
include/vcpkg_cmd_arguments.h
include/vcpkg_cmd_arguments.h
@@ -14,9 +14,9 @@ namespace vcpkg
std::unique_ptr<std::string> vcpkg_root_dir;
std::unique_ptr<std::string> target_triplet;
- opt_bool debug = opt_bool::UNSPECIFIED;
- opt_bool sendmetrics = opt_bool::UNSPECIFIED;
- opt_bool printmetrics = opt_bool::UNSPECIFIED;
+ opt_bool_t debug = opt_bool_t::UNSPECIFIED;
+ opt_bool_t sendmetrics = opt_bool_t::UNSPECIFIED;
+ opt_bool_t printmetrics = opt_bool_t::UNSPECIFIED;
std::string command;
std::vector<std::string> command_arguments;

ADD
src/opt_bool.cpp
+#include "pch.h"
+#include "opt_bool.h"
+#include "vcpkg_Checks.h"
+
+namespace vcpkg::opt_bool
+{
+ static const std::string UNSPECIFIED_NAME = "unspecified";
+ static const std::string ENABLED_NAME = "enabled";
+ static const std::string DISABLED_NAME = "disabled";
+ type parse(const std::string& s)
+ {
+ if (s == UNSPECIFIED_NAME)
+ {
+ return opt_bool_t::UNSPECIFIED;
+ }
+
+ if (s == ENABLED_NAME)
+ {
+ return opt_bool_t::ENABLED;
+ }
+
+ if (s == DISABLED_NAME)
+ {
+ return opt_bool_t::DISABLED;
+ }
+
+ Checks::exit_with_message("Could not convert string [%s] to opt_bool", s);
+ }
+}

MODIFY
src/vcpkg.cpp
src/vcpkg.cpp
@@ -202,14 +202,14 @@ int wmain(const int argc, const wchar_t* const* const argv)
const vcpkg_cmd_arguments args = vcpkg_cmd_arguments::create_from_command_line(argc, argv);
- if (args.printmetrics != opt_bool::UNSPECIFIED)
- SetPrintMetrics(args.printmetrics == opt_bool::ENABLED);
- if (args.sendmetrics != opt_bool::UNSPECIFIED)
- SetSendMetrics(args.sendmetrics == opt_bool::ENABLED);
+ if (args.printmetrics != opt_bool_t::UNSPECIFIED)
+ SetPrintMetrics(args.printmetrics == opt_bool_t::ENABLED);
+ if (args.sendmetrics != opt_bool_t::UNSPECIFIED)
+ SetSendMetrics(args.sendmetrics == opt_bool_t::ENABLED);
- if (args.debug != opt_bool::UNSPECIFIED)
+ if (args.debug != opt_bool_t::UNSPECIFIED)
{
- g_debugging = (args.debug == opt_bool::ENABLED);
+ g_debugging = (args.debug == opt_bool_t::ENABLED);
}
if (g_debugging)

MODIFY
src/vcpkg_cmd_arguments.cpp
src/vcpkg_cmd_arguments.cpp
@@ -32,11 +32,11 @@ namespace vcpkg
}
static void parse_switch(
- opt_bool new_setting,
+ opt_bool_t new_setting,
const std::string& option_name,
- opt_bool& option_field)
+ opt_bool_t& option_field)
{
- if (option_field != opt_bool::UNSPECIFIED && option_field != new_setting)
+ if (option_field != opt_bool_t::UNSPECIFIED && option_field != new_setting)
{
System::println(System::color::error, "Error: conflicting values specified for --%s", option_name);
TrackProperty("error", "error conflicting switches");
@@ -94,27 +94,27 @@ namespace vcpkg
}
if (arg == "--debug")
{
- parse_switch(opt_bool::ENABLED, "debug", args.debug);
+ parse_switch(opt_bool_t::ENABLED, "debug", args.debug);
continue;
}
if (arg == "--sendmetrics")
{
- parse_switch(opt_bool::ENABLED, "sendmetrics", args.sendmetrics);
+ parse_switch(opt_bool_t::ENABLED, "sendmetrics", args.sendmetrics);
continue;
}
if (arg == "--printmetrics")
{
- parse_switch(opt_bool::ENABLED, "printmetrics", args.printmetrics);
+ parse_switch(opt_bool_t::ENABLED, "printmetrics", args.printmetrics);
continue;
}
if (arg == "--no-sendmetrics")
{
- parse_switch(opt_bool::DISABLED, "sendmetrics", args.sendmetrics);
+ parse_switch(opt_bool_t::DISABLED, "sendmetrics", args.sendmetrics);
continue;
}
if (arg == "--no-printmetrics")
{
- parse_switch(opt_bool::DISABLED, "printmetrics", args.printmetrics);
+ parse_switch(opt_bool_t::DISABLED, "printmetrics", args.printmetrics);
continue;
}

MODIFY
vcpkglib/vcpkglib.vcxproj
vcpkglib/vcpkglib.vcxproj
<ClCompile Include="..\src\commands_version.cpp" />
<ClCompile Include="..\src\MachineType.cpp" />
<ClCompile Include="..\src\metrics.cpp" />
+ <ClCompile Include="..\src\opt_bool.cpp" />
<ClCompile Include="..\src\pch.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader>

MODIFY
vcpkglib/vcpkglib.vcxproj.filters
vcpkglib/vcpkglib.vcxproj.filters
<ClCompile Include="..\src\pch.cpp">
<Filter>Source Files</Filter>
</ClCompile>
+ <ClCompile Include="..\src\opt_bool.cpp">
+ <Filter>Source Files</Filter>
+ </ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\include\package_spec.h">

Enhance the opt_bool type