Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
kardolus
GitHub Repository: kardolus/chatgpt-cli
Path: blob/main/vendor/github.com/spf13/pflag/ip.go
2875 views
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
if s == "" {
20
return nil
21
}
22
ip := net.ParseIP(strings.TrimSpace(s))
23
if ip == nil {
24
return fmt.Errorf("failed to parse IP: %q", s)
25
}
26
*i = ipValue(ip)
27
return nil
28
}
29
30
func (i *ipValue) Type() string {
31
return "ip"
32
}
33
34
func ipConv(sval string) (interface{}, error) {
35
ip := net.ParseIP(sval)
36
if ip != nil {
37
return ip, nil
38
}
39
return nil, fmt.Errorf("invalid string being converted to IP address: %s", sval)
40
}
41
42
// GetIP return the net.IP value of a flag with the given name
43
func (f *FlagSet) GetIP(name string) (net.IP, error) {
44
val, err := f.getFlagType(name, "ip", ipConv)
45
if err != nil {
46
return nil, err
47
}
48
return val.(net.IP), nil
49
}
50
51
// IPVar defines an net.IP flag with specified name, default value, and usage string.
52
// The argument p points to an net.IP variable in which to store the value of the flag.
53
func (f *FlagSet) IPVar(p *net.IP, name string, value net.IP, usage string) {
54
f.VarP(newIPValue(value, p), name, "", usage)
55
}
56
57
// IPVarP is like IPVar, but accepts a shorthand letter that can be used after a single dash.
58
func (f *FlagSet) IPVarP(p *net.IP, name, shorthand string, value net.IP, usage string) {
59
f.VarP(newIPValue(value, p), name, shorthand, usage)
60
}
61
62
// IPVar defines an net.IP flag with specified name, default value, and usage string.
63
// The argument p points to an net.IP variable in which to store the value of the flag.
64
func IPVar(p *net.IP, name string, value net.IP, usage string) {
65
CommandLine.VarP(newIPValue(value, p), name, "", usage)
66
}
67
68
// IPVarP is like IPVar, but accepts a shorthand letter that can be used after a single dash.
69
func IPVarP(p *net.IP, name, shorthand string, value net.IP, usage string) {
70
CommandLine.VarP(newIPValue(value, p), name, shorthand, usage)
71
}
72
73
// IP defines an net.IP flag with specified name, default value, and usage string.
74
// The return value is the address of an net.IP variable that stores the value of the flag.
75
func (f *FlagSet) IP(name string, value net.IP, usage string) *net.IP {
76
p := new(net.IP)
77
f.IPVarP(p, name, "", value, usage)
78
return p
79
}
80
81
// IPP is like IP, but accepts a shorthand letter that can be used after a single dash.
82
func (f *FlagSet) IPP(name, shorthand string, value net.IP, usage string) *net.IP {
83
p := new(net.IP)
84
f.IPVarP(p, name, shorthand, value, usage)
85
return p
86
}
87
88
// IP defines an net.IP flag with specified name, default value, and usage string.
89
// The return value is the address of an net.IP variable that stores the value of the flag.
90
func IP(name string, value net.IP, usage string) *net.IP {
91
return CommandLine.IPP(name, "", value, usage)
92
}
93
94
// IPP is like IP, but accepts a shorthand letter that can be used after a single dash.
95
func IPP(name, shorthand string, value net.IP, usage string) *net.IP {
96
return CommandLine.IPP(name, shorthand, value, usage)
97
}
98
99