Add trailing newline in usage string (#1382)

## Changes

The default template includes a final newline but this was missing from
the cmdgroup template. This change also adds test coverage for inherited
flags and the flag group description.
This commit is contained in:
Pieter Noordhuis 2024-04-19 16:12:52 +02:00 committed by GitHub
parent ebbb716164
commit b296f90767
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 3 deletions

View File

@ -17,8 +17,16 @@ func TestCommandFlagGrouping(t *testing.T) {
},
}
parent := &cobra.Command{
Use: "parent",
}
parent.PersistentFlags().String("global", "", "Global flag")
parent.AddCommand(cmd)
wrappedCmd := NewCommandWithGroupFlag(cmd)
jobGroup := NewFlagGroup("Job")
jobGroup.SetDescription("Description.")
fs := jobGroup.FlagSet()
fs.String("job-name", "", "Name of the job")
fs.String("job-type", "", "Type of the job")
@ -37,9 +45,10 @@ func TestCommandFlagGrouping(t *testing.T) {
cmd.Usage()
expected := `Usage:
test [flags]
parent test [flags]
Job Flags:
Description.
--job-name string Name of the job
--job-type string Type of the job
@ -48,7 +57,11 @@ Pipeline Flags:
--pipeline-type string Type of the pipeline
Flags:
-b, --bool Bool flag`
-b, --bool Bool flag
Global Flags:
--global string Global flag
`
require.Equal(t, expected, buf.String())
require.NotNil(t, cmd.Flags().Lookup("job-name"))

View File

@ -11,4 +11,5 @@ const usageTemplate = `Usage:{{if .Command.Runnable}}
{{.NonGroupedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .Command.HasAvailableInheritedFlags}}
Global Flags:
{{.Command.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}`
{{.Command.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}
`