Skip to content
Open
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
10 changes: 7 additions & 3 deletions flag.go
Comment thread
tomasaschan marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ type NormalizedName string
type FlagSet struct {
// Usage is the function called when an error occurs while parsing flags.
// The field is a function (not a method) that may be changed to point to
// a custom error handler.
// a custom error handler. By default, it prints the default usage message.
Usage func()

// SortFlags is used to indicate, if user wants to have sorted flags in
Expand Down Expand Up @@ -1388,7 +1388,8 @@ func Parsed() bool {
var CommandLine = NewFlagSet(os.Args[0], ExitOnError)

// NewFlagSet returns a new, empty flag set with the specified name,
// error handling property and SortFlags set to true.
// error handling property, SortFlags set to true, and Usage set to
// print the default usage message.
func NewFlagSet(name string, errorHandling ErrorHandling) *FlagSet {
f := &FlagSet{
name: name,
Expand All @@ -1397,6 +1398,7 @@ func NewFlagSet(name string, errorHandling ErrorHandling) *FlagSet {
interspersed: true,
SortFlags: true,
}
f.Usage = func() { defaultUsage(f) }
return f
}

Expand All @@ -1407,9 +1409,11 @@ func (f *FlagSet) SetInterspersed(interspersed bool) {

// Init sets the name and error handling property for a flag set.
// By default, the zero FlagSet uses an empty name and the
// ContinueOnError error handling policy.
// ContinueOnError error handling policy. It also sets Usage to
// print the default usage message.
func (f *FlagSet) Init(name string, errorHandling ErrorHandling) {
f.name = name
f.errorHandling = errorHandling
f.argsLenAtDash = -1
f.Usage = func() { defaultUsage(f) }
}
37 changes: 37 additions & 0 deletions flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1626,3 +1626,40 @@ func TestVisitFlagOrder(t *testing.T) {
i++
})
}

func TestNewFlagSetDefaultUsage(t *testing.T) {
var buf bytes.Buffer
flags := NewFlagSet("test", ContinueOnError)
flags.SetOutput(&buf)
if flags.Usage == nil {
t.Fatal("expected flags.Usage to be defined, got nil")
}
flags.Bool("testflag", false, "a test flag")
flags.Usage()
got := buf.String()
if !strings.Contains(got, "Usage of test:\n") {
t.Errorf("expected usage output to contain 'Usage of test:\n', got %q", got)
}
if !strings.Contains(got, "--testflag") {
t.Errorf("expected usage output to contain '--testflag', got %q", got)
}
}

func TestFlagSetInitDefaultUsage(t *testing.T) {
var buf bytes.Buffer
var flags FlagSet
flags.Init("test-init", ContinueOnError)
flags.SetOutput(&buf)
if flags.Usage == nil {
t.Fatal("expected flags.Usage to be defined after Init, got nil")
}
flags.Bool("testflag", false, "a test flag")
flags.Usage()
got := buf.String()
if !strings.Contains(got, "Usage of test-init:\n") {
t.Errorf("expected usage output to contain 'Usage of test-init:\n', got %q", got)
}
if !strings.Contains(got, "--testflag") {
t.Errorf("expected usage output to contain '--testflag', got %q", got)
}
}
Loading