elua: lualian reorg

This commit is contained in:
Daniel Kolesa 2014-04-30 14:44:44 +01:00 committed by Daniel Kolesa
parent 5b307c282b
commit a0838343cb
2 changed files with 42 additions and 43 deletions

View File

@ -4,13 +4,24 @@
local lualian = require("lualian")
local getopt = require("getopt")
local include_dirs = {}
local output_files = {}
local gen_file = function(opts, i, fname)
local printv = opts["v"] and print or function() end
printv("Generating for file: " .. fname)
local ofile = opts["o"] and opts["o"][i] or nil
local fstream = io.stdout
if ofile then
printv(" Output file: " .. ofile)
fstream = io.open(ofile, "w")
if not fstream then
error("Cannot open output file: " .. ofile)
end
else
printv(" Output file: printing to stdout...")
end
lualian.generate(fname, opts["M"], opts["L"], opts["P"] or "", fstream)
end
local printv = function() end
local quit = false
local opts, args = getopt.parse {
getopt.parse {
usage = "Usage: %prog [OPTIONS] file1.eo file2.eo ... fileN.eo",
args = arg,
descs = {
@ -19,14 +30,12 @@ local opts, args = getopt.parse {
{ "h", "help", nil, help = "Show this message.", metavar = "CATEGORY",
callback = getopt.help_cb(io.stdout)
},
{ "v", "verbose", false, help = "Be verbose.",
callback = function() printv = print end
},
{ "v", "verbose", false, help = "Be verbose." },
{ category = "Generator" },
{ "I", "include", true, help = "Include a directory.", metavar = "DIR",
list = include_dirs
list = {}
},
{ "L", "library", true, help = "Specify a C library name." },
{ "M", "module", true, help = "Specify a module name." },
@ -35,46 +44,28 @@ local opts, args = getopt.parse {
},
{ "o", "output", true, help = "Specify output file name(s), by "
.. "default goes to stdout.",
list = output_files
list = {}
}
},
error_cb = function(parser, msg)
io.stderr:write(msg, "\n")
getopt.help(parser, io.stderr)
quit = true
end,
done_cb = function(parser, opts)
if not quit then
if opts["h"] then
quit = true
elseif not opts["L"] then
done_cb = function(parser, opts, args)
if not opts["h"] then
if not opts["L"] then
io.stderr:write("library name not specified\n")
getopt.help(parser, io.stderr)
quit = true
else
for i, v in ipairs(opts["I"] or {}) do
lualian.include_dir(v)
end
for i, fname in ipairs(args) do
gen_file(opts, i, fname)
end
end
end
end
}
if quit then return true end
for i, v in ipairs(include_dirs) do
lualian.include_dir(v)
end
for i, fname in ipairs(args) do
printv("Generating for file: " .. fname)
local fstream = io.stdout
if output_files[i] then
printv(" Output file: " .. output_files[i])
fstream = io.open(output_files[i], "w")
if not fstream then
error("Cannot open output file: " .. output_files)
end
else
printv(" Output file: printing to stdout...")
end
lualian.generate(fname, opts["M"], opts["L"], opts["P"] or "", fstream)
end
return true

View File

@ -51,8 +51,12 @@ local parse_l = function(opts, opt, descs, args, parser)
local optn = desc.alias or desc[1] or desc[2]
opts[#opts + 1] = { optn, unpack(rets) }
local optret = #rets > 1 and rets or rets[1]
opts[optn] = optret or true
if desc.list then desc.list[#desc.list + 1] = optret end
if desc.list then
desc.list[#desc.list + 1] = optret
opts[optn] = desc.list
else
opts[optn] = optret or true
end
return opts, args
end
@ -81,8 +85,12 @@ local parse_s = function(opts, optstr, descs, args, parser)
local optn = desc.alias or desc[1] or desc[2]
opts[#opts + 1] = { optn, unpack(rets) }
local optret = #rets > 1 and rets or rets[1]
opts[optn] = optret or true
if desc.list then desc.list[#desc.list + 1] = optret end
if desc.list then
desc.list[#desc.list + 1] = optret
opts[optn] = desc.list
else
opts[optn] = optret or true
end
end
return opts, args
end