mirror of https://github.com/spf13/cobra.git
Merge persistent flags before checking for a help flag.
Signed-off-by: Daniel Nephin <dnephin@gmail.com>
This commit is contained in:
parent
bc81c21bd0
commit
7faa7fcdd2
|
@ -684,6 +684,7 @@ func (c *Command) ExecuteC() (cmd *Command, err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Command) initHelpFlag() {
|
func (c *Command) initHelpFlag() {
|
||||||
|
c.mergePersistentFlags()
|
||||||
if c.Flags().Lookup("help") == nil {
|
if c.Flags().Lookup("help") == nil {
|
||||||
c.Flags().BoolP("help", "h", false, "help for "+c.Name())
|
c.Flags().BoolP("help", "h", false, "help for "+c.Name())
|
||||||
}
|
}
|
||||||
|
|
|
@ -134,6 +134,21 @@ func Test_DisableFlagParsing(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestInitHelpFlagMergesFlags(t *testing.T) {
|
||||||
|
usage := "custom flag"
|
||||||
|
baseCmd := Command{Use: "testcmd"}
|
||||||
|
baseCmd.PersistentFlags().Bool("help", false, usage)
|
||||||
|
cmd := Command{Use: "do"}
|
||||||
|
baseCmd.AddCommand(&cmd)
|
||||||
|
|
||||||
|
cmd.initHelpFlag()
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestCommandsAreSorted(t *testing.T) {
|
func TestCommandsAreSorted(t *testing.T) {
|
||||||
EnableCommandSorting = true
|
EnableCommandSorting = true
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue