-
Notifications
You must be signed in to change notification settings - Fork 4
/
url.go
56 lines (47 loc) · 970 Bytes
/
url.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package flagvar
import (
"net/url"
"strings"
)
// URL is a `flag.Value` for `url.URL` arguments.
type URL struct {
Value *url.URL
Text string
}
// Help returns a string suitable for inclusion in a flag help message.
func (fv *URL) Help() string {
return "a URL"
}
// Set is flag.Value.Set
func (fv *URL) Set(v string) error {
u, err := url.Parse(v)
if err == nil {
fv.Text = v
fv.Value = u
}
return err
}
func (fv *URL) String() string {
return fv.Text
}
// URLs is a `flag.Value` for `url.URL` arguments.
type URLs struct {
Values []*url.URL
Texts []string
}
// Help returns a string suitable for inclusion in a flag help message.
func (fv *URLs) Help() string {
return "a URL"
}
// Set is flag.Value.Set
func (fv *URLs) Set(v string) error {
u, err := url.Parse(v)
if err == nil {
fv.Texts = append(fv.Texts, v)
fv.Values = append(fv.Values, u)
}
return err
}
func (fv *URLs) String() string {
return strings.Join(fv.Texts, ",")
}