help.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // Copyright 2015 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // copied from https://github.com/coreos/rkt/blob/master/rkt/help.go
  15. package ctlv3
  16. import (
  17. "bytes"
  18. "fmt"
  19. "io"
  20. "os"
  21. "strings"
  22. "text/tabwriter"
  23. "text/template"
  24. "github.com/coreos/etcd/version"
  25. "github.com/spf13/cobra"
  26. "github.com/spf13/pflag"
  27. )
  28. var (
  29. commandUsageTemplate *template.Template
  30. templFuncs = template.FuncMap{
  31. "descToLines": func(s string) []string {
  32. // trim leading/trailing whitespace and split into slice of lines
  33. return strings.Split(strings.Trim(s, "\n\t "), "\n")
  34. },
  35. "cmdName": func(cmd *cobra.Command, startCmd *cobra.Command) string {
  36. parts := []string{cmd.Name()}
  37. for cmd.HasParent() && cmd.Parent().Name() != startCmd.Name() {
  38. cmd = cmd.Parent()
  39. parts = append([]string{cmd.Name()}, parts...)
  40. }
  41. return strings.Join(parts, " ")
  42. },
  43. }
  44. )
  45. func init() {
  46. commandUsage := `
  47. {{ $cmd := .Cmd }}\
  48. {{ $cmdname := cmdName .Cmd .Cmd.Root }}\
  49. NAME:
  50. {{ if not .Cmd.HasParent }}\
  51. {{printf "\t%s - %s" .Cmd.Name .Cmd.Short}}
  52. {{else}}\
  53. {{printf "\t%s - %s" $cmdname .Cmd.Short}}
  54. {{end}}\
  55. USAGE:
  56. {{printf "\t%s" .Cmd.UseLine}}
  57. {{ if not .Cmd.HasParent }}\
  58. VERSION:
  59. {{printf "\t%s" .Version}}
  60. {{end}}\
  61. {{if .Cmd.HasSubCommands}}\
  62. COMMANDS:
  63. {{range .SubCommands}}\
  64. {{ $cmdname := cmdName . $cmd }}\
  65. {{ if .Runnable }}\
  66. {{printf "\t%s\t%s" $cmdname .Short}}
  67. {{end}}\
  68. {{end}}\
  69. {{end}}\
  70. {{ if .Cmd.Long }}\
  71. DESCRIPTION:
  72. {{range $line := descToLines .Cmd.Long}}{{printf "\t%s" $line}}
  73. {{end}}\
  74. {{end}}\
  75. {{if .Cmd.HasLocalFlags}}\
  76. OPTIONS:
  77. {{.LocalFlags}}\
  78. {{end}}\
  79. {{if .Cmd.HasInheritedFlags}}\
  80. GLOBAL OPTIONS:
  81. {{.GlobalFlags}}\
  82. {{end}}
  83. `[1:]
  84. commandUsageTemplate = template.Must(template.New("command_usage").Funcs(templFuncs).Parse(strings.Replace(commandUsage, "\\\n", "", -1)))
  85. }
  86. func etcdFlagUsages(flagSet *pflag.FlagSet) string {
  87. x := new(bytes.Buffer)
  88. flagSet.VisitAll(func(flag *pflag.Flag) {
  89. if len(flag.Deprecated) > 0 {
  90. return
  91. }
  92. format := ""
  93. if len(flag.Shorthand) > 0 {
  94. format = " -%s, --%s"
  95. } else {
  96. format = " %s --%s"
  97. }
  98. if len(flag.NoOptDefVal) > 0 {
  99. format = format + "["
  100. }
  101. if flag.Value.Type() == "string" {
  102. // put quotes on the value
  103. format = format + "=%q"
  104. } else {
  105. format = format + "=%s"
  106. }
  107. if len(flag.NoOptDefVal) > 0 {
  108. format = format + "]"
  109. }
  110. format = format + "\t%s\n"
  111. shorthand := flag.Shorthand
  112. fmt.Fprintf(x, format, shorthand, flag.Name, flag.DefValue, flag.Usage)
  113. })
  114. return x.String()
  115. }
  116. func getSubCommands(cmd *cobra.Command) []*cobra.Command {
  117. var subCommands []*cobra.Command
  118. for _, subCmd := range cmd.Commands() {
  119. subCommands = append(subCommands, subCmd)
  120. subCommands = append(subCommands, getSubCommands(subCmd)...)
  121. }
  122. return subCommands
  123. }
  124. func usageFunc(cmd *cobra.Command) error {
  125. subCommands := getSubCommands(cmd)
  126. tabOut := getTabOutWithWriter(os.Stdout)
  127. commandUsageTemplate.Execute(tabOut, struct {
  128. Cmd *cobra.Command
  129. LocalFlags string
  130. GlobalFlags string
  131. SubCommands []*cobra.Command
  132. Version string
  133. }{
  134. cmd,
  135. etcdFlagUsages(cmd.LocalFlags()),
  136. etcdFlagUsages(cmd.InheritedFlags()),
  137. subCommands,
  138. version.Version,
  139. })
  140. tabOut.Flush()
  141. return nil
  142. }
  143. func getTabOutWithWriter(writer io.Writer) *tabwriter.Writer {
  144. aTabOut := new(tabwriter.Writer)
  145. aTabOut.Init(writer, 0, 8, 1, '\t', 0)
  146. return aTabOut
  147. }