Skip to content

Commit d981dfb

Browse files
author
Chloe Kudryavtsev
committed
improve compatibility with go/flag
go/flag in go 1.13 has a public Output() function and a Name() function - change out() to Output() (adjusting the rest of the file) - add Name() and tests for it From: @mckern (spf13#220) re: out/Output: This brings behavior inline with go's flag library, and allows for printing output directly to whatever the current FlagSet is using for output. This change will make it easier to correctly emit output to stdout or stderr (e.g. a user has requested a help screen, which should emit to stdout since it's the desired outcome).
1 parent 2e9d26c commit d981dfb

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

flag.go

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ type FlagSet struct {
160160
args []string // arguments after flags
161161
argsLenAtDash int // len(args) when a '--' was located when parsing, or -1 if no --
162162
errorHandling ErrorHandling
163-
output io.Writer // nil means stderr; use out() accessor
163+
output io.Writer // nil means stderr; use Output() accessor
164164
interspersed bool // allow interspersed option/non-option args
165165
normalizeNameFunc func(f *FlagSet, name string) NormalizedName
166166

@@ -255,13 +255,20 @@ func (f *FlagSet) normalizeFlagName(name string) NormalizedName {
255255
return n(f, name)
256256
}
257257

258-
func (f *FlagSet) out() io.Writer {
258+
// Output returns the destination for usage and error messages. os.Stderr is returned if
259+
// output was not set or was set to nil.
260+
func (f *FlagSet) Output() io.Writer {
259261
if f.output == nil {
260262
return os.Stderr
261263
}
262264
return f.output
263265
}
264266

267+
// Name returns the name of the flag set.
268+
func (f *FlagSet) Name() string {
269+
return f.name
270+
}
271+
265272
// SetOutput sets the destination for usage and error messages.
266273
// If output is nil, os.Stderr is used.
267274
func (f *FlagSet) SetOutput(output io.Writer) {
@@ -358,7 +365,7 @@ func (f *FlagSet) ShorthandLookup(name string) *Flag {
358365
}
359366
if len(name) > 1 {
360367
msg := fmt.Sprintf("can not look up shorthand which is more than one ASCII character: %q", name)
361-
fmt.Fprintf(f.out(), msg)
368+
fmt.Fprintf(f.Output(), msg)
362369
panic(msg)
363370
}
364371
c := name[0]
@@ -482,7 +489,7 @@ func (f *FlagSet) Set(name, value string) error {
482489
}
483490

484491
if flag.Deprecated != "" {
485-
fmt.Fprintf(f.out(), "Flag --%s has been deprecated, %s\n", flag.Name, flag.Deprecated)
492+
fmt.Fprintf(f.Output(), "Flag --%s has been deprecated, %s\n", flag.Name, flag.Deprecated)
486493
}
487494
return nil
488495
}
@@ -523,7 +530,7 @@ func Set(name, value string) error {
523530
// otherwise, the default values of all defined flags in the set.
524531
func (f *FlagSet) PrintDefaults() {
525532
usages := f.FlagUsages()
526-
fmt.Fprint(f.out(), usages)
533+
fmt.Fprint(f.Output(), usages)
527534
}
528535

529536
// defaultIsZeroValue returns true if the default value for this flag represents
@@ -758,7 +765,7 @@ func PrintDefaults() {
758765

759766
// defaultUsage is the default function to print a usage message.
760767
func defaultUsage(f *FlagSet) {
761-
fmt.Fprintf(f.out(), "Usage of %s:\n", f.name)
768+
fmt.Fprintf(f.Output(), "Usage of %s:\n", f.name)
762769
f.PrintDefaults()
763770
}
764771

@@ -844,7 +851,7 @@ func (f *FlagSet) AddFlag(flag *Flag) {
844851
_, alreadyThere := f.formal[normalizedFlagName]
845852
if alreadyThere {
846853
msg := fmt.Sprintf("%s flag redefined: %s", f.name, flag.Name)
847-
fmt.Fprintln(f.out(), msg)
854+
fmt.Fprintln(f.Output(), msg)
848855
panic(msg) // Happens only if flags are declared with identical names
849856
}
850857
if f.formal == nil {
@@ -860,7 +867,7 @@ func (f *FlagSet) AddFlag(flag *Flag) {
860867
}
861868
if len(flag.Shorthand) > 1 {
862869
msg := fmt.Sprintf("%q shorthand is more than one ASCII character", flag.Shorthand)
863-
fmt.Fprintf(f.out(), msg)
870+
fmt.Fprintf(f.Output(), msg)
864871
panic(msg)
865872
}
866873
if f.shorthands == nil {
@@ -870,7 +877,7 @@ func (f *FlagSet) AddFlag(flag *Flag) {
870877
used, alreadyThere := f.shorthands[c]
871878
if alreadyThere {
872879
msg := fmt.Sprintf("unable to redefine %q shorthand in %q flagset: it's already used for %q flag", c, f.name, used.Name)
873-
fmt.Fprintf(f.out(), msg)
880+
fmt.Fprintf(f.Output(), msg)
874881
panic(msg)
875882
}
876883
f.shorthands[c] = flag
@@ -909,7 +916,7 @@ func VarP(value Value, name, shorthand, usage string) {
909916
func (f *FlagSet) failf(format string, a ...interface{}) error {
910917
err := fmt.Errorf(format, a...)
911918
if f.errorHandling != ContinueOnError {
912-
fmt.Fprintln(f.out(), err)
919+
fmt.Fprintln(f.Output(), err)
913920
f.usage()
914921
}
915922
return err
@@ -1060,7 +1067,7 @@ func (f *FlagSet) parseSingleShortArg(shorthands string, args []string, fn parse
10601067
}
10611068

10621069
if flag.ShorthandDeprecated != "" {
1063-
fmt.Fprintf(f.out(), "Flag shorthand -%s has been deprecated, %s\n", flag.Shorthand, flag.ShorthandDeprecated)
1070+
fmt.Fprintf(f.Output(), "Flag shorthand -%s has been deprecated, %s\n", flag.Shorthand, flag.ShorthandDeprecated)
10641071
}
10651072

10661073
err = fn(flag, value)

flag_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,13 @@ func TestAddFlagSet(t *testing.T) {
115115
oldSet := NewFlagSet("old", ContinueOnError)
116116
newSet := NewFlagSet("new", ContinueOnError)
117117

118+
if oldSet.Name() != "old" {
119+
t.Errorf("When reading a flagset's name, expected %s, but found %s", "old", oldSet.Name())
120+
}
121+
if newSet.Name() != "new" {
122+
t.Errorf("When reading a flagset's name, expected %s, but found %s", "new", newSet.Name())
123+
}
124+
118125
oldSet.String("flag1", "flag1", "flag1")
119126
oldSet.String("flag2", "flag2", "flag2")
120127

0 commit comments

Comments
 (0)