Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save griffin-stewie/af75966f26a993938abc85fa099fbe25 to your computer and use it in GitHub Desktop.
Save griffin-stewie/af75966f26a993938abc85fa099fbe25 to your computer and use it in GitHub Desktop.
ArgumentParser で Date を扱うときの例
// 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