From f2e41df3fb6b9b2619cb7223a1ea7ceeb26bf675 Mon Sep 17 00:00:00 2001 From: Seth Hoenig Date: Thu, 26 Nov 2026 06:29:29 -0600 Subject: [PATCH] flags: we were miscounting duration flags --- commands.go | 4 ++-- commands_test.go | 54 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/commands.go b/commands.go index d592f5c..981a619 100644 --- a/commands.go +++ b/commands.go @@ -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)} } diff --git a/commands_test.go b/commands_test.go index 995df00..aa79472 100644 --- a/commands_test.go +++ b/commands_test.go @@ -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 []", @@ -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()