Lua Programming/command line parameter

Lua does not use argc and argv conventions

edit

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 Lua Programming/parameters go into the subsequent elements.

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

edit

The number of command line parameters can be determined by using the length operator:

print (#arg)    -- Number of command line parameters

Negative elements

edit

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])