diff --git a/cobra_test.go b/cobra_test.go index d5df951e..a1d32685 100644 --- a/cobra_test.go +++ b/cobra_test.go @@ -190,6 +190,7 @@ func flagInit() { cmdTimes.Flags().IntVarP(&flagi2, "inttwo", "j", 234, "help message for flag inttwo") cmdTimes.Flags().StringVarP(&flags2b, "strtwo", "t", "2", strtwoChildHelp) cmdTimes.PersistentFlags().StringVarP(&flags2b, "strtwo", "t", "2", strtwoChildHelp) + cmdTimes.LocalFlags() // populate lflags before parent is set cmdPrint.Flags().BoolVarP(&flagb3, "boolthree", "b", true, "help message for flag boolthree") cmdPrint.PersistentFlags().StringVarP(&flags3, "strthree", "s", "three", "help message for flag strthree") } @@ -210,8 +211,8 @@ func initialize() *Command { rootPersPre, echoPre, echoPersPre, timesPersPre = nil, nil, nil, nil var c = cmdRootNoRun - flagInit() commandInit() + flagInit() return c } @@ -219,8 +220,8 @@ func initializeWithSameName() *Command { tt, tp, te = nil, nil, nil rootPersPre, echoPre, echoPersPre, timesPersPre = nil, nil, nil, nil var c = cmdRootSameName - flagInit() commandInit() + flagInit() return c } @@ -910,6 +911,7 @@ func TestRootHelp(t *testing.T) { func TestFlagAccess(t *testing.T) { initialize() + cmdEcho.AddCommand(cmdTimes) local := cmdTimes.LocalFlags() inherited := cmdTimes.InheritedFlags() diff --git a/command.go b/command.go index 931c56c6..1f9bbd12 100644 --- a/command.go +++ b/command.go @@ -806,6 +806,7 @@ Simply type ` + c.Name() + ` help [path to command] for full details.`, // ResetCommands used for testing. func (c *Command) ResetCommands() { + c.parent = nil c.commands = nil c.helpCommand = nil c.parentsPflags = nil @@ -1192,6 +1193,10 @@ func (c *Command) ResetFlags() { c.flags.SetOutput(c.flagErrorBuf) c.pflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError) c.pflags.SetOutput(c.flagErrorBuf) + + c.lflags = nil + c.iflags = nil + c.parentsPflags = nil } // HasFlags checks if the command contains any flags (local plus persistent from the entire structure).