mirror of
https://github.com/clap-rs/clap
synced 2025-03-04 15:27:16 +00:00
fix: Escape special characters properly for zsh
Signed-off-by: Huan-Cheng Chang <changhc84@gmail.com>
This commit is contained in:
parent
65e5a705bc
commit
a379bae895
2 changed files with 37 additions and 6 deletions
|
@ -426,6 +426,9 @@ fn escape_help(string: &str) -> String {
|
||||||
.replace('\'', "'\\''")
|
.replace('\'', "'\\''")
|
||||||
.replace('[', "\\[")
|
.replace('[', "\\[")
|
||||||
.replace(']', "\\]")
|
.replace(']', "\\]")
|
||||||
|
.replace(':', "\\:")
|
||||||
|
.replace('$', "\\$")
|
||||||
|
.replace('`', "\\`")
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Escape value string inside single quotes and parentheses
|
/// Escape value string inside single quotes and parentheses
|
||||||
|
@ -433,6 +436,11 @@ fn escape_value(string: &str) -> String {
|
||||||
string
|
string
|
||||||
.replace('\\', "\\\\")
|
.replace('\\', "\\\\")
|
||||||
.replace('\'', "'\\''")
|
.replace('\'', "'\\''")
|
||||||
|
.replace('[', "\\[")
|
||||||
|
.replace(']', "\\]")
|
||||||
|
.replace(':', "\\:")
|
||||||
|
.replace('$', "\\$")
|
||||||
|
.replace('`', "\\`")
|
||||||
.replace('(', "\\(")
|
.replace('(', "\\(")
|
||||||
.replace(')', "\\)")
|
.replace(')', "\\)")
|
||||||
.replace(' ', "\\ ")
|
.replace(' ', "\\ ")
|
||||||
|
@ -688,3 +696,26 @@ fn write_positionals_of(p: &Command) -> String {
|
||||||
|
|
||||||
ret.join("\n")
|
ret.join("\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use crate::shells::zsh::{escape_help, escape_value};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_escape_value() {
|
||||||
|
let raw_string = "\\ [foo]() `bar https://$PATH";
|
||||||
|
assert_eq!(
|
||||||
|
escape_value(raw_string),
|
||||||
|
"\\\\\\ \\[foo\\]\\(\\)\\ \\`bar\\ https\\://\\$PATH"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_escape_help() {
|
||||||
|
let raw_string = "\\ [foo]() `bar https://$PATH";
|
||||||
|
assert_eq!(
|
||||||
|
escape_help(raw_string),
|
||||||
|
"\\\\ \\[foo\\]() \\`bar https\\://\\$PATH"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -17,10 +17,10 @@ _my-app() {
|
||||||
_arguments "${_arguments_options[@]}" \
|
_arguments "${_arguments_options[@]}" \
|
||||||
'--single-quotes[Can be '\''always'\'', '\''auto'\'', or '\''never'\'']' \
|
'--single-quotes[Can be '\''always'\'', '\''auto'\'', or '\''never'\'']' \
|
||||||
'--double-quotes[Can be "always", "auto", or "never"]' \
|
'--double-quotes[Can be "always", "auto", or "never"]' \
|
||||||
'--backticks[For more information see `echo test`]' \
|
'--backticks[For more information see \`echo test\`]' \
|
||||||
'--backslash[Avoid '\''\\n'\'']' \
|
'--backslash[Avoid '\''\\n'\'']' \
|
||||||
'--brackets[List packages \[filter\]]' \
|
'--brackets[List packages \[filter\]]' \
|
||||||
'--expansions[Execute the shell command with $SHELL]' \
|
'--expansions[Execute the shell command with \$SHELL]' \
|
||||||
'-h[Print help]' \
|
'-h[Print help]' \
|
||||||
'--help[Print help]' \
|
'--help[Print help]' \
|
||||||
'-V[Print version]' \
|
'-V[Print version]' \
|
||||||
|
@ -124,10 +124,10 @@ _my-app_commands() {
|
||||||
local commands; commands=(
|
local commands; commands=(
|
||||||
'cmd-single-quotes:Can be '\''always'\'', '\''auto'\'', or '\''never'\''' \
|
'cmd-single-quotes:Can be '\''always'\'', '\''auto'\'', or '\''never'\''' \
|
||||||
'cmd-double-quotes:Can be "always", "auto", or "never"' \
|
'cmd-double-quotes:Can be "always", "auto", or "never"' \
|
||||||
'cmd-backticks:For more information see `echo test`' \
|
'cmd-backticks:For more information see \`echo test\`' \
|
||||||
'cmd-backslash:Avoid '\''\\n'\''' \
|
'cmd-backslash:Avoid '\''\\n'\''' \
|
||||||
'cmd-brackets:List packages \[filter\]' \
|
'cmd-brackets:List packages \[filter\]' \
|
||||||
'cmd-expansions:Execute the shell command with $SHELL' \
|
'cmd-expansions:Execute the shell command with \$SHELL' \
|
||||||
'help:Print this message or the help of the given subcommand(s)' \
|
'help:Print this message or the help of the given subcommand(s)' \
|
||||||
)
|
)
|
||||||
_describe -t commands 'my-app commands' commands "$@"
|
_describe -t commands 'my-app commands' commands "$@"
|
||||||
|
@ -197,10 +197,10 @@ _my-app__help_commands() {
|
||||||
local commands; commands=(
|
local commands; commands=(
|
||||||
'cmd-single-quotes:Can be '\''always'\'', '\''auto'\'', or '\''never'\''' \
|
'cmd-single-quotes:Can be '\''always'\'', '\''auto'\'', or '\''never'\''' \
|
||||||
'cmd-double-quotes:Can be "always", "auto", or "never"' \
|
'cmd-double-quotes:Can be "always", "auto", or "never"' \
|
||||||
'cmd-backticks:For more information see `echo test`' \
|
'cmd-backticks:For more information see \`echo test\`' \
|
||||||
'cmd-backslash:Avoid '\''\\n'\''' \
|
'cmd-backslash:Avoid '\''\\n'\''' \
|
||||||
'cmd-brackets:List packages \[filter\]' \
|
'cmd-brackets:List packages \[filter\]' \
|
||||||
'cmd-expansions:Execute the shell command with $SHELL' \
|
'cmd-expansions:Execute the shell command with \$SHELL' \
|
||||||
'help:Print this message or the help of the given subcommand(s)' \
|
'help:Print this message or the help of the given subcommand(s)' \
|
||||||
)
|
)
|
||||||
_describe -t commands 'my-app help commands' commands "$@"
|
_describe -t commands 'my-app help commands' commands "$@"
|
||||||
|
|
Loading…
Add table
Reference in a new issue