Skip to content

Commit 6e7906d

Browse files
authored
Merge pull request #1831 from dearchap/fix_ts
Fix timestamp get from cmd
2 parents 1f0e15c + e1c363f commit 6e7906d

File tree

4 files changed

+46
-22
lines changed

4 files changed

+46
-22
lines changed

command_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3254,6 +3254,44 @@ func TestCommand_Duration(t *testing.T) {
32543254
r.Equal(13*time.Second, cmd.Duration("top-flag"))
32553255
}
32563256

3257+
func TestCommand_Timestamp(t *testing.T) {
3258+
t1 := time.Time{}.Add(12 * time.Second)
3259+
t2 := time.Time{}.Add(13 * time.Second)
3260+
3261+
cmd := &Command{
3262+
Name: "hello",
3263+
Flags: []Flag{
3264+
&TimestampFlag{
3265+
Name: "myflag",
3266+
Value: t1,
3267+
},
3268+
},
3269+
Action: func(ctx context.Context, c *Command) error {
3270+
return nil
3271+
},
3272+
}
3273+
3274+
pCmd := &Command{
3275+
Flags: []Flag{
3276+
&TimestampFlag{
3277+
Name: "top-flag",
3278+
Value: t2,
3279+
},
3280+
},
3281+
Commands: []*Command{
3282+
cmd,
3283+
},
3284+
}
3285+
3286+
if err := pCmd.Run(context.Background(), []string{"foo", "hello"}); err != nil {
3287+
t.Error(err)
3288+
}
3289+
3290+
r := require.New(t)
3291+
r.Equal(t1, cmd.Timestamp("myflag"))
3292+
r.Equal(t2, cmd.Timestamp("top-flag"))
3293+
}
3294+
32573295
func TestCommand_String(t *testing.T) {
32583296
set := flag.NewFlagSet("test", 0)
32593297
set.String("myflag", "hello world", "doc")

flag_timestamp.go

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cli
22

33
import (
4-
"flag"
54
"fmt"
65
"time"
76
)
@@ -85,25 +84,12 @@ func (t *timestampValue) Get() any {
8584
}
8685

8786
// Timestamp gets the timestamp from a flag name
88-
func (cmd *Command) Timestamp(name string) *time.Time {
89-
if fs := cmd.lookupFlagSet(name); fs != nil {
90-
return lookupTimestamp(name, fs, cmd.Name)
87+
func (cmd *Command) Timestamp(name string) time.Time {
88+
if v, ok := cmd.Value(name).(time.Time); ok {
89+
tracef("time.Time available for flag name %[1]q with value=%[2]v (cmd=%[3]q)", name, v, cmd.Name)
90+
return v
9191
}
92-
return nil
93-
}
94-
95-
// Fetches the timestamp value from the local timestampWrap
96-
func lookupTimestamp(name string, set *flag.FlagSet, cmdName string) *time.Time {
97-
fl := set.Lookup(name)
98-
if fl != nil {
99-
if tv, ok := fl.Value.(*timestampValue); ok {
100-
v := tv.Value()
10192

102-
tracef("timestamp available for flag name %[1]q with value=%[2]v (cmd=%[3]q)", name, v, cmdName)
103-
return v
104-
}
105-
}
106-
107-
tracef("timestamp NOT available for flag name %[1]q (cmd=%[2]q)", name, cmdName)
108-
return nil
93+
tracef("time.Time NOT available for flag name %[1]q (cmd=%[2]q)", name, cmd.Name)
94+
return time.Time{}
10995
}

godoc-current.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ func (cmd *Command) StringSlice(name string) []string
456456
StringSlice looks up the value of a local StringSliceFlag, returns nil if
457457
not found
458458

459-
func (cmd *Command) Timestamp(name string) *time.Time
459+
func (cmd *Command) Timestamp(name string) time.Time
460460
Timestamp gets the timestamp from a flag name
461461

462462
func (cmd *Command) ToFishCompletion() (string, error)

testdata/godoc-v3.x.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ func (cmd *Command) StringSlice(name string) []string
456456
StringSlice looks up the value of a local StringSliceFlag, returns nil if
457457
not found
458458

459-
func (cmd *Command) Timestamp(name string) *time.Time
459+
func (cmd *Command) Timestamp(name string) time.Time
460460
Timestamp gets the timestamp from a flag name
461461

462462
func (cmd *Command) ToFishCompletion() (string, error)

0 commit comments

Comments
 (0)