如何在Lua解释器中创建新命令 [英] How to create new commands in the Lua interpreter
问题描述
我在ubuntu上
因此在lua解释器中,您显然可以调用内置函数,例如
So in the lua interpreter you can obviously call built in functions like
> functionName(functionArgs)
> functionName(functionArgs)
我想创建一个新功能,使lua解释器在每次键入它时都能识别它.
I want to make a new function that the lua interpreter recognizes every time I type it.
有没有一种方法可以将我的函数添加到lua解释器中的本机识别函数列表中,这样我就不必在写入它的文件上调用dofile()并从那里运行它.
Is there a way that I can add my function to the list of natively recognized functions in the lua interpreter so that I don't have to call dofile() on the file I wrote it in and then run it from there.
TLDR ,我希望能够输入
> myNewFunction(functionArgs)
> myNewFunction(functionArgs)
随时在lua解释器中,并让解释器自动知道我在说什么功能.
in the lua interpreter at any time and have the interpreter automatically know what function I am talking about.
如果这是不可能的,那么至少在我位于任何目录中时,是否至少有一种方法可以运行dofile(myFile),并且lua解释器将始终能够找到包含函数的特定文件?
If this is impossible is there at at least a way that I can run dofile(myFile) while I am in any directory and the lua interpreter would always be able to find my specific file containing my function?
感谢您的帮助!
推荐答案
要查看的是LUA_INIT
(或LUA_INIT_5_3
,…)环境变量:
The thing to look at is the LUA_INIT
(or LUA_INIT_5_3
, …) environment variable:
在不带选项
-E
的情况下调用时,解释器在运行任何参数之前检查环境变量LUA_INIT_5_3
(如果未定义版本名称,则检查LUA_INIT
).如果变量内容的格式为@filename
,则lua
执行该文件.否则,lua
会自行执行字符串.
– https://www.lua.org/manual/5.3/manual.html #7
When called without option
-E
, the interpreter checks for an environment variableLUA_INIT_5_3
(orLUA_INIT
if the versioned name is not defined) before running any argument. If the variable content has the format@filename
, thenlua
executes the file. Otherwise,lua
executes the string itself.
– https://www.lua.org/manual/5.3/manual.html#7
如果您有固定的功能列表,则可以简单地创建一个文件(例如,在Windows上为${HOME}/.lua_init.lua
,可以尝试使用%APPDATA%\something
或%USERPROFILE%\something
.)然后,将功能放入该文件并设置其中一个LUA_INIT
环境变量指向该文件,并在文件路径前添加'@'. unixoid OSen的小例子:
If you have a fixed list of functions, you can simply create a file (e.g. ${HOME}/.lua_init.lua
, on Windows maybe try %APPDATA%\something
or %USERPROFILE%\something
.) Then, put your functions in that file and set one of the LUA_INIT
environment variables pointing at that file, prefixing the file path with an '@'. Small example for unixoid OSen:
$ cd # just to ensure that we are in ${HOME}
$ echo "function ping( ) print 'pong' end" >> .lua_init.lua
$ echo 'export LUA_INIT="@${HOME}/.lua_init.lua"' >> .profile
$ source .profile
$ lua
Lua 5.3.3 Copyright (C) 1994-2016 Lua.org, PUC-Rio
> ping()
pong
(对于Windows,请参见下面的Egor Skriptunoff的评论.)
(For Windows, see the comment by Egor Skriptunoff below.)
如果要自动从当前目录加载内容,则将更加困难.一种简单的方法是设置以上内容,然后添加例如
If you want to automatically load stuff from the current directory, that will be harder. A simple way would be to set up the above, then add e.g.
-- autoload '.autoload.lua' in current directory if present
if io.open( ".autoload.lua" ) then -- exists, run it
-- use pcall so we don't brick the interpreter if the
-- file contains an error but can continue anyway
local ok, err = pcall( dofile, ".autoload.lua" )
if not ok then print( "AUTOLOAD ERROR: ", err ) end
end
-- GAPING SECURITY HOLE WARNING: automatically running a file
-- with the right name in any folder could run untrusted code.
-- If you actually use this, add a whitelist of known-good
-- project directories or at the very least blacklist your
-- downloads folder, /tmp, and whatever else might end up
-- containing a file with the right name but not written by you.
放入LUA_INIT
文件.然后,要使项目/目录特定的功能自动加载,请创建一个.autoload.lua
,根据需要创建dofile
s或require
s文件,定义功能,...
into the LUA_INIT
file. Then, to get project/directory-specific functions to load automatically, create a .autoload.lua
that dofile
s or require
s files as needed, defines functions, ...
更专业的解决方案(每个文件夹不需要额外的文件)将很难执行,但是您可以运行任意Lua代码来构建所需的内容.
Fancier solutions (not requiring an extra file per folder) will be harder to do, but you can run arbitrary Lua code to build whatever you need.
这篇关于如何在Lua解释器中创建新命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!