Skip to content

Commit 744d9f6

Browse files
authored
Beautify usage template (#342)
* Beautify usage template * Add missing sections
1 parent ba89d88 commit 744d9f6

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

internal/cmd/root.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
3232
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
3333

34+
"github.com/fatih/color"
3435
"github.com/spf13/cobra"
3536
"github.com/spf13/viper"
3637
)
@@ -88,9 +89,27 @@ func NewRootCmd(version, date string, p *print.Printer) *cobra.Command {
8889
c.Flags().BoolP("help", "h", false, fmt.Sprintf("Help for %q", c.CommandPath()))
8990
})
9091

92+
beautifyUsageTemplate(cmd)
93+
9194
return cmd
9295
}
9396

97+
func beautifyUsageTemplate(cmd *cobra.Command) {
98+
cobra.AddTemplateFunc("WhiteBold", color.New(color.FgHiWhite, color.Bold).SprintFunc())
99+
usageTemplate := cmd.UsageTemplate()
100+
usageTemplate = strings.NewReplacer(
101+
`Usage:`, `{{WhiteBold "USAGE"}}`,
102+
`Examples:`, `{{WhiteBold "EXAMPLES"}}`,
103+
`Aliases:`, `{{WhiteBold "ALIASES"}}`,
104+
`Available Commands:`, `{{WhiteBold "AVAILABLE COMMANDS"}}`,
105+
`Additional Commands:`, `{{WhiteBold "ADDITIONAL COMMANDS"}}`,
106+
`Global Flags:`, `{{WhiteBold "GLOBAL FLAGS"}}`,
107+
`Flags:`, `{{WhiteBold "FLAGS"}}`,
108+
`Additional help topics:`, `{{WhiteBold "ADDITIONAL HELP TOPICS"}}`,
109+
).Replace(usageTemplate)
110+
cmd.SetUsageTemplate(usageTemplate)
111+
}
112+
94113
func configureFlags(cmd *cobra.Command) error {
95114
cmd.Flags().BoolP("version", "v", false, `Show "stackit" version`)
96115

0 commit comments

Comments
 (0)