mirror of
https://github.com/spf13/cobra
synced 2024-11-24 22:57:12 +00:00
add lnpflags field to Command
lnpflags stores localNonPersitentFlags
This commit is contained in:
parent
99d0be84df
commit
b51dcf624c
1 changed files with 14 additions and 8 deletions
22
command.go
22
command.go
|
@ -150,6 +150,8 @@ type Command struct {
|
||||||
pflags *flag.FlagSet
|
pflags *flag.FlagSet
|
||||||
// lflags contains local flags.
|
// lflags contains local flags.
|
||||||
lflags *flag.FlagSet
|
lflags *flag.FlagSet
|
||||||
|
// lnpflags contains local non persistent flags
|
||||||
|
lnpflags *flag.FlagSet
|
||||||
// iflags contains inherited flags.
|
// iflags contains inherited flags.
|
||||||
iflags *flag.FlagSet
|
iflags *flag.FlagSet
|
||||||
// parentsPflags is all persistent flags of cmd's parents.
|
// 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.
|
// LocalNonPersistentFlags are flags specific to this command which will NOT persist to subcommands.
|
||||||
func (c *Command) LocalNonPersistentFlags() *flag.FlagSet {
|
func (c *Command) LocalNonPersistentFlags() *flag.FlagSet {
|
||||||
persistentFlags := c.PersistentFlags()
|
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) {
|
c.LocalFlags().VisitAll(func(f *flag.Flag) {
|
||||||
if persistentFlags.Lookup(f.Name) == nil {
|
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.
|
// LocalFlags returns the local FlagSet specifically set in the current command.
|
||||||
|
|
Loading…
Reference in a new issue