2013-09-04 02:54:51 +04:00
|
|
|
// Copyright © 2013 Steve Francia <spf@spf13.com>.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
// Commands similar to git, go tools and other modern CLI tools
|
|
|
|
// inspired by go, go-Commander, gh and subcommand
|
|
|
|
|
|
|
|
package cobra
|
|
|
|
|
|
|
|
import (
|
2014-01-17 16:36:57 +04:00
|
|
|
"fmt"
|
2013-09-11 17:51:24 +04:00
|
|
|
"io"
|
2013-09-04 02:54:51 +04:00
|
|
|
"strings"
|
2013-09-12 18:32:51 +04:00
|
|
|
"text/template"
|
2015-09-12 01:44:03 +03:00
|
|
|
"unicode"
|
2013-09-04 02:54:51 +04:00
|
|
|
)
|
|
|
|
|
2016-03-31 16:53:34 +03:00
|
|
|
var templateFuncs = template.FuncMap{
|
2017-04-23 15:55:23 +03:00
|
|
|
"trimTrailingWhitespaces": trimTrailingWhitespaces,
|
|
|
|
"rpad": rpad,
|
2015-09-01 06:36:55 +03:00
|
|
|
}
|
|
|
|
|
2014-07-11 18:57:53 +04:00
|
|
|
var initializers []func()
|
2014-06-27 20:27:46 +04:00
|
|
|
|
2017-01-24 19:30:45 +03:00
|
|
|
// EnablePrefixMatching allows to set automatic prefix matching. Automatic prefix matching can be a dangerous thing
|
|
|
|
// to automatically enable in CLI tools.
|
2016-08-30 20:14:27 +03:00
|
|
|
// Set this to true to enable it.
|
2016-03-31 16:53:34 +03:00
|
|
|
var EnablePrefixMatching = false
|
2014-10-08 00:15:19 +04:00
|
|
|
|
2016-08-30 20:14:27 +03:00
|
|
|
// EnableCommandSorting controls sorting of the slice of commands, which is turned on by default.
|
|
|
|
// To disable sorting, set it to false.
|
2016-06-14 17:04:53 +03:00
|
|
|
var EnableCommandSorting = true
|
|
|
|
|
2016-08-30 20:14:27 +03:00
|
|
|
// AddTemplateFunc adds a template function that's available to Usage and Help
|
|
|
|
// template generation.
|
2015-09-01 06:36:55 +03:00
|
|
|
func AddTemplateFunc(name string, tmplFunc interface{}) {
|
|
|
|
templateFuncs[name] = tmplFunc
|
|
|
|
}
|
|
|
|
|
2017-04-17 20:03:07 +03:00
|
|
|
// AddTemplateFuncs adds multiple template functions that are available to Usage and
|
2016-08-30 20:14:27 +03:00
|
|
|
// Help template generation.
|
2015-09-01 06:36:55 +03:00
|
|
|
func AddTemplateFuncs(tmplFuncs template.FuncMap) {
|
|
|
|
for k, v := range tmplFuncs {
|
|
|
|
templateFuncs[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-30 20:14:27 +03:00
|
|
|
// OnInitialize takes a series of func() arguments and appends them to a slice of func().
|
2014-07-11 18:57:53 +04:00
|
|
|
func OnInitialize(y ...func()) {
|
2016-08-30 19:58:52 +03:00
|
|
|
initializers = append(initializers, y...)
|
2014-06-27 20:27:46 +04:00
|
|
|
}
|
|
|
|
|
2017-04-23 15:55:23 +03:00
|
|
|
func trimTrailingWhitespaces(s string) string {
|
2015-09-12 01:44:03 +03:00
|
|
|
return strings.TrimRightFunc(s, unicode.IsSpace)
|
|
|
|
}
|
|
|
|
|
2016-08-30 20:14:27 +03:00
|
|
|
// rpad adds padding to the right of a string.
|
2014-01-17 16:36:57 +04:00
|
|
|
func rpad(s string, padding int) string {
|
|
|
|
template := fmt.Sprintf("%%-%ds", padding)
|
|
|
|
return fmt.Sprintf(template, s)
|
|
|
|
}
|
|
|
|
|
2013-09-12 18:32:51 +04:00
|
|
|
// tmpl executes the given template text on data, writing the result to w.
|
|
|
|
func tmpl(w io.Writer, text string, data interface{}) error {
|
2017-04-23 15:55:23 +03:00
|
|
|
t := template.New("top").Funcs(templateFuncs)
|
|
|
|
return template.Must(t.Parse(text)).Execute(w, data)
|
2013-09-12 18:32:51 +04:00
|
|
|
}
|
2015-09-11 23:04:58 +03:00
|
|
|
|
2016-08-30 20:14:27 +03:00
|
|
|
// ld compares two strings and returns the levenshtein distance between them.
|
2015-09-11 23:04:58 +03:00
|
|
|
func ld(s, t string, ignoreCase bool) int {
|
|
|
|
if ignoreCase {
|
|
|
|
s = strings.ToLower(s)
|
|
|
|
t = strings.ToLower(t)
|
|
|
|
}
|
|
|
|
d := make([][]int, len(s)+1)
|
|
|
|
for i := range d {
|
|
|
|
d[i] = make([]int, len(t)+1)
|
|
|
|
}
|
|
|
|
for i := range d {
|
|
|
|
d[i][0] = i
|
|
|
|
}
|
|
|
|
for j := range d[0] {
|
|
|
|
d[0][j] = j
|
|
|
|
}
|
|
|
|
for j := 1; j <= len(t); j++ {
|
|
|
|
for i := 1; i <= len(s); i++ {
|
|
|
|
if s[i-1] == t[j-1] {
|
|
|
|
d[i][j] = d[i-1][j-1]
|
|
|
|
} else {
|
|
|
|
min := d[i-1][j]
|
|
|
|
if d[i][j-1] < min {
|
|
|
|
min = d[i][j-1]
|
|
|
|
}
|
|
|
|
if d[i-1][j-1] < min {
|
|
|
|
min = d[i-1][j-1]
|
|
|
|
}
|
|
|
|
d[i][j] = min + 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return d[len(s)][len(t)]
|
|
|
|
}
|