diff --git a/flag_test.go b/flag_test.go index cda34be..11b3265 100644 --- a/flag_test.go +++ b/flag_test.go @@ -2264,6 +2264,15 @@ func TestTimestampFlagApply_Fail_Parse_Wrong_Time(t *testing.T) { expect(t, err, fmt.Errorf("invalid value \"2006-01-02T15:04:05Z\" for flag -time: parsing time \"2006-01-02T15:04:05Z\" as \"Jan 2, 2006 at 3:04pm (MST)\": cannot parse \"2006-01-02T15:04:05Z\" as \"Jan\"")) } +func TestTimestampFlagValueFromContext(t *testing.T) { + set := flag.NewFlagSet("test", 0) + now := time.Now() + set.Var(NewTimestamp(now), "myflag", "doc") + ctx := NewContext(nil, set, nil) + f := &TimestampFlag{Name: "myflag"} + expect(t, f.ValueFromContext(ctx), &now) +} + type flagDefaultTestCase struct { name string flag Flag diff --git a/flag_timestamp.go b/flag_timestamp.go index ed06418..a3b230e 100644 --- a/flag_timestamp.go +++ b/flag_timestamp.go @@ -164,6 +164,11 @@ func (f *TimestampFlag) Apply(set *flag.FlagSet) error { return nil } +// ValueFromContext returns the flag’s value in the given Context. +func (f *TimestampFlag) ValueFromContext(ctx *Context) *time.Time { + return ctx.Timestamp(f.Name) +} + // Timestamp gets the timestamp from a flag name func (c *Context) Timestamp(name string) *time.Time { if fs := c.lookupFlagSet(name); fs != nil {