From 0d45bd801d640d15ecd422961b52c1e51ef09abe Mon Sep 17 00:00:00 2001 From: Brian Pursley Date: Sun, 28 Aug 2022 11:56:46 -0400 Subject: [PATCH] fix: show flags that shadow parent persistent flag in child help This fixes a bug where a child flag that shadows (has the same name as) a parent persistent flag would not be shown in the child command's help output and the parent flag would be shown instead under the global flags section. This change makes the help output consistent with the observed behavior during execution, where the child flag is the one that is actually used. Fixes https://github.com/spf13/cobra/issues/1651 Merge https://github.com/spf13/cobra/pull/1776 --- command.go | 3 ++- command_test.go | 39 ++++++++++++++++++++++++++++++++++----- 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/command.go b/command.go index bd757a1..9f3830d 100644 --- a/command.go +++ b/command.go @@ -1679,7 +1679,8 @@ func (c *Command) LocalFlags() *zflag.FlagSet { } addToLocal := func(f *zflag.Flag) { - if c.lflags.Lookup(f.Name) == nil && c.parentsPflags.Lookup(f.Name) == nil { + // Add the flag if it is not a parent PFlag, or it shadows a parent PFlag + if c.lflags.Lookup(f.Name) == nil && f != c.parentsPflags.Lookup(f.Name) { c.lflags.AddFlag(f) } } diff --git a/command_test.go b/command_test.go index 151575c..153ca97 100644 --- a/command_test.go +++ b/command_test.go @@ -712,10 +712,7 @@ func TestEmptyInputs(t *testing.T) { } } -func TestOverwrittenFlag(t *testing.T) { - // TODO: This test fails, but should work. - t.Skip() - +func TestChildFlagShadowsParentPersistentFlag(t *testing.T) { parent := &zulu.Command{Use: "parent", RunE: emptyRun} child := &zulu.Command{Use: "child", RunE: emptyRun} @@ -737,7 +734,7 @@ func TestOverwrittenFlag(t *testing.T) { } if childInherited.Lookup("intf") != nil { - t.Errorf(`InheritedFlags should not contain overwritten flag "intf"`) + t.Errorf(`InheritedFlags should not contain shadowed flag "intf"`) } if childLocal.Lookup("intf") == nil { t.Error(`LocalFlags expected to contain "intf", got "nil"`) @@ -905,6 +902,38 @@ func TestHelpCommandExecutedOnChild(t *testing.T) { checkStringContains(t, output, childCmd.Long) } +func TestHelpCommandExecutedOnChildWithFlagThatShadowsParentFlag(t *testing.T) { + parent := &zulu.Command{Use: "parent", RunE: emptyRun} + child := &zulu.Command{Use: "child", RunE: emptyRun} + parent.AddCommand(child) + + parent.PersistentFlags().Bool("foo", false, "parent foo usage") + parent.PersistentFlags().Bool("bar", false, "parent bar usage") + child.Flags().Bool("foo", false, "child foo usage") // This shadows parent's foo flag + child.Flags().Bool("baz", false, "child baz usage") + + got, err := executeCommand(parent, "help", "child") + if err != nil { + t.Errorf("Unexpected error: %v", err) + } + + expected := `Usage: + parent child [flags] + +Flags: + --baz child baz usage + --foo child foo usage + -h, --help help for child + +Global Flags: + --bar parent bar usage +` + + if got != expected { + t.Errorf("Help text mismatch.\nExpected:\n%s\n\nGot:\n%s\n", expected, got) + } +} + func TestSetHelpCommand(t *testing.T) { c := &zulu.Command{Use: "c", RunE: emptyRun} c.AddCommand(&zulu.Command{Use: "empty", RunE: emptyRun})