From 1ce0cc6db4029d97571db82f85092fccedb572ce Mon Sep 17 00:00:00 2001 From: childe Date: Tue, 3 Apr 2018 19:55:18 +0800 Subject: make x.Parsed() return true after AddGoFlagSet(x) and pflag.Parse() (#162) * make GoFlagSets.Parsed() true after pflag.Parse some third part lib such as glog use go flag package, and its some actions depends on if goflag.Parsed(). * add test case for goflag.CommandLine.Parsed() * add comment to goflag.CommandLine.Parsed test case --- flag.go | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'flag.go') diff --git a/flag.go b/flag.go index f0acbb8..e4e0c94 100644 --- a/flag.go +++ b/flag.go @@ -101,6 +101,7 @@ package pflag import ( "bytes" "errors" + goflag "flag" "fmt" "io" "os" @@ -162,6 +163,8 @@ type FlagSet struct { output io.Writer // nil means stderr; use out() accessor interspersed bool // allow interspersed option/non-option args normalizeNameFunc func(f *FlagSet, name string) NormalizedName + + addedGoFlagSets []*goflag.FlagSet } // A Flag represents the state of a flag. @@ -1098,6 +1101,11 @@ func (f *FlagSet) parseArgs(args []string, fn parseFunc) (err error) { // are defined and before flags are accessed by the program. // The return value will be ErrHelp if -help was set but not defined. func (f *FlagSet) Parse(arguments []string) error { + if f.addedGoFlagSets != nil { + for _, goFlagSet := range f.addedGoFlagSets { + goFlagSet.Parse(nil) + } + } f.parsed = true if len(arguments) < 0 { -- cgit v1.2.3