From 7faa7fcdd20c7484bc89fdd48acb67ed44b8ba2e Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Tue, 31 May 2016 15:38:12 -0700 Subject: [PATCH] Merge persistent flags before checking for a help flag. Signed-off-by: Daniel Nephin --- command.go | 1 + command_test.go | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/command.go b/command.go index a8b0fa56..0b74c2da 100644 --- a/command.go +++ b/command.go @@ -684,6 +684,7 @@ func (c *Command) ExecuteC() (cmd *Command, err error) { } func (c *Command) initHelpFlag() { + c.mergePersistentFlags() if c.Flags().Lookup("help") == nil { c.Flags().BoolP("help", "h", false, "help for "+c.Name()) } diff --git a/command_test.go b/command_test.go index 127ca0e6..3d10ba81 100644 --- a/command_test.go +++ b/command_test.go @@ -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) { EnableCommandSorting = true