Needless Abuse of Lua
So, this is pretty awful. Cool too. Bust mostly awful.
bestoptionsna.lua
local function setoptions(f)
local options = {}
local currentname = nil
local env = setmetatable({},
{
__index = function(self, method)
if method == 'set' then
return function(name)
if currentname ~= nil then
error('need to call "to" first')
end
currentname = name
end
elseif method == 'to' then
return function(value)
if currentname == nil then
error('need to call "set" before "to"')
end
options[currentname] = value
currentname = nil
end
elseif method == 'enable' then
return function(name)
options[name] = true
end
end
end
})
-- close enough to fsetenv
load(string.dump(f), nil, nil, env)()
return options
end
local function pptable(t)
for k,v in pairs(t) do
print(k, '=', v)
end
end
-- Cool way
local options = setoptions(function()
set "port" to "8080"
set "listen" to "127.0.0.1"
enable "debug"
end)
pptable(options)
-- Boring way
options = {port='8080', listen='127.0.0.1', debug=true}
pptable(options)
Output
debug = true
port = 8080
listen = 127.0.0.1
debug = true
port = 8080
listen = 127.0.0.1