parent
db6b9a8b3f
commit
fe69f2e3a3
13
command.go
13
command.go
@ -571,18 +571,19 @@ func (c *Command) execute(a []string) (err error) {
|
|||||||
|
|
||||||
// initialize help flag as the last point possible to allow for user
|
// initialize help flag as the last point possible to allow for user
|
||||||
// overriding
|
// overriding
|
||||||
c.initHelpFlag()
|
c.InitDefaultHelpFlag()
|
||||||
|
|
||||||
err = c.ParseFlags(a)
|
err = c.ParseFlags(a)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return c.FlagErrorFunc()(c, err)
|
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.
|
// Also say we need help if the command isn't runnable.
|
||||||
helpVal, err := c.Flags().GetBool("help")
|
helpVal, err := c.Flags().GetBool("help")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// should be impossible to get here as we always declare a help
|
// 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")
|
c.Println("\"help\" flag declared as non-bool. Please correct your code")
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -722,7 +723,9 @@ func (c *Command) ExecuteC() (cmd *Command, err error) {
|
|||||||
return cmd, nil
|
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()
|
c.mergePersistentFlags()
|
||||||
if c.Flags().Lookup("help") == nil {
|
if c.Flags().Lookup("help") == nil {
|
||||||
usage := "help for "
|
usage := "help for "
|
||||||
@ -755,7 +758,7 @@ func (c *Command) initHelpCmd() {
|
|||||||
c.Printf("Unknown help topic %#q\n", args)
|
c.Printf("Unknown help topic %#q\n", args)
|
||||||
c.Root().Usage()
|
c.Root().Usage()
|
||||||
} else {
|
} else {
|
||||||
cmd.initHelpFlag() // make possible 'help' flag to be shown
|
cmd.InitDefaultHelpFlag() // make possible 'help' flag to be shown
|
||||||
cmd.Help()
|
cmd.Help()
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -146,7 +146,7 @@ func TestInitHelpFlagMergesFlags(t *testing.T) {
|
|||||||
cmd := Command{Use: "do"}
|
cmd := Command{Use: "do"}
|
||||||
baseCmd.AddCommand(&cmd)
|
baseCmd.AddCommand(&cmd)
|
||||||
|
|
||||||
cmd.initHelpFlag()
|
cmd.InitDefaultHelpFlag()
|
||||||
actual := cmd.Flags().Lookup("help").Usage
|
actual := cmd.Flags().Lookup("help").Usage
|
||||||
if actual != 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)
|
t.Fatalf("Expected the help flag from the base command with usage '%s', but got the default with usage '%s'", usage, actual)
|
||||||
|
Loading…
Reference in New Issue
Block a user