diff options
author | Ian Lance Taylor <iant@golang.org> | 2020-12-23 09:57:37 -0800 |
---|---|---|
committer | Ian Lance Taylor <iant@golang.org> | 2020-12-30 15:13:24 -0800 |
commit | cfcbb4227fb20191e04eb8d7766ae6202f526afd (patch) | |
tree | e2effea96f6f204451779f044415c2385e45042b /libgo/go/flag | |
parent | 0696141107d61483f38482b941549959a0d7f613 (diff) | |
download | gcc-cfcbb4227fb20191e04eb8d7766ae6202f526afd.zip gcc-cfcbb4227fb20191e04eb8d7766ae6202f526afd.tar.gz gcc-cfcbb4227fb20191e04eb8d7766ae6202f526afd.tar.bz2 |
libgo: update to Go1.16beta1 release
This does not yet include support for the //go:embed directive added
in this release.
* Makefile.am (check-runtime): Don't create check-runtime-dir.
(mostlyclean-local): Don't remove check-runtime-dir.
(check-go-tool, check-vet): Copy in go.mod and modules.txt.
(check-cgo-test, check-carchive-test): Add go.mod file.
* Makefile.in: Regenerate.
Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/280172
Diffstat (limited to 'libgo/go/flag')
-rw-r--r-- | libgo/go/flag/example_func_test.go | 41 | ||||
-rw-r--r-- | libgo/go/flag/flag.go | 22 | ||||
-rw-r--r-- | libgo/go/flag/flag_test.go | 55 |
3 files changed, 112 insertions, 6 deletions
diff --git a/libgo/go/flag/example_func_test.go b/libgo/go/flag/example_func_test.go new file mode 100644 index 0000000..7c30c5e --- /dev/null +++ b/libgo/go/flag/example_func_test.go @@ -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} +} diff --git a/libgo/go/flag/flag.go b/libgo/go/flag/flag.go index 286bba6..a8485f0 100644 --- a/libgo/go/flag/flag.go +++ b/libgo/go/flag/flag.go @@ -278,6 +278,12 @@ func (d *durationValue) Get() interface{} { return time.Duration(*d) } func (d *durationValue) String() string { return (*time.Duration)(d).String() } +type funcValue func(string) error + +func (f funcValue) Set(s string) error { return f(s) } + +func (f funcValue) String() string { return "" } + // Value is the interface to the dynamic value stored in a flag. // (The default value is represented as a string.) // @@ -296,7 +302,7 @@ type Value interface { // Getter is an interface that allows the contents of a Value to be retrieved. // It wraps the Value interface, rather than being part of it, because it // appeared after Go 1 and its compatibility rules. All Value types provided -// by this package satisfy the Getter interface. +// by this package satisfy the Getter interface, except the type used by Func. type Getter interface { Value Get() interface{} @@ -830,6 +836,20 @@ func Duration(name string, value time.Duration, usage string) *time.Duration { return CommandLine.Duration(name, value, usage) } +// Func defines a flag with the specified name and usage string. +// Each time the flag is seen, fn is called with the value of the flag. +// If fn returns a non-nil error, it will be treated as a flag value parsing error. +func (f *FlagSet) Func(name, usage string, fn func(string) error) { + f.Var(funcValue(fn), name, usage) +} + +// Func defines a flag with the specified name and usage string. +// Each time the flag is seen, fn is called with the value of the flag. +// If fn returns a non-nil error, it will be treated as a flag value parsing error. +func Func(name, usage string, fn func(string) error) { + CommandLine.Func(name, usage, fn) +} + // Var defines a flag with the specified name and usage string. The type and // value of the flag are represented by the first argument, of type Value, which // typically holds a user-defined implementation of Value. For instance, the diff --git a/libgo/go/flag/flag_test.go b/libgo/go/flag/flag_test.go index a01a5e4..06cab79 100644 --- a/libgo/go/flag/flag_test.go +++ b/libgo/go/flag/flag_test.go @@ -10,7 +10,6 @@ import ( "fmt" "internal/testenv" "io" - "io/ioutil" "os" "os/exec" "runtime" @@ -38,6 +37,7 @@ func TestEverything(t *testing.T) { String("test_string", "0", "string value") Float64("test_float64", 0, "float64 value") Duration("test_duration", 0, "time.Duration value") + Func("test_func", "func value", func(string) error { return nil }) m := make(map[string]*Flag) desired := "0" @@ -52,6 +52,8 @@ func TestEverything(t *testing.T) { ok = true case f.Name == "test_duration" && f.Value.String() == desired+"s": ok = true + case f.Name == "test_func" && f.Value.String() == "": + ok = true } if !ok { t.Error("Visit: bad value", f.Value.String(), "for", f.Name) @@ -59,7 +61,7 @@ func TestEverything(t *testing.T) { } } VisitAll(visitor) - if len(m) != 8 { + if len(m) != 9 { t.Error("VisitAll misses some flags") for k, v := range m { t.Log(k, *v) @@ -82,9 +84,10 @@ func TestEverything(t *testing.T) { Set("test_string", "1") Set("test_float64", "1") Set("test_duration", "1s") + Set("test_func", "1") desired = "1" Visit(visitor) - if len(m) != 8 { + if len(m) != 9 { t.Error("Visit fails after set") for k, v := range m { t.Log(k, *v) @@ -257,6 +260,48 @@ func TestUserDefined(t *testing.T) { } } +func TestUserDefinedFunc(t *testing.T) { + var flags FlagSet + flags.Init("test", ContinueOnError) + var ss []string + flags.Func("v", "usage", func(s string) error { + ss = append(ss, s) + return nil + }) + if err := flags.Parse([]string{"-v", "1", "-v", "2", "-v=3"}); err != nil { + t.Error(err) + } + if len(ss) != 3 { + t.Fatal("expected 3 args; got ", len(ss)) + } + expect := "[1 2 3]" + if got := fmt.Sprint(ss); got != expect { + t.Errorf("expected value %q got %q", expect, got) + } + // test usage + var buf strings.Builder + flags.SetOutput(&buf) + flags.Parse([]string{"-h"}) + if usage := buf.String(); !strings.Contains(usage, "usage") { + t.Errorf("usage string not included: %q", usage) + } + // test Func error + flags = *NewFlagSet("test", ContinueOnError) + flags.Func("v", "usage", func(s string) error { + return fmt.Errorf("test error") + }) + // flag not set, so no error + if err := flags.Parse(nil); err != nil { + t.Error(err) + } + // flag set, expect error + if err := flags.Parse([]string{"-v", "1"}); err == nil { + t.Error("expected error; got none") + } else if errMsg := err.Error(); !strings.Contains(errMsg, "test error") { + t.Errorf(`error should contain "test error"; got %q`, errMsg) + } +} + func TestUserDefinedForCommandLine(t *testing.T) { const help = "HELP" var result string @@ -499,7 +544,7 @@ func TestGetters(t *testing.T) { func TestParseError(t *testing.T) { for _, typ := range []string{"bool", "int", "int64", "uint", "uint64", "float64", "duration"} { fs := NewFlagSet("parse error test", ContinueOnError) - fs.SetOutput(ioutil.Discard) + fs.SetOutput(io.Discard) _ = fs.Bool("bool", false, "") _ = fs.Int("int", 0, "") _ = fs.Int64("int64", 0, "") @@ -530,7 +575,7 @@ func TestRangeError(t *testing.T) { } for _, arg := range bad { fs := NewFlagSet("parse error test", ContinueOnError) - fs.SetOutput(ioutil.Discard) + fs.SetOutput(io.Discard) _ = fs.Int("int", 0, "") _ = fs.Int64("int64", 0, "") _ = fs.Uint("uint", 0, "") |