From 52e7b45312f82ec91ad00508f9b0d1be856f3809 Mon Sep 17 00:00:00 2001 From: Dmytro Milinevskyi Date: Sat, 4 Nov 2023 00:51:27 +0100 Subject: [PATCH 1/3] flags: clarify documentation that LocalFlags related function do not modify the state --- command.go | 48 +++++++++++++++++++++--------------------------- 1 file changed, 21 insertions(+), 27 deletions(-) diff --git a/command.go b/command.go index 2fbe6c131..ba34628fb 100644 --- a/command.go +++ b/command.go @@ -153,10 +153,6 @@ 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 @@ -1653,6 +1649,7 @@ 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() @@ -1666,58 +1663,57 @@ 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() - 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 := flag.NewFlagSet(c.Name(), flag.ContinueOnError) + if c.flagErrorBuf == nil { + c.flagErrorBuf = new(bytes.Buffer) } - c.lflags.SortFlags = c.Flags().SortFlags + lflags.SetOutput(c.flagErrorBuf) + lflags.SortFlags = c.Flags().SortFlags if c.globNormFunc != nil { - c.lflags.SetNormalizeFunc(c.globNormFunc) + 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 c.lflags.Lookup(f.Name) == nil && f != c.parentsPflags.Lookup(f.Name) { - c.lflags.AddFlag(f) + if lflags.Lookup(f.Name) == nil && f != c.parentsPflags.Lookup(f.Name) { + lflags.AddFlag(f) } } c.Flags().VisitAll(addToLocal) c.PersistentFlags().VisitAll(addToLocal) - return c.lflags + return 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() - 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 := flag.NewFlagSet(c.Name(), flag.ContinueOnError) + if c.flagErrorBuf == nil { + c.flagErrorBuf = new(bytes.Buffer) } + iflags.SetOutput(c.flagErrorBuf) local := c.LocalFlags() if c.globNormFunc != nil { - c.iflags.SetNormalizeFunc(c.globNormFunc) + iflags.SetNormalizeFunc(c.globNormFunc) } c.parentsPflags.VisitAll(func(f *flag.Flag) { - if c.iflags.Lookup(f.Name) == nil && local.Lookup(f.Name) == nil { - c.iflags.AddFlag(f) + if iflags.Lookup(f.Name) == nil && local.Lookup(f.Name) == nil { + iflags.AddFlag(f) } }) - return c.iflags + return 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() } @@ -1743,8 +1739,6 @@ 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 } From 637ba7bbc1b7b22a9118597d39c3896851a93d7f Mon Sep 17 00:00:00 2001 From: Dmytro Milinevskyi Date: Sat, 6 Jan 2024 18:06:53 +0100 Subject: [PATCH 2/3] Revert "flags: clarify documentation that LocalFlags related function do not modify the state" This reverts commit 52e7b45312f82ec91ad00508f9b0d1be856f3809. --- command.go | 48 +++++++++++++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/command.go b/command.go index ba34628fb..2fbe6c131 100644 --- a/command.go +++ b/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 } From 51056406ff87c2a58d3da37799676b10848bdee1 Mon Sep 17 00:00:00 2001 From: Dmytro Milinevskyi Date: Sat, 6 Jan 2024 18:09:15 +0100 Subject: [PATCH 3/3] flags: clarify documentation that LocalFlags related function do not modify the state --- command.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/command.go b/command.go index 2fbe6c131..7ea89740a 100644 --- a/command.go +++ b/command.go @@ -154,8 +154,10 @@ type Command struct { // pflags contains persistent flags. pflags *flag.FlagSet // lflags contains local flags. + // This field does not represent internal state, it's used as a cache to optimise LocalFlags function call lflags *flag.FlagSet // iflags contains inherited flags. + // This field does not represent internal state, it's used as a cache to optimise InheritedFlags function call iflags *flag.FlagSet // parentsPflags is all persistent flags of cmd's parents. parentsPflags *flag.FlagSet @@ -1653,6 +1655,7 @@ 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() @@ -1666,6 +1669,7 @@ 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() @@ -1693,6 +1697,7 @@ func (c *Command) LocalFlags() *flag.FlagSet { } // 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() @@ -1718,6 +1723,7 @@ func (c *Command) InheritedFlags() *flag.FlagSet { } // 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() }