ipnet.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. var _ = strings.TrimSpace
  25. func newIPNetValue(val net.IPNet, p *net.IPNet) *ipNetValue {
  26. *p = val
  27. return (*ipNetValue)(p)
  28. }
  29. func ipNetConv(sval string) (interface{}, error) {
  30. _, n, err := net.ParseCIDR(strings.TrimSpace(sval))
  31. if err == nil {
  32. return *n, nil
  33. }
  34. return nil, fmt.Errorf("invalid string being converted to IPNet: %s", sval)
  35. }
  36. // GetIPNet return the net.IPNet value of a flag with the given name
  37. func (f *FlagSet) GetIPNet(name string) (net.IPNet, error) {
  38. val, err := f.getFlagType(name, "ipNet", ipNetConv)
  39. if err != nil {
  40. return net.IPNet{}, err
  41. }
  42. return val.(net.IPNet), nil
  43. }
  44. // IPNetVar defines an net.IPNet flag with specified name, default value, and usage string.
  45. // The argument p points to an net.IPNet variable in which to store the value of the flag.
  46. func (f *FlagSet) IPNetVar(p *net.IPNet, name string, value net.IPNet, usage string) {
  47. f.VarP(newIPNetValue(value, p), name, "", usage)
  48. }
  49. // IPNetVarP is like IPNetVar, but accepts a shorthand letter that can be used after a single dash.
  50. func (f *FlagSet) IPNetVarP(p *net.IPNet, name, shorthand string, value net.IPNet, usage string) {
  51. f.VarP(newIPNetValue(value, p), name, shorthand, usage)
  52. }
  53. // IPNetVar defines an net.IPNet flag with specified name, default value, and usage string.
  54. // The argument p points to an net.IPNet variable in which to store the value of the flag.
  55. func IPNetVar(p *net.IPNet, name string, value net.IPNet, usage string) {
  56. CommandLine.VarP(newIPNetValue(value, p), name, "", usage)
  57. }
  58. // IPNetVarP is like IPNetVar, but accepts a shorthand letter that can be used after a single dash.
  59. func IPNetVarP(p *net.IPNet, name, shorthand string, value net.IPNet, usage string) {
  60. CommandLine.VarP(newIPNetValue(value, p), name, shorthand, usage)
  61. }
  62. // IPNet defines an net.IPNet flag with specified name, default value, and usage string.
  63. // The return value is the address of an net.IPNet variable that stores the value of the flag.
  64. func (f *FlagSet) IPNet(name string, value net.IPNet, usage string) *net.IPNet {
  65. p := new(net.IPNet)
  66. f.IPNetVarP(p, name, "", value, usage)
  67. return p
  68. }
  69. // IPNetP is like IPNet, but accepts a shorthand letter that can be used after a single dash.
  70. func (f *FlagSet) IPNetP(name, shorthand string, value net.IPNet, usage string) *net.IPNet {
  71. p := new(net.IPNet)
  72. f.IPNetVarP(p, name, shorthand, value, usage)
  73. return p
  74. }
  75. // IPNet defines an net.IPNet flag with specified name, default value, and usage string.
  76. // The return value is the address of an net.IPNet variable that stores the value of the flag.
  77. func IPNet(name string, value net.IPNet, usage string) *net.IPNet {
  78. return CommandLine.IPNetP(name, "", value, usage)
  79. }
  80. // IPNetP is like IPNet, but accepts a shorthand letter that can be used after a single dash.
  81. func IPNetP(name, shorthand string, value net.IPNet, usage string) *net.IPNet {
  82. return CommandLine.IPNetP(name, shorthand, value, usage)
  83. }