aboutsummaryrefslogtreecommitdiff
path: root/src/flag
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2016-10-20 15:05:51 -0400
committerRuss Cox <rsc@golang.org>2016-10-26 16:13:40 +0000
commit49543695c56486d6a941e55520b37a9d098d3379 (patch)
tree171c24be185df7c89d8da3adfb32e41c62cea910 /src/flag
parentf9027d61ab48154e4cb29c50e356a3f462840e01 (diff)
downloadgo-49543695c56486d6a941e55520b37a9d098d3379.tar.gz
go-49543695c56486d6a941e55520b37a9d098d3379.zip
flag: arrange for FlagSet.Usage to be non-nil by default
This allows callers to invoke f.Usage() themselves and get the default usage handler instead of a panic (from calling a nil function). Fixes #16955. Change-Id: Ie337fd9e1f85daf78c5eae7b5c41d5ad8c1f89bf Reviewed-on: https://go-review.googlesource.com/31576 Run-TryBot: Russ Cox <rsc@golang.org> Reviewed-by: Rob Pike <r@golang.org>
Diffstat (limited to 'src/flag')
-rw-r--r--src/flag/export_test.go1
-rw-r--r--src/flag/flag.go21
2 files changed, 16 insertions, 6 deletions
diff --git a/src/flag/export_test.go b/src/flag/export_test.go
index 12d3dc76df..edbe83c664 100644
--- a/src/flag/export_test.go
+++ b/src/flag/export_test.go
@@ -13,5 +13,6 @@ import "os"
// exit the program.
func ResetForTesting(usage func()) {
CommandLine = NewFlagSet(os.Args[0], ContinueOnError)
+ CommandLine.Usage = commandLineUsage
Usage = usage
}
diff --git a/src/flag/flag.go b/src/flag/flag.go
index cb9f2837bf..bbbc55a279 100644
--- a/src/flag/flag.go
+++ b/src/flag/flag.go
@@ -502,7 +502,7 @@ func PrintDefaults() {
}
// defaultUsage is the default function to print a usage message.
-func defaultUsage(f *FlagSet) {
+func (f *FlagSet) defaultUsage() {
if f.name == "" {
fmt.Fprintf(f.out(), "Usage:\n")
} else {
@@ -821,11 +821,7 @@ func (f *FlagSet) failf(format string, a ...interface{}) error {
// or the appropriate default usage function otherwise.
func (f *FlagSet) usage() {
if f.Usage == nil {
- if f == CommandLine {
- Usage()
- } else {
- defaultUsage(f)
- }
+ f.defaultUsage()
} else {
f.Usage()
}
@@ -955,6 +951,18 @@ func Parsed() bool {
// methods of CommandLine.
var CommandLine = NewFlagSet(os.Args[0], ExitOnError)
+func init() {
+ // Override generic FlagSet default Usage with call to global Usage.
+ // Note: This is not CommandLine.Usage = Usage,
+ // because we want any eventual call to use any updated value of Usage,
+ // not the value it has when this line is run.
+ CommandLine.Usage = commandLineUsage
+}
+
+func commandLineUsage() {
+ Usage()
+}
+
// NewFlagSet returns a new, empty flag set with the specified name and
// error handling property.
func NewFlagSet(name string, errorHandling ErrorHandling) *FlagSet {
@@ -962,6 +970,7 @@ func NewFlagSet(name string, errorHandling ErrorHandling) *FlagSet {
name: name,
errorHandling: errorHandling,
}
+ f.Usage = f.defaultUsage
return f
}