From 75daccd5b88bc3b896829d68ac9529ecf9aeae92 Mon Sep 17 00:00:00 2001 From: Euan Kemp Date: Tue, 2 Aug 2016 14:49:33 -0700 Subject: [PATCH 1/2] Don't display hidden flags in bash completions --- bash_completions.go | 9 +++++++++ bash_completions_test.go | 20 ++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/bash_completions.go b/bash_completions.go index 236dee6..3b01324 100644 --- a/bash_completions.go +++ b/bash_completions.go @@ -421,6 +421,9 @@ func writeFlags(cmd *Command, w io.Writer) error { localNonPersistentFlags := cmd.LocalNonPersistentFlags() var visitErr error cmd.NonInheritedFlags().VisitAll(func(flag *pflag.Flag) { + if flag.Hidden { + return + } if err := writeFlag(flag, w); err != nil { visitErr = err return @@ -442,6 +445,9 @@ func writeFlags(cmd *Command, w io.Writer) error { return visitErr } cmd.InheritedFlags().VisitAll(func(flag *pflag.Flag) { + if flag.Hidden { + return + } if err := writeFlag(flag, w); err != nil { visitErr = err return @@ -468,6 +474,9 @@ func writeRequiredFlag(cmd *Command, w io.Writer) error { flags := cmd.NonInheritedFlags() var visitErr error flags.VisitAll(func(flag *pflag.Flag) { + if flag.Hidden { + return + } for key := range flag.Annotations { switch key { case BashCompOneRequiredFlag: diff --git a/bash_completions_test.go b/bash_completions_test.go index 6957f8b..be0d209 100644 --- a/bash_completions_test.go +++ b/bash_completions_test.go @@ -138,3 +138,23 @@ func TestBashCompletions(t *testing.T) { t.Fatalf("shellcheck failed: %v", err) } } + +func TestBashCompletionHiddenFlag(t *testing.T) { + var cmdTrue = &Command{ + Use: "does nothing", + Run: func(cmd *Command, args []string) {}, + } + + const flagName = "hidden-foo-bar-baz" + + var flagValue bool + cmdTrue.Flags().BoolVar(&flagValue, flagName, false, "hidden flag") + cmdTrue.Flags().MarkHidden(flagName) + + out := new(bytes.Buffer) + cmdTrue.GenBashCompletion(out) + bashCompletion := out.String() + if strings.Contains(bashCompletion, flagName) { + t.Error("expected completion to not include %q flag: Got %v", flagName, bashCompletion) + } +} From 40e19b3f3b43ae017d94f2ee16e18dff402f8031 Mon Sep 17 00:00:00 2001 From: Euan Kemp Date: Tue, 2 Aug 2016 15:01:33 -0700 Subject: [PATCH 2/2] Don't display deprecated flags in bash completions --- bash_completions.go | 10 +++++++--- bash_completions_test.go | 20 ++++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/bash_completions.go b/bash_completions.go index 3b01324..5b3e855 100644 --- a/bash_completions.go +++ b/bash_completions.go @@ -421,7 +421,7 @@ func writeFlags(cmd *Command, w io.Writer) error { localNonPersistentFlags := cmd.LocalNonPersistentFlags() var visitErr error cmd.NonInheritedFlags().VisitAll(func(flag *pflag.Flag) { - if flag.Hidden { + if nonCompletableFlag(flag) { return } if err := writeFlag(flag, w); err != nil { @@ -445,7 +445,7 @@ func writeFlags(cmd *Command, w io.Writer) error { return visitErr } cmd.InheritedFlags().VisitAll(func(flag *pflag.Flag) { - if flag.Hidden { + if nonCompletableFlag(flag) { return } if err := writeFlag(flag, w); err != nil { @@ -474,7 +474,7 @@ func writeRequiredFlag(cmd *Command, w io.Writer) error { flags := cmd.NonInheritedFlags() var visitErr error flags.VisitAll(func(flag *pflag.Flag) { - if flag.Hidden { + if nonCompletableFlag(flag) { return } for key := range flag.Annotations { @@ -583,6 +583,10 @@ func (cmd *Command) GenBashCompletion(w io.Writer) error { return postscript(w, cmd.Name()) } +func nonCompletableFlag(flag *pflag.Flag) bool { + return flag.Hidden || len(flag.Deprecated) > 0 +} + func (cmd *Command) GenBashCompletionFile(filename string) error { outFile, err := os.Create(filename) if err != nil { diff --git a/bash_completions_test.go b/bash_completions_test.go index be0d209..5f4cb4d 100644 --- a/bash_completions_test.go +++ b/bash_completions_test.go @@ -158,3 +158,23 @@ func TestBashCompletionHiddenFlag(t *testing.T) { t.Error("expected completion to not include %q flag: Got %v", flagName, bashCompletion) } } + +func TestBashCompletionDeprecatedFlag(t *testing.T) { + var cmdTrue = &Command{ + Use: "does nothing", + Run: func(cmd *Command, args []string) {}, + } + + const flagName = "deprecated-foo-bar-baz" + + var flagValue bool + cmdTrue.Flags().BoolVar(&flagValue, flagName, false, "hidden flag") + cmdTrue.Flags().MarkDeprecated(flagName, "use --does-not-exist instead") + + out := new(bytes.Buffer) + cmdTrue.GenBashCompletion(out) + bashCompletion := out.String() + if strings.Contains(bashCompletion, flagName) { + t.Errorf("expected completion to not include %q flag: Got %v", flagName, bashCompletion) + } +}