Skip to content
Myr

Make initHelpFlag public

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

Used for solving #424

authored by Albert Nigmatzianov · committed by Bjørn Erik Pedersen · parent db6b9a8 · Browse files · 2 files+9 added−6 removed
This commit

State

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

Diff stats

2 files+9 added−6 removed

  • other:+9 added−6 removed

2 files+9 added−6 removed

modified:command_test.go+1 added-1 removed
144 unmodified lines
145 baseCmd.PersistentFlags().Bool("help", false, usage)146 cmd := Command{Use: "do"}147 baseCmd.AddCommand(&cmd)148149 cmd.initHelpFlag()149 cmd.InitDefaultHelpFlag()150 actual := cmd.Flags().Lookup("help").Usage151 if actual != usage {152 t.Fatalf("Expected the help flag from the base command with usage '%s', but got the default with usage '%s'", usage, actual)153 }
131 unmodified lines
modified:command.go+8 added-5 removed
569 unmodified lines
570 }571572 // initialize help flag as the last point possible to allow for user573 // overriding574 c.initHelpFlag()574 c.InitDefaultHelpFlag()575576 err = c.ParseFlags(a)577 if err != nil {578 return c.FlagErrorFunc()(c, err)579 }580580 // If help is called, regardless of other flags, return we want help581 // If help is called, regardless of other flags, return we want help.582 // Also say we need help if the command isn't runnable.583 helpVal, err := c.Flags().GetBool("help")584 if err != nil {585 // should be impossible to get here as we always declare a help585 // flag in initHelpFlag()586 // flag in InitDefaultHelpFlag()587 c.Println("\"help\" flag declared as non-bool. Please correct your code")588 return err589 }590
131 unmodified lines
722 }723 return cmd, nil724}725726// InitDefaultHelpFlag adds default help flag to c.727// It is called automatically by executing the c or by calling help and usage.725func (c *Command) initHelpFlag() {728func (c *Command) InitDefaultHelpFlag() {729 c.mergePersistentFlags()730 if c.Flags().Lookup("help") == nil {731 usage := "help for "732 if c.Name() == "" {
24 unmodified lines
757 if cmd == nil || e != nil {758 c.Printf("Unknown help topic %#q\n", args)759 c.Root().Usage()760 } else {758 cmd.initHelpFlag() // make possible 'help' flag to be shown761 cmd.InitDefaultHelpFlag() // make possible 'help' flag to be shown762 cmd.Help()763 }764 },765 }
499 unmodified lines