-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #39557 Change-Id: Ida578f7484335e8c6bf927255f75377eda63b563 GitHub-Last-Rev: b97294f7669c24011e5b093179d65636512a84cd GitHub-Pull-Request: golang/go#39880 Reviewed-on: https://go-review.googlesource.com/c/go/+/240014 Reviewed-by: Russ Cox <[email protected]> Trust: Ian Lance Taylor <[email protected]>
- Loading branch information
1 parent
2ae2a94
commit 1632851
Showing
3 changed files
with
110 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright 2020 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 ( | ||
"errors" | ||
"flag" | ||
"fmt" | ||
"net" | ||
"os" | ||
) | ||
|
||
func ExampleFunc() { | ||
fs := flag.NewFlagSet("ExampleFunc", flag.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 errors.New("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} | ||
// | ||
// invalid value "256.0.0.1" for flag -ip: could not parse IP | ||
// Usage of ExampleFunc: | ||
// -ip IP address | ||
// IP address to parse | ||
// {ip: <nil>, loopback: false} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters