mirror of
https://github.com/TangentFoxy/argparse.git
synced 2025-07-28 11:02:20 +00:00
Argname can be an array
This commit is contained in:
@@ -143,5 +143,19 @@ describe("tests related to usage message generation", function()
|
|||||||
parser:get_usage()
|
parser:get_usage()
|
||||||
)
|
)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
it("uses array of argnames provided by user", function()
|
||||||
|
local parser = Parser "foo"
|
||||||
|
:add_help(false)
|
||||||
|
parser:option "--pair"
|
||||||
|
:args(2)
|
||||||
|
:count "*"
|
||||||
|
:argname{"<key>", "<value>"}
|
||||||
|
|
||||||
|
assert.equal(
|
||||||
|
[=[Usage: foo [--pair <key> <value>]]=],
|
||||||
|
parser:get_usage()
|
||||||
|
)
|
||||||
|
end)
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
@@ -122,6 +122,14 @@ do -- Create classes with setters
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function argname(self, value)
|
||||||
|
if type(value) ~= "string" then
|
||||||
|
if type(value) ~= "table" then
|
||||||
|
error(("bad field 'argname' (string or table expected, got %s)"):format(type(value)))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
local function add_help(self, param)
|
local function add_help(self, param)
|
||||||
if self._has_help then
|
if self._has_help then
|
||||||
table.remove(self._options)
|
table.remove(self._options)
|
||||||
@@ -191,7 +199,7 @@ do -- Create classes with setters
|
|||||||
default = typecheck.string "default",
|
default = typecheck.string "default",
|
||||||
defmode = typecheck.string "defmode",
|
defmode = typecheck.string "defmode",
|
||||||
convert = convert,
|
convert = convert,
|
||||||
argname = typecheck.string "argname"
|
argname = argname
|
||||||
})
|
})
|
||||||
|
|
||||||
Option = add_setters(Argument:extends {
|
Option = add_setters(Argument:extends {
|
||||||
@@ -211,28 +219,27 @@ do -- Create classes with setters
|
|||||||
convert = convert,
|
convert = convert,
|
||||||
overwrite = typecheck.boolean "overwrite",
|
overwrite = typecheck.boolean "overwrite",
|
||||||
action = typecheck["function"] "action",
|
action = typecheck["function"] "action",
|
||||||
argname = typecheck.string "argname"
|
argname = argname
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
function Argument:_get_argument_list()
|
function Argument:_get_argument_list()
|
||||||
local argname = self:_get_argname()
|
|
||||||
local buf = {}
|
local buf = {}
|
||||||
local required_argname = argname
|
|
||||||
|
|
||||||
if self._default and self._defmode:find "a" then
|
|
||||||
required_argname = "[" .. argname .. "]"
|
|
||||||
end
|
|
||||||
|
|
||||||
local i = 1
|
local i = 1
|
||||||
|
|
||||||
while i <= math.min(self._minargs, 3) do
|
while i <= math.min(self._minargs, 3) do
|
||||||
table.insert(buf, required_argname)
|
local argname = self:_get_argname_i(i)
|
||||||
|
|
||||||
|
if self._default and self._defmode:find "a" then
|
||||||
|
argname = "[" .. argname .. "]"
|
||||||
|
end
|
||||||
|
|
||||||
|
table.insert(buf, argname)
|
||||||
i = i+1
|
i = i+1
|
||||||
end
|
end
|
||||||
|
|
||||||
while i <= math.min(self._maxargs, 3) do
|
while i <= math.min(self._maxargs, 3) do
|
||||||
table.insert(buf, "[" .. argname .. "]")
|
table.insert(buf, "[" .. self:_get_argname_i(i) .. "]")
|
||||||
i = i+1
|
i = i+1
|
||||||
|
|
||||||
if self._maxargs == math.huge then
|
if self._maxargs == math.huge then
|
||||||
@@ -279,6 +286,16 @@ function Argument:_get_type()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function Argument:_get_argname_i(i)
|
||||||
|
local argname = self:_get_argname()
|
||||||
|
|
||||||
|
if type(argname) == "table" then
|
||||||
|
return argname[i]
|
||||||
|
else
|
||||||
|
return argname
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
function Argument:_get_argname()
|
function Argument:_get_argname()
|
||||||
return self._argname or ("<"..self._name..">")
|
return self._argname or ("<"..self._name..">")
|
||||||
end
|
end
|
||||||
|
Reference in New Issue
Block a user