Skip to content

Commit

Permalink
Merge pull request #1949 from BlackHole1/add-global-options
Browse files Browse the repository at this point in the history
feat(command): show global options in command help
  • Loading branch information
abennett authored Jul 11, 2024
2 parents 6d6416e + eba3ce6 commit 937cfe9
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 3 deletions.
13 changes: 13 additions & 0 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,19 @@ func (cmd *Command) appendFlag(fl Flag) {
}
}

// VisiblePersistentFlags returns a slice of [PersistentFlag] with Persistent=true and Hidden=false.
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
8 changes: 7 additions & 1 deletion godoc-current.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ DESCRIPTION:

OPTIONS:{{template "visibleFlagCategoryTemplate" .}}{{else if .VisibleFlags}}

OPTIONS:{{template "visibleFlagTemplate" .}}{{end}}
OPTIONS:{{template "visibleFlagTemplate" .}}{{end}}{{if .VisiblePersistentFlags}}

GLOBAL OPTIONS:{{template "visiblePersistentFlagTemplate" .}}{{end}}
`
CommandHelpTemplate is the text template for the command help topic. cli.go
uses text/template to render templates. You can render custom help text by
Expand Down Expand Up @@ -516,6 +518,10 @@ func (cmd *Command) VisibleFlagCategories() []VisibleFlagCategory
func (cmd *Command) VisibleFlags() []Flag
VisibleFlags returns a slice of the Flags with Hidden=false

func (cmd *Command) VisiblePersistentFlags() []Flag
VisiblePersistentFlags returns a slice of PersistentFlag with
Persistent=true and Hidden=false.

type CommandCategories interface {
// AddCommand adds a command to a category, creating a new category if necessary.
AddCommand(category string, command *Command)
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
45 changes: 45 additions & 0 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
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
8 changes: 7 additions & 1 deletion testdata/godoc-v3.x.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ DESCRIPTION:

OPTIONS:{{template "visibleFlagCategoryTemplate" .}}{{else if .VisibleFlags}}

OPTIONS:{{template "visibleFlagTemplate" .}}{{end}}
OPTIONS:{{template "visibleFlagTemplate" .}}{{end}}{{if .VisiblePersistentFlags}}

GLOBAL OPTIONS:{{template "visiblePersistentFlagTemplate" .}}{{end}}
`
CommandHelpTemplate is the text template for the command help topic. cli.go
uses text/template to render templates. You can render custom help text by
Expand Down Expand Up @@ -516,6 +518,10 @@ func (cmd *Command) VisibleFlagCategories() []VisibleFlagCategory
func (cmd *Command) VisibleFlags() []Flag
VisibleFlags returns a slice of the Flags with Hidden=false

func (cmd *Command) VisiblePersistentFlags() []Flag
VisiblePersistentFlags returns a slice of PersistentFlag with
Persistent=true and Hidden=false.

type CommandCategories interface {
// AddCommand adds a command to a category, creating a new category if necessary.
AddCommand(category string, command *Command)
Expand Down

0 comments on commit 937cfe9

Please sign in to comment.