| 1 | /*++ |
| 2 | |
| 3 | Copyright (c) Microsoft. All rights reserved. |
| 4 | |
| 5 | Module Name: |
| 6 | |
| 7 | OptionParser.h |
| 8 | |
| 9 | Abstract: |
| 10 | |
| 11 | Helper for parsing string-keyed driver options (e.g. WSLC volume driver |
| 12 | options) into typed values with consistent error reporting. |
| 13 | |
| 14 | Each accessor records the keys it consumed so RejectUnknown() can flag |
| 15 | options the caller passed but the driver does not support. All errors are |
| 16 | surfaced through the WSLC localized message strings. |
| 17 | |
| 18 | --*/ |
| 19 | |
| 20 | #pragma once |
| 21 | |
| 22 | #include <cerrno> |
| 23 | #include <limits> |
| 24 | #include <map> |
| 25 | #include <optional> |
| 26 | #include <set> |
| 27 | #include <string> |
| 28 | #include <string_view> |
| 29 | #include <type_traits> |
| 30 | |
| 31 | namespace wsl::windows::service::wslc { |
| 32 | |
| 33 | class OptionParser |
| 34 | { |
| 35 | public: |
| 36 | NON_COPYABLE(OptionParser); |
| 37 | |
| 38 | // Options map must outlive this OptionParser. |
| 39 | explicit OptionParser(const std::map<std::string, std::string>& Options) noexcept; |
| 40 | |
| 41 | // Required unsigned integer. Throws E_INVALIDARG if missing, empty, |
| 42 | // signed, non-decimal, overflows, or > Max. |
| 43 | template <typename T> |
| 44 | T Required(std::string_view Key, T Max = (std::numeric_limits<T>::max)()); |
| 45 | |
| 46 | // As Required, but returns nullopt when the key is absent. |
| 47 | template <typename T> |
| 48 | std::optional<T> Optional(std::string_view Key, T Max = (std::numeric_limits<T>::max)()); |
| 49 | |
| 50 | // Parses an optional boolean via wsl::shared::string::ParseBool |
| 51 | // ("0"/"1"/"true"/"false", case-insensitive). Throws on unknown values. |
| 52 | std::optional<bool> OptionalBool(std::string_view Key); |
| 53 | |
| 54 | // Throws E_INVALIDARG on the first option that was supplied but never consumed. |
| 55 | void RejectUnknown(); |
| 56 | |
| 57 | private: |
| 58 | // Returns the value for Key (nullptr if absent) and marks it consumed. |
| 59 | const std::string* Find(std::string_view Key); |
| 60 | |
| 61 | [[noreturn]] static void ThrowInvalid(std::string_view Key, const std::string& Value); |
| 62 | [[noreturn]] static void ThrowMissing(std::string_view Key); |
| 63 | [[noreturn]] static void ThrowUnknown(std::string_view Key); |
| 64 | |
| 65 | template <typename T> |
| 66 | static T ParseUnsignedValue(std::string_view Key, const std::string& Value, T Max); |
| 67 | |
| 68 | const std::map<std::string, std::string>& m_options; |
| 69 | std::set<std::string, std::less<>> m_consumed; |
| 70 | }; |
| 71 | |
| 72 | template <typename T> |
| 73 | inline T OptionParser::Required(std::string_view Key, T Max) |
| 74 | { |
| 75 | const auto* value = Find(Key); |
| 76 | if (value == nullptr) |
| 77 | { |
| 78 | ThrowMissing(Key); |
| 79 | } |
| 80 | |
| 81 | return ParseUnsignedValue<T>(Key, *value, Max); |
| 82 | } |
| 83 | |
| 84 | template <typename T> |
| 85 | inline std::optional<T> OptionParser::Optional(std::string_view Key, T Max) |
| 86 | { |
| 87 | const auto* value = Find(Key); |
| 88 | if (value == nullptr) |
| 89 | { |
| 90 | return std::nullopt; |
| 91 | } |
| 92 | |
| 93 | return ParseUnsignedValue<T>(Key, *value, Max); |
| 94 | } |
| 95 | |
| 96 | template <typename T> |
| 97 | inline T OptionParser::ParseUnsignedValue(std::string_view Key, const std::string& Value, T Max) |
| 98 | { |
| 99 | static_assert(std::is_unsigned_v<T>, "OptionParser numeric accessors only support unsigned integer types"); |
| 100 | |
| 101 | // strtoull treats a leading '-' as unsigned wraparound and accepts '+'; |
| 102 | // reject both (and empty input) up-front. |
| 103 | if (Value.empty() || Value.front() == '-' || Value.front() == '+') |
| 104 | { |
| 105 | ThrowInvalid(Key, Value); |
| 106 | } |
| 107 | |
| 108 | errno = 0; |
| 109 | char* end = nullptr; |
| 110 | const auto parsed = wsl::shared::string::ToUInt64(Value.c_str(), &end, 10); |
| 111 | // Capture errno immediately so debug allocators/logging hooks can't stomp it. |
| 112 | const int parseErrno = errno; |
| 113 | if (parseErrno != 0 || end == nullptr || *end != '\0' || parsed > static_cast<uint64_t>(Max)) |
| 114 | { |
| 115 | ThrowInvalid(Key, Value); |
| 116 | } |
| 117 | |
| 118 | return static_cast<T>(parsed); |
| 119 | } |
| 120 | |
| 121 | } // namespace wsl::windows::service::wslc |