elua: error callback in getopt

This commit is contained in:
Daniel Kolesa 2014-04-29 10:41:39 +01:00 committed by Daniel Kolesa
parent c9b1378217
commit 131826a083
2 changed files with 13 additions and 12 deletions

View File

@ -17,8 +17,8 @@ local opts, args, arg_parser = getopt.parse {
usage = "Usage: %prog [OPTIONS] file1.eo file2.eo ... fileN.eo",
args = arg, descs = {
{ "h", "help", false, help = "Show this message.",
callback = function(d, p)
getopt.help_cb(io.stdout)(d, p)
callback = function(d, parser)
getopt.help(parser, io.stdout)
quit = true
end
},
@ -45,15 +45,13 @@ local opts, args, arg_parser = getopt.parse {
output_files[#output_files + 1] = v
end
}
}
}, error_cb = function(parser, msg)
io.stderr:write(msg, "\n")
getopt.help(parser, io.stderr)
quit = true
end
}
if not opts then
io.stderr:write(args, "\n")
getopt.help(arg_parser, io.stderr)
return
end
if quit then return end
if not libname then

View File

@ -2,17 +2,17 @@
local M = {}
local prefixes = { "-", "--" }
local get_desc = function(opt, j, descs)
for i, v in ipairs(descs) do
if v[j] == opt then
return v
end
end
error("option --" .. opt .. " not recognized", 4)
error("option " .. prefixes[j] .. opt .. " not recognized", 4)
end
local prefixes = { "-", "--" }
local is_arg = function(opt, j, descs)
if opt == "--" then return true end
for i, v in ipairs(descs) do
@ -102,6 +102,9 @@ end
M.parse = function(parser)
local ret, opts, args = pcall(getopt_u, parser)
if not ret then
if parser.error_cb then
parser:error_cb(opts)
end
return nil, opts
end
return opts, args, parser