mirror of
https://github.com/spf13/cobra
synced 2025-05-07 13:57:21 +00:00
Complete subcommands when TraverseChildren is true in bash completion
The current bash completion logic does not complete subcommands when a local flag is set. There is also a bug where subcommands are sometimes still getting completed. see: #1172 If TraverseChildren is true we should allow subcommands to be completed even if a local flag is set. Closes #1172 Signed-off-by: Paul Holzinger <paul.holzinger@web.de>
This commit is contained in:
parent
7034e0c888
commit
a3adff60ac
2 changed files with 33 additions and 4 deletions
|
@ -495,12 +495,14 @@ func writeFlag(buf *bytes.Buffer, flag *pflag.Flag, cmd *Command) {
|
|||
|
||||
func writeLocalNonPersistentFlag(buf *bytes.Buffer, flag *pflag.Flag) {
|
||||
name := flag.Name
|
||||
format := " local_nonpersistent_flags+=(\"--%s"
|
||||
format := " local_nonpersistent_flags+=(\"--%[1]s\")\n"
|
||||
if len(flag.NoOptDefVal) == 0 {
|
||||
format += "="
|
||||
format += " local_nonpersistent_flags+=(\"--%[1]s=\")\n"
|
||||
}
|
||||
format += "\")\n"
|
||||
buf.WriteString(fmt.Sprintf(format, name))
|
||||
if len(flag.Shorthand) > 0 {
|
||||
buf.WriteString(fmt.Sprintf(" local_nonpersistent_flags+=(\"-%s\")\n", flag.Shorthand))
|
||||
}
|
||||
}
|
||||
|
||||
// Setup annotations for go completions for registered flags
|
||||
|
@ -535,7 +537,9 @@ func writeFlags(buf *bytes.Buffer, cmd *Command) {
|
|||
if len(flag.Shorthand) > 0 {
|
||||
writeShortFlag(buf, flag, cmd)
|
||||
}
|
||||
if localNonPersistentFlags.Lookup(flag.Name) != nil {
|
||||
// localNonPersistentFlags are used to stop the completion of subcommands when one is set
|
||||
// if TraverseChildren is true we should allow to complete subcommands
|
||||
if localNonPersistentFlags.Lookup(flag.Name) != nil && !cmd.Root().TraverseChildren {
|
||||
writeLocalNonPersistentFlag(buf, flag)
|
||||
}
|
||||
})
|
||||
|
|
|
@ -193,6 +193,13 @@ func TestBashCompletions(t *testing.T) {
|
|||
checkOmit(t, output, `two_word_flags+=("--two-w-default")`)
|
||||
checkOmit(t, output, `two_word_flags+=("-T")`)
|
||||
|
||||
// check local nonpersistent flag
|
||||
check(t, output, `local_nonpersistent_flags+=("--two")`)
|
||||
check(t, output, `local_nonpersistent_flags+=("--two=")`)
|
||||
check(t, output, `local_nonpersistent_flags+=("-t")`)
|
||||
check(t, output, `local_nonpersistent_flags+=("--two-w-default")`)
|
||||
check(t, output, `local_nonpersistent_flags+=("-T")`)
|
||||
|
||||
checkOmit(t, output, deprecatedCmd.Name())
|
||||
|
||||
// If available, run shellcheck against the script.
|
||||
|
@ -235,3 +242,21 @@ func TestBashCompletionDeprecatedFlag(t *testing.T) {
|
|||
t.Errorf("expected completion to not include %q flag: Got %v", flagName, output)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBashCompletionTraverseChildren(t *testing.T) {
|
||||
c := &Command{Use: "c", Run: emptyRun, TraverseChildren: true}
|
||||
|
||||
c.Flags().StringP("string-flag", "s", "", "string flag")
|
||||
c.Flags().BoolP("bool-flag", "b", false, "bool flag")
|
||||
|
||||
buf := new(bytes.Buffer)
|
||||
c.GenBashCompletion(buf)
|
||||
output := buf.String()
|
||||
|
||||
// check that local nonpersistent flag are not set since we have TraverseChildren set to true
|
||||
checkOmit(t, output, `local_nonpersistent_flags+=("--string-flag")`)
|
||||
checkOmit(t, output, `local_nonpersistent_flags+=("--string-flag=")`)
|
||||
checkOmit(t, output, `local_nonpersistent_flags+=("-s")`)
|
||||
checkOmit(t, output, `local_nonpersistent_flags+=("--bool-flag")`)
|
||||
checkOmit(t, output, `local_nonpersistent_flags+=("-b")`)
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue