mirror of
https://github.com/TangentFoxy/argparse.git
synced 2025-07-28 11:02: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()
|
it("uses custom help option", function()
|
||||||
local parser = Parser "foo"
|
local parser = Parser "foo"
|
||||||
:add_help {name = "/?"}
|
:add_help "/?"
|
||||||
assert.equal(table.concat({
|
assert.equal(table.concat({
|
||||||
"Usage: foo [/?]",
|
"Usage: foo [/?]",
|
||||||
"",
|
"",
|
||||||
|
@@ -6,10 +6,7 @@ do -- Create classes with setters
|
|||||||
local function add_setters(cl, fields)
|
local function add_setters(cl, fields)
|
||||||
for field, setter in pairs(fields) do
|
for field, setter in pairs(fields) do
|
||||||
cl[field] = function(self, value)
|
cl[field] = function(self, value)
|
||||||
if setter then
|
|
||||||
setter(self, value)
|
setter(self, value)
|
||||||
end
|
|
||||||
|
|
||||||
self["_"..field] = value
|
self["_"..field] = value
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
@@ -125,6 +122,28 @@ do -- Create classes with setters
|
|||||||
end
|
end
|
||||||
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 {
|
Parser = add_setters(class {
|
||||||
__name = "Parser",
|
__name = "Parser",
|
||||||
_arguments = {},
|
_arguments = {},
|
||||||
@@ -137,7 +156,8 @@ do -- Create classes with setters
|
|||||||
epilog = typecheck.string "epilog",
|
epilog = typecheck.string "epilog",
|
||||||
require_command = typecheck.boolean "require_command",
|
require_command = typecheck.boolean "require_command",
|
||||||
usage = typecheck.string "usage",
|
usage = typecheck.string "usage",
|
||||||
help = typecheck.string "help"
|
help = typecheck.string "help",
|
||||||
|
add_help = add_help
|
||||||
})
|
})
|
||||||
|
|
||||||
Command = add_setters(Parser:extends {
|
Command = add_setters(Parser:extends {
|
||||||
@@ -152,7 +172,8 @@ do -- Create classes with setters
|
|||||||
require_command = typecheck.boolean "require_command",
|
require_command = typecheck.boolean "require_command",
|
||||||
action = typecheck["function"] "action",
|
action = typecheck["function"] "action",
|
||||||
usage = typecheck.string "usage",
|
usage = typecheck.string "usage",
|
||||||
help = typecheck.string "help"
|
help = typecheck.string "help",
|
||||||
|
add_help = add_help
|
||||||
})
|
})
|
||||||
|
|
||||||
Argument = add_setters(class {
|
Argument = add_setters(class {
|
||||||
@@ -341,30 +362,6 @@ function Parser:command(...)
|
|||||||
return command
|
return command
|
||||||
end
|
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 max_usage_width = 70
|
||||||
local usage_welcome = "Usage: "
|
local usage_welcome = "Usage: "
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user