Completions: validate/escape some missed things

This commit is contained in:
Paul Ouellette
2019-07-23 17:46:16 -04:00
parent a0c5ddf102
commit 5f0cf3721e
3 changed files with 15 additions and 5 deletions

View File

@@ -96,7 +96,7 @@ _comptest() {
{-h,--help}"[Show this help message and exit]" \ {-h,--help}"[Show this help message and exit]" \
"--completion[Output a shell completion script for the specified shell]: :(bash zsh fish)" \ "--completion[Output a shell completion script for the specified shell]: :(bash zsh fish)" \
"*"{-v,--verbose}"[Set the verbosity level]" \ "*"{-v,--verbose}"[Set the verbosity level]" \
{-f,--files}"[A description with illegal \' characters]:*: :_files" \ {-f,--files}"[A description with illegal \"' characters]:*: :_files" \
": :_comptest_cmds" \ ": :_comptest_cmds" \
"*:: :->args" \ "*:: :->args" \
&& return 0 && return 0
@@ -193,7 +193,7 @@ complete -c comptest -n '__fish_use_subcommand' -xa 'admin' -d 'Rock server admi
complete -c comptest -s h -l help -d 'Show this help message and exit' complete -c comptest -s h -l help -d 'Show this help message and exit'
complete -c comptest -l completion -xa 'bash zsh fish' -d 'Output a shell completion script for the specified shell' complete -c comptest -l completion -xa 'bash zsh fish' -d 'Output a shell completion script for the specified shell'
complete -c comptest -s v -l verbose -d 'Set the verbosity level' complete -c comptest -s v -l verbose -d 'Set the verbosity level'
complete -c comptest -s f -l files -r -d 'A description with illegal \\\' characters' complete -c comptest -s f -l files -r -d 'A description with illegal "\' characters'
complete -c comptest -n '__fish_seen_subcommand_from help' -xa 'help completion install i admin' complete -c comptest -n '__fish_seen_subcommand_from help' -xa 'help completion install i admin'
complete -c comptest -n '__fish_seen_subcommand_from help' -s h -l help -d 'Show this help message and exit' complete -c comptest -n '__fish_seen_subcommand_from help' -s h -l help -d 'Show this help message and exit'

View File

@@ -12,7 +12,7 @@ parser:flag "-v --verbose"
:count "*" :count "*"
parser:option "-f --files" parser:option "-f --files"
:description "A description with illegal \\' characters." :description "A description with illegal \"' characters."
:args "+" :args "+"
local install = parser:command "install i" local install = parser:command "install i"

View File

@@ -1110,6 +1110,15 @@ function Parser:_is_shell_safe()
end end
end end
end end
for _, argument in ipairs(self._arguments) do
if argument._choices then
for _, choice in ipairs(argument._choices) do
if choice:find("[%s'\"]") then
return false
end
end
end
end
for _, command in ipairs(self._commands) do for _, command in ipairs(self._commands) do
if not command:_is_shell_safe() then if not command:_is_shell_safe() then
return false return false
@@ -1314,7 +1323,8 @@ function Parser:_zsh_arguments(buf, cmd_name, indent)
table.insert(line, option._name) table.insert(line, option._name)
end end
if option._description then if option._description then
table.insert(line, "[" .. get_short_description(option) .. "]") local description = get_short_description(option):gsub('["%]:]', "\\%0")
table.insert(line, "[" .. description .. "]")
end end
if option._maxargs == math.huge then if option._maxargs == math.huge then
table.insert(line, ":*") table.insert(line, ":*")
@@ -1369,7 +1379,7 @@ function Parser:_zsh_cmds(buf, cmd_name)
table.insert(line, '"' .. command._name) table.insert(line, '"' .. command._name)
end end
if command._description then if command._description then
table.insert(line, ":" .. get_short_description(command)) table.insert(line, ":" .. get_short_description(command):gsub('["]', "\\%0"))
end end
table.insert(buf, " " .. table.concat(line) .. '"') table.insert(buf, " " .. table.concat(line) .. '"')
end end