ipnet.go 3.3 KB

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