Path: blob/main/vendor/github.com/spf13/pflag/ip.go
2875 views
package pflag12import (3"fmt"4"net"5"strings"6)78// -- net.IP value9type ipValue net.IP1011func newIPValue(val net.IP, p *net.IP) *ipValue {12*p = val13return (*ipValue)(p)14}1516func (i *ipValue) String() string { return net.IP(*i).String() }17func (i *ipValue) Set(s string) error {18if s == "" {19return nil20}21ip := net.ParseIP(strings.TrimSpace(s))22if ip == nil {23return fmt.Errorf("failed to parse IP: %q", s)24}25*i = ipValue(ip)26return nil27}2829func (i *ipValue) Type() string {30return "ip"31}3233func ipConv(sval string) (interface{}, error) {34ip := net.ParseIP(sval)35if ip != nil {36return ip, nil37}38return nil, fmt.Errorf("invalid string being converted to IP address: %s", sval)39}4041// GetIP return the net.IP value of a flag with the given name42func (f *FlagSet) GetIP(name string) (net.IP, error) {43val, err := f.getFlagType(name, "ip", ipConv)44if err != nil {45return nil, err46}47return val.(net.IP), nil48}4950// 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.52func (f *FlagSet) IPVar(p *net.IP, name string, value net.IP, usage string) {53f.VarP(newIPValue(value, p), name, "", usage)54}5556// IPVarP is like IPVar, but accepts a shorthand letter that can be used after a single dash.57func (f *FlagSet) IPVarP(p *net.IP, name, shorthand string, value net.IP, usage string) {58f.VarP(newIPValue(value, p), name, shorthand, usage)59}6061// IPVar defines an net.IP flag with specified name, default value, and usage string.62// The argument p points to an net.IP variable in which to store the value of the flag.63func IPVar(p *net.IP, name string, value net.IP, usage string) {64CommandLine.VarP(newIPValue(value, p), name, "", usage)65}6667// IPVarP is like IPVar, but accepts a shorthand letter that can be used after a single dash.68func IPVarP(p *net.IP, name, shorthand string, value net.IP, usage string) {69CommandLine.VarP(newIPValue(value, p), name, shorthand, usage)70}7172// 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.74func (f *FlagSet) IP(name string, value net.IP, usage string) *net.IP {75p := new(net.IP)76f.IPVarP(p, name, "", value, usage)77return p78}7980// IPP is like IP, but accepts a shorthand letter that can be used after a single dash.81func (f *FlagSet) IPP(name, shorthand string, value net.IP, usage string) *net.IP {82p := new(net.IP)83f.IPVarP(p, name, shorthand, value, usage)84return p85}8687// IP defines an net.IP flag with specified name, default value, and usage string.88// The return value is the address of an net.IP variable that stores the value of the flag.89func IP(name string, value net.IP, usage string) *net.IP {90return CommandLine.IPP(name, "", value, usage)91}9293// IPP is like IP, but accepts a shorthand letter that can be used after a single dash.94func IPP(name, shorthand string, value net.IP, usage string) *net.IP {95return CommandLine.IPP(name, shorthand, value, usage)96}979899