cobra.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // Copyright © 2013 Steve Francia <spf@spf13.com>.
  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. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. // Commands similar to git, go tools and other modern CLI tools
  14. // inspired by go, go-Commander, gh and subcommand
  15. package cobra
  16. import (
  17. "fmt"
  18. "io"
  19. "reflect"
  20. "strconv"
  21. "strings"
  22. "text/template"
  23. "time"
  24. "unicode"
  25. )
  26. var templateFuncs = template.FuncMap{
  27. "trim": strings.TrimSpace,
  28. "trimRightSpace": trimRightSpace,
  29. "trimTrailingWhitespaces": trimRightSpace,
  30. "appendIfNotPresent": appendIfNotPresent,
  31. "rpad": rpad,
  32. "gt": Gt,
  33. "eq": Eq,
  34. }
  35. var initializers []func()
  36. // EnablePrefixMatching allows to set automatic prefix matching. Automatic prefix matching can be a dangerous thing
  37. // to automatically enable in CLI tools.
  38. // Set this to true to enable it.
  39. var EnablePrefixMatching = false
  40. // EnableCommandSorting controls sorting of the slice of commands, which is turned on by default.
  41. // To disable sorting, set it to false.
  42. var EnableCommandSorting = true
  43. // MousetrapHelpText enables an information splash screen on Windows
  44. // if the CLI is started from explorer.exe.
  45. // To disable the mousetrap, just set this variable to blank string ("").
  46. // Works only on Microsoft Windows.
  47. var MousetrapHelpText string = `This is a command line tool.
  48. You need to open cmd.exe and run it from there.
  49. `
  50. // MousetrapDisplayDuration controls how long the MousetrapHelpText message is displayed on Windows
  51. // if the CLI is started from explorer.exe. Set to 0 to wait for the return key to be pressed.
  52. // To disable the mousetrap, just set MousetrapHelpText to blank string ("").
  53. // Works only on Microsoft Windows.
  54. var MousetrapDisplayDuration time.Duration = 5 * time.Second
  55. // AddTemplateFunc adds a template function that's available to Usage and Help
  56. // template generation.
  57. func AddTemplateFunc(name string, tmplFunc interface{}) {
  58. templateFuncs[name] = tmplFunc
  59. }
  60. // AddTemplateFuncs adds multiple template functions that are available to Usage and
  61. // Help template generation.
  62. func AddTemplateFuncs(tmplFuncs template.FuncMap) {
  63. for k, v := range tmplFuncs {
  64. templateFuncs[k] = v
  65. }
  66. }
  67. // OnInitialize sets the passed functions to be run when each command's
  68. // Execute method is called.
  69. func OnInitialize(y ...func()) {
  70. initializers = append(initializers, y...)
  71. }
  72. // FIXME Gt is unused by cobra and should be removed in a version 2. It exists only for compatibility with users of cobra.
  73. // Gt takes two types and checks whether the first type is greater than the second. In case of types Arrays, Chans,
  74. // Maps and Slices, Gt will compare their lengths. Ints are compared directly while strings are first parsed as
  75. // ints and then compared.
  76. func Gt(a interface{}, b interface{}) bool {
  77. var left, right int64
  78. av := reflect.ValueOf(a)
  79. switch av.Kind() {
  80. case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice:
  81. left = int64(av.Len())
  82. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  83. left = av.Int()
  84. case reflect.String:
  85. left, _ = strconv.ParseInt(av.String(), 10, 64)
  86. }
  87. bv := reflect.ValueOf(b)
  88. switch bv.Kind() {
  89. case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice:
  90. right = int64(bv.Len())
  91. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  92. right = bv.Int()
  93. case reflect.String:
  94. right, _ = strconv.ParseInt(bv.String(), 10, 64)
  95. }
  96. return left > right
  97. }
  98. // FIXME Eq is unused by cobra and should be removed in a version 2. It exists only for compatibility with users of cobra.
  99. // Eq takes two types and checks whether they are equal. Supported types are int and string. Unsupported types will panic.
  100. func Eq(a interface{}, b interface{}) bool {
  101. av := reflect.ValueOf(a)
  102. bv := reflect.ValueOf(b)
  103. switch av.Kind() {
  104. case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice:
  105. panic("Eq called on unsupported type")
  106. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  107. return av.Int() == bv.Int()
  108. case reflect.String:
  109. return av.String() == bv.String()
  110. }
  111. return false
  112. }
  113. func trimRightSpace(s string) string {
  114. return strings.TrimRightFunc(s, unicode.IsSpace)
  115. }
  116. // FIXME appendIfNotPresent is unused by cobra and should be removed in a version 2. It exists only for compatibility with users of cobra.
  117. // appendIfNotPresent will append stringToAppend to the end of s, but only if it's not yet present in s.
  118. func appendIfNotPresent(s, stringToAppend string) string {
  119. if strings.Contains(s, stringToAppend) {
  120. return s
  121. }
  122. return s + " " + stringToAppend
  123. }
  124. // rpad adds padding to the right of a string.
  125. func rpad(s string, padding int) string {
  126. template := fmt.Sprintf("%%-%ds", padding)
  127. return fmt.Sprintf(template, s)
  128. }
  129. // tmpl executes the given template text on data, writing the result to w.
  130. func tmpl(w io.Writer, text string, data interface{}) error {
  131. t := template.New("top")
  132. t.Funcs(templateFuncs)
  133. template.Must(t.Parse(text))
  134. return t.Execute(w, data)
  135. }
  136. // ld compares two strings and returns the levenshtein distance between them.
  137. func ld(s, t string, ignoreCase bool) int {
  138. if ignoreCase {
  139. s = strings.ToLower(s)
  140. t = strings.ToLower(t)
  141. }
  142. d := make([][]int, len(s)+1)
  143. for i := range d {
  144. d[i] = make([]int, len(t)+1)
  145. }
  146. for i := range d {
  147. d[i][0] = i
  148. }
  149. for j := range d[0] {
  150. d[0][j] = j
  151. }
  152. for j := 1; j <= len(t); j++ {
  153. for i := 1; i <= len(s); i++ {
  154. if s[i-1] == t[j-1] {
  155. d[i][j] = d[i-1][j-1]
  156. } else {
  157. min := d[i-1][j]
  158. if d[i][j-1] < min {
  159. min = d[i][j-1]
  160. }
  161. if d[i-1][j-1] < min {
  162. min = d[i-1][j-1]
  163. }
  164. d[i][j] = min + 1
  165. }
  166. }
  167. }
  168. return d[len(s)][len(t)]
  169. }
  170. func stringInSlice(a string, list []string) bool {
  171. for _, b := range list {
  172. if b == a {
  173. return true
  174. }
  175. }
  176. return false
  177. }