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