mirror of
https://github.com/spf13/cobra
synced 2025-05-07 22:07:23 +00:00
Properly escape help text
Closes #989 Authored-by: Cornelius Weig <https://github.com/corneliusweig> Signed-off-by: Chmouel Boudjnah <chmouel@chmouel.com>
This commit is contained in:
parent
8d84e2e222
commit
ee8ca02120
2 changed files with 24 additions and 2 deletions
|
@ -25,6 +25,7 @@ var (
|
||||||
"extractFlags": zshCompExtractFlag,
|
"extractFlags": zshCompExtractFlag,
|
||||||
"genFlagEntryForZshArguments": zshCompGenFlagEntryForArguments,
|
"genFlagEntryForZshArguments": zshCompGenFlagEntryForArguments,
|
||||||
"extractArgsCompletions": zshCompExtractArgumentCompletionHintsForRendering,
|
"extractArgsCompletions": zshCompExtractArgumentCompletionHintsForRendering,
|
||||||
|
"escapeText": zshCompQuoteFlagDescription,
|
||||||
}
|
}
|
||||||
zshCompletionText = `
|
zshCompletionText = `
|
||||||
{{/* should accept Command (that contains subcommands) as parameter */}}
|
{{/* should accept Command (that contains subcommands) as parameter */}}
|
||||||
|
@ -41,7 +42,7 @@ function {{$cmdPath}} {
|
||||||
case $state in
|
case $state in
|
||||||
cmnds)
|
cmnds)
|
||||||
commands=({{range .Commands}}{{if not .Hidden}}
|
commands=({{range .Commands}}{{if not .Hidden}}
|
||||||
"{{.Name}}:{{.Short}}"{{end}}{{end}}
|
"{{.Name}}:{{.Short | escapeText}}"{{end}}{{end}}
|
||||||
)
|
)
|
||||||
_describe "command" commands
|
_describe "command" commands
|
||||||
;;
|
;;
|
||||||
|
@ -334,5 +335,7 @@ func zshCompFlagCouldBeSpecifiedMoreThenOnce(f *pflag.Flag) bool {
|
||||||
func zshCompQuoteFlagDescription(s string) string {
|
func zshCompQuoteFlagDescription(s string) string {
|
||||||
return strings.NewReplacer("'", `'\''`,
|
return strings.NewReplacer("'", `'\''`,
|
||||||
"[", `\[`,
|
"[", `\[`,
|
||||||
"]", `\]`).Replace(s)
|
"]", `\]`,
|
||||||
|
"$(", `\$(`,
|
||||||
|
).Replace(s)
|
||||||
}
|
}
|
||||||
|
|
|
@ -240,6 +240,25 @@ func TestGenZshCompletion(t *testing.T) {
|
||||||
`--ptest\[ptest]:filename:_files -g "-\(/\)"`,
|
`--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 {
|
for _, tc := range tcs {
|
||||||
|
|
Loading…
Add table
Reference in a new issue