mirror of
https://github.com/TangentFoxy/argparse.git
synced 2025-07-28 11:02:20 +00:00
Implement add_help_command parser property
Like add_help, but it adds a command instead of a flag.
This commit is contained in:
@@ -227,6 +227,49 @@ local add_help = {"add_help", function(self, value)
|
|||||||
end
|
end
|
||||||
end}
|
end}
|
||||||
|
|
||||||
|
local add_help_command = {"add_help_command", function(self, value)
|
||||||
|
typecheck("add_help_command", {"boolean", "string", "table"}, value)
|
||||||
|
|
||||||
|
if self._help_command_idx then
|
||||||
|
table.remove(self._commands, self._help_command_idx)
|
||||||
|
self._help_command_idx = nil
|
||||||
|
end
|
||||||
|
|
||||||
|
if value then
|
||||||
|
local help = self:command()
|
||||||
|
:description "Show help for commands."
|
||||||
|
help:argument "command"
|
||||||
|
:description "The command to show help for."
|
||||||
|
:args "?"
|
||||||
|
:action(function(_, _, cmd)
|
||||||
|
if not cmd then
|
||||||
|
print(self:get_help())
|
||||||
|
os.exit(0)
|
||||||
|
else
|
||||||
|
for _, command in ipairs(self._commands) do
|
||||||
|
for _, alias in ipairs(command._aliases) do
|
||||||
|
if alias == cmd then
|
||||||
|
print(command:get_help())
|
||||||
|
os.exit(0)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
help:error(("invalid command: %s"):format(cmd))
|
||||||
|
end)
|
||||||
|
|
||||||
|
if value ~= true then
|
||||||
|
help = help(value)
|
||||||
|
end
|
||||||
|
|
||||||
|
if not help._name then
|
||||||
|
help "help"
|
||||||
|
end
|
||||||
|
|
||||||
|
self._help_command_idx = #self._commands
|
||||||
|
end
|
||||||
|
end}
|
||||||
|
|
||||||
local Parser = class({
|
local Parser = class({
|
||||||
_arguments = {},
|
_arguments = {},
|
||||||
_options = {},
|
_options = {},
|
||||||
@@ -252,7 +295,8 @@ 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_help_command
|
||||||
})
|
})
|
||||||
|
|
||||||
local Command = class({
|
local Command = class({
|
||||||
|
Reference in New Issue
Block a user