elua: getopt support for callbacks
This commit is contained in:
parent
cde3ce5220
commit
46f07a670e
|
@ -7,7 +7,9 @@ local getopt = require("getopt")
|
||||||
local opts, args, arg_parser = getopt.parse {
|
local opts, args, arg_parser = getopt.parse {
|
||||||
usage = "Usage: %prog [OPTIONS] file1.eo file2.eo ... fileN.eo",
|
usage = "Usage: %prog [OPTIONS] file1.eo file2.eo ... fileN.eo",
|
||||||
args = arg, descs = {
|
args = arg, descs = {
|
||||||
{ "h", "help" , false, help = "Show this message." },
|
{ "h", "help" , false, help = "Show this message.",
|
||||||
|
callback = getopt.help_cb(io.stdout)
|
||||||
|
},
|
||||||
{ "v", "verbose", false, help = "Be verbose." },
|
{ "v", "verbose", false, help = "Be verbose." },
|
||||||
{ "I", "include", true, help = "Include a directory.",
|
{ "I", "include", true, help = "Include a directory.",
|
||||||
metavar = "DIR"
|
metavar = "DIR"
|
||||||
|
@ -35,12 +37,11 @@ local libname, modname, cprefix = nil, nil, ""
|
||||||
|
|
||||||
local printv = function() end
|
local printv = function() end
|
||||||
|
|
||||||
|
local quits = { ["h"] = true }
|
||||||
|
|
||||||
for i, opt in ipairs(opts) do
|
for i, opt in ipairs(opts) do
|
||||||
local on = opt[1]
|
local on = opt[1]
|
||||||
if on == "h" then
|
if quits[on] then return end
|
||||||
getopt.help(arg_parser, io.stdout)
|
|
||||||
return
|
|
||||||
end
|
|
||||||
if on == "v" then
|
if on == "v" then
|
||||||
printv = print
|
printv = print
|
||||||
elseif on == "I" then
|
elseif on == "I" then
|
||||||
|
|
|
@ -23,7 +23,7 @@ local is_arg = function(opt, j, descs)
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
local parse_l = function(opts, opt, descs, args)
|
local parse_l = function(opts, opt, descs, args, parser)
|
||||||
local optval
|
local optval
|
||||||
local i = opt:find("=")
|
local i = opt:find("=")
|
||||||
if i then
|
if i then
|
||||||
|
@ -45,11 +45,14 @@ local parse_l = function(opts, opt, descs, args)
|
||||||
elseif optval then
|
elseif optval then
|
||||||
error("option --" .. opt .. " cannot have an argument", 3)
|
error("option --" .. opt .. " cannot have an argument", 3)
|
||||||
end
|
end
|
||||||
opts[#opts + 1] = { desc.alias or desc[1] or desc[2], optval }
|
local rets
|
||||||
|
if desc.callback then rets = { desc:callback(parser, optval) } end
|
||||||
|
if not rets or #rets == 0 then rets = { optval } end
|
||||||
|
opts[#opts + 1] = { desc.alias or desc[1] or desc[2], unpack(rets) }
|
||||||
return opts, args
|
return opts, args
|
||||||
end
|
end
|
||||||
|
|
||||||
local parse_s = function(opts, optstr, descs, args)
|
local parse_s = function(opts, optstr, descs, args, parser)
|
||||||
while optstr ~= "" do
|
while optstr ~= "" do
|
||||||
local optval
|
local optval
|
||||||
local opt = optstr:sub(1, 1)
|
local opt = optstr:sub(1, 1)
|
||||||
|
@ -68,7 +71,10 @@ local parse_s = function(opts, optstr, descs, args)
|
||||||
end
|
end
|
||||||
optval, optstr = optstr, ""
|
optval, optstr = optstr, ""
|
||||||
end
|
end
|
||||||
opts[#opts + 1] = { desc.alias or desc[1] or desc[2], optval }
|
local rets
|
||||||
|
if desc.callback then rets = { desc:callback(parser, optval) } end
|
||||||
|
if not rets or #rets == 0 then rets = { optval } end
|
||||||
|
opts[#opts + 1] = { desc.alias or desc[1] or desc[2], unpack(rets) }
|
||||||
end
|
end
|
||||||
return opts, args
|
return opts, args
|
||||||
end
|
end
|
||||||
|
@ -84,10 +90,10 @@ local getopt_u = function(parser)
|
||||||
end
|
end
|
||||||
if args[1]:sub(1, 2) == "--" then
|
if args[1]:sub(1, 2) == "--" then
|
||||||
opts, args = parse_l(opts, args[1]:sub(3), descs,
|
opts, args = parse_l(opts, args[1]:sub(3), descs,
|
||||||
{ unpack(args, 2) })
|
{ unpack(args, 2) }, parser)
|
||||||
else
|
else
|
||||||
opts, args = parse_s(opts, args[1]:sub(2), descs,
|
opts, args = parse_s(opts, args[1]:sub(2), descs,
|
||||||
{ unpack(args, 2) })
|
{ unpack(args, 2) }, parser)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return opts, args
|
return opts, args
|
||||||
|
@ -164,12 +170,18 @@ M.help = function(parser, f)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
M.geometry_parse = function(v)
|
M.geometry_parse_cb = function(desc, parser, v)
|
||||||
return v:match("^(%d+):(%d+):(%d+):(%d+)$")
|
return v:match("^(%d+):(%d+):(%d+):(%d+)$")
|
||||||
end
|
end
|
||||||
|
|
||||||
M.size_parse = function(v)
|
M.size_parse_cb = function(desc, parser, v)
|
||||||
return v:match("^(%d+)x(%d+)$")
|
return v:match("^(%d+)x(%d+)$")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
M.help_cb = function(fstream)
|
||||||
|
return function(desc, parser, v)
|
||||||
|
M.help(parser, fstream)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
return M
|
return M
|
Loading…
Reference in New Issue