@@ -37,16 +37,19 @@ public struct Option<Value>: OptionProtocol {
3737
3838 // Different Value types will encode to arguments differently.
3939 // Using unwrap, this can be handled individually per type or collectively by protocol
40- private let unwrap : @Sendable ( Value ) -> [ String ]
41- internal var unwrapped : [ String ] {
40+ private let unwrap : @Sendable ( Value ) -> String ?
41+ internal var unwrapped : String ? {
4242 unwrap ( wrappedValue)
4343 }
4444
45- func encoding( key: String ? = nil ) -> [ OptionEncoding ] {
45+ func encoding( key: String ? = nil ) -> OptionEncoding ? {
4646 guard let _key = keyOverride ?? key else {
47- return [ ]
47+ return nil
4848 }
49- return unwrapped. map { OptionEncoding ( key: _key, value: $0) }
49+ guard let unwrapped else {
50+ return nil
51+ }
52+ return OptionEncoding ( key: _key, value: unwrapped)
5053 }
5154
5255 /// Get the Option's argument encoding. If `keyOverRide` and `key` are both `nil`, it will return an empty array.
@@ -55,7 +58,7 @@ public struct Option<Value>: OptionProtocol {
5558 /// - key: Optionally provide a key value.
5659 /// - Returns: The argument encoding which is an array of strings
5760 public func arguments( key: String ? = nil ) -> [ String ] {
58- encoding ( key: key) . flatMap { $0 . arguments ( ) }
61+ encoding ( key: key) ? . arguments ( ) ?? [ ]
5962 }
6063
6164 /// Initializes a new option when not used as a `@propertyWrapper`
@@ -64,7 +67,7 @@ public struct Option<Value>: OptionProtocol {
6467 /// - key: Explicit key value
6568 /// - wrappedValue: The underlying value
6669 /// - unwrap: A closure for mapping a Value to [String]
67- public init ( key: some CustomStringConvertible , value: Value , unwrap: @escaping @Sendable ( Value ) -> [ String ] ) {
70+ public init ( key: some CustomStringConvertible , value: Value , unwrap: @escaping @Sendable ( Value ) -> String ? ) {
6871 keyOverride = key. description
6972 wrappedValue = value
7073 self . unwrap = unwrap
@@ -76,7 +79,7 @@ public struct Option<Value>: OptionProtocol {
7679 /// - wrappedValue: The underlying value
7780 /// - _ key: Optional explicit key value
7881 /// - _ unwrap: A closure for mapping a Value to [String]
79- public init ( wrappedValue: Value , _ key: String ? = nil , _ unwrap: @escaping @Sendable ( Value ) -> [ String ] ) {
82+ public init ( wrappedValue: Value , _ key: String ? = nil , _ unwrap: @escaping @Sendable ( Value ) -> String ? ) {
8083 keyOverride = key
8184 self . wrappedValue = wrappedValue
8285 self . unwrap = unwrap
@@ -128,8 +131,8 @@ extension Option where Value: CustomStringConvertible {
128131 }
129132
130133 @Sendable
131- public static func unwrap( _ value: Value ) -> [ String ] {
132- [ value. description]
134+ public static func unwrap( _ value: Value ) -> String ? {
135+ value. description
133136 }
134137}
135138
@@ -157,8 +160,8 @@ extension Option where Value: RawRepresentable, Value.RawValue: CustomStringConv
157160 }
158161
159162 @Sendable
160- public static func unwrap( _ value: Value ) -> [ String ] {
161- [ value. rawValue. description]
163+ public static func unwrap( _ value: Value ) -> String ? {
164+ value. rawValue. description
162165 }
163166}
164167
@@ -188,8 +191,8 @@ extension Option where Value: CustomStringConvertible, Value: RawRepresentable,
188191 }
189192
190193 @Sendable
191- public static func unwrap( _ value: Value ) -> [ String ] {
192- [ value. rawValue. description]
194+ public static func unwrap( _ value: Value ) -> String ? {
195+ value. rawValue. description
193196 }
194197}
195198
@@ -223,62 +226,25 @@ extension Option {
223226 }
224227
225228 @Sendable
226- public static func unwrap< Wrapped> ( _ value: Wrapped ? ) -> [ String ] where Wrapped: CustomStringConvertible ,
229+ public static func unwrap< Wrapped> ( _ value: Wrapped ? ) -> String ? where Wrapped: CustomStringConvertible ,
227230 Value == Wrapped ?
228231 {
229- [ value? . description] . compactMap { $0 }
230- }
231- }
232-
233- // MARK: Convenience initializers when Value == Sequence<E>
234-
235- extension Option {
236- /// Initializes a new option when not used as a `@propertyWrapper`
237- ///
238- /// - Parameters
239- /// - key: Explicit key value
240- /// - wrappedValue: The underlying value
241- public init < E> ( key: some CustomStringConvertible , values: Value ) where Value: Sequence , Value. Element == E ,
242- E: CustomStringConvertible
243- {
244- keyOverride = key. description
245- wrappedValue = values
246- unwrap = Self . unwrap ( _: )
247- }
248-
249- /// Initializes a new option when used as a `@propertyWrapper`
250- ///
251- /// - Parameters
252- /// - wrappedValue: The underlying value
253- /// - _ key: Optional explicit key value
254- public init < E> ( wrappedValue: Value , _ key: String ? = nil ) where Value: Sequence , Value. Element == E ,
255- E: CustomStringConvertible
256- {
257- keyOverride = key
258- self . wrappedValue = wrappedValue
259- unwrap = Self . unwrap ( _: )
260- }
261-
262- @Sendable
263- public static func unwrap< E> ( _ value: Value ) -> [ String ] where Value: Sequence , Value. Element == E ,
264- E: CustomStringConvertible
265- {
266- value. map ( \E . description)
232+ value? . description
267233 }
268234}
269235
270236// MARK: ExpressibleBy...Literal conformances
271237
272238extension Option : ExpressibleByIntegerLiteral where Value: BinaryInteger , Value. IntegerLiteralType == Int {
273239 public init ( integerLiteral value: IntegerLiteralType ) {
274- self . init ( wrappedValue: Value ( integerLiteral: value) , nil ) { [ $0. description] }
240+ self . init ( wrappedValue: Value ( integerLiteral: value) , nil ) { $0. description }
275241 }
276242}
277243
278244#if os(macOS)
279245 extension Option : ExpressibleByFloatLiteral where Value: BinaryFloatingPoint {
280246 public init ( floatLiteral value: FloatLiteralType ) {
281- self . init ( wrappedValue: Value ( value) , nil ) { [ $0. formatted ( ) ] }
247+ self . init ( wrappedValue: Value ( value) , nil ) { $0. formatted ( ) }
282248 }
283249 }
284250#endif
@@ -307,8 +273,10 @@ extension Option: ExpressibleByStringInterpolation where Value: StringProtocol {
307273 }
308274}
309275
276+ // MARK: Coding
277+
310278extension Option : DecodableWithConfiguration where Value: Decodable {
311- public init ( from decoder: Decoder , configuration: @escaping @Sendable ( Value ) -> [ String ] ) throws {
279+ public init ( from decoder: Decoder , configuration: @escaping @Sendable ( Value ) -> String ? ) throws {
312280 let container = try decoder. singleValueContainer ( )
313281 try self . init ( wrappedValue: container. decode ( Value . self) , nil , configuration)
314282 }
0 commit comments