Skip to content

Commit cdb0e71

Browse files
authored
Convert realValue method to function, update docs (#298)
1 parent bafa74a commit cdb0e71

File tree

3 files changed

+21
-28
lines changed

3 files changed

+21
-28
lines changed

Sources/ArgumentParser/Parsable Types/ParsableArguments.swift

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,25 @@ extension ParsableArguments {
208208
}
209209
}
210210

211+
/// Unboxes the given value if it is a `nil` value.
212+
///
213+
/// If the value passed is the `.none` case of any optional type, this function
214+
/// returns `nil`.
215+
///
216+
/// let intAsAny = (1 as Int?) as Any
217+
/// let nilAsAny = (nil as Int?) as Any
218+
/// nilOrValue(intAsAny) // Optional(1) as Any?
219+
/// nilOrValue(nilAsAny) // nil as Any?
220+
func nilOrValue(_ value: Any) -> Any? {
221+
if case Optional<Any>.none = value {
222+
return nil
223+
} else {
224+
return value
225+
}
226+
}
227+
228+
/// Existential protocol for property wrappers, so that they can provide
229+
/// the argument set that they define.
211230
protocol ArgumentSetProvider {
212231
func argumentSet(for key: InputKey) -> ArgumentSet
213232
}
@@ -241,7 +260,7 @@ extension ArgumentSet {
241260
key: InputKey(rawValue: codingKey),
242261
kind: .default,
243262
parser: { _ in nil },
244-
default: Mirror.realValue(for: child),
263+
default: nilOrValue(child.value),
245264
completion: .default)
246265
definition.help.help = .hidden
247266
return ArgumentSet(definition)

Sources/ArgumentParser/Utilities/MirrorExtensions.swift

Lines changed: 0 additions & 26 deletions
This file was deleted.

Tests/ArgumentParserUnitTests/MirrorTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ extension MirrorTests {
2929
}
3030
XCTAssertEqual(stringValue, expectedString)
3131
} else {
32-
XCTAssertNil(Mirror.realValue(for: child))
32+
XCTAssertNil(nilOrValue(child.value))
3333
// This is why we use `unwrapedOptionalValue` for optionality checks
3434
// Even though the `value` is `nil` this returns `false`
3535
XCTAssertFalse(child.value as Any? == nil)

0 commit comments

Comments
 (0)