Created
April 7, 2020 13:35
-
-
Save griffin-stewie/af75966f26a993938abc85fa099fbe25 to your computer and use it in GitHub Desktop.
ArgumentParser で Date を扱うときの例
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://forums.swift.org/t/support-for-date/34797/2?u=griffin-stewie | |
func parseDate(_ formatter: DateFormatter) -> (String) throws -> Date { | |
{ arg in | |
guard let date = formatter.date(from: arg) else { | |
throw ValidationError("Invalid date") | |
} | |
return date | |
} | |
} | |
let shortFormatter = DateFormatter() | |
shortFormatter.dateStyle = .short | |
// .....later | |
@Argument(transform: parseDate(shortFormatter)) | |
var date: Date |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment