cobra.go 4.6 KB

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