40

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?

mr calendar
  • 945
  • 5
  • 11
  • 21
  • 1
    I'm curious what "immature/cumbersome" solutions you've seen to such a [straightforward problem](http://www.google.com/search?q=lua+command+line+arguments&btnI=1). – Miles May 31 '10 at 20:36
  • 2
    http://lua-users.org/wiki/CommandLineModule seems cumbersome compared to a built in argc, argv; getopt (at C:/Program Files/Lua/5.1/docs/stdlib/getopt.html on my machine) is has TODO written all over it and the advertised example is blank. Thanks for the pointer to the manual for arg. Google didn't do for me what it did for you, I've been all over the place trying to find this out. It's one of the things about being a n00b (again) - the simplest things just don't seem to work :-( – mr calendar May 31 '10 at 21:05

5 Answers5

56

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

Norman Ramsey
  • 198,648
  • 61
  • 360
  • 533
  • 1
    You're absolutely right, I am! Whereabouts in the manual is that? I'm not finding my way round it very well ATM. Cheers – mr calendar May 31 '10 at 20:34
  • Thank you for the answer! I tried `lua -i -- one two three` on lua 5.2.4 and got `cannot open one: No such file or directory`. That makes me think the `/dev/null` is required (per `[script [args]]` in the manual, not `[script] [args]`) --- am I understanding correctly? If you don't object, I will update the answer to explain. Much appreciated! – cxw Nov 20 '17 at 14:55
  • @cxw re: `/dev/null is required` You could put any lua script path there. `/dev/null` just means there is no script to run before entering interactive mode. – Jesse Chisholm Jul 16 '19 at 20:01
26

In addition to the arg table, ... contains the arguments (arg[1] and up) used to invoke the script.

% lua -i -- /dev/null one two three
Lua 5.1.3  Copyright (C) 1994-2008 Lua.org, PUC-Rio
> print(...)
one     two     three
RBerteig
  • 41,948
  • 7
  • 88
  • 128
daurnimator
  • 4,091
  • 18
  • 34
5

Lua stores arguments in a table. This table is the "arg" table. You can access the passed arguments inside using arg[1], arg[2], ...

arg[0] is the name of the lua program. arg[1] is the first argument passed, arg[2] is the second argument passed and so on...

Rahul
  • 2,515
  • 3
  • 25
  • 29
2

If you run file.lua in cmd of freeswitch

freeswitch> luarun prog.lua arg1

You can use prog.lua:

#print(argv[1])

And run: $lua prog.lua arg1 (run in script folder) You can use prong.lua:

#print(arg[1])
0

For OP and future visitors,

The library of Lua doesn't contain injecting a table 'args' into globals from command line switches. The program built from lua.c does, however, it is near impossible to use. The reason it is impossible to use is that program does NOT like multiple switches.

This is one of the reasons why my REPL/code executor LuaConsole was built. It gives you the table args as well as sends a tuple to the root pcall function (your executing environment is really a top-level pcall with probably an error handler attached). So both args[n] and local a = {...}; a[n] ...; all work properly with as many switches as you want. For example, -e to execute code from cmd line, -l to specify libraries, etc. It supports anything lua51 and up.

If you are having troubles with the program in the library, I highly suggest you check out https://www.github.com/tilkinsc/LuaConsole as it will save you the headache of dealing with a broken-feeling program. There are also other alternatives out there such as for web, fengari.

Hope this helps you!

user2262111
  • 563
  • 1
  • 6
  • 16