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
3 changes: 1 addition & 2 deletions duration.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,8 @@ func FlagVar(p *time.Duration, name string, value time.Duration, usage string) {

// FlagSet is like [FlagVar] but for the specified flag set. Replacement for [flag.FlagSet.DurationVar].
func FlagSetVar(fs *flag.FlagSet, p *time.Duration, name string, value time.Duration, usage string) {
d := Duration(value)
*p = value
Copy link

Copilot AI Sep 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix creates a potential issue where *p is set to the raw time.Duration value before registering the flag. This means the flag's String() method will return the standard Go duration format (e.g., '27h0m0s') instead of the human-readable format (e.g., '1d3h') for default values in help text. Consider creating a Duration instance first to ensure proper formatting: d := Duration(value); *p = time.Duration(d); fs.Var((*Duration)(p), name, usage)

Suggested change
*p = value
d := Duration(value)
*p = time.Duration(d)

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the fix actually - need value indeed to be set for default to work

fs.Var((*Duration)(p), name, usage)
*p = time.Duration(d)
}

// NextTime takes a partially parsed time.Time (without date) and returns the time in the future
Expand Down
32 changes: 32 additions & 0 deletions duration_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package duration_test

import (
"bytes"
"flag"
"fmt"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -185,3 +187,33 @@ func ExampleFlag() {
// After set: 1d1h
// After set (dereferenced): 25h0m0s
}

func TestHelpShowsDefault(t *testing.T) {
fs := flag.NewFlagSet("test", flag.ContinueOnError)
var buf bytes.Buffer
fs.SetOutput(&buf)
var d time.Duration
duration.FlagSetVar(fs, &d, "duration", 1*duration.Day+3*time.Hour, "example of duration flag with days support")
// print defaults
fs.PrintDefaults()
// check:
out := buf.String()
if !strings.Contains(out, "(default 1d3h)") {
t.Errorf("expected help to include default 1d3h, got:\n%s", out)
}
}

func TestHelpNotShowingDefaultZeroValue(t *testing.T) {
fs := flag.NewFlagSet("test", flag.ContinueOnError)
var buf bytes.Buffer
fs.SetOutput(&buf)
var d time.Duration
duration.FlagSetVar(fs, &d, "duration", 0, "example of duration flag with days support")
// print defaults
fs.PrintDefaults()
// check:
out := buf.String()
if strings.Contains(out, "(default") {
t.Errorf("expected help to not include default for zero value, got:\n%s", out)
}
}
Loading