From 12df56c7e6e2d6e3f1b885aa5b2ea89dfa440bf6 Mon Sep 17 00:00:00 2001 From: Chmouel Boudjnah Date: Thu, 4 Jul 2019 16:16:22 +0200 Subject: [PATCH] Make sure we quote brackets when generating zsh completion Signed-off-by: Chmouel Boudjnah --- zsh_completions.go | 3 ++- zsh_completions_test.go | 11 +++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/zsh_completions.go b/zsh_completions.go index 12755482f0..5b0bff30e3 100644 --- a/zsh_completions.go +++ b/zsh_completions.go @@ -332,5 +332,6 @@ func zshCompFlagCouldBeSpecifiedMoreThenOnce(f *pflag.Flag) bool { } func zshCompQuoteFlagDescription(s string) string { - return strings.Replace(s, "'", `'\''`, -1) + replacer := strings.NewReplacer("'", `'\''`, "[", `\[`, "]", `\]`) + return replacer.Replace(s) } diff --git a/zsh_completions_test.go b/zsh_completions_test.go index e53fa886e5..8079f6cf72 100644 --- a/zsh_completions_test.go +++ b/zsh_completions_test.go @@ -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 {