bytes.go 3.3 KB

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