Last active
August 29, 2015 14:20
-
-
Save mdavey/1a8e330105d046b0ee60 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
debug = true | |
port = 8080 | |
listen = 127.0.0.1 | |
debug = true | |
port = 8080 | |
listen = 127.0.0.1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment