如何列出命令行中存在的模块并检查功能? [英] How can I list modules and check functions exist at the command line?

查看:54
本文介绍了如何列出命令行中存在的模块并检查功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就像许多(Windows)用户"一样,我不想花时间学习从源代码进行编译. 因此,Lua对于业余爱好者来说似乎是一个很好的选择.

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.如何列出可用于任何给定解释器实例的模块?

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

某些二进制发行版具有许多编译为DLL的模块,有些将其添加到主EXE中. 很高兴知道EXE中内置了哪些模块,并检查cpath是否找到了其他DLL模块.

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.有没有办法在Lua的命令行中获得帮助?

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

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

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.是否可以将GSL-Shell中的此功能修改为帮助系统的基础? (即使只是确认给定功能的存在也会有所帮助)

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

推荐答案

Q2:没有像这样的标准帮助功能.已经做出了一些努力来标准化文档格式,但是据我所知,这些方法都没有受到太大的吸引.

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:该功能肯定可以用作帮助系统的基础,前提是您已正确设置了帮助文件.

Q3: That function could certainly be used as the basis of a help system assuming you had help files set up appropriately.

这就是说,如果您只想查找给定模块中可用的功能,则通常可以转储模块表并进行查找.请参见lua演示中的全局示例.

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.

这篇关于如何列出命令行中存在的模块并检查功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆