Path: blob/main/vendor/github.com/spf13/pflag/text.go
2875 views
package pflag12import (3"encoding"4"fmt"5"reflect"6)78// following is copied from go 1.23.4 flag.go9type textValue struct{ p encoding.TextUnmarshaler }1011func newTextValue(val encoding.TextMarshaler, p encoding.TextUnmarshaler) textValue {12ptrVal := reflect.ValueOf(p)13if ptrVal.Kind() != reflect.Ptr {14panic("variable value type must be a pointer")15}16defVal := reflect.ValueOf(val)17if defVal.Kind() == reflect.Ptr {18defVal = defVal.Elem()19}20if defVal.Type() != ptrVal.Type().Elem() {21panic(fmt.Sprintf("default type does not match variable type: %v != %v", defVal.Type(), ptrVal.Type().Elem()))22}23ptrVal.Elem().Set(defVal)24return textValue{p}25}2627func (v textValue) Set(s string) error {28return v.p.UnmarshalText([]byte(s))29}3031func (v textValue) Get() interface{} {32return v.p33}3435func (v textValue) String() string {36if m, ok := v.p.(encoding.TextMarshaler); ok {37if b, err := m.MarshalText(); err == nil {38return string(b)39}40}41return ""42}4344//end of copy4546func (v textValue) Type() string {47return reflect.ValueOf(v.p).Type().Name()48}4950// GetText set out, which implements encoding.UnmarshalText, to the value of a flag with given name51func (f *FlagSet) GetText(name string, out encoding.TextUnmarshaler) error {52flag := f.Lookup(name)53if flag == nil {54return fmt.Errorf("flag accessed but not defined: %s", name)55}56if flag.Value.Type() != reflect.TypeOf(out).Name() {57return fmt.Errorf("trying to get %s value of flag of type %s", reflect.TypeOf(out).Name(), flag.Value.Type())58}59return out.UnmarshalText([]byte(flag.Value.String()))60}6162// TextVar defines a flag with a specified name, default value, and usage string. The argument p must be a pointer to a variable that will hold the value of the flag, and p must implement encoding.TextUnmarshaler. If the flag is used, the flag value will be passed to p's UnmarshalText method. The type of the default value must be the same as the type of p.63func (f *FlagSet) TextVar(p encoding.TextUnmarshaler, name string, value encoding.TextMarshaler, usage string) {64f.VarP(newTextValue(value, p), name, "", usage)65}6667// TextVarP is like TextVar, but accepts a shorthand letter that can be used after a single dash.68func (f *FlagSet) TextVarP(p encoding.TextUnmarshaler, name, shorthand string, value encoding.TextMarshaler, usage string) {69f.VarP(newTextValue(value, p), name, shorthand, usage)70}7172// TextVar defines a flag with a specified name, default value, and usage string. The argument p must be a pointer to a variable that will hold the value of the flag, and p must implement encoding.TextUnmarshaler. If the flag is used, the flag value will be passed to p's UnmarshalText method. The type of the default value must be the same as the type of p.73func TextVar(p encoding.TextUnmarshaler, name string, value encoding.TextMarshaler, usage string) {74CommandLine.VarP(newTextValue(value, p), name, "", usage)75}7677// TextVarP is like TextVar, but accepts a shorthand letter that can be used after a single dash.78func TextVarP(p encoding.TextUnmarshaler, name, shorthand string, value encoding.TextMarshaler, usage string) {79CommandLine.VarP(newTextValue(value, p), name, shorthand, usage)80}818283