10 // BytesHex adapts []byte for use as a flag. Value of flag is HEX encoded
11 type bytesHexValue []byte
13 // String implements pflag.Value.String.
14 func (bytesHex bytesHexValue) String() string {
15 return fmt.Sprintf("%X", []byte(bytesHex))
18 // Set implements pflag.Value.Set.
19 func (bytesHex *bytesHexValue) Set(value string) error {
20 bin, err := hex.DecodeString(strings.TrimSpace(value))
31 // Type implements pflag.Value.Type.
32 func (*bytesHexValue) Type() string {
36 func newBytesHexValue(val []byte, p *[]byte) *bytesHexValue {
38 return (*bytesHexValue)(p)
41 func bytesHexConv(sval string) (interface{}, error) {
43 bin, err := hex.DecodeString(sval)
49 return nil, fmt.Errorf("invalid string being converted to Bytes: %s %s", sval, err)
52 // GetBytesHex return the []byte value of a flag with the given name
53 func (f *FlagSet) GetBytesHex(name string) ([]byte, error) {
54 val, err := f.getFlagType(name, "bytesHex", bytesHexConv)
60 return val.([]byte), nil
63 // BytesHexVar defines an []byte flag with specified name, default value, and usage string.
64 // The argument p points to an []byte variable in which to store the value of the flag.
65 func (f *FlagSet) BytesHexVar(p *[]byte, name string, value []byte, usage string) {
66 f.VarP(newBytesHexValue(value, p), name, "", usage)
69 // BytesHexVarP is like BytesHexVar, but accepts a shorthand letter that can be used after a single dash.
70 func (f *FlagSet) BytesHexVarP(p *[]byte, name, shorthand string, value []byte, usage string) {
71 f.VarP(newBytesHexValue(value, p), name, shorthand, usage)
74 // BytesHexVar defines an []byte flag with specified name, default value, and usage string.
75 // The argument p points to an []byte variable in which to store the value of the flag.
76 func BytesHexVar(p *[]byte, name string, value []byte, usage string) {
77 CommandLine.VarP(newBytesHexValue(value, p), name, "", usage)
80 // BytesHexVarP is like BytesHexVar, but accepts a shorthand letter that can be used after a single dash.
81 func BytesHexVarP(p *[]byte, name, shorthand string, value []byte, usage string) {
82 CommandLine.VarP(newBytesHexValue(value, p), name, shorthand, usage)
85 // BytesHex defines an []byte flag with specified name, default value, and usage string.
86 // The return value is the address of an []byte variable that stores the value of the flag.
87 func (f *FlagSet) BytesHex(name string, value []byte, usage string) *[]byte {
89 f.BytesHexVarP(p, name, "", value, usage)
93 // BytesHexP is like BytesHex, but accepts a shorthand letter that can be used after a single dash.
94 func (f *FlagSet) BytesHexP(name, shorthand string, value []byte, usage string) *[]byte {
96 f.BytesHexVarP(p, name, shorthand, value, usage)
100 // BytesHex defines an []byte flag with specified name, default value, and usage string.
101 // The return value is the address of an []byte variable that stores the value of the flag.
102 func BytesHex(name string, value []byte, usage string) *[]byte {
103 return CommandLine.BytesHexP(name, "", value, usage)
106 // BytesHexP is like BytesHex, but accepts a shorthand letter that can be used after a single dash.
107 func BytesHexP(name, shorthand string, value []byte, usage string) *[]byte {
108 return CommandLine.BytesHexP(name, shorthand, value, usage)
111 // BytesBase64 adapts []byte for use as a flag. Value of flag is Base64 encoded
112 type bytesBase64Value []byte
114 // String implements pflag.Value.String.
115 func (bytesBase64 bytesBase64Value) String() string {
116 return base64.StdEncoding.EncodeToString([]byte(bytesBase64))
119 // Set implements pflag.Value.Set.
120 func (bytesBase64 *bytesBase64Value) Set(value string) error {
121 bin, err := base64.StdEncoding.DecodeString(strings.TrimSpace(value))
132 // Type implements pflag.Value.Type.
133 func (*bytesBase64Value) Type() string {
137 func newBytesBase64Value(val []byte, p *[]byte) *bytesBase64Value {
139 return (*bytesBase64Value)(p)
142 func bytesBase64ValueConv(sval string) (interface{}, error) {
144 bin, err := base64.StdEncoding.DecodeString(sval)
149 return nil, fmt.Errorf("invalid string being converted to Bytes: %s %s", sval, err)
152 // GetBytesBase64 return the []byte value of a flag with the given name
153 func (f *FlagSet) GetBytesBase64(name string) ([]byte, error) {
154 val, err := f.getFlagType(name, "bytesBase64", bytesBase64ValueConv)
160 return val.([]byte), nil
163 // BytesBase64Var defines an []byte flag with specified name, default value, and usage string.
164 // The argument p points to an []byte variable in which to store the value of the flag.
165 func (f *FlagSet) BytesBase64Var(p *[]byte, name string, value []byte, usage string) {
166 f.VarP(newBytesBase64Value(value, p), name, "", usage)
169 // BytesBase64VarP is like BytesBase64Var, but accepts a shorthand letter that can be used after a single dash.
170 func (f *FlagSet) BytesBase64VarP(p *[]byte, name, shorthand string, value []byte, usage string) {
171 f.VarP(newBytesBase64Value(value, p), name, shorthand, usage)
174 // BytesBase64Var defines an []byte flag with specified name, default value, and usage string.
175 // The argument p points to an []byte variable in which to store the value of the flag.
176 func BytesBase64Var(p *[]byte, name string, value []byte, usage string) {
177 CommandLine.VarP(newBytesBase64Value(value, p), name, "", usage)
180 // BytesBase64VarP is like BytesBase64Var, but accepts a shorthand letter that can be used after a single dash.
181 func BytesBase64VarP(p *[]byte, name, shorthand string, value []byte, usage string) {
182 CommandLine.VarP(newBytesBase64Value(value, p), name, shorthand, usage)
185 // BytesBase64 defines an []byte flag with specified name, default value, and usage string.
186 // The return value is the address of an []byte variable that stores the value of the flag.
187 func (f *FlagSet) BytesBase64(name string, value []byte, usage string) *[]byte {
189 f.BytesBase64VarP(p, name, "", value, usage)
193 // BytesBase64P is like BytesBase64, but accepts a shorthand letter that can be used after a single dash.
194 func (f *FlagSet) BytesBase64P(name, shorthand string, value []byte, usage string) *[]byte {
196 f.BytesBase64VarP(p, name, shorthand, value, usage)
200 // BytesBase64 defines an []byte flag with specified name, default value, and usage string.
201 // The return value is the address of an []byte variable that stores the value of the flag.
202 func BytesBase64(name string, value []byte, usage string) *[]byte {
203 return CommandLine.BytesBase64P(name, "", value, usage)
206 // BytesBase64P is like BytesBase64, but accepts a shorthand letter that can be used after a single dash.
207 func BytesBase64P(name, shorthand string, value []byte, usage string) *[]byte {
208 return CommandLine.BytesBase64P(name, shorthand, value, usage)