OSDN Git Service

new repo
[bytom/vapor.git] / vendor / github.com / spf13 / cobra / cobra.go
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
14 // Commands similar to git, go tools and other modern CLI tools
15 // inspired by go, go-Commander, gh and subcommand
16
17 package cobra
18
19 import (
20         "fmt"
21         "io"
22         "reflect"
23         "strconv"
24         "strings"
25         "text/template"
26         "unicode"
27 )
28
29 var templateFuncs = template.FuncMap{
30         "trim":                    strings.TrimSpace,
31         "trimRightSpace":          trimRightSpace,
32         "trimTrailingWhitespaces": trimRightSpace,
33         "appendIfNotPresent":      appendIfNotPresent,
34         "rpad":                    rpad,
35         "gt":                      Gt,
36         "eq":                      Eq,
37 }
38
39 var initializers []func()
40
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
45
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
49
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.
55
56 You need to open cmd.exe and run it from there.
57 `
58
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
63 }
64
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 {
69                 templateFuncs[k] = v
70         }
71 }
72
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...)
76 }
77
78 // FIXME Gt is unused by cobra and should be removed in a version 2. It exists only for compatibility with users of cobra.
79
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 {
84         var left, right int64
85         av := reflect.ValueOf(a)
86
87         switch av.Kind() {
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:
91                 left = av.Int()
92         case reflect.String:
93                 left, _ = strconv.ParseInt(av.String(), 10, 64)
94         }
95
96         bv := reflect.ValueOf(b)
97
98         switch bv.Kind() {
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:
102                 right = bv.Int()
103         case reflect.String:
104                 right, _ = strconv.ParseInt(bv.String(), 10, 64)
105         }
106
107         return left > right
108 }
109
110 // FIXME Eq is unused by cobra and should be removed in a version 2. It exists only for compatibility with users of cobra.
111
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)
116
117         switch av.Kind() {
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()
122         case reflect.String:
123                 return av.String() == bv.String()
124         }
125         return false
126 }
127
128 func trimRightSpace(s string) string {
129         return strings.TrimRightFunc(s, unicode.IsSpace)
130 }
131
132 // FIXME appendIfNotPresent is unused by cobra and should be removed in a version 2. It exists only for compatibility with users of cobra.
133
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) {
137                 return s
138         }
139         return s + " " + stringToAppend
140 }
141
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)
146 }
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
156 // ld compares two strings and returns the levenshtein distance between them.
157 func ld(s, t string, ignoreCase bool) int {
158         if ignoreCase {
159                 s = strings.ToLower(s)
160                 t = strings.ToLower(t)
161         }
162         d := make([][]int, len(s)+1)
163         for i := range d {
164                 d[i] = make([]int, len(t)+1)
165         }
166         for i := range d {
167                 d[i][0] = i
168         }
169         for j := range d[0] {
170                 d[0][j] = j
171         }
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]
176                         } else {
177                                 min := d[i-1][j]
178                                 if d[i][j-1] < min {
179                                         min = d[i][j-1]
180                                 }
181                                 if d[i-1][j-1] < min {
182                                         min = d[i-1][j-1]
183                                 }
184                                 d[i][j] = min + 1
185                         }
186                 }
187
188         }
189         return d[len(s)][len(t)]
190 }