Matthias Andreas Benkard | 832a54e | 2019-01-29 09:27:38 +0100 | [diff] [blame^] | 1 | package pflag |
| 2 | |
| 3 | import ( |
| 4 | "encoding/hex" |
| 5 | "fmt" |
| 6 | "strings" |
| 7 | ) |
| 8 | |
| 9 | // BytesHex adapts []byte for use as a flag. Value of flag is HEX encoded |
| 10 | type bytesHexValue []byte |
| 11 | |
| 12 | func (bytesHex bytesHexValue) String() string { |
| 13 | return fmt.Sprintf("%X", []byte(bytesHex)) |
| 14 | } |
| 15 | |
| 16 | func (bytesHex *bytesHexValue) Set(value string) error { |
| 17 | bin, err := hex.DecodeString(strings.TrimSpace(value)) |
| 18 | |
| 19 | if err != nil { |
| 20 | return err |
| 21 | } |
| 22 | |
| 23 | *bytesHex = bin |
| 24 | |
| 25 | return nil |
| 26 | } |
| 27 | |
| 28 | func (*bytesHexValue) Type() string { |
| 29 | return "bytesHex" |
| 30 | } |
| 31 | |
| 32 | func newBytesHexValue(val []byte, p *[]byte) *bytesHexValue { |
| 33 | *p = val |
| 34 | return (*bytesHexValue)(p) |
| 35 | } |
| 36 | |
| 37 | func bytesHexConv(sval string) (interface{}, error) { |
| 38 | |
| 39 | bin, err := hex.DecodeString(sval) |
| 40 | |
| 41 | if err == nil { |
| 42 | return bin, nil |
| 43 | } |
| 44 | |
| 45 | return nil, fmt.Errorf("invalid string being converted to Bytes: %s %s", sval, err) |
| 46 | } |
| 47 | |
| 48 | // GetBytesHex return the []byte value of a flag with the given name |
| 49 | func (f *FlagSet) GetBytesHex(name string) ([]byte, error) { |
| 50 | val, err := f.getFlagType(name, "bytesHex", bytesHexConv) |
| 51 | |
| 52 | if err != nil { |
| 53 | return []byte{}, err |
| 54 | } |
| 55 | |
| 56 | return val.([]byte), nil |
| 57 | } |
| 58 | |
| 59 | // BytesHexVar defines an []byte flag with specified name, default value, and usage string. |
| 60 | // The argument p points to an []byte variable in which to store the value of the flag. |
| 61 | func (f *FlagSet) BytesHexVar(p *[]byte, name string, value []byte, usage string) { |
| 62 | f.VarP(newBytesHexValue(value, p), name, "", usage) |
| 63 | } |
| 64 | |
| 65 | // BytesHexVarP is like BytesHexVar, but accepts a shorthand letter that can be used after a single dash. |
| 66 | func (f *FlagSet) BytesHexVarP(p *[]byte, name, shorthand string, value []byte, usage string) { |
| 67 | f.VarP(newBytesHexValue(value, p), name, shorthand, usage) |
| 68 | } |
| 69 | |
| 70 | // BytesHexVar defines an []byte flag with specified name, default value, and usage string. |
| 71 | // The argument p points to an []byte variable in which to store the value of the flag. |
| 72 | func BytesHexVar(p *[]byte, name string, value []byte, usage string) { |
| 73 | CommandLine.VarP(newBytesHexValue(value, p), name, "", usage) |
| 74 | } |
| 75 | |
| 76 | // BytesHexVarP is like BytesHexVar, but accepts a shorthand letter that can be used after a single dash. |
| 77 | func BytesHexVarP(p *[]byte, name, shorthand string, value []byte, usage string) { |
| 78 | CommandLine.VarP(newBytesHexValue(value, p), name, shorthand, usage) |
| 79 | } |
| 80 | |
| 81 | // BytesHex defines an []byte flag with specified name, default value, and usage string. |
| 82 | // The return value is the address of an []byte variable that stores the value of the flag. |
| 83 | func (f *FlagSet) BytesHex(name string, value []byte, usage string) *[]byte { |
| 84 | p := new([]byte) |
| 85 | f.BytesHexVarP(p, name, "", value, usage) |
| 86 | return p |
| 87 | } |
| 88 | |
| 89 | // BytesHexP is like BytesHex, but accepts a shorthand letter that can be used after a single dash. |
| 90 | func (f *FlagSet) BytesHexP(name, shorthand string, value []byte, usage string) *[]byte { |
| 91 | p := new([]byte) |
| 92 | f.BytesHexVarP(p, name, shorthand, value, usage) |
| 93 | return p |
| 94 | } |
| 95 | |
| 96 | // BytesHex defines an []byte flag with specified name, default value, and usage string. |
| 97 | // The return value is the address of an []byte variable that stores the value of the flag. |
| 98 | func BytesHex(name string, value []byte, usage string) *[]byte { |
| 99 | return CommandLine.BytesHexP(name, "", value, usage) |
| 100 | } |
| 101 | |
| 102 | // BytesHexP is like BytesHex, but accepts a shorthand letter that can be used after a single dash. |
| 103 | func BytesHexP(name, shorthand string, value []byte, usage string) *[]byte { |
| 104 | return CommandLine.BytesHexP(name, shorthand, value, usage) |
| 105 | } |