简体   繁体   English

如何通过lua CLI将参数传递给lua文件

[英]How can I pass the arguments to the lua file through the lua CLI

I have a LUA CLI which takes in the lua command, 我有一个LUA CLI,它接受lua命令,

Something like this (lua)> # 像这样的东西(lua)>#

Now here inorder to execute the lua file I run the command 现在,为了执行lua文件,我运行命令

(lua)> # dofile("a.lua") (lua)>#dofile(“a.lua”)

I want a command which will execute the file and also pass a argument to it. 我想要一个执行文件的命令,并传递一个参数。

Now here I want to pass a argument to the "a.lua" file which will take this argument and call one more lua file, and this 2nd lua file is called according to the argument, So, I need to parse this argument. 现在我想把一个参数传递给“a.lua”文件,该文件将接受这个参数并再调用一个lua文件,并根据参数调用第二个lua文件,所以,我需要解析这个参数。

Please, can somebody tell me about the parsing commands that will be used in a.lua. 请有人告诉我将在a.lua中使用的解析命令。 I mean what are the functions to be used to parse it. 我的意思是用于解析它的函数是什么。

Please, can somebody tell me how to pass a argument to this file "a.lua". 拜托,有人可以告诉我如何将参数传递给这个文件“a.lua”。

Now here inorder to execute the lua file I run the command 现在,为了执行lua文件,我运行命令

This is generally not how you execute Lua files. 这通常不是您执行Lua文件的方式。 Usually, if you have some Lua script, you execute it with this command: lua a.lua . 通常,如果您有一些Lua脚本,则使用以下命令执行它: lua a.lua You don't type lua and then use the interface there to execute it. 您不要键入lua然后使用那里的接口来执行它。

Using a proper command line to execute the script, you can pass string parameters to the file: lua a.lua someParam "Param with spaces" . 使用适当的命令行来执行脚本,您可以将字符串参数传递给文件: lua a.lua someParam "Param with spaces" The a.lua script can then fetch these parameters using the standard Lua ... mechanic: 然后, a.lua脚本可以使用标准的Lua ...机制来获取这些参数:

local params = {...}
params[1] -- first parameter, if any.
params[2] -- second parameter, if any.
#params   -- number of parameters.

However, if you insist on trying to execute this using your method of invoking the interpreter (with lua ) and typing commands into it one by one, then you can do this: 但是,如果您坚持尝试使用调用解释器(使用lua )并逐个输入命令的方法来执行此操作,那么您可以这样做:

> GlobalVariable = assert(loadfile(`a.lua`))
> GlobalVariable(--[[Insert parameters here]])

However, if you don't want to do it in two steps, with the intermediate global variable, you can do it in one: 但是,如果您不想分两步使用中间全局变量,则可以在一个步骤中执行:

> assert(loadfile(`a.lua`))(--[[Insert parameters here]])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM