From 68e6b5ce6684075b07ae7958f375eeed3ce29780 Mon Sep 17 00:00:00 2001 From: Kelly Hofmann <55991524+k3llymariee@users.noreply.github.com> Date: Fri, 10 May 2024 16:46:59 -0700 Subject: [PATCH] fix: update usage template to only show flags when present (#273) * update usage template to only show flags when present * pr feedback --- cmd/flags/toggle.go | 2 +- cmd/resources/resources.go | 49 +++++++------------------------------- cmd/root.go | 2 ++ cmd/templates.go | 36 +++++++++++++++++++++++----- 4 files changed, 41 insertions(+), 48 deletions(-) diff --git a/cmd/flags/toggle.go b/cmd/flags/toggle.go index 10ead9aa..95efa1f7 100644 --- a/cmd/flags/toggle.go +++ b/cmd/flags/toggle.go @@ -23,7 +23,7 @@ func NewToggleOnCmd(client resources.Client) *cobra.Command { Use: "toggle-on", } - cmd.SetUsageTemplate(resourcescmd.OperationUsageTemplate()) + cmd.SetUsageTemplate(resourcescmd.SubcommandUsageTemplate()) initFlags(cmd) return cmd diff --git a/cmd/resources/resources.go b/cmd/resources/resources.go index 97f66c45..0452a85f 100644 --- a/cmd/resources/resources.go +++ b/cmd/resources/resources.go @@ -210,7 +210,7 @@ func NewResourceCmd( }, } - cmd.SetUsageTemplate(getResourceUsageTemplate()) + cmd.SetUsageTemplate(SubcommandUsageTemplate()) parentCmd.AddCommand(cmd) parentCmd.Annotations[resourceName] = "resource" @@ -356,7 +356,7 @@ func NewOperationCmd(parentCmd *cobra.Command, client resources.Client, op Opera Use: op.Use, } - cmd.SetUsageTemplate(OperationUsageTemplate()) + cmd.SetUsageTemplate(SubcommandUsageTemplate()) opCmd.cmd = cmd _ = opCmd.initFlags() @@ -366,7 +366,7 @@ func NewOperationCmd(parentCmd *cobra.Command, client resources.Client, op Opera return cmd } -func getResourceUsageTemplate() string { +func SubcommandUsageTemplate() string { return `Usage:{{if .Runnable}} {{.UseLine}}{{end}}{{if .HasAvailableSubCommands}} {{.CommandPath}} [command]{{end}}{{if gt (len .Aliases) 0}} @@ -384,49 +384,16 @@ Available Commands:{{range $cmds}}{{if (or .IsAvailableCommand (eq .Name "help") {{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if not .AllChildCommandsHaveGroup}} Additional Commands:{{range $cmds}}{{if (and (eq .GroupID "") (or .IsAvailableCommand (eq .Name "help")))}} - {{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}} - -Flags: -{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}} - -Global Flags: -{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasHelpSubCommands}} - -Additional help topics:{{range .Commands}}{{if .IsAdditionalHelpTopicCommand}} - {{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableSubCommands}} - -Use "{{.CommandPath}} [command] --help" for more information about a command.{{end}} -` -} - -func OperationUsageTemplate() string { - return `Usage:{{if .Runnable}} - {{.UseLine}}{{end}}{{if .HasAvailableSubCommands}} - {{.CommandPath}} [command]{{end}}{{if gt (len .Aliases) 0}} - -Aliases: - {{.NameAndAliases}}{{end}}{{if .HasExample}} - -Examples: -{{.Example}}{{end}}{{if .HasAvailableSubCommands}}{{$cmds := .Commands}}{{if eq (len .Groups) 0}} - -Available Commands:{{range $cmds}}{{if (or .IsAvailableCommand (eq .Name "help"))}} - {{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{else}}{{range $group := .Groups}} - -{{.Title}}{{range $cmds}}{{if (and (eq .GroupID $group.ID) (or .IsAvailableCommand (eq .Name "help")))}} - {{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if not .AllChildCommandsHaveGroup}} - -Additional Commands:{{range $cmds}}{{if (and (eq .GroupID "") (or .IsAvailableCommand (eq .Name "help")))}} - {{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}} + {{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}{{if gt (len WrappedRequiredFlagUsages .) 0}} Required flags: -{{WrappedRequiredFlagUsages . | trimTrailingWhitespaces}} +{{WrappedRequiredFlagUsages . | trimTrailingWhitespaces}}{{end}}{{if HasOptionalFlags .}} Optional flags: -{{WrappedOptionalFlagUsages . | trimTrailingWhitespaces}} +{{WrappedOptionalFlagUsages . | trimTrailingWhitespaces}}{{end}} -Global Flags: -{{rpad " -h, --help" 29}} Help for this command +Global flags: +{{rpad " -h, --help" 29}} Get help about any command {{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasHelpSubCommands}} Additional help topics:{{range .Commands}}{{if .IsAdditionalHelpTopicCommand}} diff --git a/cmd/root.go b/cmd/root.go index 2ef22db9..b1c13aa1 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -232,6 +232,8 @@ See each command's help for details on how to use the generated script.`, rootCm cobra.AddTemplateFunc("WrappedRequiredFlagUsages", WrappedRequiredFlagUsages) cobra.AddTemplateFunc("WrappedOptionalFlagUsages", WrappedOptionalFlagUsages) + cobra.AddTemplateFunc("HasRequiredFlags", HasRequiredFlags) + cobra.AddTemplateFunc("HasOptionalFlags", HasOptionalFlags) rootCmd.cmd.SetUsageTemplate(getUsageTemplate()) err = rootCmd.Execute() diff --git a/cmd/templates.go b/cmd/templates.go index bc7cde54..9b30072d 100644 --- a/cmd/templates.go +++ b/cmd/templates.go @@ -28,29 +28,53 @@ Flags: ` } +func HasRequiredFlags(cmd *cobra.Command) bool { + var numFlags int + cmd.LocalFlags().VisitAll(func(flag *pflag.Flag) { + _, ok := flag.Annotations["required"] + if ok { + numFlags += 1 + } + }) + + return numFlags > 0 +} + +func HasOptionalFlags(cmd *cobra.Command) bool { + var numFlags int + cmd.LocalFlags().VisitAll(func(flag *pflag.Flag) { + _, ok := flag.Annotations["required"] + if !ok && flag.Name != "help" { + numFlags += 1 + } + }) + + return numFlags > 0 +} + func WrappedRequiredFlagUsages(cmd *cobra.Command) string { - nonRequestParamsFlags := pflag.NewFlagSet("request", pflag.ExitOnError) + flagSet := pflag.NewFlagSet("request", pflag.ExitOnError) cmd.LocalFlags().VisitAll(func(flag *pflag.Flag) { if _, ok := flag.Annotations["required"]; ok { - nonRequestParamsFlags.AddFlag(flag) + flagSet.AddFlag(flag) } }) - return nonRequestParamsFlags.FlagUsagesWrapped(getTerminalWidth()) + return flagSet.FlagUsagesWrapped(getTerminalWidth()) } func WrappedOptionalFlagUsages(cmd *cobra.Command) string { - nonRequestParamsFlags := pflag.NewFlagSet("request", pflag.ExitOnError) + flagSet := pflag.NewFlagSet("request", pflag.ExitOnError) cmd.LocalFlags().VisitAll(func(flag *pflag.Flag) { _, ok := flag.Annotations["required"] if !ok && flag.Name != "help" { - nonRequestParamsFlags.AddFlag(flag) + flagSet.AddFlag(flag) } }) - return nonRequestParamsFlags.FlagUsagesWrapped(getTerminalWidth()) + return flagSet.FlagUsagesWrapped(getTerminalWidth()) } func getTerminalWidth() int {