You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
3.0 KiB

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