Skip to content

Commit

Permalink
feat(command): show global options in command help
Browse files Browse the repository at this point in the history
If there are persistent flags, they should also be displayed in the command.

Signed-off-by: Kevin Cui <[email protected]>
  • Loading branch information
BlackHole1 committed Jul 9, 2024
1 parent 6d6416e commit 366f717
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 4 deletions.
12 changes: 12 additions & 0 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,18 @@ func (cmd *Command) appendFlag(fl Flag) {
}
}

func (cmd *Command) VisiblePersistentFlags() []Flag {
var flags []Flag
for _, fl := range cmd.Root().Flags {
pfl, ok := fl.(PersistentFlag)
if !ok || !pfl.IsPersistent() {
continue
}
flags = append(flags, fl)
}
return visibleFlags(flags)
}

func (cmd *Command) appendCommand(aCmd *Command) {
if !hasCommand(cmd.Commands, aCmd) {
aCmd.parent = cmd
Expand Down
4 changes: 4 additions & 0 deletions help.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,10 @@ func printHelpCustom(out io.Writer, templ string, data interface{}, customFuncs
handleTemplateError(err)
}

if _, err := t.New("visiblePersistentFlagTemplate").Parse(visiblePersistentFlagTemplate); err != nil {
handleTemplateError(err)
}

if _, err := t.New("visibleGlobalFlagCategoryTemplate").Parse(strings.Replace(visibleFlagCategoryTemplate, "OPTIONS", "GLOBAL OPTIONS", -1)); err != nil {
handleTemplateError(err)
}
Expand Down
51 changes: 48 additions & 3 deletions help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,51 @@ UsageText`,
"expected output to include usage text")
}

func TestShowSubcommandHelp_GlobalOptions(t *testing.T) {
cmd := &Command{
Flags: []Flag{
&StringFlag{
Name: "foo",
Persistent: true,
},
},
Commands: []*Command{
{
Name: "frobbly",
Flags: []Flag{
&StringFlag{
Name: "bar",
},
},
Action: func(context.Context, *Command) error {
return nil
},
},
},
}

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

_ = cmd.Run(buildTestContext(t), []string{"foo", "frobbly", "--help"})

expected := `NAME:
foo frobbly
USAGE:
foo frobbly [command [command options]]
OPTIONS:
--bar value
--help, -h show help
GLOBAL OPTIONS:
--foo value
`

assert.Contains(t, output.String(), expected, "expected output to include global options")
}

func TestShowSubcommandHelp_SubcommandUsageText(t *testing.T) {
cmd := &Command{
Commands: []*Command{
Expand Down Expand Up @@ -1652,13 +1697,13 @@ COMMANDS:
GLOBAL OPTIONS:
--help, -h show help
--m2 value
--strd value
--m2 value
--strd value
cat1
--intd value, --altd1 value, --altd2 value (default: 0)
--m1 value
--m1 value
`, output.String())
}
Expand Down
7 changes: 6 additions & 1 deletion template.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ var visibleFlagCategoryTemplate = `{{range .VisibleFlagCategories}}
var visibleFlagTemplate = `{{range $i, $e := .VisibleFlags}}
{{wrap $e.String 6}}{{end}}`

var visiblePersistentFlagTemplate = `{{range $i, $e := .VisiblePersistentFlags}}
{{wrap $e.String 6}}{{end}}`

var versionTemplate = `{{if .Version}}{{if not .HideVersion}}
VERSION:
Expand Down Expand Up @@ -80,7 +83,9 @@ DESCRIPTION:
OPTIONS:{{template "visibleFlagCategoryTemplate" .}}{{else if .VisibleFlags}}
OPTIONS:{{template "visibleFlagTemplate" .}}{{end}}
OPTIONS:{{template "visibleFlagTemplate" .}}{{end}}{{if .VisiblePersistentFlags}}
GLOBAL OPTIONS:{{template "visiblePersistentFlagTemplate" .}}{{end}}
`

// SubcommandHelpTemplate is the text template for the subcommand help topic.
Expand Down

0 comments on commit 366f717

Please sign in to comment.