read_globals
.
。 我使用lua5.4.
如果您在常数中具有
从您的.luacheckrc
加载文件:
-- Your normal luacheck config here. We'll modify this table later.
read_globals = {
-- Can manually define globals too.
}
-- Below code will load your constants files and append them to
-- read_globals.
-- Load script with constants and add to populate dest_globals.
local function append_globals(dest_globals, script)
-- Create a fallback env so lua stdlib is available, but we
-- have a clean list of globals.
local env = setmetatable({}, {__index = _G})
local fn = loadfile(script, "t", env)
fn()
for key,val in pairs(env) do
table.insert(dest_globals, key)
end
end
-- Pass a global table created above to populate with globals:
-- read_globals, files["*.lua"].read_globals, etc.
xpcall(append_globals, print, read_globals, "absolute/path/to/constants.lua")
如果常数显示为缺失变量,请在命令行上运行Luacheck进行调试(您应该获取输出)。
不幸的是,它使用了绝对路径,因为
debug.getInfo和其他技巧在Luacheckrc中不工作 - 它们返回“块”而不是文件名。 wowever
,如果您的所有LUA代码都在一个通用目录中,则可以尝试从当前工作目录中获取绝对路径,因为Luacheck可能与您的LUA代码相同的文件夹运行:
local cwd = io.popen("cd"):read('*all')
-- All code lives in the script folder: c:/proj/script/*.lua
local root_dir = cwd:match("(.*[/\\])script[/\\]")
xpcall(append_globals, print, read_globals, root_dir .."script/constants.lua")
require
。您可以在运行时使用require
或修改它。将其放在任何电话之前::
$LUA_PATH_5_4