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

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

很抱歉如果这是一个非常简单的问题 - 但是...

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

一些二进制发行版已经编译了许多作为DLL的模块,而有些则将它们添加到主EXE中。知道EXE中构建了哪些模块,检查cpath是否发现了其他DLL模块将是一个不错的选择。

Q2.是否有一种方式可以在Lua的命令行中获取帮助信息?

由于我是Lua的新手,我希望有一种简单的方式来获取任何给定函数的帮助信息。在一些解释型语言中,存在help(“fname”)函数,例如Matlab。

Q3.是否可以修改来自GSL-Shell的此函数以成为帮助系统的基础?

(即使只是确认给定函数的存在,也会有所帮助)


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)
    返回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 =模块[func]
            返回help_text
        end
    end
end

help_init()

-- 声明一个全局函数
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
    ---我们可以检查该函数是否存在吗?
    print(txt or“未找到给定函数的帮助信息”)
end
点赞
用户258523
用户258523

Q2:

There isn't any standard help feature like that. There have been a couple of efforts to standardize on a documentation format but, to my knowledge, none of them has ever gotten much traction.

目前还没有像那样的标准帮助功能。已经有了一些努力去标准化文档格式,但据我所知,它们都没有得到大量关注。

Q3:

那个函数肯定可以被用作帮助系统的基础,只要你已经适当地设置了帮助文件。

话虽如此,如果你只想找出给定模块可用的函数,通常可以转储模块表并找出。查看 Lua 演示中的 全局例子 作为参考。

2013-11-04 17:32:31