ip.go 3.0 KB

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