// For use in testing func (c *Command) ResetFlags() { c.flagErrorBuf = new(bytes.Buffer) c.flagErrorBuf.Reset() c.flags = flag.NewFlagSet(c.Name(), flag.ContinueOnError) c.flags.SetOutput(c.flagErrorBuf) c.pflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError) c.pflags.SetOutput(c.flagErrorBuf) }
// All Flags which were inherited from parents commands func (c *Command) InheritedFlags() *flag.FlagSet { c.mergePersistentFlags() inherited := flag.NewFlagSet(c.Name(), flag.ContinueOnError) local := c.LocalFlags() var rmerge func(x *Command) rmerge = func(x *Command) { if x.HasPersistentFlags() { x.PersistentFlags().VisitAll(func(f *flag.Flag) { if inherited.Lookup(f.Name) == nil && local.Lookup(f.Name) == nil { inherited.AddFlag(f) } }) } if x.HasParent() { rmerge(x.parent) } } if c.HasParent() { rmerge(c.parent) } return inherited }
func TestBindPFlags(t *testing.T) { flagSet := pflag.NewFlagSet("test", pflag.ContinueOnError) var testValues = map[string]*string{ "host": nil, "port": nil, "endpoint": nil, } var mutatedTestValues = map[string]string{ "host": "localhost", "port": "6060", "endpoint": "/public", } for name, _ := range testValues { testValues[name] = flagSet.String(name, "", "test") } err := BindPFlags(flagSet) if err != nil { t.Fatalf("error binding flag set, %v", err) } flagSet.VisitAll(func(flag *pflag.Flag) { flag.Value.Set(mutatedTestValues[flag.Name]) flag.Changed = true }) for name, expected := range mutatedTestValues { assert.Equal(t, Get(name), expected) } }
// Get the Persistent FlagSet specifically set in the current command func (c *Command) PersistentFlags() *flag.FlagSet { if c.pflags == nil { c.pflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError) if c.flagErrorBuf == nil { c.flagErrorBuf = new(bytes.Buffer) } c.pflags.SetOutput(c.flagErrorBuf) } return c.pflags }
// Get the local FlagSet specifically set in the current command func (c *Command) LocalFlags() *flag.FlagSet { c.mergePersistentFlags() local := flag.NewFlagSet(c.Name(), flag.ContinueOnError) c.lflags.VisitAll(func(f *flag.Flag) { local.AddFlag(f) }) if !c.HasParent() { flag.CommandLine.VisitAll(func(f *flag.Flag) { if local.Lookup(f.Name) == nil { local.AddFlag(f) } }) } return local }
func (c *Command) mergePersistentFlags() { var rmerge func(x *Command) // Save the set of local flags if c.lflags == nil { c.lflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError) if c.flagErrorBuf == nil { c.flagErrorBuf = new(bytes.Buffer) } c.lflags.SetOutput(c.flagErrorBuf) addtolocal := func(f *flag.Flag) { c.lflags.AddFlag(f) } c.Flags().VisitAll(addtolocal) c.PersistentFlags().VisitAll(addtolocal) } rmerge = func(x *Command) { if !x.HasParent() { flag.CommandLine.VisitAll(func(f *flag.Flag) { if x.PersistentFlags().Lookup(f.Name) == nil { x.PersistentFlags().AddFlag(f) } }) } if x.HasPersistentFlags() { x.PersistentFlags().VisitAll(func(f *flag.Flag) { if c.Flags().Lookup(f.Name) == nil { c.Flags().AddFlag(f) } }) } if x.HasParent() { rmerge(x.parent) } } rmerge(c) }