如何在命令行中列出模块并检查函数是否存在?

6

像许多“(Windows)用户”一样,我不想花时间学习如何从源代码编译任何东西。因此,Lua似乎是业余爱好者的一个非常好的选择。

如果这是一个非常简单的问题,我很抱歉——

Q1. 如何列出可用于任何给定解释器实例的模块?

某些二进制发行版已经编译了许多模块作为DLL,而有些则将它们添加到主EXE中。知道哪些模块内置在EXE中,并检查cpath是否找到任何其他DLL模块,会很好。

Q2. 有没有办法在Lua命令行中获得帮助?

由于我是新手,我想要一种简单的方法来获取任何给定函数的帮助。在一些解释性语言中,有一个help("fname")函数,Matlab就是一个很好的例子。

Q3. GSL-Shell中的这个函数能否修改为帮助系统的基础?(即使它只是确认给定函数的存在也会有所帮助)

local ffi = require 'ffi'

local help_files = {'graphics', 'matrix', 'iter', 'integ', 'ode', 'nlfit', 'vegas', 'rng', 'fft'}

local cdata_table = {'matrix', 'complex matrix', 'complex'}

local function help_init( ... )
    local REG = debug.getregistry()
    REG['GSL.help_hook'] = {}
end

local function open_module(modname)
    local fullname = string.format('help/%s', modname)
    local m = require(fullname)
    return m
end

local function search_help(func)
    for k, modname in ipairs(help_files) do
        local mt = getmetatable(func)
        local module = open_module(modname)
        if module[func] then
            local help_text = module[func]
            return help_text
        end
    end
end
help_init()
-- declare a global function function help(x) local txt if type(x) == 'function' then txt = search_help(x) elseif type(x) == 'userdata' then local mt = getmetatable(x) if mt then txt = search_help(mt) end elseif type(x) == 'cdata' then local cname = gsl_type(x) if cname then txt = search_help(cname) end end --- Could we check that the function exists? print(txt or "未找到给定函数的帮助") end

Q1:没有内置或真正集中的方法来做到这一点。我的建议是查看LuaDistLuaRocks Lua软件包管理器 :)另外...也许您会发现LFW(Lua for Windows)项目也很有用... - Kamiccolo
1个回答

1

问题2:没有像那样的标准帮助功能。已经有一些努力标准化文档格式,但据我所知,它们中的任何一个都没有得到很大的关注。

问题3:如果您已经适当地设置了帮助文件,那么该函数肯定可以用作帮助系统的基础。

话虽如此,如果您只想找出给定模块中可用的函数,通常可以转储模块表并找出。例如,请参见lua演示中的全局示例


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接