Skip to content
Myr

fix: show flags that shadow parent persistent flag in child help (#1776)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

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.

authored by Brian Pursley · committed by GitHub · parent dbf85f6 · Browse files · 2 files+36 added−6 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+36 added−6 removed

  • other:+36 added−6 removed

2 files+36 added−6 removed

modified:command_test.go+34 added-5 removed
705 unmodified lines
706 t.Errorf("flagValue expected: %v, got %v", 7, flagValue)707 }708}709710func TestOverwrittenFlag(t *testing.T) {711 // TODO: This test fails, but should work.712 t.Skip()713710func TestChildFlagShadowsParentPersistentFlag(t *testing.T) {711 parent := &Command{Use: "parent", Run: emptyRun}712 child := &Command{Use: "child", Run: emptyRun}713714 parent.PersistentFlags().Bool("boolf", false, "")
13 unmodified lines
728 t.Error(`InheritedFlags expected to contain "boolf", got "nil"`)729 }730731 if childInherited.Lookup("intf") != nil {735 t.Errorf(`InheritedFlags should not contain overwritten flag "intf"`)732 t.Errorf(`InheritedFlags should not contain shadowed flag "intf"`)733 }734 if childLocal.Lookup("intf") == nil {735 t.Error(`LocalFlags expected to contain "intf", got "nil"`)736 }
146 unmodified lines
883884 checkStringContains(t, output, childCmd.Long)885}886887func TestHelpCommandExecutedOnChildWithFlagThatShadowsParentFlag(t *testing.T) {888 parent := &Command{Use: "parent", Run: emptyRun}889 child := &Command{Use: "child", Run: emptyRun}890 parent.AddCommand(child)891892 parent.PersistentFlags().Bool("foo", false, "parent foo usage")893 parent.PersistentFlags().Bool("bar", false, "parent bar usage")894 child.Flags().Bool("foo", false, "child foo usage") // This shadows parent's foo flag895 child.Flags().Bool("baz", false, "child baz usage")896897 got, err := executeCommand(parent, "help", "child")898 if err != nil {899 t.Errorf("Unexpected error: %v", err)900 }901902 expected := `Usage:903 parent child [flags]904905Flags:906 --baz child baz usage907 --foo child foo usage908 -h, --help help for child909910Global Flags:911 --bar parent bar usage912`913914 if got != expected {915 t.Errorf("Help text mismatch.\nExpected:\n%s\n\nGot:\n%s\n", expected, got)916 }917}918919func TestSetHelpCommand(t *testing.T) {920 c := &Command{Use: "c", Run: emptyRun}921 c.AddCommand(&Command{Use: "empty", Run: emptyRun})922
1302 unmodified lines
modified:command.go+2 added-1 removed
1503 unmodified lines
1504 c.lflags.SetNormalizeFunc(c.globNormFunc)1505 }15061507 addToLocal := func(f *flag.Flag) {1508 if c.lflags.Lookup(f.Name) == nil && c.parentsPflags.Lookup(f.Name) == nil {1508 // Add the flag if it is not a parent PFlag, or it shadows a parent PFlag1509 if c.lflags.Lookup(f.Name) == nil && f != c.parentsPflags.Lookup(f.Name) {1510 c.lflags.AddFlag(f)1511 }1512 }1513 c.Flags().VisitAll(addToLocal)
184 unmodified lines