mirror of
https://github.com/TangentFoxy/argparse.git
synced 2025-07-28 02:52:20 +00:00
Make add_help an actual field
This commit is contained in:
@@ -23,7 +23,7 @@ describe("tests related to help message generation", function()
|
||||
|
||||
it("uses custom help option", function()
|
||||
local parser = Parser "foo"
|
||||
:add_help {name = "/?"}
|
||||
:add_help "/?"
|
||||
assert.equal(table.concat({
|
||||
"Usage: foo [/?]",
|
||||
"",
|
||||
|
@@ -6,10 +6,7 @@ do -- Create classes with setters
|
||||
local function add_setters(cl, fields)
|
||||
for field, setter in pairs(fields) do
|
||||
cl[field] = function(self, value)
|
||||
if setter then
|
||||
setter(self, value)
|
||||
end
|
||||
|
||||
self["_"..field] = value
|
||||
return self
|
||||
end
|
||||
@@ -125,6 +122,28 @@ do -- Create classes with setters
|
||||
end
|
||||
end
|
||||
|
||||
local function add_help(self, param)
|
||||
if self._has_help then
|
||||
table.remove(self._options)
|
||||
self._has_help = false
|
||||
end
|
||||
|
||||
if param then
|
||||
local help = self:flag()
|
||||
:description "Show this help message and exit. "
|
||||
:action(function()
|
||||
io.stdout:write(self:get_help() .. "\r\n")
|
||||
os.exit(0)
|
||||
end)(param)
|
||||
|
||||
if not help._name then
|
||||
help "-h" "--help"
|
||||
end
|
||||
|
||||
self._has_help = true
|
||||
end
|
||||
end
|
||||
|
||||
Parser = add_setters(class {
|
||||
__name = "Parser",
|
||||
_arguments = {},
|
||||
@@ -137,7 +156,8 @@ do -- Create classes with setters
|
||||
epilog = typecheck.string "epilog",
|
||||
require_command = typecheck.boolean "require_command",
|
||||
usage = typecheck.string "usage",
|
||||
help = typecheck.string "help"
|
||||
help = typecheck.string "help",
|
||||
add_help = add_help
|
||||
})
|
||||
|
||||
Command = add_setters(Parser:extends {
|
||||
@@ -152,7 +172,8 @@ do -- Create classes with setters
|
||||
require_command = typecheck.boolean "require_command",
|
||||
action = typecheck["function"] "action",
|
||||
usage = typecheck.string "usage",
|
||||
help = typecheck.string "help"
|
||||
help = typecheck.string "help",
|
||||
add_help = add_help
|
||||
})
|
||||
|
||||
Argument = add_setters(class {
|
||||
@@ -341,30 +362,6 @@ function Parser:command(...)
|
||||
return command
|
||||
end
|
||||
|
||||
function Parser:add_help(param)
|
||||
if self._has_help then
|
||||
table.remove(self._options)
|
||||
self._has_help = false
|
||||
end
|
||||
|
||||
if param then
|
||||
local help = self:flag()
|
||||
:description "Show this help message and exit. "
|
||||
:action(function()
|
||||
io.stdout:write(self:get_help() .. "\r\n")
|
||||
os.exit(0)
|
||||
end)(param)
|
||||
|
||||
if not help._name then
|
||||
help "-h" "--help"
|
||||
end
|
||||
|
||||
self._has_help = true
|
||||
end
|
||||
|
||||
return self
|
||||
end
|
||||
|
||||
local max_usage_width = 70
|
||||
local usage_welcome = "Usage: "
|
||||
|
||||
|
Reference in New Issue
Block a user