How can I list modules and check functions exist at the command line?

杀马特。学长 韩版系。学妹 提交于 2019-12-10 14:51:36

问题


Like many "(windows) users" I do not want to spend time learning to compile anything from source. So Lua seems a very good choice for a hobbyist.

Sorry if this is a very simple problem - but...

Q1. How can I list the modules available to any given instance of the interpreter?

Some binary distros have a number of modules compiled as DLLs, and some have them added into the main EXE. It would be nice to know which modules are built in to the EXE, and check the cpath is finding any other DLL modules.

Q2. Is there a way to get help at the command line in Lua?

As I am new to Lua I would like an easy way to get help for any given function. In some interpreted languages there is a help("fname") function, Matlab is a good example.

Q3. Could this function from GSL-Shell be modified as a basis for a help system? (Even if it just confirmed the existence of a given function it would help)

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 "No help found for the given function")
end

回答1:


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: That function could certainly be used as the basis of a help system assuming you had help files set up appropriately.

That being said if you just want to find out what functions are available from a given module you can generally just dump the modules table and find out. See the globals example from the lua demo as an example.



来源:https://stackoverflow.com/questions/19767135/how-can-i-list-modules-and-check-functions-exist-at-the-command-line

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!