411 REFERENCE · Wikibooks
Lua Programming/command line parameter
Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.
Lua does not use argc and argv conventions
The lua programming language does not use argc and argv conventions for the command line parameters. Instead, the command line parameters to the ../main script/ are provided through the global /table/ arg. The script name is placed into ../element/ zero of arg, and the script ../parameters go into the subsequent ../element/s.
-- Display the command line parameters
print(arg[0]) -- Name of the script
print(arg[1]) -- First parameter
print(arg[2]) -- Second parameter
Determining the number of command line parameters
The number of command line parameters can be determined by using the /length operator/:
print (#arg) -- Number of command line parameters
Negative elements
If the ../script/ is being invoked from the shell command line by providing the name of the ../script/ to the lua ../interpreter/, then the name of the interpreter and any command line parameters for the interpreter will be available in the variable arg using negative element numbers:
-- The lua interpreter name and parameters will be here
print(arg[-3])
print(arg[-2])
print(arg[-1])