diff options
| author | Eric Paris <[email protected]> | 2015-04-07 16:50:29 -0400 |
|---|---|---|
| committer | Eric Paris <[email protected]> | 2015-04-28 18:37:40 -0400 |
| commit | 8824ec2f84efc5232daca3cb5403690b695190eb (patch) | |
| tree | 5565e551b0c1a9d1f9f7d90562d01c97edf31205 /flag_test.go | |
| parent | 60d4c375939ff7ba397a84117d5281256abb298f (diff) | |
Ability to mark flags as deprecated
They will not show up in usage or help, but they will still work.
The usage message will print on os.Stderr any time the flag is set.
Diffstat (limited to 'flag_test.go')
| -rw-r--r-- | flag_test.go | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/flag_test.go b/flag_test.go index c4055ed..a1478e2 100644 --- a/flag_test.go +++ b/flag_test.go @@ -7,6 +7,7 @@ package pflag_test import ( "bytes" "fmt" + "io" "io/ioutil" "os" "sort" @@ -445,3 +446,74 @@ func TestTermination(t *testing.T) { t.Errorf("expected argument %q got %q", arg2, f.Args()[1]) } } + +func TestDeprecatedFlagInDocs(t *testing.T) { + f := NewFlagSet("bob", ContinueOnError) + f.Bool("badflag", true, "always true") + f.MarkDeprecated("badflag", "use --good-flag instead") + + out := new(bytes.Buffer) + f.SetOutput(out) + f.PrintDefaults() + + if strings.Contains(out.String(), "badflag") { + t.Errorf("found deprecated flag in usage!") + } +} + +func parseReturnStderr(t *testing.T, f *FlagSet, args []string) (string, error) { + oldStderr := os.Stderr + r, w, _ := os.Pipe() + os.Stderr = w + + err := f.Parse(args) + + outC := make(chan string) + // copy the output in a separate goroutine so printing can't block indefinitely + go func() { + var buf bytes.Buffer + io.Copy(&buf, r) + outC <- buf.String() + }() + + w.Close() + os.Stderr = oldStderr + out := <-outC + + return out, err +} + +func TestDeprecatedFlagUsage(t *testing.T) { + f := NewFlagSet("bob", ContinueOnError) + f.Bool("badflag", true, "always true") + usageMsg := "use --good-flag instead" + f.MarkDeprecated("badflag", usageMsg) + + args := []string{"--badflag"} + out, err := parseReturnStderr(t, f, args) + if err != nil { + t.Fatal("expected no error; got ", err) + } + + if !strings.Contains(out, usageMsg) { + t.Errorf("usageMsg not printed when using a deprecated flag!") + } +} + +func TestDeprecatedFlagUsageNormalized(t *testing.T) { + f := NewFlagSet("bob", ContinueOnError) + f.Bool("bad-double_flag", true, "always true") + f.SetWordSeparators([]string{"-", "_"}) + usageMsg := "use --good-flag instead" + f.MarkDeprecated("bad_double-flag", usageMsg) + + args := []string{"--bad_double_flag"} + out, err := parseReturnStderr(t, f, args) + if err != nil { + t.Fatal("expected no error; got ", err) + } + + if !strings.Contains(out, usageMsg) { + t.Errorf("usageMsg not printed when using a deprecated flag!") + } +} |
