--- Simplified getopt, based on Svenne Panne's Haskell GetOpt.
-- Usage:
--
options = {Option {...}, ...}
-- getopt.processArgs ()
prog = {name[, banner] [, purpose] [, notes] [, usage]}
-opt=arg
or -opt arg
.Req
(uired),
-- Opt
(ional)
-- @field var descriptive name for the argument
_G.Option = Object {_init = {"name", "desc", "type", "var"}}
--- Options table constructor: adds lookup tables for the option names
local function makeOptions (t)
t = list.concat (t or {},
{Option {{"version", "V"},
"output version information and exit"},
Option {{"help", "h"},
"display this help and exit"}}
)
local name = {}
for v in list.elems (t) do
for j, s in pairs (v.name) do
if name[s] then
warn ("duplicate option '%s'", s)
end
name[s] = v
end
end
t.name = name
return t
end
--- Produce usage info for the given options
-- @param header header string
-- @param optDesc option descriptors
-- @param pageWidth width to format to [78]
-- @return formatted string
function usageInfo (header, optDesc, pageWidth)
pageWidth = pageWidth or 78
-- Format the usage info for a single option
-- @param opt the Option table
-- @return options
-- @return description
local function fmtOpt (opt)
local function fmtName (o)
return "-" .. o
end
local function fmtArg ()
if opt.type == nil then
return ""
elseif opt.type == "Req" then
return "=" .. opt.var
else
return "[=" .. opt.var .. "]"
end
end
local textName = list.reverse (list.map (fmtName, opt.name))
textName[#textName] = textName[#textName] .. fmtArg ()
return {table.concat ({table.concat (textName, ", ")}, ", "),
opt.desc}
end
local function sameLen (xs)
local n = math.max (unpack (list.map (string.len, xs)))
for i, v in pairs (xs) do
xs[i] = string.sub (v .. string.rep (" ", n), 1, n)
end
return xs, n
end
local function paste (x, y)
return " " .. x .. " " .. y
end
local function wrapper (w, i)
return function (s)
return string.wrap (s, w, i, 0)
end
end
local optText = ""
if #optDesc > 0 then
local cols = list.transpose (list.map (fmtOpt, optDesc))
local width
cols[1], width = sameLen (cols[1])
cols[2] = list.map (wrapper (pageWidth, width + 4), cols[2])
optText = "\n\n" ..
table.concat (list.mapWith (paste,
list.transpose ({sameLen (cols[1]),
cols[2]})),
"\n")
end
return header .. optText
end
--- Emit a usage message.
function usage ()
local usage, purpose, notes = "[OPTION]... [FILE]...", "", ""
if prog.usage then
usage = prog.usage
end
if prog.purpose then
purpose = "\n" .. prog.purpose
end
if prog.notes then
notes = "\n\n"
if not string.find (prog.notes, "\n") then
notes = notes .. string.wrap (prog.notes)
else
notes = notes .. prog.notes
end
end
io.writelines (getopt.usageInfo ("Usage: " .. prog.name .. " " .. usage .. purpose,
options)
.. notes)
end
--- Simple getOpt wrapper.
-- Adds -version
/-V
and
-- -help
/-h
automatically;
-- stops program if there was an error, or if -help
or
-- -version
was used.
function processArgs ()
local totArgs = #arg
options = makeOptions (options)
local errors
_G.arg, opt, errors = getopt.getOpt (arg, options)
if (opt.version or opt.help) and prog.banner then
io.writelines (prog.banner)
end
if #errors > 0 or opt.help then
local name = prog.name
prog.name = nil
if #errors > 0 then
warn (table.concat (errors, "\n") .. "\n")
end
prog.name = name
getopt.usage ()
if #errors > 0 then
error ()
end
end
if opt.version or opt.help then
os.exit ()
end
end
_G.options = nil
-- A small and hopefully enlightening example:
if type (_DEBUG) == "table" and _DEBUG.std then
options = makeOptions ({
Option {{"verbose", "v"}, "verbosely list files"},
Option {{"output", "o"}, "dump to FILE", "Opt", "FILE"},
Option {{"name", "n"}, "only dump USER's files", "Req", "USER"},
})
function test (cmdLine)
local nonOpts, opts, errors = getopt.getOpt (cmdLine, options)
if #errors == 0 then
print ("options=" .. tostring (opts) ..
" args=" .. tostring (nonOpts) .. "\n")
else
print (table.concat (errors, "\n") .. "\n" ..
getopt.usageInfo ("Usage: foobar [OPTION...] FILE...",
options))
end
end
-- FIXME: Turn the following documentation into unit tests
prog = {name = "foobar"} -- for errors
-- Example runs:
test {"foo", "-v"}
-- options={verbose={1}} args={1=foo}
test {"foo", "--", "-v"}
-- options={} args={1=foo,2=-v}
test {"-o", "-V", "-name", "bar", "--name=baz"}
-- options={name={"baz"},version={1},output={1}} args={}
test {"-foo"}
-- unrecognized option `-foo'
-- Usage: foobar [OPTION]... [FILE]...
--
-- -v, -verbose verbosely list files
-- -o, -output[=FILE] dump to FILE
-- -n, -name=USER only dump USER's files
-- -V, -version output version information and exit
-- -h, -help display this help and exit
end