khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 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 | func newIPNetValue(val net.IPNet, p *net.IPNet) *ipNetValue { |
| 31 | *p = val |
| 32 | return (*ipNetValue)(p) |
| 33 | } |
| 34 | |
| 35 | func ipNetConv(sval string) (interface{}, error) { |
| 36 | _, n, err := net.ParseCIDR(strings.TrimSpace(sval)) |
| 37 | if err == nil { |
| 38 | return *n, nil |
| 39 | } |
| 40 | return nil, fmt.Errorf("invalid string being converted to IPNet: %s", sval) |
| 41 | } |
| 42 | |
| 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) |
| 46 | if err != nil { |
| 47 | return net.IPNet{}, err |
| 48 | } |
| 49 | return val.(net.IPNet), nil |
| 50 | } |
| 51 | |
| 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) |
| 56 | } |
| 57 | |
| 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) |
| 61 | } |
| 62 | |
| 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) |
| 67 | } |
| 68 | |
| 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) |
| 72 | } |
| 73 | |
| 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 { |
| 77 | p := new(net.IPNet) |
| 78 | f.IPNetVarP(p, name, "", value, usage) |
| 79 | return p |
| 80 | } |
| 81 | |
| 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 { |
| 84 | p := new(net.IPNet) |
| 85 | f.IPNetVarP(p, name, shorthand, value, usage) |
| 86 | return p |
| 87 | } |
| 88 | |
| 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) |
| 93 | } |
| 94 | |
| 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) |
| 98 | } |