diff options
| author | Eric Paris <[email protected]> | 2018-04-10 17:30:10 -0400 |
|---|---|---|
| committer | GitHub <[email protected]> | 2018-04-10 17:30:10 -0400 |
| commit | 329ebf1e04800d11a25047832495199d366580f3 (patch) | |
| tree | 0ceb84cd3f4c7c2302d06ad5fc61f57b49696259 | |
| parent | 1ce0cc6db4029d97571db82f85092fccedb572ce (diff) | |
Allow Users To Show Deprecated Flags (#163)
They have to explicitly unhide a deprecated flag to keep the current behavior.
| -rw-r--r-- | flag.go | 10 | ||||
| -rw-r--r-- | flag_test.go | 27 |
2 files changed, 33 insertions, 4 deletions
@@ -285,10 +285,10 @@ func (f *FlagSet) HasFlags() bool { } // HasAvailableFlags returns a bool to indicate if the FlagSet has any flags -// definied that are not hidden or deprecated. +// that are not hidden. func (f *FlagSet) HasAvailableFlags() bool { for _, flag := range f.formal { - if !flag.Hidden && len(flag.Deprecated) == 0 { + if !flag.Hidden { return true } } @@ -398,6 +398,7 @@ func (f *FlagSet) MarkDeprecated(name string, usageMessage string) error { return fmt.Errorf("deprecated message for flag %q must be set", name) } flag.Deprecated = usageMessage + flag.Hidden = true return nil } @@ -668,7 +669,7 @@ func (f *FlagSet) FlagUsagesWrapped(cols int) string { maxlen := 0 f.VisitAll(func(flag *Flag) { - if flag.Deprecated != "" || flag.Hidden { + if flag.Hidden { return } @@ -715,6 +716,9 @@ func (f *FlagSet) FlagUsagesWrapped(cols int) string { line += fmt.Sprintf(" (default %s)", flag.DefValue) } } + if len(flag.Deprecated) != 0 { + line += fmt.Sprintf(" (DEPRECATED: %s)", flag.Deprecated) + } lines = append(lines, line) }) diff --git a/flag_test.go b/flag_test.go index 3c13de3..f600f0a 100644 --- a/flag_test.go +++ b/flag_test.go @@ -966,10 +966,14 @@ func TestTermination(t *testing.T) { } } -func TestDeprecatedFlagInDocs(t *testing.T) { +func getDeprecatedFlagSet() *FlagSet { f := NewFlagSet("bob", ContinueOnError) f.Bool("badflag", true, "always true") f.MarkDeprecated("badflag", "use --good-flag instead") + return f +} +func TestDeprecatedFlagInDocs(t *testing.T) { + f := getDeprecatedFlagSet() out := new(bytes.Buffer) f.SetOutput(out) @@ -980,6 +984,27 @@ func TestDeprecatedFlagInDocs(t *testing.T) { } } +func TestUnHiddenDeprecatedFlagInDocs(t *testing.T) { + f := getDeprecatedFlagSet() + flg := f.Lookup("badflag") + if flg == nil { + t.Fatalf("Unable to lookup 'bob' in TestUnHiddenDeprecatedFlagInDocs") + } + flg.Hidden = false + + out := new(bytes.Buffer) + f.SetOutput(out) + f.PrintDefaults() + + defaults := out.String() + if !strings.Contains(defaults, "badflag") { + t.Errorf("Did not find deprecated flag in usage!") + } + if !strings.Contains(defaults, "use --good-flag instead") { + t.Errorf("Did not find 'use --good-flag instead' in defaults") + } +} + func TestDeprecatedFlagShorthandInDocs(t *testing.T) { f := NewFlagSet("bob", ContinueOnError) name := "noshorthandflag" |
