forked from mirror/cobra
zsh-completion: added escapinng of single quotes in flag description.
This commit is contained in:
parent
66a98807d4
commit
8822449c0f
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue