Merge remote-tracking branch 'luarocks/refs/pull/5/head'

This commit is contained in:
daurnimator
2019-06-18 00:08:08 -07:00
3 changed files with 44 additions and 49 deletions

View File

@@ -31,13 +31,13 @@ If the property ``add_help`` of a parser is set to ``false``, no help option wil
Help command
------------
If the property ``add_help_command`` of a parser is set to ``true``, a help command will be added to it. A string or table value can be used to configure the command.
A help command can be added to the parser using the ``:add_help_command([value])`` method. It accepts an optional string or table value which is used to configure the command.
.. code-block:: lua
:linenos:
local parser = argparse()
:add_help_command(true)
:add_help_command()
parser:command "install"
:description "Install a rock."
@@ -173,7 +173,6 @@ Property Type
``require_command`` Boolean
``handle_options`` Boolean
``add_help`` Boolean or string or table
``add_help_command`` Boolean or string or table
``command_target`` String
``usage_max_width`` Number
``usage_margin`` Number

View File

@@ -1,9 +1,10 @@
#!/usr/bin/env lua
local Parser = require "argparse"
local parser = Parser()
:description "A testing program."
:add_help_command(true)
:add_help_command()
:require_command(false)
parser:argument "input"

View File

@@ -227,49 +227,6 @@ 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(("unknown 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 = {},
@@ -295,8 +252,7 @@ local Parser = class({
typechecked("help_usage_margin", "number"),
typechecked("help_description_margin", "number"),
typechecked("help_max_width", "number"),
add_help,
add_help_command
add_help
})
local Command = class({
@@ -1088,6 +1044,45 @@ function Parser:get_help()
return table.concat(blocks, "\n\n")
end
function Parser:add_help_command(value)
if value then
assert(type(value) == "string" or type(value) == "table",
("bad argument #1 to 'add_help_command' (string or table expected, got %s)"):format(type(value)))
end
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(("unknown command '%s'"):format(cmd))
end)
if value then
help = help(value)
end
if not help._name then
help "help"
end
return self
end
local function get_tip(context, wrong_name)
local context_pool = {}
local possible_name