github.com/bosssauce/ponzu@v0.11.1-0.20200102001432-9bc41b703131/cmd/ponzu/usage.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"sort"
     6  	"strings"
     7  	"text/template"
     8  	"unicode"
     9  
    10  	"github.com/spf13/cobra"
    11  )
    12  
    13  var templateFuncs = template.FuncMap{
    14  	"rpad": rpad,
    15  	"trimTrailingWhitespaces": trimRightSpace,
    16  }
    17  
    18  var tmpl = `{{with (or .Cmd.Long .Cmd.Short)}}{{. | trimTrailingWhitespaces}}
    19  
    20  {{end}}{{if or .Cmd.Runnable .Cmd.HasSubCommands}}Usage:{{if .Cmd.Runnable}}
    21    {{.Cmd.UseLine}}{{end}}{{if .Cmd.HasAvailableSubCommands}}
    22    {{.Cmd.CommandPath}} [command]{{end}}{{if gt (len .Cmd.Aliases) 0}}
    23  
    24  Aliases:
    25    {{.Cmd.NameAndAliases}}{{end}}{{if .Cmd.HasExample}}
    26  
    27  Examples:
    28  {{.Cmd.Example}}{{end}}{{if .Cmd.HasAvailableSubCommands}}
    29  
    30  Available Commands:{{range .Cmd.Commands}}{{if (or .IsAvailableCommand false)}}
    31    {{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .Cmd.HasAvailableLocalFlags}}
    32  
    33  Flags for all commands:
    34  {{.Cmd.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{range .Subs}}{{if (and .IsAvailableCommand .HasAvailableLocalFlags)}}
    35  
    36  Flags for '{{.Name}}' command:
    37  {{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{end}}{{if .Cmd.HasHelpSubCommands}}
    38  
    39  Additional help topics:{{range .Cmd.Commands}}{{if .Cmd.IsAdditionalHelpTopicCommand}}
    40    {{rpad .Cmd.CommandPath .Cmd.CommandPathPadding}} {{.Cmd.Short}}{{end}}{{end}}{{end}}{{if .Cmd.HasAvailableSubCommands}}
    41  
    42  Use "{{.Cmd.CommandPath}} [command] --help" for more information about a command.{{end}}
    43  {{end}}`
    44  
    45  var helpCmd = &cobra.Command{
    46  	Use:   "help",
    47  	Short: "help about any command",
    48  	Run: func(cmd *cobra.Command, args []string) {
    49  		cmd, _, e := rootCmd.Find(args)
    50  		if cmd == nil || e != nil {
    51  			rootCmd.Printf("Unknown help topic %#q\n", args)
    52  			rootCmd.Usage()
    53  			return
    54  		}
    55  		t := template.New("help")
    56  		t.Funcs(templateFuncs)
    57  		template.Must(t.Parse(tmpl))
    58  		if len(args) > 0 {
    59  			rootCmd.HelpFunc()(cmd, args)
    60  			return
    61  		}
    62  
    63  		sortByName := func(i, j int) bool { return cmds[i].Name() < cmds[j].Name() }
    64  		sort.Slice(cmds, sortByName)
    65  
    66  		err := t.Execute(cmd.OutOrStdout(), struct {
    67  			Cmd  *cobra.Command
    68  			Subs []*cobra.Command
    69  		}{
    70  			Cmd:  rootCmd,
    71  			Subs: cmds})
    72  		if err != nil {
    73  			cmd.Println(err)
    74  		}
    75  	},
    76  }
    77  
    78  var cmds []*cobra.Command
    79  
    80  // RegisterCmdlineCommand adds a cobra command to the root command and makes it
    81  // known to the main package
    82  func RegisterCmdlineCommand(cmd *cobra.Command) {
    83  	rootCmd.AddCommand(cmd)
    84  	cmds = append(cmds, cmd)
    85  }
    86  
    87  func init() {
    88  	rootCmd.SetHelpCommand(helpCmd)
    89  }
    90  
    91  // rpad adds padding to the right of a string.
    92  func rpad(s string, padding int) string {
    93  	template := fmt.Sprintf("%%-%ds", padding)
    94  	return fmt.Sprintf(template, s)
    95  }
    96  
    97  func trimRightSpace(s string) string {
    98  	return strings.TrimRightFunc(s, unicode.IsSpace)
    99  }