interactive lua: command line arguments

mr calendar picture mr calendar · May 31, 2010 · Viewed 66k times · Source

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?

Answer

Norman Ramsey picture Norman Ramsey · May 31, 2010

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!).