mirror of
https://github.com/spf13/cobra
synced 2025-04-27 17:17:20 +00:00
Revert "flags: clarify documentation that LocalFlags related function do not modify the state"
This reverts commit 52e7b45312
.
This commit is contained in:
parent
52e7b45312
commit
637ba7bbc1
1 changed files with 27 additions and 21 deletions
48
command.go
48
command.go
|
@ -153,6 +153,10 @@ type Command struct {
|
|||
flags *flag.FlagSet
|
||||
// pflags contains persistent flags.
|
||||
pflags *flag.FlagSet
|
||||
// lflags contains local flags.
|
||||
lflags *flag.FlagSet
|
||||
// iflags contains inherited flags.
|
||||
iflags *flag.FlagSet
|
||||
// parentsPflags is all persistent flags of cmd's parents.
|
||||
parentsPflags *flag.FlagSet
|
||||
// globNormFunc is the global normalization function
|
||||
|
@ -1649,7 +1653,6 @@ func (c *Command) Flags() *flag.FlagSet {
|
|||
}
|
||||
|
||||
// LocalNonPersistentFlags are flags specific to this command which will NOT persist to subcommands.
|
||||
// This function does not modify the flags of the current command, it's purpose is to return the current state.
|
||||
func (c *Command) LocalNonPersistentFlags() *flag.FlagSet {
|
||||
persistentFlags := c.PersistentFlags()
|
||||
|
||||
|
@ -1663,57 +1666,58 @@ func (c *Command) LocalNonPersistentFlags() *flag.FlagSet {
|
|||
}
|
||||
|
||||
// LocalFlags returns the local FlagSet specifically set in the current command.
|
||||
// This function does not modify the flags of the current command, it's purpose is to return the current state.
|
||||
func (c *Command) LocalFlags() *flag.FlagSet {
|
||||
c.mergePersistentFlags()
|
||||
|
||||
lflags := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
|
||||
if c.flagErrorBuf == nil {
|
||||
c.flagErrorBuf = new(bytes.Buffer)
|
||||
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)
|
||||
}
|
||||
lflags.SetOutput(c.flagErrorBuf)
|
||||
lflags.SortFlags = c.Flags().SortFlags
|
||||
c.lflags.SortFlags = c.Flags().SortFlags
|
||||
if c.globNormFunc != nil {
|
||||
lflags.SetNormalizeFunc(c.globNormFunc)
|
||||
c.lflags.SetNormalizeFunc(c.globNormFunc)
|
||||
}
|
||||
|
||||
addToLocal := func(f *flag.Flag) {
|
||||
// Add the flag if it is not a parent PFlag, or it shadows a parent PFlag
|
||||
if lflags.Lookup(f.Name) == nil && f != c.parentsPflags.Lookup(f.Name) {
|
||||
lflags.AddFlag(f)
|
||||
if c.lflags.Lookup(f.Name) == nil && f != c.parentsPflags.Lookup(f.Name) {
|
||||
c.lflags.AddFlag(f)
|
||||
}
|
||||
}
|
||||
c.Flags().VisitAll(addToLocal)
|
||||
c.PersistentFlags().VisitAll(addToLocal)
|
||||
return lflags
|
||||
return c.lflags
|
||||
}
|
||||
|
||||
// InheritedFlags returns all flags which were inherited from parent commands.
|
||||
// This function does not modify the flags of the current command, it's purpose is to return the current state.
|
||||
func (c *Command) InheritedFlags() *flag.FlagSet {
|
||||
c.mergePersistentFlags()
|
||||
|
||||
iflags := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
|
||||
if c.flagErrorBuf == nil {
|
||||
c.flagErrorBuf = new(bytes.Buffer)
|
||||
if c.iflags == nil {
|
||||
c.iflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)
|
||||
if c.flagErrorBuf == nil {
|
||||
c.flagErrorBuf = new(bytes.Buffer)
|
||||
}
|
||||
c.iflags.SetOutput(c.flagErrorBuf)
|
||||
}
|
||||
iflags.SetOutput(c.flagErrorBuf)
|
||||
|
||||
local := c.LocalFlags()
|
||||
if c.globNormFunc != nil {
|
||||
iflags.SetNormalizeFunc(c.globNormFunc)
|
||||
c.iflags.SetNormalizeFunc(c.globNormFunc)
|
||||
}
|
||||
|
||||
c.parentsPflags.VisitAll(func(f *flag.Flag) {
|
||||
if iflags.Lookup(f.Name) == nil && local.Lookup(f.Name) == nil {
|
||||
iflags.AddFlag(f)
|
||||
if c.iflags.Lookup(f.Name) == nil && local.Lookup(f.Name) == nil {
|
||||
c.iflags.AddFlag(f)
|
||||
}
|
||||
})
|
||||
return iflags
|
||||
return c.iflags
|
||||
}
|
||||
|
||||
// NonInheritedFlags returns all flags which were not inherited from parent commands.
|
||||
// This function does not modify the flags of the current command, it's purpose is to return the current state.
|
||||
func (c *Command) NonInheritedFlags() *flag.FlagSet {
|
||||
return c.LocalFlags()
|
||||
}
|
||||
|
@ -1739,6 +1743,8 @@ func (c *Command) ResetFlags() {
|
|||
c.pflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)
|
||||
c.pflags.SetOutput(c.flagErrorBuf)
|
||||
|
||||
c.lflags = nil
|
||||
c.iflags = nil
|
||||
c.parentsPflags = nil
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue