Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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,

Something like this (lua)> #

Now here inorder to execute the lua file I run the command

(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.

Please, can somebody tell me about the parsing commands that will be used in 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".

like image 284
Invictus Avatar asked Dec 05 '22 17:12

Invictus


1 Answers

Now here inorder to execute the lua file I run the command

This is generally not how you execute Lua files. Usually, if you have some Lua script, you execute it with this command: lua a.lua. You don't type lua and then use the interface there to execute it.

Using a proper command line to execute the script, you can pass string parameters to the file: lua a.lua someParam "Param with spaces". The a.lua script can then fetch these parameters using the standard Lua ... mechanic:

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:

> 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]])
like image 197
Nicol Bolas Avatar answered Feb 15 '23 22:02

Nicol Bolas