diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-16 19:23:18 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-16 19:23:18 +0000 |
commit | 43a123c1ae6613b3efeed291fa552ecd909d3acf (patch) | |
tree | fd92518b7024bc74031f78a1cf9e454b65e73665 /src/flag/example_textvar_test.go | |
parent | Initial commit. (diff) | |
download | golang-1.20-43a123c1ae6613b3efeed291fa552ecd909d3acf.tar.xz golang-1.20-43a123c1ae6613b3efeed291fa552ecd909d3acf.zip |
Adding upstream version 1.20.14.upstream/1.20.14upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/flag/example_textvar_test.go')
-rw-r--r-- | src/flag/example_textvar_test.go | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/src/flag/example_textvar_test.go b/src/flag/example_textvar_test.go new file mode 100644 index 0000000..8b8cbf6 --- /dev/null +++ b/src/flag/example_textvar_test.go @@ -0,0 +1,35 @@ +// Copyright 2022 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package flag_test + +import ( + "flag" + "fmt" + "net" + "os" +) + +func ExampleTextVar() { + fs := flag.NewFlagSet("ExampleTextVar", flag.ContinueOnError) + fs.SetOutput(os.Stdout) + var ip net.IP + fs.TextVar(&ip, "ip", net.IPv4(192, 168, 0, 100), "`IP address` to parse") + fs.Parse([]string{"-ip", "127.0.0.1"}) + fmt.Printf("{ip: %v}\n\n", ip) + + // 256 is not a valid IPv4 component + ip = nil + fs.Parse([]string{"-ip", "256.0.0.1"}) + fmt.Printf("{ip: %v}\n\n", ip) + + // Output: + // {ip: 127.0.0.1} + // + // invalid value "256.0.0.1" for flag -ip: invalid IP address: 256.0.0.1 + // Usage of ExampleTextVar: + // -ip IP address + // IP address to parse (default 192.168.0.100) + // {ip: <nil>} +} |