diff options
| author | Alexander Karatarakis <alkarata@microsoft.com> | 2017-01-31 13:31:45 -0800 |
|---|---|---|
| committer | Alexander Karatarakis <alkarata@microsoft.com> | 2017-02-01 11:42:41 -0800 |
| commit | bd1a10e5b97b073731cbb97e26611edf317c16d5 (patch) | |
| tree | 0c55b8322c3d6598ebbe67985ee3c2f5a2421c86 | |
| parent | f2d40c5b81cba14c86d836bc21978c4f3f691538 (diff) | |
| download | vcpkg-bd1a10e5b97b073731cbb97e26611edf317c16d5.tar.gz vcpkg-bd1a10e5b97b073731cbb97e26611edf317c16d5.zip | |
Enhance the opt_bool type
| -rw-r--r-- | toolsrc/include/opt_bool.h | 28 | ||||
| -rw-r--r-- | toolsrc/include/vcpkg_cmd_arguments.h | 6 | ||||
| -rw-r--r-- | toolsrc/src/opt_bool.cpp | 29 | ||||
| -rw-r--r-- | toolsrc/src/vcpkg.cpp | 12 | ||||
| -rw-r--r-- | toolsrc/src/vcpkg_cmd_arguments.cpp | 16 | ||||
| -rw-r--r-- | toolsrc/vcpkglib/vcpkglib.vcxproj | 1 | ||||
| -rw-r--r-- | toolsrc/vcpkglib/vcpkglib.vcxproj.filters | 3 |
7 files changed, 75 insertions, 20 deletions
diff --git a/toolsrc/include/opt_bool.h b/toolsrc/include/opt_bool.h index 42133fb7a..06642a399 100644 --- a/toolsrc/include/opt_bool.h +++ b/toolsrc/include/opt_bool.h @@ -1,11 +1,33 @@ #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 diff --git a/toolsrc/include/vcpkg_cmd_arguments.h b/toolsrc/include/vcpkg_cmd_arguments.h index 79e12841d..91f7de8ac 100644 --- a/toolsrc/include/vcpkg_cmd_arguments.h +++ b/toolsrc/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; diff --git a/toolsrc/src/opt_bool.cpp b/toolsrc/src/opt_bool.cpp new file mode 100644 index 000000000..324936fb4 --- /dev/null +++ b/toolsrc/src/opt_bool.cpp @@ -0,0 +1,29 @@ +#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); + } +} diff --git a/toolsrc/src/vcpkg.cpp b/toolsrc/src/vcpkg.cpp index 36bd0a40a..3e313c702 100644 --- a/toolsrc/src/vcpkg.cpp +++ b/toolsrc/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) diff --git a/toolsrc/src/vcpkg_cmd_arguments.cpp b/toolsrc/src/vcpkg_cmd_arguments.cpp index e0d307077..fdeb6e877 100644 --- a/toolsrc/src/vcpkg_cmd_arguments.cpp +++ b/toolsrc/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; } diff --git a/toolsrc/vcpkglib/vcpkglib.vcxproj b/toolsrc/vcpkglib/vcpkglib.vcxproj index e6fbfb65a..e0374774c 100644 --- a/toolsrc/vcpkglib/vcpkglib.vcxproj +++ b/toolsrc/vcpkglib/vcpkglib.vcxproj @@ -198,6 +198,7 @@ <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> diff --git a/toolsrc/vcpkglib/vcpkglib.vcxproj.filters b/toolsrc/vcpkglib/vcpkglib.vcxproj.filters index 7ddfba6f2..40bde1536 100644 --- a/toolsrc/vcpkglib/vcpkglib.vcxproj.filters +++ b/toolsrc/vcpkglib/vcpkglib.vcxproj.filters @@ -153,6 +153,9 @@ <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"> |
