From 46f07a670e37213cf7f6f938d7ad60fdd878106f Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Tue, 29 Apr 2014 10:28:00 +0100 Subject: [PATCH] elua: getopt support for callbacks --- src/bin/elua/lualian.lua | 11 ++++++----- src/bin/elua/modules/getopt.lua | 28 ++++++++++++++++++++-------- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/src/bin/elua/lualian.lua b/src/bin/elua/lualian.lua index 76e4f4a541..d4699c233f 100644 --- a/src/bin/elua/lualian.lua +++ b/src/bin/elua/lualian.lua @@ -7,7 +7,9 @@ local getopt = require("getopt") 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." }, + { "h", "help" , false, help = "Show this message.", + callback = getopt.help_cb(io.stdout) + }, { "v", "verbose", false, help = "Be verbose." }, { "I", "include", true, help = "Include a directory.", metavar = "DIR" @@ -35,12 +37,11 @@ local libname, modname, cprefix = nil, nil, "" local printv = function() end +local quits = { ["h"] = true } + for i, opt in ipairs(opts) do local on = opt[1] - if on == "h" then - getopt.help(arg_parser, io.stdout) - return - end + if quits[on] then return end if on == "v" then printv = print elseif on == "I" then diff --git a/src/bin/elua/modules/getopt.lua b/src/bin/elua/modules/getopt.lua index cbc564e328..f90b0e70d0 100644 --- a/src/bin/elua/modules/getopt.lua +++ b/src/bin/elua/modules/getopt.lua @@ -23,7 +23,7 @@ local is_arg = function(opt, j, descs) return false end -local parse_l = function(opts, opt, descs, args) +local parse_l = function(opts, opt, descs, args, parser) local optval local i = opt:find("=") if i then @@ -45,11 +45,14 @@ local parse_l = function(opts, opt, descs, args) elseif optval then error("option --" .. opt .. " cannot have an argument", 3) 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 end -local parse_s = function(opts, optstr, descs, args) +local parse_s = function(opts, optstr, descs, args, parser) while optstr ~= "" do local optval local opt = optstr:sub(1, 1) @@ -68,7 +71,10 @@ local parse_s = function(opts, optstr, descs, args) end optval, optstr = optstr, "" 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 return opts, args end @@ -84,10 +90,10 @@ local getopt_u = function(parser) end if args[1]:sub(1, 2) == "--" then opts, args = parse_l(opts, args[1]:sub(3), descs, - { unpack(args, 2) }) + { unpack(args, 2) }, parser) else opts, args = parse_s(opts, args[1]:sub(2), descs, - { unpack(args, 2) }) + { unpack(args, 2) }, parser) end end return opts, args @@ -164,12 +170,18 @@ M.help = function(parser, f) end end -M.geometry_parse = function(v) +M.geometry_parse_cb = function(desc, parser, v) return v:match("^(%d+):(%d+):(%d+):(%d+)$") end -M.size_parse = function(v) +M.size_parse_cb = function(desc, parser, v) return v:match("^(%d+)x(%d+)$") end +M.help_cb = function(fstream) + return function(desc, parser, v) + M.help(parser, fstream) + end +end + return M \ No newline at end of file