cobra.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. "unicode"
  24. )
  25. var templateFuncs = template.FuncMap{
  26. "trim": strings.TrimSpace,
  27. "trimRightSpace": trimRightSpace,
  28. "appendIfNotPresent": appendIfNotPresent,
  29. "rpad": rpad,
  30. "gt": Gt,
  31. "eq": Eq,
  32. }
  33. var initializers []func()
  34. // automatic prefix matching can be a dangerous thing to automatically enable in CLI tools.
  35. // Set this to true to enable it
  36. var EnablePrefixMatching = false
  37. //EnableCommandSorting controls sorting of the slice of commands, which is turned on by default.
  38. //To disable sorting, set it to false.
  39. var EnableCommandSorting = true
  40. //AddTemplateFunc adds a template function that's available to Usage and Help
  41. //template generation.
  42. func AddTemplateFunc(name string, tmplFunc interface{}) {
  43. templateFuncs[name] = tmplFunc
  44. }
  45. //AddTemplateFuncs adds multiple template functions availalble to Usage and
  46. //Help template generation.
  47. func AddTemplateFuncs(tmplFuncs template.FuncMap) {
  48. for k, v := range tmplFuncs {
  49. templateFuncs[k] = v
  50. }
  51. }
  52. //OnInitialize takes a series of func() arguments and appends them to a slice of func().
  53. func OnInitialize(y ...func()) {
  54. for _, x := range y {
  55. initializers = append(initializers, x)
  56. }
  57. }
  58. //Gt takes two types and checks whether the first type is greater than the second. In case of types Arrays, Chans,
  59. //Maps and Slices, Gt will compare their lengths. Ints are compared directly while strings are first parsed as
  60. //ints and then compared.
  61. func Gt(a interface{}, b interface{}) bool {
  62. var left, right int64
  63. av := reflect.ValueOf(a)
  64. switch av.Kind() {
  65. case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice:
  66. left = int64(av.Len())
  67. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  68. left = av.Int()
  69. case reflect.String:
  70. left, _ = strconv.ParseInt(av.String(), 10, 64)
  71. }
  72. bv := reflect.ValueOf(b)
  73. switch bv.Kind() {
  74. case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice:
  75. right = int64(bv.Len())
  76. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  77. right = bv.Int()
  78. case reflect.String:
  79. right, _ = strconv.ParseInt(bv.String(), 10, 64)
  80. }
  81. return left > right
  82. }
  83. //Eq takes two types and checks whether they are equal. Supported types are int and string. Unsupported types will panic.
  84. func Eq(a interface{}, b interface{}) bool {
  85. av := reflect.ValueOf(a)
  86. bv := reflect.ValueOf(b)
  87. switch av.Kind() {
  88. case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice:
  89. panic("Eq called on unsupported type")
  90. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  91. return av.Int() == bv.Int()
  92. case reflect.String:
  93. return av.String() == bv.String()
  94. }
  95. return false
  96. }
  97. func trimRightSpace(s string) string {
  98. return strings.TrimRightFunc(s, unicode.IsSpace)
  99. }
  100. // appendIfNotPresent will append stringToAppend to the end of s, but only if it's not yet present in s
  101. func appendIfNotPresent(s, stringToAppend string) string {
  102. if strings.Contains(s, stringToAppend) {
  103. return s
  104. }
  105. return s + " " + stringToAppend
  106. }
  107. //rpad adds padding to the right of a string
  108. func rpad(s string, padding int) string {
  109. template := fmt.Sprintf("%%-%ds", padding)
  110. return fmt.Sprintf(template, s)
  111. }
  112. // tmpl executes the given template text on data, writing the result to w.
  113. func tmpl(w io.Writer, text string, data interface{}) error {
  114. t := template.New("top")
  115. t.Funcs(templateFuncs)
  116. template.Must(t.Parse(text))
  117. return t.Execute(w, data)
  118. }
  119. // ld compares two strings and returns the levenshtein distance between them
  120. func ld(s, t string, ignoreCase bool) int {
  121. if ignoreCase {
  122. s = strings.ToLower(s)
  123. t = strings.ToLower(t)
  124. }
  125. d := make([][]int, len(s)+1)
  126. for i := range d {
  127. d[i] = make([]int, len(t)+1)
  128. }
  129. for i := range d {
  130. d[i][0] = i
  131. }
  132. for j := range d[0] {
  133. d[0][j] = j
  134. }
  135. for j := 1; j <= len(t); j++ {
  136. for i := 1; i <= len(s); i++ {
  137. if s[i-1] == t[j-1] {
  138. d[i][j] = d[i-1][j-1]
  139. } else {
  140. min := d[i-1][j]
  141. if d[i][j-1] < min {
  142. min = d[i][j-1]
  143. }
  144. if d[i-1][j-1] < min {
  145. min = d[i-1][j-1]
  146. }
  147. d[i][j] = min + 1
  148. }
  149. }
  150. }
  151. return d[len(s)][len(t)]
  152. }