diff --git a/src/argparse.lua b/src/argparse.lua index 4a28d05..9d4f401 100644 --- a/src/argparse.lua +++ b/src/argparse.lua @@ -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({