|
Warning: this is an htmlized version!
The original is here, and the conversion rules are here. |
-- This file:
-- http://anggtwu.net/LUA/GetOpt1.lua.html
-- http://anggtwu.net/LUA/GetOpt1.lua
-- (find-angg "LUA/GetOpt1.lua")
-- Author: Eduardo Ochs <eduardoochs@gmail.com>
--
-- (defun e () (interactive) (find-angg "LUA/GetOpt1.lua"))
-- «.GetOpt» (to "GetOpt")
-- «.GetOpt-tests» (to "GetOpt-tests")
-- «GetOpt» (to ".GetOpt")
GetOpt = Class {
type = "GetOpt",
__tostring = function (g) return g:tostring() end,
__index = {
tostring = function (g) return mapconcat(mytostring, g.args, " ") end,
tostring = function (g) return table.concat(g.args, " ") end,
setargs = function (g,...) g.args = HTable(pack(...)); return g end,
unpack = function (g) return myunpack(g.args) end,
select = function (g,n) return select(n, g:unpack()) end,
drop = function (g,n) return g:setargs(g:select(n+1)) end,
run = function (g,...) g:setargs(...):run1() end,
run1 = function (g) if g.args[1] then g:rest(0) else g:noargs() end end,
noargs = function (g) end,
nomoreargs = function (g) end,
otherargs = function (g) print("Unprocessed: "..g:tostring()); error() end,
rest = function (g,n)
g:drop(n)
local head = g.args[1]
if not head then return g:nomoreargs() end
if g[head] then return g[head](g, g:select(2)) end
return g:otherargs()
end,
},
}
-- «GetOpt-tests» (to ".GetOpt-tests")
--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "GetOpt1.lua"
mygetopt = GetOpt {
["-one"] = function (g,a) PP("-one:", a); g:rest(2) end,
["-two"] = function (g,a,b) PP("-two:", a,b); g:rest(3) end,
}
= mygetopt:setargs("-one", "bla", "-two", "foo", "bar")
= mygetopt
= mygetopt:drop(2)
= mygetopt:drop(3)
= mygetopt:run("-one", "bla", "-two", "foo", "bar")
= mygetopt:run("-one", "bla", "-two", "foo", "bar", "???", "blop")
= mygetopt:rest(0)
= mygetopt
= mygetopt
= mygetopt:drop(0)
= mygetopt
;; (find-pil3page (+ 19 77) "8.4 Errors")
args = HTable(pack("-one", "bla", "-two", "foo", "bar")),
--]]
-- Local Variables:
-- coding: utf-8-unix
-- End: