Path: blob/main/vendor/github.com/spf13/pflag/bytes.go
2875 views
package pflag12import (3"encoding/base64"4"encoding/hex"5"fmt"6"strings"7)89// BytesHex adapts []byte for use as a flag. Value of flag is HEX encoded10type bytesHexValue []byte1112// String implements pflag.Value.String.13func (bytesHex bytesHexValue) String() string {14return fmt.Sprintf("%X", []byte(bytesHex))15}1617// Set implements pflag.Value.Set.18func (bytesHex *bytesHexValue) Set(value string) error {19bin, err := hex.DecodeString(strings.TrimSpace(value))2021if err != nil {22return err23}2425*bytesHex = bin2627return nil28}2930// Type implements pflag.Value.Type.31func (*bytesHexValue) Type() string {32return "bytesHex"33}3435func newBytesHexValue(val []byte, p *[]byte) *bytesHexValue {36*p = val37return (*bytesHexValue)(p)38}3940func bytesHexConv(sval string) (interface{}, error) {4142bin, err := hex.DecodeString(sval)4344if err == nil {45return bin, nil46}4748return nil, fmt.Errorf("invalid string being converted to Bytes: %s %s", sval, err)49}5051// GetBytesHex return the []byte value of a flag with the given name52func (f *FlagSet) GetBytesHex(name string) ([]byte, error) {53val, err := f.getFlagType(name, "bytesHex", bytesHexConv)5455if err != nil {56return []byte{}, err57}5859return val.([]byte), nil60}6162// BytesHexVar defines an []byte flag with specified name, default value, and usage string.63// The argument p points to an []byte variable in which to store the value of the flag.64func (f *FlagSet) BytesHexVar(p *[]byte, name string, value []byte, usage string) {65f.VarP(newBytesHexValue(value, p), name, "", usage)66}6768// BytesHexVarP is like BytesHexVar, but accepts a shorthand letter that can be used after a single dash.69func (f *FlagSet) BytesHexVarP(p *[]byte, name, shorthand string, value []byte, usage string) {70f.VarP(newBytesHexValue(value, p), name, shorthand, usage)71}7273// BytesHexVar defines an []byte flag with specified name, default value, and usage string.74// The argument p points to an []byte variable in which to store the value of the flag.75func BytesHexVar(p *[]byte, name string, value []byte, usage string) {76CommandLine.VarP(newBytesHexValue(value, p), name, "", usage)77}7879// BytesHexVarP is like BytesHexVar, but accepts a shorthand letter that can be used after a single dash.80func BytesHexVarP(p *[]byte, name, shorthand string, value []byte, usage string) {81CommandLine.VarP(newBytesHexValue(value, p), name, shorthand, usage)82}8384// BytesHex defines an []byte flag with specified name, default value, and usage string.85// The return value is the address of an []byte variable that stores the value of the flag.86func (f *FlagSet) BytesHex(name string, value []byte, usage string) *[]byte {87p := new([]byte)88f.BytesHexVarP(p, name, "", value, usage)89return p90}9192// BytesHexP is like BytesHex, but accepts a shorthand letter that can be used after a single dash.93func (f *FlagSet) BytesHexP(name, shorthand string, value []byte, usage string) *[]byte {94p := new([]byte)95f.BytesHexVarP(p, name, shorthand, value, usage)96return p97}9899// BytesHex defines an []byte flag with specified name, default value, and usage string.100// The return value is the address of an []byte variable that stores the value of the flag.101func BytesHex(name string, value []byte, usage string) *[]byte {102return CommandLine.BytesHexP(name, "", value, usage)103}104105// BytesHexP is like BytesHex, but accepts a shorthand letter that can be used after a single dash.106func BytesHexP(name, shorthand string, value []byte, usage string) *[]byte {107return CommandLine.BytesHexP(name, shorthand, value, usage)108}109110// BytesBase64 adapts []byte for use as a flag. Value of flag is Base64 encoded111type bytesBase64Value []byte112113// String implements pflag.Value.String.114func (bytesBase64 bytesBase64Value) String() string {115return base64.StdEncoding.EncodeToString([]byte(bytesBase64))116}117118// Set implements pflag.Value.Set.119func (bytesBase64 *bytesBase64Value) Set(value string) error {120bin, err := base64.StdEncoding.DecodeString(strings.TrimSpace(value))121122if err != nil {123return err124}125126*bytesBase64 = bin127128return nil129}130131// Type implements pflag.Value.Type.132func (*bytesBase64Value) Type() string {133return "bytesBase64"134}135136func newBytesBase64Value(val []byte, p *[]byte) *bytesBase64Value {137*p = val138return (*bytesBase64Value)(p)139}140141func bytesBase64ValueConv(sval string) (interface{}, error) {142143bin, err := base64.StdEncoding.DecodeString(sval)144if err == nil {145return bin, nil146}147148return nil, fmt.Errorf("invalid string being converted to Bytes: %s %s", sval, err)149}150151// GetBytesBase64 return the []byte value of a flag with the given name152func (f *FlagSet) GetBytesBase64(name string) ([]byte, error) {153val, err := f.getFlagType(name, "bytesBase64", bytesBase64ValueConv)154155if err != nil {156return []byte{}, err157}158159return val.([]byte), nil160}161162// BytesBase64Var defines an []byte flag with specified name, default value, and usage string.163// The argument p points to an []byte variable in which to store the value of the flag.164func (f *FlagSet) BytesBase64Var(p *[]byte, name string, value []byte, usage string) {165f.VarP(newBytesBase64Value(value, p), name, "", usage)166}167168// BytesBase64VarP is like BytesBase64Var, but accepts a shorthand letter that can be used after a single dash.169func (f *FlagSet) BytesBase64VarP(p *[]byte, name, shorthand string, value []byte, usage string) {170f.VarP(newBytesBase64Value(value, p), name, shorthand, usage)171}172173// BytesBase64Var defines an []byte flag with specified name, default value, and usage string.174// The argument p points to an []byte variable in which to store the value of the flag.175func BytesBase64Var(p *[]byte, name string, value []byte, usage string) {176CommandLine.VarP(newBytesBase64Value(value, p), name, "", usage)177}178179// BytesBase64VarP is like BytesBase64Var, but accepts a shorthand letter that can be used after a single dash.180func BytesBase64VarP(p *[]byte, name, shorthand string, value []byte, usage string) {181CommandLine.VarP(newBytesBase64Value(value, p), name, shorthand, usage)182}183184// BytesBase64 defines an []byte flag with specified name, default value, and usage string.185// The return value is the address of an []byte variable that stores the value of the flag.186func (f *FlagSet) BytesBase64(name string, value []byte, usage string) *[]byte {187p := new([]byte)188f.BytesBase64VarP(p, name, "", value, usage)189return p190}191192// BytesBase64P is like BytesBase64, but accepts a shorthand letter that can be used after a single dash.193func (f *FlagSet) BytesBase64P(name, shorthand string, value []byte, usage string) *[]byte {194p := new([]byte)195f.BytesBase64VarP(p, name, shorthand, value, usage)196return p197}198199// BytesBase64 defines an []byte flag with specified name, default value, and usage string.200// The return value is the address of an []byte variable that stores the value of the flag.201func BytesBase64(name string, value []byte, usage string) *[]byte {202return CommandLine.BytesBase64P(name, "", value, usage)203}204205// BytesBase64P is like BytesBase64, but accepts a shorthand letter that can be used after a single dash.206func BytesBase64P(name, shorthand string, value []byte, usage string) *[]byte {207return CommandLine.BytesBase64P(name, shorthand, value, usage)208}209210211