mirror of
https://github.com/TangentFoxy/argparse.git
synced 2025-07-28 11:02:20 +00:00
Make add_complete a Parser method
This commit is contained in:
@@ -227,27 +227,6 @@ local add_help = {"add_help", function(self, value)
|
|||||||
end
|
end
|
||||||
end}
|
end}
|
||||||
|
|
||||||
local add_complete = {"add_complete", function(self, value)
|
|
||||||
typecheck("add_complete", {"nil", "string", "table"}, value)
|
|
||||||
|
|
||||||
local complete = self:option()
|
|
||||||
:description "Output a shell completion script for the specified shell."
|
|
||||||
:args(1)
|
|
||||||
:choices {"bash", "zsh", "fish"}
|
|
||||||
:action(function(_, _, shell)
|
|
||||||
print(self["get_" .. shell .. "_complete"](self))
|
|
||||||
os.exit(0)
|
|
||||||
end)
|
|
||||||
|
|
||||||
if value then
|
|
||||||
complete = complete(value)
|
|
||||||
end
|
|
||||||
|
|
||||||
if not complete._name then
|
|
||||||
complete "--completion"
|
|
||||||
end
|
|
||||||
end}
|
|
||||||
|
|
||||||
local Parser = class({
|
local Parser = class({
|
||||||
_arguments = {},
|
_arguments = {},
|
||||||
_options = {},
|
_options = {},
|
||||||
@@ -273,8 +252,7 @@ local Parser = class({
|
|||||||
typechecked("help_usage_margin", "number"),
|
typechecked("help_usage_margin", "number"),
|
||||||
typechecked("help_description_margin", "number"),
|
typechecked("help_description_margin", "number"),
|
||||||
typechecked("help_max_width", "number"),
|
typechecked("help_max_width", "number"),
|
||||||
add_help,
|
add_help
|
||||||
add_complete
|
|
||||||
})
|
})
|
||||||
|
|
||||||
local Command = class({
|
local Command = class({
|
||||||
@@ -1105,6 +1083,32 @@ function Parser:add_help_command(value)
|
|||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function Parser:add_complete(value)
|
||||||
|
if value then
|
||||||
|
assert(type(value) == "string" or type(value) == "table",
|
||||||
|
("bad argument #1 to 'add_complete' (string or table expected, got %s)"):format(type(value)))
|
||||||
|
end
|
||||||
|
|
||||||
|
local complete = self:option()
|
||||||
|
:description "Output a shell completion script for the specified shell."
|
||||||
|
:args(1)
|
||||||
|
:choices {"bash", "zsh", "fish"}
|
||||||
|
:action(function(_, _, shell)
|
||||||
|
print(self["get_" .. shell .. "_complete"](self))
|
||||||
|
os.exit(0)
|
||||||
|
end)
|
||||||
|
|
||||||
|
if value then
|
||||||
|
complete = complete(value)
|
||||||
|
end
|
||||||
|
|
||||||
|
if not complete._name then
|
||||||
|
complete "--completion"
|
||||||
|
end
|
||||||
|
|
||||||
|
return self
|
||||||
|
end
|
||||||
|
|
||||||
local function get_short_description(element)
|
local function get_short_description(element)
|
||||||
local short = element._description:match("^(.-)%.%s")
|
local short = element._description:match("^(.-)%.%s")
|
||||||
return short or element._description:match("^(.-)%.?$")
|
return short or element._description:match("^(.-)%.?$")
|
||||||
|
Reference in New Issue
Block a user