Restores the ability to fetch local only flags
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by fabianofranz · committed by spf13 · parent e1e66f7 · Browse files · 1 file·+62 added−11 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+62 added−11 removed
- other:+62 added−11 removed
Provenance
- Commit
efb045e- Tree
1b002d3- Author
fabianofranz <contact@fabianofranz.com>- Committer
spf13 <steve.francia@gmail.com>- Parent
e1e66f7
1 file changed+62 added−11 removed
modified:command.go+62 added-11 removed
38 unmodified lines39 // The short description shown in the 'help' output.40 Short string41 // The long message shown in the 'help <this-command>' output.42 Long string43- // Set of flags specific to this command.43+ // Full set of flags44 flags *flag.FlagSet45- // Set of flags children commands will inherit45+ // Set of flags childrens of this command will inherit46 pflags *flag.FlagSet47 // Run runs the command.48 // The args are the arguments after the command name.49 Run func(cmd *Command, args []string)149 unmodified lines199 {{ if .HasSubCommands}}200 Available Commands: {{range .Commands}}{{if .Runnable}}201 {{rpad .Use .UsagePadding }} {{.Short}}{{end}}{{end}}202 {{end}}203-{{ if .HasFlags}} Available Flags:204-{{.Flags.FlagUsages}}{{end}}{{if .HasParent}}{{if and (gt .Commands 0) (gt .Parent.Commands 1) }}203+{{ if .HasLocalFlags}}Flags:204+{{.LocalFlags.FlagUsages}}{{end}}205+{{ if .HasAnyPersistentFlags}}Global Flags:206+{{.AllPersistentFlags.FlagUsages}}{{end}}{{if .HasParent}}{{if and (gt .Commands 0) (gt .Parent.Commands 1) }}207 Additional help topics: {{if gt .Commands 0 }}{{range .Commands}}{{if not .Runnable}} {{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if gt .Parent.Commands 1 }}{{range .Parent.Commands}}{{if .Runnable}}{{if not (eq .Name $cmd.Name) }}{{end}}208 {{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{end}}209 {{end}}{{ if .HasSubCommands }}210 Use "{{.Root.Name}} help [command]" for more information about that command.472 unmodified lines683 func (c *Command) HasParent() bool {684 return c.parent != nil685 }686 685-// Get the Commands FlagSet687+// Get the complete FlagSet that applies to this command (local and persistent declared here and by all parents)688 func (c *Command) Flags() *flag.FlagSet {689 if c.flags == nil {690 c.flags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)691 if c.flagErrorBuf == nil {4 unmodified lines696 }697 return c.flags698 }699 698-// Get the Commands Persistent FlagSet700+// Get the local FlagSet specifically set in the current command701+func (c *Command) LocalFlags() *flag.FlagSet {702+ c.mergePersistentFlags()703+704+ local := flag.NewFlagSet(c.Name(), flag.ContinueOnError)705+ allPersistent := c.AllPersistentFlags()706+707+ c.Flags().VisitAll(func(f *flag.Flag) {708+ if allPersistent.Lookup(f.Name) == nil {709+ local.AddFlag(f)710+ }711+ })712+713+ return local714+}715+716+// Get the Persistent FlagSet specifically set in the current command717 func (c *Command) PersistentFlags() *flag.FlagSet {718 if c.pflags == nil {719 c.pflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)720 if c.flagErrorBuf == nil {3 unmodified lines724 }725 return c.pflags726 }727 728+// Get the Persistent FlagSet traversing the Command hierarchy729+func (c *Command) AllPersistentFlags() *flag.FlagSet {730+ allPersistent := flag.NewFlagSet(c.Name(), flag.ContinueOnError)731+732+ var visit func(x *Command)733+ visit = func(x *Command) {734+ if x.HasPersistentFlags() {735+ x.PersistentFlags().VisitAll(func(f *flag.Flag) {736+ if allPersistent.Lookup(f.Name) == nil {737+ allPersistent.AddFlag(f)738+ }739+ })740+ }741+ if x.HasParent() {742+ visit(x.parent)743+ }744+ }745+746+ visit(c)747+748+ return allPersistent749+}750+751 // For use in testing752 func (c *Command) ResetFlags() {753 c.flagErrorBuf = new(bytes.Buffer)754 c.flagErrorBuf.Reset()2 unmodified lines757 c.pflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)758 c.pflags.SetOutput(c.flagErrorBuf)759 }760 720-// Does the command contain flags (local not persistent)761+// Does the command contain any flags (local plus persistent from the entire structure)762 func (c *Command) HasFlags() bool {763 return c.Flags().HasFlags()764 }765 1 unmodified line767 func (c *Command) HasPersistentFlags() bool {768 return c.PersistentFlags().HasFlags()769 }770 771+// Does the command hierarchy contain persistent flags772+func (c *Command) HasAnyPersistentFlags() bool {773+ return c.AllPersistentFlags().HasFlags()774+}775+776+// Does the command has flags specifically declared locally777+func (c *Command) HasLocalFlags() bool {778+ return c.LocalFlags().HasFlags()779+}780+781 // Climbs up the command tree looking for matching flag782 func (c *Command) Flag(name string) (flag *flag.Flag) {783 flag = c.Flags().Lookup(name)784 31 unmodified lines816 //always return nil because upstream library is inconsistent & we always check the error buffer anyway817 return nil818 }819 820+func (c *Command) Parent() *Command {821+ return c.parent822+}823+824 func (c *Command) mergePersistentFlags() {825 var rmerge func(x *Command)826 827 rmerge = func(x *Command) {10 unmodified lines838 }839 840 rmerge(c)841 }787-788-func (c *Command) Parent() *Command {789- return c.parent790-}