Path: blob/main/vendor/github.com/spf13/cobra/cobra.go
2875 views
// Copyright 2013-2023 The Cobra Authors1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// 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 and12// limitations under the License.1314// Commands similar to git, go tools and other modern CLI tools15// inspired by go, go-Commander, gh and subcommand1617package cobra1819import (20"fmt"21"io"22"os"23"reflect"24"strconv"25"strings"26"text/template"27"time"28"unicode"29)3031var templateFuncs = template.FuncMap{32"trim": strings.TrimSpace,33"trimRightSpace": trimRightSpace,34"trimTrailingWhitespaces": trimRightSpace,35"appendIfNotPresent": appendIfNotPresent,36"rpad": rpad,37"gt": Gt,38"eq": Eq,39}4041var initializers []func()42var finalizers []func()4344const (45defaultPrefixMatching = false46defaultCommandSorting = true47defaultCaseInsensitive = false48defaultTraverseRunHooks = false49)5051// EnablePrefixMatching allows setting automatic prefix matching. Automatic prefix matching can be a dangerous thing52// to automatically enable in CLI tools.53// Set this to true to enable it.54var EnablePrefixMatching = defaultPrefixMatching5556// EnableCommandSorting controls sorting of the slice of commands, which is turned on by default.57// To disable sorting, set it to false.58var EnableCommandSorting = defaultCommandSorting5960// EnableCaseInsensitive allows case-insensitive commands names. (case sensitive by default)61var EnableCaseInsensitive = defaultCaseInsensitive6263// EnableTraverseRunHooks executes persistent pre-run and post-run hooks from all parents.64// By default this is disabled, which means only the first run hook to be found is executed.65var EnableTraverseRunHooks = defaultTraverseRunHooks6667// MousetrapHelpText enables an information splash screen on Windows68// if the CLI is started from explorer.exe.69// To disable the mousetrap, just set this variable to blank string ("").70// Works only on Microsoft Windows.71var MousetrapHelpText = `This is a command line tool.7273You need to open cmd.exe and run it from there.74`7576// MousetrapDisplayDuration controls how long the MousetrapHelpText message is displayed on Windows77// if the CLI is started from explorer.exe. Set to 0 to wait for the return key to be pressed.78// To disable the mousetrap, just set MousetrapHelpText to blank string ("").79// Works only on Microsoft Windows.80var MousetrapDisplayDuration = 5 * time.Second8182// AddTemplateFunc adds a template function that's available to Usage and Help83// template generation.84func AddTemplateFunc(name string, tmplFunc interface{}) {85templateFuncs[name] = tmplFunc86}8788// AddTemplateFuncs adds multiple template functions that are available to Usage and89// Help template generation.90func AddTemplateFuncs(tmplFuncs template.FuncMap) {91for k, v := range tmplFuncs {92templateFuncs[k] = v93}94}9596// OnInitialize sets the passed functions to be run when each command's97// Execute method is called.98func OnInitialize(y ...func()) {99initializers = append(initializers, y...)100}101102// OnFinalize sets the passed functions to be run when each command's103// Execute method is terminated.104func OnFinalize(y ...func()) {105finalizers = append(finalizers, y...)106}107108// FIXME Gt is unused by cobra and should be removed in a version 2. It exists only for compatibility with users of cobra.109110// Gt takes two types and checks whether the first type is greater than the second. In case of types Arrays, Chans,111// Maps and Slices, Gt will compare their lengths. Ints are compared directly while strings are first parsed as112// ints and then compared.113func Gt(a interface{}, b interface{}) bool {114var left, right int64115av := reflect.ValueOf(a)116117switch av.Kind() {118case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice:119left = int64(av.Len())120case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:121left = av.Int()122case reflect.String:123left, _ = strconv.ParseInt(av.String(), 10, 64)124}125126bv := reflect.ValueOf(b)127128switch bv.Kind() {129case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice:130right = int64(bv.Len())131case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:132right = bv.Int()133case reflect.String:134right, _ = strconv.ParseInt(bv.String(), 10, 64)135}136137return left > right138}139140// FIXME Eq is unused by cobra and should be removed in a version 2. It exists only for compatibility with users of cobra.141142// Eq takes two types and checks whether they are equal. Supported types are int and string. Unsupported types will panic.143func Eq(a interface{}, b interface{}) bool {144av := reflect.ValueOf(a)145bv := reflect.ValueOf(b)146147switch av.Kind() {148case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice:149panic("Eq called on unsupported type")150case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:151return av.Int() == bv.Int()152case reflect.String:153return av.String() == bv.String()154}155return false156}157158func trimRightSpace(s string) string {159return strings.TrimRightFunc(s, unicode.IsSpace)160}161162// FIXME appendIfNotPresent is unused by cobra and should be removed in a version 2. It exists only for compatibility with users of cobra.163164// appendIfNotPresent will append stringToAppend to the end of s, but only if it's not yet present in s.165func appendIfNotPresent(s, stringToAppend string) string {166if strings.Contains(s, stringToAppend) {167return s168}169return s + " " + stringToAppend170}171172// rpad adds padding to the right of a string.173func rpad(s string, padding int) string {174formattedString := fmt.Sprintf("%%-%ds", padding)175return fmt.Sprintf(formattedString, s)176}177178func tmpl(text string) *tmplFunc {179return &tmplFunc{180tmpl: text,181fn: func(w io.Writer, data interface{}) error {182t := template.New("top")183t.Funcs(templateFuncs)184template.Must(t.Parse(text))185return t.Execute(w, data)186},187}188}189190// ld compares two strings and returns the levenshtein distance between them.191func ld(s, t string, ignoreCase bool) int {192if ignoreCase {193s = strings.ToLower(s)194t = strings.ToLower(t)195}196d := make([][]int, len(s)+1)197for i := range d {198d[i] = make([]int, len(t)+1)199d[i][0] = i200}201for j := range d[0] {202d[0][j] = j203}204for j := 1; j <= len(t); j++ {205for i := 1; i <= len(s); i++ {206if s[i-1] == t[j-1] {207d[i][j] = d[i-1][j-1]208} else {209min := d[i-1][j]210if d[i][j-1] < min {211min = d[i][j-1]212}213if d[i-1][j-1] < min {214min = d[i-1][j-1]215}216d[i][j] = min + 1217}218}219220}221return d[len(s)][len(t)]222}223224func stringInSlice(a string, list []string) bool {225for _, b := range list {226if b == a {227return true228}229}230return false231}232233// CheckErr prints the msg with the prefix 'Error:' and exits with error code 1. If the msg is nil, it does nothing.234func CheckErr(msg interface{}) {235if msg != nil {236fmt.Fprintln(os.Stderr, "Error:", msg)237os.Exit(1)238}239}240241// WriteStringAndCheck writes a string into a buffer, and checks if the error is not nil.242func WriteStringAndCheck(b io.StringWriter, s string) {243_, err := b.WriteString(s)244CheckErr(err)245}246247248