1 // Copyright © 2013 Steve Francia <spf@spf13.com>.
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
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.
14 // Commands similar to git, go tools and other modern CLI tools
15 // inspired by go, go-Commander, gh and subcommand
29 var templateFuncs = template.FuncMap{
30 "trim": strings.TrimSpace,
31 "trimRightSpace": trimRightSpace,
32 "trimTrailingWhitespaces": trimRightSpace,
33 "appendIfNotPresent": appendIfNotPresent,
39 var initializers []func()
41 // EnablePrefixMatching allows to set automatic prefix matching. Automatic prefix matching can be a dangerous thing
42 // to automatically enable in CLI tools.
43 // Set this to true to enable it.
44 var EnablePrefixMatching = false
46 // EnableCommandSorting controls sorting of the slice of commands, which is turned on by default.
47 // To disable sorting, set it to false.
48 var EnableCommandSorting = true
50 // MousetrapHelpText enables an information splash screen on Windows
51 // if the CLI is started from explorer.exe.
52 // To disable the mousetrap, just set this variable to blank string ("").
53 // Works only on Microsoft Windows.
54 var MousetrapHelpText string = `This is a command line tool.
56 You need to open cmd.exe and run it from there.
59 // AddTemplateFunc adds a template function that's available to Usage and Help
60 // template generation.
61 func AddTemplateFunc(name string, tmplFunc interface{}) {
62 templateFuncs[name] = tmplFunc
65 // AddTemplateFuncs adds multiple template functions that are available to Usage and
66 // Help template generation.
67 func AddTemplateFuncs(tmplFuncs template.FuncMap) {
68 for k, v := range tmplFuncs {
73 // OnInitialize takes a series of func() arguments and appends them to a slice of func().
74 func OnInitialize(y ...func()) {
75 initializers = append(initializers, y...)
78 // FIXME Gt is unused by cobra and should be removed in a version 2. It exists only for compatibility with users of cobra.
80 // Gt takes two types and checks whether the first type is greater than the second. In case of types Arrays, Chans,
81 // Maps and Slices, Gt will compare their lengths. Ints are compared directly while strings are first parsed as
82 // ints and then compared.
83 func Gt(a interface{}, b interface{}) bool {
85 av := reflect.ValueOf(a)
88 case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice:
89 left = int64(av.Len())
90 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
93 left, _ = strconv.ParseInt(av.String(), 10, 64)
96 bv := reflect.ValueOf(b)
99 case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice:
100 right = int64(bv.Len())
101 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
104 right, _ = strconv.ParseInt(bv.String(), 10, 64)
110 // FIXME Eq is unused by cobra and should be removed in a version 2. It exists only for compatibility with users of cobra.
112 // Eq takes two types and checks whether they are equal. Supported types are int and string. Unsupported types will panic.
113 func Eq(a interface{}, b interface{}) bool {
114 av := reflect.ValueOf(a)
115 bv := reflect.ValueOf(b)
118 case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice:
119 panic("Eq called on unsupported type")
120 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
121 return av.Int() == bv.Int()
123 return av.String() == bv.String()
128 func trimRightSpace(s string) string {
129 return strings.TrimRightFunc(s, unicode.IsSpace)
132 // FIXME appendIfNotPresent is unused by cobra and should be removed in a version 2. It exists only for compatibility with users of cobra.
134 // appendIfNotPresent will append stringToAppend to the end of s, but only if it's not yet present in s.
135 func appendIfNotPresent(s, stringToAppend string) string {
136 if strings.Contains(s, stringToAppend) {
139 return s + " " + stringToAppend
142 // rpad adds padding to the right of a string.
143 func rpad(s string, padding int) string {
144 template := fmt.Sprintf("%%-%ds", padding)
145 return fmt.Sprintf(template, s)
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)
156 // ld compares two strings and returns the levenshtein distance between them.
157 func ld(s, t string, ignoreCase bool) int {
159 s = strings.ToLower(s)
160 t = strings.ToLower(t)
162 d := make([][]int, len(s)+1)
164 d[i] = make([]int, len(t)+1)
169 for j := range d[0] {
172 for j := 1; j <= len(t); j++ {
173 for i := 1; i <= len(s); i++ {
174 if s[i-1] == t[j-1] {
175 d[i][j] = d[i-1][j-1]
181 if d[i-1][j-1] < min {
189 return d[len(s)][len(t)]