Implement add_help_command parser property

Like add_help, but it adds a command instead of a flag.
This commit is contained in:
Paul Ouellette
2019-05-22 21:54:25 -04:00
parent c5af087ad0
commit 6e3c4d5305

View File

@@ -227,6 +227,49 @@ local add_help = {"add_help", function(self, value)
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({
_arguments = {},
_options = {},
@@ -252,7 +295,8 @@ local Parser = class({
typechecked("help_usage_margin", "number"),
typechecked("help_description_margin", "number"),
typechecked("help_max_width", "number"),
add_help
add_help,
add_help_command
})
local Command = class({