Skip to content

Commit

Permalink
Properly escape help text
Browse files Browse the repository at this point in the history
Closes spf13#989

Authored-by: Cornelius Weig <https://github.com/corneliusweig>
Signed-off-by: Chmouel Boudjnah <chmouel@chmouel.com>
  • Loading branch information
chmouel committed Feb 11, 2020
1 parent 8d84e2e commit ee8ca02
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
7 changes: 5 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 @@ -334,5 +335,7 @@ func zshCompFlagCouldBeSpecifiedMoreThenOnce(f *pflag.Flag) bool {
func zshCompQuoteFlagDescription(s string) string {
return strings.NewReplacer("'", `'\''`,
"[", `\[`,
"]", `\]`).Replace(s)
"]", `\]`,
"$(", `\$(`,
).Replace(s)
}
19 changes: 19 additions & 0 deletions zsh_completions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,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

0 comments on commit ee8ca02

Please sign in to comment.