Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass and read arguments to a lua program?

Tags:

lua

Equivalent to main(int argc, char*argv[]) of C. For example: ./foo.lua -a -b how do I read -a and -b from foo.lua program?

like image 810
Jack Avatar asked May 21 '12 01:05

Jack


2 Answers

Command line arguments are in the global table arg. See here for details. Since there is no argparse/optparse library you will need to handle the logic for short and long switches yourself.

like image 94
SpliFF Avatar answered Oct 28 '22 17:10

SpliFF


The command line arguments are also available as real arguments to the script, which are vararg functions. So you can do:

local x,y,z = ...

If you need to loop over the command line arguments, use the arg table.

like image 28
lhf Avatar answered Oct 28 '22 17:10

lhf