add lnpflags field to Command

lnpflags stores localNonPersitentFlags
This commit is contained in:
Jun Nishimura 2023-07-23 15:12:57 +09:00
parent 99d0be84df
commit b51dcf624c

View file

@ -150,6 +150,8 @@ type Command struct {
pflags *flag.FlagSet
// lflags contains local flags.
lflags *flag.FlagSet
// lnpflags contains local non persistent flags
lnpflags *flag.FlagSet
// iflags contains inherited flags.
iflags *flag.FlagSet
// parentsPflags is all persistent flags of cmd's parents.
@ -1602,15 +1604,19 @@ func (c *Command) Flags() *flag.FlagSet {
// LocalNonPersistentFlags are flags specific to this command which will NOT persist to subcommands.
func (c *Command) LocalNonPersistentFlags() *flag.FlagSet {
if c.lnpflags == nil {
persistentFlags := c.PersistentFlags()
out := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
c.lnpflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)
c.LocalFlags().VisitAll(func(f *flag.Flag) {
if persistentFlags.Lookup(f.Name) == nil {
out.AddFlag(f)
f.Changed = false
c.lnpflags.AddFlag(f)
}
})
return out
}
return c.lnpflags
}
// LocalFlags returns the local FlagSet specifically set in the current command.