本站源代码
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

210 Zeilen
7.0KB

  1. package pflag
  2. import (
  3. "encoding/base64"
  4. "encoding/hex"
  5. "fmt"
  6. "strings"
  7. )
  8. // BytesHex adapts []byte for use as a flag. Value of flag is HEX encoded
  9. type bytesHexValue []byte
  10. // String implements pflag.Value.String.
  11. func (bytesHex bytesHexValue) String() string {
  12. return fmt.Sprintf("%X", []byte(bytesHex))
  13. }
  14. // Set implements pflag.Value.Set.
  15. func (bytesHex *bytesHexValue) Set(value string) error {
  16. bin, err := hex.DecodeString(strings.TrimSpace(value))
  17. if err != nil {
  18. return err
  19. }
  20. *bytesHex = bin
  21. return nil
  22. }
  23. // Type implements pflag.Value.Type.
  24. func (*bytesHexValue) Type() string {
  25. return "bytesHex"
  26. }
  27. func newBytesHexValue(val []byte, p *[]byte) *bytesHexValue {
  28. *p = val
  29. return (*bytesHexValue)(p)
  30. }
  31. func bytesHexConv(sval string) (interface{}, error) {
  32. bin, err := hex.DecodeString(sval)
  33. if err == nil {
  34. return bin, nil
  35. }
  36. return nil, fmt.Errorf("invalid string being converted to Bytes: %s %s", sval, err)
  37. }
  38. // GetBytesHex return the []byte value of a flag with the given name
  39. func (f *FlagSet) GetBytesHex(name string) ([]byte, error) {
  40. val, err := f.getFlagType(name, "bytesHex", bytesHexConv)
  41. if err != nil {
  42. return []byte{}, err
  43. }
  44. return val.([]byte), nil
  45. }
  46. // BytesHexVar defines an []byte flag with specified name, default value, and usage string.
  47. // The argument p points to an []byte variable in which to store the value of the flag.
  48. func (f *FlagSet) BytesHexVar(p *[]byte, name string, value []byte, usage string) {
  49. f.VarP(newBytesHexValue(value, p), name, "", usage)
  50. }
  51. // BytesHexVarP is like BytesHexVar, but accepts a shorthand letter that can be used after a single dash.
  52. func (f *FlagSet) BytesHexVarP(p *[]byte, name, shorthand string, value []byte, usage string) {
  53. f.VarP(newBytesHexValue(value, p), name, shorthand, usage)
  54. }
  55. // BytesHexVar defines an []byte flag with specified name, default value, and usage string.
  56. // The argument p points to an []byte variable in which to store the value of the flag.
  57. func BytesHexVar(p *[]byte, name string, value []byte, usage string) {
  58. CommandLine.VarP(newBytesHexValue(value, p), name, "", usage)
  59. }
  60. // BytesHexVarP is like BytesHexVar, but accepts a shorthand letter that can be used after a single dash.
  61. func BytesHexVarP(p *[]byte, name, shorthand string, value []byte, usage string) {
  62. CommandLine.VarP(newBytesHexValue(value, p), name, shorthand, usage)
  63. }
  64. // BytesHex defines an []byte flag with specified name, default value, and usage string.
  65. // The return value is the address of an []byte variable that stores the value of the flag.
  66. func (f *FlagSet) BytesHex(name string, value []byte, usage string) *[]byte {
  67. p := new([]byte)
  68. f.BytesHexVarP(p, name, "", value, usage)
  69. return p
  70. }
  71. // BytesHexP is like BytesHex, but accepts a shorthand letter that can be used after a single dash.
  72. func (f *FlagSet) BytesHexP(name, shorthand string, value []byte, usage string) *[]byte {
  73. p := new([]byte)
  74. f.BytesHexVarP(p, name, shorthand, value, usage)
  75. return p
  76. }
  77. // BytesHex defines an []byte flag with specified name, default value, and usage string.
  78. // The return value is the address of an []byte variable that stores the value of the flag.
  79. func BytesHex(name string, value []byte, usage string) *[]byte {
  80. return CommandLine.BytesHexP(name, "", value, usage)
  81. }
  82. // BytesHexP is like BytesHex, but accepts a shorthand letter that can be used after a single dash.
  83. func BytesHexP(name, shorthand string, value []byte, usage string) *[]byte {
  84. return CommandLine.BytesHexP(name, shorthand, value, usage)
  85. }
  86. // BytesBase64 adapts []byte for use as a flag. Value of flag is Base64 encoded
  87. type bytesBase64Value []byte
  88. // String implements pflag.Value.String.
  89. func (bytesBase64 bytesBase64Value) String() string {
  90. return base64.StdEncoding.EncodeToString([]byte(bytesBase64))
  91. }
  92. // Set implements pflag.Value.Set.
  93. func (bytesBase64 *bytesBase64Value) Set(value string) error {
  94. bin, err := base64.StdEncoding.DecodeString(strings.TrimSpace(value))
  95. if err != nil {
  96. return err
  97. }
  98. *bytesBase64 = bin
  99. return nil
  100. }
  101. // Type implements pflag.Value.Type.
  102. func (*bytesBase64Value) Type() string {
  103. return "bytesBase64"
  104. }
  105. func newBytesBase64Value(val []byte, p *[]byte) *bytesBase64Value {
  106. *p = val
  107. return (*bytesBase64Value)(p)
  108. }
  109. func bytesBase64ValueConv(sval string) (interface{}, error) {
  110. bin, err := base64.StdEncoding.DecodeString(sval)
  111. if err == nil {
  112. return bin, nil
  113. }
  114. return nil, fmt.Errorf("invalid string being converted to Bytes: %s %s", sval, err)
  115. }
  116. // GetBytesBase64 return the []byte value of a flag with the given name
  117. func (f *FlagSet) GetBytesBase64(name string) ([]byte, error) {
  118. val, err := f.getFlagType(name, "bytesBase64", bytesBase64ValueConv)
  119. if err != nil {
  120. return []byte{}, err
  121. }
  122. return val.([]byte), nil
  123. }
  124. // BytesBase64Var defines an []byte flag with specified name, default value, and usage string.
  125. // The argument p points to an []byte variable in which to store the value of the flag.
  126. func (f *FlagSet) BytesBase64Var(p *[]byte, name string, value []byte, usage string) {
  127. f.VarP(newBytesBase64Value(value, p), name, "", usage)
  128. }
  129. // BytesBase64VarP is like BytesBase64Var, but accepts a shorthand letter that can be used after a single dash.
  130. func (f *FlagSet) BytesBase64VarP(p *[]byte, name, shorthand string, value []byte, usage string) {
  131. f.VarP(newBytesBase64Value(value, p), name, shorthand, usage)
  132. }
  133. // BytesBase64Var defines an []byte flag with specified name, default value, and usage string.
  134. // The argument p points to an []byte variable in which to store the value of the flag.
  135. func BytesBase64Var(p *[]byte, name string, value []byte, usage string) {
  136. CommandLine.VarP(newBytesBase64Value(value, p), name, "", usage)
  137. }
  138. // BytesBase64VarP is like BytesBase64Var, but accepts a shorthand letter that can be used after a single dash.
  139. func BytesBase64VarP(p *[]byte, name, shorthand string, value []byte, usage string) {
  140. CommandLine.VarP(newBytesBase64Value(value, p), name, shorthand, usage)
  141. }
  142. // BytesBase64 defines an []byte flag with specified name, default value, and usage string.
  143. // The return value is the address of an []byte variable that stores the value of the flag.
  144. func (f *FlagSet) BytesBase64(name string, value []byte, usage string) *[]byte {
  145. p := new([]byte)
  146. f.BytesBase64VarP(p, name, "", value, usage)
  147. return p
  148. }
  149. // BytesBase64P is like BytesBase64, but accepts a shorthand letter that can be used after a single dash.
  150. func (f *FlagSet) BytesBase64P(name, shorthand string, value []byte, usage string) *[]byte {
  151. p := new([]byte)
  152. f.BytesBase64VarP(p, name, shorthand, value, usage)
  153. return p
  154. }
  155. // BytesBase64 defines an []byte flag with specified name, default value, and usage string.
  156. // The return value is the address of an []byte variable that stores the value of the flag.
  157. func BytesBase64(name string, value []byte, usage string) *[]byte {
  158. return CommandLine.BytesBase64P(name, "", value, usage)
  159. }
  160. // BytesBase64P is like BytesBase64, but accepts a shorthand letter that can be used after a single dash.
  161. func BytesBase64P(name, shorthand string, value []byte, usage string) *[]byte {
  162. return CommandLine.BytesBase64P(name, shorthand, value, usage)
  163. }
上海开阖软件有限公司 沪ICP备12045867号-1