Go has a few predefined types in its flag package. How would I pass a date?
I have mimicked the URL parsing example but it looks a bit verbose and I am missing smth.
package main
import (
"flag"
"fmt"
"time"
)
const dateLayout = "2006-01-02"
type DateValue struct {
Date *time.Time
}
func (v DateValue) String() string {
if v.Date != nil {
return v.Date.Format(dateLayout)
}
return ""
}
func (v DateValue) Set(str string) error {
if t, err := time.Parse(dateLayout, str); err != nil {
return err
} else {
*v.Date = t
}
return nil
}
func main() {
var d = &time.Time{}
flag.Var(&DateValue{d}, "date", "Date part of ISO")
flag.Parse([]string{"--date", "2022-08-08"})
fmt.Printf(`{date: %q}`, d)
}
Alas, this fails with an error. You can see it at the playground
./prog.go:35:13: too many arguments in call to flag.Parse
have ([]string)
want ()