[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Reading a conf file
- From: Jerome Vuarand <jerome.vuarand@...>
- Date: Wed, 28 Sep 2016 17:41:49 +0100
2016-09-28 6:41 GMT+01:00 Russell Haley <russ.haley@gmail.com>:
> I've also considered just serializing/deserializing lua tables, but at
> some point I have to support reading and changing standard conf files
> (and eventually wpa_supplication too).
What is "standard conf files"?
> Any feedback would be dandy.
Like others that already replied, I use the Lua parser for all my
config files. I wrote a simple module that handles that:
https://bitbucket.org/doub/config
A typical usage example goes like this:
local config = require 'config'
local options = {
foo = 42,
bar = "hello",
baz = nil,
}
config.load(options, "myapp.conf")
config.args(options, ...)
if #options >= 1 then options.foo = options[1] end
config.check("myapp", {
{type(options.foo)=='number', "invalid foo"},
}, [[
usage: myapp [options] <foo>
options:
-foo <n> a number
-bar <bar> a name
]])
I don't usually serialize config, but I have a serializer module
there: https://bitbucket.org/doub/dump
With it I would save the config like that:
local dump = require 'dump'
local file = io.open("myapp.conf", "wb")
for k,v in pairs(options) do
file:write(k.." = "..dump.tostring(v).."\n")
end
file:close()
Feel free to steal ideas, pieces of code or the whole thing. And I'd
be happy to answer any question about it :D