Don't display hidden flags in bash completions

This commit is contained in:
Euan Kemp 2016-08-02 14:49:33 -07:00
parent f62e98d28a
commit 75daccd5b8
2 changed files with 29 additions and 0 deletions

View File

@ -421,6 +421,9 @@ 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 {
return
}
if err := writeFlag(flag, w); err != nil { if err := writeFlag(flag, w); err != nil {
visitErr = err visitErr = err
return return
@ -442,6 +445,9 @@ 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 {
return
}
if err := writeFlag(flag, w); err != nil { if err := writeFlag(flag, w); err != nil {
visitErr = err visitErr = err
return return
@ -468,6 +474,9 @@ 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 {
return
}
for key := range flag.Annotations { for key := range flag.Annotations {
switch key { switch key {
case BashCompOneRequiredFlag: case BashCompOneRequiredFlag:

View File

@ -138,3 +138,23 @@ func TestBashCompletions(t *testing.T) {
t.Fatalf("shellcheck failed: %v", err) 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)
}
}