Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,18 +300,21 @@ func (cmd *Command) appendFlag(fl Flag) {
}
}

// VisiblePersistentFlags returns a slice of [LocalFlag] with Persistent=true and Hidden=false.
// VisiblePersistentFlags returns inherited persistent flags with Hidden=false.
func (cmd *Command) VisiblePersistentFlags() []Flag {
if cmd.isCompletionCommand {
return nil
}
var flags []Flag
for _, fl := range cmd.Root().Flags {
pfl, ok := fl.(LocalFlag)
if !ok || pfl.IsLocal() {
continue
lineage := cmd.Lineage()
for i := len(lineage) - 1; i > 0; i-- {
for _, fl := range lineage[i].allFlags() {
pfl, ok := fl.(LocalFlag)
if !ok || pfl.IsLocal() {
continue
}
flags = append(flags, fl)
}
flags = append(flags, fl)
}
return visibleFlags(flags)
}
Expand Down
46 changes: 46 additions & 0 deletions help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,52 @@
assert.Contains(t, output.String(), expected, "expected output to include global options")
}

func TestShowNestedSubcommandHelp_InheritedPersistentFlags(t *testing.T) {
cmd := &Command{
Flags: []Flag{
&StringFlag{
Name: "root-persistent",
Local: false,
},
},
Commands: []*Command{
{
Name: "mid",
Flags: []Flag{
&StringFlag{
Name: "mid-persistent",
Local: false,
},
&StringFlag{
Name: "mid-local",

Check failure on line 878 in help_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

File is not properly formatted (gofumpt)
Local: true,
},
},
Commands: []*Command{
{
Name: "leaf",
Flags: []Flag{
&StringFlag{Name: "leaf-local"},
},
},
},
},
},
}

output := &bytes.Buffer{}
cmd.Writer = output

_ = cmd.Run(buildTestContext(t), []string{"root", "mid", "leaf", "--help"})

assert.Contains(t, output.String(), "GLOBAL OPTIONS:",
"expected help to include a global options section")
assert.Contains(t, output.String(), "--root-persistent string",
"expected help to include the root persistent flag")
assert.Contains(t, output.String(), "--mid-persistent string",
"expected help to include the intermediate persistent flag")
}

func TestShowSubcommandHelp_GlobalOptions_HideHelpCommand(t *testing.T) {
cmd := &Command{
Flags: []Flag{
Expand Down
Loading