Skip to content
Myr

Merge pull request #136 from eparis/late-init-help-flag-fix

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Fix the late init help flag

authored by Eric Paris · committed by Eric Paris · parents 4c4f2d9, ae28810 · Browse files · 1 file+18 added−5 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+18 added−5 removed

  • other:+18 added−5 removed

1 file+18 added−5 removed

modified:command.go+18 added-5 removed
90 unmodified lines
91 usageTemplate string // Can be defined by Application92 helpTemplate string // Can be defined by Application93 helpFunc func(*Command, []string) // Help can be defined by application94 helpCommand *Command // The help command95 helpFlagVal bool95 // The global normalization function that we can use on every pFlag set and children commands96 globNormFunc func(f *flag.FlagSet, name string) flag.NormalizedName97}98
349 unmodified lines
448 if len(c.Deprecated) > 0 {449 c.Printf("Command %q is deprecated, %s\n", c.Name(), c.Deprecated)450 }451452 // initialize help flag as the last point possible to allow for user453 // overriding454 c.initHelpFlag()455456 err = c.ParseFlags(a)457 if err != nil {458 return err459 }460 // If help is called, regardless of other flags, return we want help461 // Also say we need help if c.Run is nil.462 helpVal, err := c.Flags().GetBool("help")463 if err != nil {464 // should be impossible to get here as we always declare a help465 // flag in initHelpFlag()466 c.Println("\"help\" flag declared as non-bool. Please correct your code")467 return err468 }459 if c.helpFlagVal || !c.Runnable() {469 if helpVal || !c.Runnable() {470 return flag.ErrHelp471 }472473 c.preRun()
61 unmodified lines
535 }536537 // initialize help as the last point possible to allow for user538 // overriding529 c.initHelp()539 c.initHelpCmd()540541 var args []string542543 if len(c.args) == 0 {
25 unmodified lines
569570 return571}572563func (c *Command) initHelp() {573func (c *Command) initHelpFlag() {574 if c.Flags().Lookup("help") == nil {565 c.Flags().BoolVarP(&c.helpFlagVal, "help", "h", false, "help for "+c.Name())575 c.Flags().BoolP("help", "h", false, "help for "+c.Name())576 }577}578579func (c *Command) initHelpCmd() {580 if c.helpCommand == nil {581 if !c.HasSubCommands() {582 return583 }
463 unmodified lines