Make initHelpFlag public

Used for solving #424
This commit is contained in:
Albert Nigmatzianov 2017-04-27 10:55:15 +02:00 committed by Bjørn Erik Pedersen
parent db6b9a8b3f
commit fe69f2e3a3
2 changed files with 9 additions and 6 deletions

View file

@ -571,18 +571,19 @@ func (c *Command) execute(a []string) (err error) {
// initialize help flag as the last point possible to allow for user
// overriding
c.initHelpFlag()
c.InitDefaultHelpFlag()
err = c.ParseFlags(a)
if err != nil {
return c.FlagErrorFunc()(c, err)
}
// If help is called, regardless of other flags, return we want help
// If help is called, regardless of other flags, return we want help.
// Also say we need help if the command isn't runnable.
helpVal, err := c.Flags().GetBool("help")
if err != nil {
// should be impossible to get here as we always declare a help
// flag in initHelpFlag()
// flag in InitDefaultHelpFlag()
c.Println("\"help\" flag declared as non-bool. Please correct your code")
return err
}
@ -722,7 +723,9 @@ func (c *Command) ExecuteC() (cmd *Command, err error) {
return cmd, nil
}
func (c *Command) initHelpFlag() {
// InitDefaultHelpFlag adds default help flag to c.
// It is called automatically by executing the c or by calling help and usage.
func (c *Command) InitDefaultHelpFlag() {
c.mergePersistentFlags()
if c.Flags().Lookup("help") == nil {
usage := "help for "
@ -755,7 +758,7 @@ func (c *Command) initHelpCmd() {
c.Printf("Unknown help topic %#q\n", args)
c.Root().Usage()
} else {
cmd.initHelpFlag() // make possible 'help' flag to be shown
cmd.InitDefaultHelpFlag() // make possible 'help' flag to be shown
cmd.Help()
}
},

View file

@ -146,7 +146,7 @@ func TestInitHelpFlagMergesFlags(t *testing.T) {
cmd := Command{Use: "do"}
baseCmd.AddCommand(&cmd)
cmd.initHelpFlag()
cmd.InitDefaultHelpFlag()
actual := cmd.Flags().Lookup("help").Usage
if actual != usage {
t.Fatalf("Expected the help flag from the base command with usage '%s', but got the default with usage '%s'", usage, actual)