Complete options if current word starts with dash

This commit is contained in:
Paul Ouellette
2019-06-11 00:23:40 -04:00
parent 5b733ba50a
commit f18902eb4c

View File

@@ -1162,11 +1162,11 @@ end
function Parser:_bash_get_cmd(buf) function Parser:_bash_get_cmd(buf)
local cmds = {} local cmds = {}
for _, command in ipairs(self._commands) do for idx, command in ipairs(self._commands) do
local pattern = ("%s)"):format(table.concat(command._aliases, "|")) table.insert(cmds, (" "):rep(12) .. ("%s)"):format(table.concat(command._aliases, "|")))
local cmd_assign = ('cmd="%s"'):format(command._aliases[1]) if idx ~= self._help_command_idx then
table.insert(cmds, (" "):rep(12) .. pattern) table.insert(cmds, (" "):rep(16) .. ('cmd="%s"'):format(command._aliases[1]))
table.insert(cmds, (" "):rep(16) .. cmd_assign) end
table.insert(cmds, (" "):rep(16) .. "break") table.insert(cmds, (" "):rep(16) .. "break")
table.insert(cmds, (" "):rep(16) .. ";;") table.insert(cmds, (" "):rep(16) .. ";;")
end end
@@ -1185,17 +1185,10 @@ end
function Parser:_bash_cmd_completions(buf) function Parser:_bash_cmd_completions(buf)
local subcmds = {} local subcmds = {}
for idx, command in ipairs(self._commands) do for idx, command in ipairs(self._commands) do
if #command._options > 0 then if #command._options > 0 and idx ~= self._help_command_idx then
table.insert(subcmds, (" "):rep(8) .. command._aliases[1] .. ")") table.insert(subcmds, (" "):rep(8) .. command._aliases[1] .. ")")
command:_bash_option_args(subcmds, 12) command:_bash_option_args(subcmds, 12)
table.insert(subcmds, (" "):rep(12) .. ('opts="$opts %s"'):format(get_options(command)))
local opts
if idx == self._help_option_idx then
opts = ('opts="%s"'):format(get_commands(self))
else
opts = ('opts="$opts %s"'):format(get_options(command))
end
table.insert(subcmds, (" "):rep(12) .. opts)
table.insert(subcmds, (" "):rep(12) .. ";;") table.insert(subcmds, (" "):rep(12) .. ";;")
end end
end end
@@ -1203,7 +1196,7 @@ function Parser:_bash_cmd_completions(buf)
local cmd_completions = ([[ local cmd_completions = ([[
case "$cmd" in case "$cmd" in
%s) %s)
opts="$opts %s" COMPREPLY=($(compgen -W "%s" -- "$cur"))
;; ;;
%s %s
esac esac
@@ -1212,17 +1205,14 @@ function Parser:_bash_cmd_completions(buf)
end end
function Parser:get_bash_complete() function Parser:get_bash_complete()
local buf = {} local buf = {([[
local head = ([[
_%s() { _%s() {
local cur prev cmd opts arg local cur prev cmd opts arg
cur="${COMP_WORDS[COMP_CWORD]}" cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}" prev="${COMP_WORDS[COMP_CWORD-1]}"
cmd="%s" cmd="%s"
opts="%s" opts="%s"
]]):format(self._name, self._name, get_options(self)) ]]):format(self._name, self._name, get_options(self))}
table.insert(buf, head)
self:_bash_option_args(buf, 4) self:_bash_option_args(buf, 4)
@@ -1231,7 +1221,11 @@ _%s() {
self:_bash_cmd_completions(buf) self:_bash_cmd_completions(buf)
end end
table.insert(buf, ' COMPREPLY=($(compgen -W "$opts" -- "$cur"))') table.insert(buf, [=[
if [[ "$cur" = -* ]]
then
COMPREPLY=($(compgen -W "$opts" -- "$cur"))
fi]=])
table.insert(buf, "}\n") table.insert(buf, "}\n")
local complete = ("complete -F _%s -o bashdefault -o default %s") local complete = ("complete -F _%s -o bashdefault -o default %s")
:format(self._name, self._name) :format(self._name, self._name)