[翻译]UCI API

http://wiki.openwrt.org/doc/techref/uci

Lua上的uci

对于那些喜欢Lua的人, UCI已经将相关的代码封装到libuci-lua里. 只要安装这个包, 你就可以在Lua中使用UCI.

1
require("uci")

API

The api is quite simple

top level entry point

uci.cursor() (that instantiates a uci context instance) e.g.

1
uci = uci.cursor()

或者

1
uci = uci.cursor(nil, "/var/state")

if you want to involve state vars
on that you can call the usual operations

1
uci:get("config", "sectionname", "option")

returns string or nil for not found

1
uci:set("config", "sectionname", "option", "value")

sets simple string value

1
uci:set("config", "sectionname", "option", { "foo", "bar" })

sets list value

1
uci:delete("config", "section", "option")

deletes option

1
uci:delete("config", "section")

deletes section

1
uci:add("config", "type")

adds new anon section “type” and returns its name

1
uci:set("config", "name", "type")

adds new section “name” with type “type”

1
uci:foreach("config", "type", function(s) ... end)

iterates over all sections of type “type” and invokes callback function for each “s” within the callback. s is a table containing all options and two special properties
s[‘.type’] → section type
s[‘.name’] → section name
If the callback function returns false [NB: not nil!], foreach() will terminate at that point without iterating over any remaining sections. foreach() returns true if at least one section exists and the callback function didn’t raise an error for it; false otherwise.

1
uci:reorder("config", "sectionname", position)

Move a section to another position. Position starts at 0. This is for example handy to change the wireless config order (changing priority).

1
uci:revert("config")

discards any changes made to the configuration, that have not yet been committed

1
uci:commit("config")

commits (saves) the changed configuration to the corresponding file in /etc/config
That’s basically all you need


Arylo Yeung, Typescripter, 暂留在鹅厂
tomail:arylo.open@gmail.com