Don't display deprecated flags in bash completions

This commit is contained in:
Euan Kemp 2016-08-02 15:01:33 -07:00
parent 75daccd5b8
commit 40e19b3f3b
2 changed files with 27 additions and 3 deletions

View File

@ -421,7 +421,7 @@ func writeFlags(cmd *Command, w io.Writer) error {
localNonPersistentFlags := cmd.LocalNonPersistentFlags() localNonPersistentFlags := cmd.LocalNonPersistentFlags()
var visitErr error var visitErr error
cmd.NonInheritedFlags().VisitAll(func(flag *pflag.Flag) { cmd.NonInheritedFlags().VisitAll(func(flag *pflag.Flag) {
if flag.Hidden { if nonCompletableFlag(flag) {
return return
} }
if err := writeFlag(flag, w); err != nil { if err := writeFlag(flag, w); err != nil {
@ -445,7 +445,7 @@ func writeFlags(cmd *Command, w io.Writer) error {
return visitErr return visitErr
} }
cmd.InheritedFlags().VisitAll(func(flag *pflag.Flag) { cmd.InheritedFlags().VisitAll(func(flag *pflag.Flag) {
if flag.Hidden { if nonCompletableFlag(flag) {
return return
} }
if err := writeFlag(flag, w); err != nil { if err := writeFlag(flag, w); err != nil {
@ -474,7 +474,7 @@ func writeRequiredFlag(cmd *Command, w io.Writer) error {
flags := cmd.NonInheritedFlags() flags := cmd.NonInheritedFlags()
var visitErr error var visitErr error
flags.VisitAll(func(flag *pflag.Flag) { flags.VisitAll(func(flag *pflag.Flag) {
if flag.Hidden { if nonCompletableFlag(flag) {
return return
} }
for key := range flag.Annotations { for key := range flag.Annotations {
@ -583,6 +583,10 @@ func (cmd *Command) GenBashCompletion(w io.Writer) error {
return postscript(w, cmd.Name()) 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 { func (cmd *Command) GenBashCompletionFile(filename string) error {
outFile, err := os.Create(filename) outFile, err := os.Create(filename)
if err != nil { if err != nil {

View File

@ -158,3 +158,23 @@ func TestBashCompletionHiddenFlag(t *testing.T) {
t.Error("expected completion to not include %q flag: Got %v", flagName, bashCompletion) 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)
}
}