From 8822449c0fb9575f1d2453e80cd3e55ab8fc2844 Mon Sep 17 00:00:00 2001 From: Haim Ashkenazi Date: Sat, 17 Mar 2018 20:55:27 +0200 Subject: [PATCH] zsh-completion: added escapinng of single quotes in flag description. --- zsh_completions.go | 8 ++++++-- zsh_completions_test.go | 11 +++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/zsh_completions.go b/zsh_completions.go index 4705a90..59f5531 100644 --- a/zsh_completions.go +++ b/zsh_completions.go @@ -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) +} diff --git a/zsh_completions_test.go b/zsh_completions_test.go index 22a66d6..6788797 100644 --- a/zsh_completions_test.go +++ b/zsh_completions_test.go @@ -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 {