Skip to content

Commit

Permalink
feat: show cmd aliases (#342)
Browse files Browse the repository at this point in the history
  • Loading branch information
jfatta authored Jul 30, 2021
1 parent 93e264d commit ee96843
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions internal/cli/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ func WrappedNonRequestParamsFlagUsages(cmd *cobra.Command) string {
return nonRequestParamsFlags.FlagUsagesWrapped(getTerminalWidth())
}

// WrappedAliases returns a formatted string containing the command aliases if defined, otherwise an empty string.
func WrappedAliases(cmd *cobra.Command) string {
if len(cmd.Aliases) > 0 {
return ansi.Faint(ansi.Italic(fmt.Sprintf("[%s]", strings.Join(cmd.Aliases, ", "))))
}
return ""
}

//
// Private functions
//
Expand Down Expand Up @@ -96,7 +104,7 @@ func namespaceUsageTemplate() string {
{{.Example}}{{end}}{{if .HasAvailableSubCommands}}
%s{{range .Commands}}{{if (.IsAvailableCommand)}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}
{{rpad .Name .NamePadding }} {{.Short}} {{WrappedAliases .}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}
%s
{{WrappedLocalFlagUsages . | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}}
Expand Down Expand Up @@ -131,7 +139,7 @@ func resourceUsageTemplate() string {
{{.Example}}{{end}}{{if .HasAvailableSubCommands}}
%s{{range .Commands}}{{if (or .IsAvailableCommand (eq .Name "help"))}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}
{{rpad .Name .NamePadding }} {{.Short}} {{WrappedAliases .}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}
%s
{{WrappedLocalFlagUsages . | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}}
Expand Down Expand Up @@ -170,4 +178,5 @@ func init() {
cobra.AddTemplateFunc("WrappedLocalFlagUsages", WrappedLocalFlagUsages)
cobra.AddTemplateFunc("WrappedRequestParamsFlagUsages", WrappedRequestParamsFlagUsages)
cobra.AddTemplateFunc("WrappedNonRequestParamsFlagUsages", WrappedNonRequestParamsFlagUsages)
cobra.AddTemplateFunc("WrappedAliases", WrappedAliases)
}

0 comments on commit ee96843

Please sign in to comment.