9
// IPNet adapts net.IPNet for use as a flag.
10
type ipNetValue net.IPNet
12
func (ipnet ipNetValue) String() string {
17
func (ipnet *ipNetValue) Set(value string) error {
18
_, n, err := net.ParseCIDR(strings.TrimSpace(value))
22
*ipnet = ipNetValue(*n)
26
func (*ipNetValue) Type() string {
30
func newIPNetValue(val net.IPNet, p *net.IPNet) *ipNetValue {
32
return (*ipNetValue)(p)
35
func ipNetConv(sval string) (interface{}, error) {
36
_, n, err := net.ParseCIDR(strings.TrimSpace(sval))
40
return nil, fmt.Errorf("invalid string being converted to IPNet: %s", sval)
43
// GetIPNet return the net.IPNet value of a flag with the given name
44
func (f *FlagSet) GetIPNet(name string) (net.IPNet, error) {
45
val, err := f.getFlagType(name, "ipNet", ipNetConv)
47
return net.IPNet{}, err
49
return val.(net.IPNet), nil
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 (f *FlagSet) IPNetVar(p *net.IPNet, name string, value net.IPNet, usage string) {
55
f.VarP(newIPNetValue(value, p), name, "", usage)
58
// IPNetVarP is like IPNetVar, but accepts a shorthand letter that can be used after a single dash.
59
func (f *FlagSet) IPNetVarP(p *net.IPNet, name, shorthand string, value net.IPNet, usage string) {
60
f.VarP(newIPNetValue(value, p), name, shorthand, usage)
63
// IPNetVar defines an net.IPNet flag with specified name, default value, and usage string.
64
// The argument p points to an net.IPNet variable in which to store the value of the flag.
65
func IPNetVar(p *net.IPNet, name string, value net.IPNet, usage string) {
66
CommandLine.VarP(newIPNetValue(value, p), name, "", usage)
69
// IPNetVarP is like IPNetVar, but accepts a shorthand letter that can be used after a single dash.
70
func IPNetVarP(p *net.IPNet, name, shorthand string, value net.IPNet, usage string) {
71
CommandLine.VarP(newIPNetValue(value, p), name, shorthand, usage)
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 (f *FlagSet) IPNet(name string, value net.IPNet, usage string) *net.IPNet {
78
f.IPNetVarP(p, name, "", value, usage)
82
// IPNetP is like IPNet, but accepts a shorthand letter that can be used after a single dash.
83
func (f *FlagSet) IPNetP(name, shorthand string, value net.IPNet, usage string) *net.IPNet {
85
f.IPNetVarP(p, name, shorthand, value, usage)
89
// IPNet defines an net.IPNet flag with specified name, default value, and usage string.
90
// The return value is the address of an net.IPNet variable that stores the value of the flag.
91
func IPNet(name string, value net.IPNet, usage string) *net.IPNet {
92
return CommandLine.IPNetP(name, "", value, usage)
95
// IPNetP is like IPNet, but accepts a shorthand letter that can be used after a single dash.
96
func IPNetP(name, shorthand string, value net.IPNet, usage string) *net.IPNet {
97
return CommandLine.IPNetP(name, shorthand, value, usage)