Jack
Jack

Reputation: 16724

How to pass and read arguments to a lua program?

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?

Upvotes: 12

Views: 20018

Answers (3)

Kipras
Kipras

Reputation: 2704

You can use the excellent argparse library. Docs are here: http://argparse.readthedocs.io/en/stable/

Upvotes: 1

lhf
lhf

Reputation: 72412

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.

Upvotes: 7

SpliFF
SpliFF

Reputation: 39014

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.

Upvotes: 12

Related Questions