Skip to content

Commit

Permalink
SetActual() impl
Browse files Browse the repository at this point in the history
  • Loading branch information
gobwas committed Oct 16, 2020
1 parent 1e4d95a commit 5386ade
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 4 deletions.
26 changes: 26 additions & 0 deletions flagutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,32 @@ func CombineFlags(f0, f1 *flag.Flag) *flag.Flag {
return &r
}

// SetActual makes flag look like it has been set within flag set.
// If flag set doesn't has flag with given SetActual() does nothing.
// Original value of found flag remains untouched, so it is safe to use with
// flags that accumulate values of multiple Set() calls.
func SetActual(fs *flag.FlagSet, name string) {
f := fs.Lookup(name)
if f == nil {
return
}
orig := f.Value
defer func() {
f.Value = orig
}()
var didSet bool
f.Value = value{
doSet: func(s string) error {
didSet = s == "dummy"
return nil
},
}
fs.Set(name, "dummy")
if !didSet {
panic("flagutil: make specified didn't work well")
}
}

func mergeUsage(name, s0, s1 string) string {
switch {
case s0 == "":
Expand Down
44 changes: 40 additions & 4 deletions flagutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,21 @@ import (
"github.com/gobwas/flagutil/parse"
)

func TestSetActual(t *testing.T) {
fs := flag.NewFlagSet(t.Name(), flag.PanicOnError)
fs.String("flag", "default", "usage")

f := fs.Lookup("flag")
f.Value = OverrideSet(f.Value, func(string) error {
t.Fatalf("unexpected value change")
return nil
})

mustNotBeActual(t, fs, "flag")
SetActual(fs, "flag")
mustBeActual(t, fs, "flag")
}

type fullParser struct {
Parser
Printer
Expand Down Expand Up @@ -160,7 +175,7 @@ func ExampleMerge() {
)
// Now we need to setup same flag (probably from some different place).
// Setting it up again in a superset will cause error.
Merge(fs, func(sub *flag.FlagSet) {
MergeInto(fs, func(sub *flag.FlagSet) {
// Notice that default value of this flag is different.
// However, it will be discarded in favour of default value from superset.
sub.StringVar(&s1,
Expand Down Expand Up @@ -197,7 +212,7 @@ func ExampleMerge_different_types() {
"foo", "42",
"some flag usage here",
)
Merge(fs, func(sub *flag.FlagSet) {
MergeInto(fs, func(sub *flag.FlagSet) {
sub.IntVar(&i,
"foo", 84,
"another flag usage here",
Expand All @@ -222,10 +237,10 @@ func TestMerge(t *testing.T) {
"foo", "bar",
"superset usage",
)
Merge(fs, func(fs *flag.FlagSet) {
MergeInto(fs, func(fs *flag.FlagSet) {
fs.StringVar(&s1, "foo", "baz", "subset1 usage")
})
Merge(fs, func(fs *flag.FlagSet) {
MergeInto(fs, func(fs *flag.FlagSet) {
fs.StringVar(&s2, "foo", "baq", "subset2 usage")
})
if s0 == s1 || s1 == s2 {
Expand Down Expand Up @@ -323,6 +338,27 @@ func mustBeDefined(t *testing.T, fs *flag.FlagSet, name string) {
}
}

func mustBeActual(t *testing.T, fs *flag.FlagSet, name string) {
if !isActual(fs, name) {
t.Fatalf("want flag %q to be actual in set", name)
}
}

func mustNotBeActual(t *testing.T, fs *flag.FlagSet, name string) {
if isActual(fs, name) {
t.Fatalf("want flag %q to not be actual in set", name)
}
}

func isActual(fs *flag.FlagSet, name string) (actual bool) {
fs.Visit(func(f *flag.Flag) {
if f.Name == name {
actual = true
}
})
return
}

func TestCombineFlags(t *testing.T) {
for _, test := range []struct {
name string
Expand Down

0 comments on commit 5386ade

Please sign in to comment.