1 // Copyright 2020 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
16 fs := flag.NewFlagSet("ExampleFunc", flag.ContinueOnError)
17 fs.SetOutput(os.Stdout)
19 fs.Func("ip", "`IP address` to parse", func(s string) error {
22 return errors.New("could not parse IP")
26 fs.Parse([]string{"-ip", "127.0.0.1"})
27 fmt.Printf("{ip: %v, loopback: %t}\n\n", ip, ip.IsLoopback())
29 // 256 is not a valid IPv4 component
30 fs.Parse([]string{"-ip", "256.0.0.1"})
31 fmt.Printf("{ip: %v, loopback: %t}\n\n", ip, ip.IsLoopback())
34 // {ip: 127.0.0.1, loopback: true}
36 // invalid value "256.0.0.1" for flag -ip: could not parse IP
37 // Usage of ExampleFunc:
39 // IP address to parse
40 // {ip: <nil>, loopback: false}
43 func ExampleBoolFunc() {
44 fs := flag.NewFlagSet("ExampleBoolFunc", flag.ContinueOnError)
45 fs.SetOutput(os.Stdout)
47 fs.BoolFunc("log", "logs a dummy message", func(s string) error {
48 fmt.Println("dummy message:", s)
51 fs.Parse([]string{"-log"})
52 fs.Parse([]string{"-log=0"})
55 // dummy message: true