mirror of
https://github.com/spf13/cobra
synced 2024-11-24 22:57:12 +00:00
new InheritedFlags() command to tell all flags which persisted from a parent
This commit is contained in:
parent
07a9dc0024
commit
5049ee8606
1 changed files with 28 additions and 0 deletions
28
command.go
28
command.go
|
@ -713,6 +713,34 @@ func (c *Command) LocalFlags() *flag.FlagSet {
|
||||||
return local
|
return local
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// All Flags which were inherited from parents commands
|
||||||
|
func (c *Command) InheritedFlags() *flag.FlagSet {
|
||||||
|
c.mergePersistentFlags()
|
||||||
|
|
||||||
|
local := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
|
||||||
|
|
||||||
|
var rmerge func(x *Command)
|
||||||
|
|
||||||
|
rmerge = func(x *Command) {
|
||||||
|
if x.HasPersistentFlags() {
|
||||||
|
x.PersistentFlags().VisitAll(func(f *flag.Flag) {
|
||||||
|
if local.Lookup(f.Name) == nil {
|
||||||
|
local.AddFlag(f)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
if x.HasParent() {
|
||||||
|
rmerge(x.parent)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if c.HasParent() {
|
||||||
|
rmerge(c.parent)
|
||||||
|
}
|
||||||
|
|
||||||
|
return local
|
||||||
|
}
|
||||||
|
|
||||||
// Get the Persistent FlagSet specifically set in the current command
|
// Get the Persistent FlagSet specifically set in the current command
|
||||||
func (c *Command) PersistentFlags() *flag.FlagSet {
|
func (c *Command) PersistentFlags() *flag.FlagSet {
|
||||||
if c.pflags == nil {
|
if c.pflags == nil {
|
||||||
|
|
Loading…
Reference in a new issue