ipmask.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package pflag
  2. import (
  3. "fmt"
  4. "net"
  5. "strconv"
  6. )
  7. // -- net.IPMask value
  8. type ipMaskValue net.IPMask
  9. func newIPMaskValue(val net.IPMask, p *net.IPMask) *ipMaskValue {
  10. *p = val
  11. return (*ipMaskValue)(p)
  12. }
  13. func (i *ipMaskValue) String() string { return net.IPMask(*i).String() }
  14. func (i *ipMaskValue) Set(s string) error {
  15. ip := ParseIPv4Mask(s)
  16. if ip == nil {
  17. return fmt.Errorf("failed to parse IP mask: %q", s)
  18. }
  19. *i = ipMaskValue(ip)
  20. return nil
  21. }
  22. func (i *ipMaskValue) Type() string {
  23. return "ipMask"
  24. }
  25. // ParseIPv4Mask written in IP form (e.g. 255.255.255.0).
  26. // This function should really belong to the net package.
  27. func ParseIPv4Mask(s string) net.IPMask {
  28. mask := net.ParseIP(s)
  29. if mask == nil {
  30. if len(s) != 8 {
  31. return nil
  32. }
  33. // net.IPMask.String() actually outputs things like ffffff00
  34. // so write a horrible parser for that as well :-(
  35. m := []int{}
  36. for i := 0; i < 4; i++ {
  37. b := "0x" + s[2*i:2*i+2]
  38. d, err := strconv.ParseInt(b, 0, 0)
  39. if err != nil {
  40. return nil
  41. }
  42. m = append(m, int(d))
  43. }
  44. s := fmt.Sprintf("%d.%d.%d.%d", m[0], m[1], m[2], m[3])
  45. mask = net.ParseIP(s)
  46. if mask == nil {
  47. return nil
  48. }
  49. }
  50. return net.IPv4Mask(mask[12], mask[13], mask[14], mask[15])
  51. }
  52. func parseIPv4Mask(sval string) (interface{}, error) {
  53. mask := ParseIPv4Mask(sval)
  54. if mask == nil {
  55. return nil, fmt.Errorf("unable to parse %s as net.IPMask", sval)
  56. }
  57. return mask, nil
  58. }
  59. // GetIPv4Mask return the net.IPv4Mask value of a flag with the given name
  60. func (f *FlagSet) GetIPv4Mask(name string) (net.IPMask, error) {
  61. val, err := f.getFlagType(name, "ipMask", parseIPv4Mask)
  62. if err != nil {
  63. return nil, err
  64. }
  65. return val.(net.IPMask), nil
  66. }
  67. // IPMaskVar defines an net.IPMask flag with specified name, default value, and usage string.
  68. // The argument p points to an net.IPMask variable in which to store the value of the flag.
  69. func (f *FlagSet) IPMaskVar(p *net.IPMask, name string, value net.IPMask, usage string) {
  70. f.VarP(newIPMaskValue(value, p), name, "", usage)
  71. }
  72. // IPMaskVarP is like IPMaskVar, but accepts a shorthand letter that can be used after a single dash.
  73. func (f *FlagSet) IPMaskVarP(p *net.IPMask, name, shorthand string, value net.IPMask, usage string) {
  74. f.VarP(newIPMaskValue(value, p), name, shorthand, usage)
  75. }
  76. // IPMaskVar defines an net.IPMask flag with specified name, default value, and usage string.
  77. // The argument p points to an net.IPMask variable in which to store the value of the flag.
  78. func IPMaskVar(p *net.IPMask, name string, value net.IPMask, usage string) {
  79. CommandLine.VarP(newIPMaskValue(value, p), name, "", usage)
  80. }
  81. // IPMaskVarP is like IPMaskVar, but accepts a shorthand letter that can be used after a single dash.
  82. func IPMaskVarP(p *net.IPMask, name, shorthand string, value net.IPMask, usage string) {
  83. CommandLine.VarP(newIPMaskValue(value, p), name, shorthand, usage)
  84. }
  85. // IPMask defines an net.IPMask flag with specified name, default value, and usage string.
  86. // The return value is the address of an net.IPMask variable that stores the value of the flag.
  87. func (f *FlagSet) IPMask(name string, value net.IPMask, usage string) *net.IPMask {
  88. p := new(net.IPMask)
  89. f.IPMaskVarP(p, name, "", value, usage)
  90. return p
  91. }
  92. // IPMaskP is like IPMask, but accepts a shorthand letter that can be used after a single dash.
  93. func (f *FlagSet) IPMaskP(name, shorthand string, value net.IPMask, usage string) *net.IPMask {
  94. p := new(net.IPMask)
  95. f.IPMaskVarP(p, name, shorthand, value, usage)
  96. return p
  97. }
  98. // IPMask defines an net.IPMask flag with specified name, default value, and usage string.
  99. // The return value is the address of an net.IPMask variable that stores the value of the flag.
  100. func IPMask(name string, value net.IPMask, usage string) *net.IPMask {
  101. return CommandLine.IPMaskP(name, "", value, usage)
  102. }
  103. // IPMaskP is like IP, but accepts a shorthand letter that can be used after a single dash.
  104. func IPMaskP(name, shorthand string, value net.IPMask, usage string) *net.IPMask {
  105. return CommandLine.IPMaskP(name, shorthand, value, usage)
  106. }