Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure we quote brackets when generating zsh completion #899

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions zsh_completions.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var (
"extractFlags": zshCompExtractFlag,
"genFlagEntryForZshArguments": zshCompGenFlagEntryForArguments,
"extractArgsCompletions": zshCompExtractArgumentCompletionHintsForRendering,
"escapeText": zshCompQuoteFlagDescription,
}
zshCompletionText = `
{{/* should accept Command (that contains subcommands) as parameter */}}
Expand All @@ -41,7 +42,7 @@ function {{$cmdPath}} {
case $state in
cmnds)
commands=({{range .Commands}}{{if not .Hidden}}
"{{.Name}}:{{.Short}}"{{end}}{{end}}
"{{.Name}}:{{.Short | escapeText}}"{{end}}{{end}}
)
_describe "command" commands
;;
Expand Down Expand Up @@ -332,5 +333,9 @@ func zshCompFlagCouldBeSpecifiedMoreThenOnce(f *pflag.Flag) bool {
}

func zshCompQuoteFlagDescription(s string) string {
return strings.Replace(s, "'", `'\''`, -1)
return strings.NewReplacer("'", `'\''`,
"[", `\[`,
"]", `\]`,
"$(", `\$(`,
).Replace(s)
}
30 changes: 30 additions & 0 deletions zsh_completions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,17 @@ func TestGenZshCompletion(t *testing.T) {
`--private\[Don'\\''t show public info]`,
},
},
{
name: "flag description with brackets ([]) shouldn't break the completion file",
root: func() *Command {
r := genTestCommand("root", true)
r.Flags().Bool("level", false, "[ALERT]")
return r
}(),
expectedExpressions: []string{
`--level[\[ALERT\]]`,
},
},
{
name: "argument completion for file with and without patterns",
root: func() *Command {
Expand Down Expand Up @@ -229,6 +240,25 @@ func TestGenZshCompletion(t *testing.T) {
`--ptest\[ptest]:filename:_files -g "-\(/\)"`,
},
},
{
name: "escape text in subcommand description",
root: func() *Command {
r := &Command{
Use: "rootcmd",
Long: "Long rootcmd description",
}
d := &Command{
Use: "subcmd1",
Short: "$(echo foo)",
Run: emptyRun,
}
r.AddCommand(d)
return r
}(),
expectedExpressions: []string{
`\$\(echo foo\)`,
},
},
}

for _, tc := range tcs {
Expand Down