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

fix: update usage template to only show flags when present #273

Merged
merged 2 commits into from
May 10, 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
2 changes: 1 addition & 1 deletion cmd/flags/toggle.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
49 changes: 8 additions & 41 deletions cmd/resources/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func NewResourceCmd(
},
}

cmd.SetUsageTemplate(getResourceUsageTemplate())
cmd.SetUsageTemplate(SubcommandUsageTemplate())
parentCmd.AddCommand(cmd)
parentCmd.Annotations[resourceName] = "resource"

Expand Down Expand Up @@ -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()
Expand All @@ -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}}
Expand All @@ -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}}
Expand Down
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
36 changes: 30 additions & 6 deletions cmd/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading