forked from cornfeedhobo/pflag
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample_func_test.go
41 lines (35 loc) · 1023 Bytes
/
example_func_test.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
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package zflag_test
import (
"fmt"
"net"
"os"
"github.com/zulucmd/zflag/v2"
)
// Copyright 2020 The Go Authors. All rights reserved.
func ExampleFunc() {
fs := zflag.NewFlagSet("ExampleFunc", zflag.ContinueOnError)
fs.SetOutput(os.Stdout)
var ip net.IP
fs.Func("ip", "`IP address` to parse", func(s string) error {
ip = net.ParseIP(s)
if ip == nil {
return fmt.Errorf("could not parse IP")
}
return nil
})
_ = fs.Parse([]string{"--ip", "127.0.0.1"})
fmt.Printf("{ip: %v, loopback: %t}\n\n", ip, ip.IsLoopback())
// 256 is not a valid IPv4 component
_ = fs.Parse([]string{"--ip", "256.0.0.1"})
fmt.Printf("{ip: %v, loopback: %t}\n\n", ip, ip.IsLoopback())
// Output:
// {ip: 127.0.0.1, loopback: true}
//
// Usage of ExampleFunc:
// --ip IP address IP address to parse
//
// invalid argument "256.0.0.1" for "--ip" flag: could not parse IP
// {ip: <nil>, loopback: false}
}