cobra.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // Copyright 2013-2023 The Cobra 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. // Commands similar to git, go tools and other modern CLI tools
  15. // inspired by go, go-Commander, gh and subcommand
  16. package cobra
  17. import (
  18. "fmt"
  19. "io"
  20. "os"
  21. "reflect"
  22. "strconv"
  23. "strings"
  24. "text/template"
  25. "time"
  26. "unicode"
  27. )
  28. var templateFuncs = template.FuncMap{
  29. "trim": strings.TrimSpace,
  30. "trimRightSpace": trimRightSpace,
  31. "trimTrailingWhitespaces": trimRightSpace,
  32. "appendIfNotPresent": appendIfNotPresent,
  33. "rpad": rpad,
  34. "gt": Gt,
  35. "eq": Eq,
  36. }
  37. var initializers []func()
  38. var finalizers []func()
  39. const (
  40. defaultPrefixMatching = false
  41. defaultCommandSorting = true
  42. defaultCaseInsensitive = false
  43. defaultTraverseRunHooks = false
  44. )
  45. // EnablePrefixMatching allows setting automatic prefix matching. Automatic prefix matching can be a dangerous thing
  46. // to automatically enable in CLI tools.
  47. // Set this to true to enable it.
  48. var EnablePrefixMatching = defaultPrefixMatching
  49. // EnableCommandSorting controls sorting of the slice of commands, which is turned on by default.
  50. // To disable sorting, set it to false.
  51. var EnableCommandSorting = defaultCommandSorting
  52. // EnableCaseInsensitive allows case-insensitive commands names. (case sensitive by default)
  53. var EnableCaseInsensitive = defaultCaseInsensitive
  54. // EnableTraverseRunHooks executes persistent pre-run and post-run hooks from all parents.
  55. // By default this is disabled, which means only the first run hook to be found is executed.
  56. var EnableTraverseRunHooks = defaultTraverseRunHooks
  57. // MousetrapHelpText enables an information splash screen on Windows
  58. // if the CLI is started from explorer.exe.
  59. // To disable the mousetrap, just set this variable to blank string ("").
  60. // Works only on Microsoft Windows.
  61. var MousetrapHelpText = `This is a command line tool.
  62. You need to open cmd.exe and run it from there.
  63. `
  64. // MousetrapDisplayDuration controls how long the MousetrapHelpText message is displayed on Windows
  65. // if the CLI is started from explorer.exe. Set to 0 to wait for the return key to be pressed.
  66. // To disable the mousetrap, just set MousetrapHelpText to blank string ("").
  67. // Works only on Microsoft Windows.
  68. var MousetrapDisplayDuration = 5 * time.Second
  69. // AddTemplateFunc adds a template function that's available to Usage and Help
  70. // template generation.
  71. func AddTemplateFunc(name string, tmplFunc interface{}) {
  72. templateFuncs[name] = tmplFunc
  73. }
  74. // AddTemplateFuncs adds multiple template functions that are available to Usage and
  75. // Help template generation.
  76. func AddTemplateFuncs(tmplFuncs template.FuncMap) {
  77. for k, v := range tmplFuncs {
  78. templateFuncs[k] = v
  79. }
  80. }
  81. // OnInitialize sets the passed functions to be run when each command's
  82. // Execute method is called.
  83. func OnInitialize(y ...func()) {
  84. initializers = append(initializers, y...)
  85. }
  86. // OnFinalize sets the passed functions to be run when each command's
  87. // Execute method is terminated.
  88. func OnFinalize(y ...func()) {
  89. finalizers = append(finalizers, y...)
  90. }
  91. // FIXME Gt is unused by cobra and should be removed in a version 2. It exists only for compatibility with users of cobra.
  92. // Gt takes two types and checks whether the first type is greater than the second. In case of types Arrays, Chans,
  93. // Maps and Slices, Gt will compare their lengths. Ints are compared directly while strings are first parsed as
  94. // ints and then compared.
  95. func Gt(a interface{}, b interface{}) bool {
  96. var left, right int64
  97. av := reflect.ValueOf(a)
  98. switch av.Kind() {
  99. case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice:
  100. left = int64(av.Len())
  101. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  102. left = av.Int()
  103. case reflect.String:
  104. left, _ = strconv.ParseInt(av.String(), 10, 64)
  105. }
  106. bv := reflect.ValueOf(b)
  107. switch bv.Kind() {
  108. case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice:
  109. right = int64(bv.Len())
  110. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  111. right = bv.Int()
  112. case reflect.String:
  113. right, _ = strconv.ParseInt(bv.String(), 10, 64)
  114. }
  115. return left > right
  116. }
  117. // FIXME Eq is unused by cobra and should be removed in a version 2. It exists only for compatibility with users of cobra.
  118. // Eq takes two types and checks whether they are equal. Supported types are int and string. Unsupported types will panic.
  119. func Eq(a interface{}, b interface{}) bool {
  120. av := reflect.ValueOf(a)
  121. bv := reflect.ValueOf(b)
  122. switch av.Kind() {
  123. case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice:
  124. panic("Eq called on unsupported type")
  125. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  126. return av.Int() == bv.Int()
  127. case reflect.String:
  128. return av.String() == bv.String()
  129. }
  130. return false
  131. }
  132. func trimRightSpace(s string) string {
  133. return strings.TrimRightFunc(s, unicode.IsSpace)
  134. }
  135. // FIXME appendIfNotPresent is unused by cobra and should be removed in a version 2. It exists only for compatibility with users of cobra.
  136. // appendIfNotPresent will append stringToAppend to the end of s, but only if it's not yet present in s.
  137. func appendIfNotPresent(s, stringToAppend string) string {
  138. if strings.Contains(s, stringToAppend) {
  139. return s
  140. }
  141. return s + " " + stringToAppend
  142. }
  143. // rpad adds padding to the right of a string.
  144. func rpad(s string, padding int) string {
  145. formattedString := fmt.Sprintf("%%-%ds", padding)
  146. return fmt.Sprintf(formattedString, s)
  147. }
  148. // tmpl executes the given template text on data, writing the result to w.
  149. func tmpl(w io.Writer, text string, data interface{}) error {
  150. t := template.New("top")
  151. t.Funcs(templateFuncs)
  152. template.Must(t.Parse(text))
  153. return t.Execute(w, data)
  154. }
  155. // ld compares two strings and returns the levenshtein distance between them.
  156. func ld(s, t string, ignoreCase bool) int {
  157. if ignoreCase {
  158. s = strings.ToLower(s)
  159. t = strings.ToLower(t)
  160. }
  161. d := make([][]int, len(s)+1)
  162. for i := range d {
  163. d[i] = make([]int, len(t)+1)
  164. }
  165. for i := range d {
  166. d[i][0] = i
  167. }
  168. for j := range d[0] {
  169. d[0][j] = j
  170. }
  171. for j := 1; j <= len(t); j++ {
  172. for i := 1; i <= len(s); i++ {
  173. if s[i-1] == t[j-1] {
  174. d[i][j] = d[i-1][j-1]
  175. } else {
  176. min := d[i-1][j]
  177. if d[i][j-1] < min {
  178. min = d[i][j-1]
  179. }
  180. if d[i-1][j-1] < min {
  181. min = d[i-1][j-1]
  182. }
  183. d[i][j] = min + 1
  184. }
  185. }
  186. }
  187. return d[len(s)][len(t)]
  188. }
  189. func stringInSlice(a string, list []string) bool {
  190. for _, b := range list {
  191. if b == a {
  192. return true
  193. }
  194. }
  195. return false
  196. }
  197. // CheckErr prints the msg with the prefix 'Error:' and exits with error code 1. If the msg is nil, it does nothing.
  198. func CheckErr(msg interface{}) {
  199. if msg != nil {
  200. fmt.Fprintln(os.Stderr, "Error:", msg)
  201. os.Exit(1)
  202. }
  203. }
  204. // WriteStringAndCheck writes a string into a buffer, and checks if the error is not nil.
  205. func WriteStringAndCheck(b io.StringWriter, s string) {
  206. _, err := b.WriteString(s)
  207. CheckErr(err)
  208. }