Skip to content

Commit

Permalink
Use the customization point for Optional default values in help
Browse files Browse the repository at this point in the history
  • Loading branch information
natecook1000 committed Mar 22, 2020
1 parent e371989 commit ab0f98f
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions Sources/ArgumentParser/Parsable Types/ExpressibleByArgument.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ public protocol ExpressibleByArgument {
var defaultValueDescription: String { get }
}

extension ExpressibleByArgument {
public var defaultValueDescription: String {
"\(self)"
}
}

extension String: ExpressibleByArgument {
public init?(argument: String) {
self = argument
Expand All @@ -35,6 +41,13 @@ extension Optional: ExpressibleByArgument where Wrapped: ExpressibleByArgument {
return nil
}
}

public var defaultValueDescription: String {
guard let value = self else {
return "none"
}
return "\(value)"
}
}

extension RawRepresentable where Self: ExpressibleByArgument, RawValue: ExpressibleByArgument {
Expand Down Expand Up @@ -70,17 +83,3 @@ extension Float: ExpressibleByArgument {}
extension Double: ExpressibleByArgument {}

extension Bool: ExpressibleByArgument {}

extension ExpressibleByArgument {

public var defaultValueDescription: String {

let mirror = Mirror(reflecting: self)

if mirror.displayStyle == .optional, let value = mirror.children.first?.value {
return "\(value)"
}

return "\(self)"
}
}

0 comments on commit ab0f98f

Please sign in to comment.