interactive lua: command line arguments

自古美人都是妖i 提交于 2019-12-03 08:16:15

问题


I wish to do

 lua prog.lua arg1 arg2

from the command line

Inside prog.lua, I want to say, for instance

print (arg1, arg2, '\n')

Lua doesn't seem to have argv[1] etc and the methods I've seen for dealing with command line arguments seem to be immature and / or cumbersome. Am I missing something?


回答1:


You're missing the arg vector, which has the elements you want in arg[1], arg[2], and so on:

% lua -i -- /dev/null one two three
Lua 5.1.3  Copyright (C) 1994-2008 Lua.org, PUC-Rio
> print(arg[2])
two
> 

More info in the Lua manual section on Lua standalone (thanks Miles!).




回答2:


In addition to the arg table, ... contains the arguments (arg[1] and up) used to invoke the script.

% lua -i -- /dev/null one two three
Lua 5.1.3  Copyright (C) 1994-2008 Lua.org, PUC-Rio
> print(...)
one     two     three



回答3:


Lua stores arguments in a table. This table is the "arg" table. You can access the passed arguments inside using arg[1], arg[2], ...

arg[0] is the name of the lua program. arg[1] is the first argument passed, arg[2] is the second argument passed and so on...




回答4:


If you run file.lua in cmd of freeswitch

freeswitch> luarun prog.lua arg1

You can use prog.lua:

#print(argv[1])

And run: $lua prog.lua arg1 (run in script folder) You can use prong.lua:

#print(arg[1])


来源:https://stackoverflow.com/questions/2945819/interactive-lua-command-line-arguments

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