vanity

vanity - A tiny server for golang vanity redirects
Log | Files | Refs | README | LICENSE

ip.go (3043B)


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