mirror of
https://github.com/TangentFoxy/argparse.git
synced 2025-07-28 02:52:20 +00:00
Bash completions: complete option argument choices
This commit is contained in:
@@ -1130,6 +1130,32 @@ local function get_commands(parser)
|
|||||||
return table.concat(commands, " ")
|
return table.concat(commands, " ")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function Parser:_bash_option_args(buf, indent)
|
||||||
|
local opts = {}
|
||||||
|
for _, option in ipairs(self._options) do
|
||||||
|
if option._choices or option._minargs > 0 then
|
||||||
|
local pattern = ("%s)"):format(table.concat(option._aliases, "|"))
|
||||||
|
local compreply
|
||||||
|
if option._choices then
|
||||||
|
compreply = ('COMPREPLY=($(compgen -W "%s" -- "$cur"))')
|
||||||
|
:format(table.concat(option._choices, " "))
|
||||||
|
else
|
||||||
|
compreply = ('COMPREPLY=($(compgen -f "$cur"))')
|
||||||
|
end
|
||||||
|
table.insert(opts, (" "):rep(indent + 4) .. pattern)
|
||||||
|
table.insert(opts, (" "):rep(indent + 8) .. compreply)
|
||||||
|
table.insert(opts, (" "):rep(indent + 8) .. "return 0")
|
||||||
|
table.insert(opts, (" "):rep(indent + 8) .. ";;")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if #opts > 0 then
|
||||||
|
table.insert(buf, (" "):rep(indent) .. 'case "$prev" in')
|
||||||
|
table.insert(buf, table.concat(opts, "\n"))
|
||||||
|
table.insert(buf, (" "):rep(indent) .. "esac\n")
|
||||||
|
end
|
||||||
|
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 _, command in ipairs(self._commands) do
|
||||||
@@ -1157,6 +1183,7 @@ function Parser:_bash_cmd_completions(buf)
|
|||||||
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 then
|
||||||
table.insert(subcmds, (" "):rep(8) .. command._aliases[1] .. ")")
|
table.insert(subcmds, (" "):rep(8) .. command._aliases[1] .. ")")
|
||||||
|
command:_bash_option_args(subcmds, 12)
|
||||||
|
|
||||||
local opts
|
local opts
|
||||||
if idx == self._help_option_idx then
|
if idx == self._help_option_idx then
|
||||||
@@ -1193,6 +1220,8 @@ _%s() {
|
|||||||
]]):format(self._name, self._name, get_options(self))
|
]]):format(self._name, self._name, get_options(self))
|
||||||
table.insert(buf, head)
|
table.insert(buf, head)
|
||||||
|
|
||||||
|
self:_bash_option_args(buf, 4)
|
||||||
|
|
||||||
if #self._commands > 0 then
|
if #self._commands > 0 then
|
||||||
self:_bash_get_cmd(buf)
|
self:_bash_get_cmd(buf)
|
||||||
self:_bash_cmd_completions(buf)
|
self:_bash_cmd_completions(buf)
|
||||||
|
Reference in New Issue
Block a user