Skip to content
Myr

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

1 file+62 added−11 removed

modified:command.go+62 added-11 removed
38 unmodified lines
39 // 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 lines
199{{ if .HasSubCommands}}200Available 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) }}207Additional 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 }}210Use "{{.Root.Name}} help [command]" for more information about that command.
472 unmodified lines
683func (c *Command) HasParent() bool {684 return c.parent != nil685}686685// Get the Commands FlagSet687// Get the complete FlagSet that applies to this command (local and persistent declared here and by all parents)688func (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 lines
696 }697 return c.flags698}699698// Get the Commands Persistent FlagSet700// Get the local FlagSet specifically set in the current command701func (c *Command) LocalFlags() *flag.FlagSet {702 c.mergePersistentFlags()703704 local := flag.NewFlagSet(c.Name(), flag.ContinueOnError)705 allPersistent := c.AllPersistentFlags()706707 c.Flags().VisitAll(func(f *flag.Flag) {708 if allPersistent.Lookup(f.Name) == nil {709 local.AddFlag(f)710 }711 })712713 return local714}715716// Get the Persistent FlagSet specifically set in the current command717func (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 lines
724 }725 return c.pflags726}727728// Get the Persistent FlagSet traversing the Command hierarchy729func (c *Command) AllPersistentFlags() *flag.FlagSet {730 allPersistent := flag.NewFlagSet(c.Name(), flag.ContinueOnError)731732 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 }745746 visit(c)747748 return allPersistent749}750751// For use in testing752func (c *Command) ResetFlags() {753 c.flagErrorBuf = new(bytes.Buffer)754 c.flagErrorBuf.Reset()
2 unmodified lines
757 c.pflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)758 c.pflags.SetOutput(c.flagErrorBuf)759}760720// Does the command contain flags (local not persistent)761// Does the command contain any flags (local plus persistent from the entire structure)762func (c *Command) HasFlags() bool {763 return c.Flags().HasFlags()764}765
1 unmodified line
767func (c *Command) HasPersistentFlags() bool {768 return c.PersistentFlags().HasFlags()769}770771// Does the command hierarchy contain persistent flags772func (c *Command) HasAnyPersistentFlags() bool {773 return c.AllPersistentFlags().HasFlags()774}775776// Does the command has flags specifically declared locally777func (c *Command) HasLocalFlags() bool {778 return c.LocalFlags().HasFlags()779}780781// Climbs up the command tree looking for matching flag782func (c *Command) Flag(name string) (flag *flag.Flag) {783 flag = c.Flags().Lookup(name)784
31 unmodified lines
816 //always return nil because upstream library is inconsistent & we always check the error buffer anyway817 return nil818}819820func (c *Command) Parent() *Command {821 return c.parent822}823824func (c *Command) mergePersistentFlags() {825 var rmerge func(x *Command)826827 rmerge = func(x *Command) {
10 unmodified lines
838 }839840 rmerge(c)841}787788func (c *Command) Parent() *Command {789 return c.parent790}