Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(command): show global options in command help #1949

Merged
merged 3 commits into from
Jul 11, 2024
Merged
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
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 {
BlackHole1 marked this conversation as resolved.
Show resolved Hide resolved
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
BlackHole1 marked this conversation as resolved.
Show resolved Hide resolved
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 @@
handleTemplateError(err)
}

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

Check warning on line 414 in help.go

View check run for this annotation

Codecov / codecov/patch

help.go#L414

Added line #L414 was not covered by tests
}

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
Loading