Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,8 +400,8 @@ func (c *Component) GetDuration(flag string) time.Duration {
}

func (c *Component) GetDurations(flag string) []time.Duration {
if n := c.vals.intCount(flag); n == 0 {
f := c.Flags.Get(flag)
if n := c.vals.durationCount(flag); n == 0 {
f := c.combine().Get(flag)
if f.Default != nil {
return []time.Duration{f.Default.Value.(time.Duration)}
}
Expand Down
54 changes: 54 additions & 0 deletions commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1340,6 +1340,30 @@ func TestComponent_GetDurations(t *testing.T) {
},
},
},
{
name: "repeated durations provided with default",
expText: "hello [1m0s 2m0s 3m0s]",
expCode: Success,
args: []string{"--ttl", "1m", "--ttl", "2m", "--ttl", "3m"},
root: &Component{
Flags: Flags{
{
Type: DurationFlag,
Long: "ttl",
Repeats: true,
Require: true,
Default: &Default{
Value: 9 * time.Minute,
},
},
},
Function: func(c *Component) Code {
ttls := c.GetDurations("ttl")
output = fmt.Sprintf("hello %v", ttls)
return Success
},
},
},
{
name: "repeated durations not provided no default not required",
expText: "hello []",
Expand Down Expand Up @@ -2094,6 +2118,36 @@ func TestGlobal_flag_duration(t *testing.T) {
must.Eq(t, "ttl is 2m0s", output)
}

func TestGlobal_flag_durations(t *testing.T) {
t.Parallel()

var output string

config := &Configuration{
Top: &Component{
Function: func(c *Component) Code {
output = fmt.Sprintf("ttls are %v", c.GetDurations("ttl"))
return Success
},
},
Globals: Flags{
{
Type: DurationFlag,
Long: "ttl",
Repeats: true,
Default: &Default{
Value: 2 * time.Minute,
},
},
},
}

c := New(config)
result := c.Run()
must.Zero(t, result)
must.Eq(t, "ttls are [2m0s]", output)
}

func TestArguments_top(t *testing.T) {
t.Parallel()

Expand Down