zsh-completion: added escapinng of single quotes in flag description.

This commit is contained in:
Haim Ashkenazi 2018-03-17 20:55:27 +02:00 committed by Steve Francia
parent 66a98807d4
commit 8822449c0f
2 changed files with 17 additions and 2 deletions

View File

@ -139,7 +139,7 @@ func genFlagEntryForSingleOptionFlag(f *pflag.Flag) string {
}
extras = genZshFlagEntryExtras(f)
return fmt.Sprintf(`'%s%s[%s]%s'`, multiMark, option, f.Usage, extras)
return fmt.Sprintf(`'%s%s[%s]%s'`, multiMark, option, quoteDescription(f.Usage), extras)
}
func genFlagEntryForMultiOptionFlag(f *pflag.Flag) string {
@ -154,7 +154,7 @@ func genFlagEntryForMultiOptionFlag(f *pflag.Flag) string {
parenMultiMark, f.Shorthand, parenMultiMark, f.Name, curlyMultiMark, f.Shorthand, curlyMultiMark, f.Name)
extras = genZshFlagEntryExtras(f)
return fmt.Sprintf(`%s'[%s]%s'`, options, f.Usage, extras)
return fmt.Sprintf(`%s'[%s]%s'`, options, quoteDescription(f.Usage), extras)
}
func genZshFlagEntryExtras(f *pflag.Flag) string {
@ -177,3 +177,7 @@ func flagCouldBeSpecifiedMoreThenOnce(f *pflag.Flag) bool {
return strings.Contains(f.Value.Type(), "Slice") ||
strings.Contains(f.Value.Type(), "Array")
}
func quoteDescription(s string) string {
return strings.Replace(s, "'", `'\''`, -1)
}

View File

@ -146,6 +146,17 @@ func TestGenZshCompletion(t *testing.T) {
"function _root {",
},
},
{
name: "flag description with single quote (') shouldn't break quotes in completion file",
root: func() *Command {
r := genTestCommand("root", true)
r.Flags().Bool("private", false, "Don't show public info")
return r
}(),
expectedExpressions: []string{
`--private\[Don'\\''t show public info]`,
},
},
}
for _, tc := range tcs {