Source file
src/flag/example_value_test.go
1
2
3
4
5 package flag_test
6
7 import (
8 "flag"
9 "fmt"
10 "net/url"
11 )
12
13 type URLValue struct {
14 URL *url.URL
15 }
16
17 func (v URLValue) String() string {
18 if v.URL != nil {
19 return v.URL.String()
20 }
21 return ""
22 }
23
24 func (v URLValue) Set(s string) error {
25 if u, err := url.Parse(s); err != nil {
26 return err
27 } else {
28 *v.URL = *u
29 }
30 return nil
31 }
32
33 var u = &url.URL{}
34
35 func ExampleValue() {
36 fs := flag.NewFlagSet("ExampleValue", flag.ExitOnError)
37 fs.Var(&URLValue{u}, "url", "URL to parse")
38
39 fs.Parse([]string{"-url", "https://golang.org/pkg/flag/"})
40 fmt.Printf(`{scheme: %q, host: %q, path: %q}`, u.Scheme, u.Host, u.Path)
41
42
43
44 }
45
View as plain text