diff --git a/flag_test.go b/flag_test.go index 7dbfbdc..ced5514 100644 --- a/flag_test.go +++ b/flag_test.go @@ -1836,6 +1836,17 @@ func TestTimestampFlagApply(t *testing.T) { expect(t, *fl.Value.timestamp, expectedResult) } +func TestTimestampFlagApplyValue(t *testing.T) { + expectedResult, _ := time.Parse(time.RFC3339, "2006-01-02T15:04:05Z") + fl := TimestampFlag{Name: "time", Aliases: []string{"t"}, Layout: time.RFC3339, Value: NewTimestamp(expectedResult)} + set := flag.NewFlagSet("test", 0) + _ = fl.Apply(set) + + err := set.Parse([]string{""}) + expect(t, err, nil) + expect(t, *fl.Value.timestamp, expectedResult) +} + func TestTimestampFlagApply_Fail_Parse_Wrong_Layout(t *testing.T) { fl := TimestampFlag{Name: "time", Aliases: []string{"t"}, Layout: "randomlayout"} set := flag.NewFlagSet("test", 0) diff --git a/flag_timestamp.go b/flag_timestamp.go index 9fac1d1..0382a6b 100644 --- a/flag_timestamp.go +++ b/flag_timestamp.go @@ -118,7 +118,9 @@ func (f *TimestampFlag) Apply(set *flag.FlagSet) error { if f.Layout == "" { return fmt.Errorf("timestamp Layout is required") } - f.Value = &Timestamp{} + if f.Value == nil { + f.Value = &Timestamp{} + } f.Value.SetLayout(f.Layout) if val, ok := flagFromEnvOrFile(f.EnvVars, f.FilePath); ok {