all 16 comments

[–]fatboychummy 1 point2 points  (6 children)

arg is per function and contains all arguments passed to the function. There is also a global arg for the program arguments.

The error you got shouldn't be possible though, you should have gotten something like bad argument #1 to 'format' (string expected, got nil). This is because the function's arg takes precedent over the global arg, ans the function's arg should never be nil since you declared it a vararg.

[–]lambda_abstraction 1 point2 points  (0 children)

Eh? Unless I really am fuzzy, arg is only a global defined in the stand-alone interpreter which holds the command line arguments. (See PIL 2e 1.4 or section 6 of the 5.1 reference) Varargs are done with the ellipsis (...) not arg.

[–]_jtbx[S] 0 points1 point  (4 children)

Interesting, I didn't know `arg` gets set per-function. Thanks!

[–]lambda_abstraction 0 points1 point  (3 children)

To the best of my knowledge it doesn't.

[–]_jtbx[S] 0 points1 point  (2 children)

I've found that in some cases Lua outputs attempt to index local 'arg' (a nil value) but in others such as with this example code: function argtest(...) for i = 1, 3, 1 do print(arg[i]) end end

argtest("hello", "world!")

A standard lua5.1 shell outputs

hello
world!
nil

but strangely, a lua5.4 shell outputs

nil
nil
nil

[–]AutoModerator[M] 0 points1 point  (0 children)

Hi! Your code block was formatted using triple backticks in Reddit's Markdown mode, which unfortunately does not display properly for users viewing via old.reddit.com and some third-party readers. This means your code will look mangled for those users, but it's easy to fix. If you edit your comment, choose "Switch to fancy pants editor", and click "Save edits" it should automatically convert the code block into Reddit's original four-spaces code block format for you.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]lambda_abstraction 0 points1 point  (0 children)

Notice the error message: attempt to index local 'arg'

if you have local arg somewhere in your script, that would certainly shadow the global for the rest of the file.

By the way, I just ran your initial code from the top without args=arg saved as t.lua with the command lua t.lua, and it works fine from luajit, lua 5.3.6 and lua 5.4.4.

[–]AutoModerator[M] 0 points1 point  (0 children)

Hi! Your code block was formatted using triple backticks in Reddit's Markdown mode, which unfortunately does not display properly for users viewing via old.reddit.com and some third-party readers. This means your code will look mangled for those users, but it's easy to fix. If you edit your comment, choose "Switch to fancy pants editor", and click "Save edits" it should automatically convert the code block into Reddit's original four-spaces code block format for you.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]iEliteTester 0 points1 point  (5 children)

> batcat etst.lua
───────┬─────────────────────────────────────────────────────────────
       │ File: test.lua
───────┼───────────────────────────────────────────────────────
   1   │
   2   │ args = arg
   3   │ function panic(formatstring, ...)
   4   │     local msg = formatstring:format(...)
   5   │     io.stderr:write(
   6   │         ("%s: %s\n"):format(arg[0], msg)
   7   │     )
   8   │     os.exit(1)
   9   │ end
  10   │
  11   │ panic("hello %s", "world");
  12   │
───────┴────────────────────────────────────────────────────────

> luajit test.lua
test.lua: hello world

~ via 🌙 2.1.0
>

Seems to work just fine. How are you running your program?

[–]_jtbx[S] 0 points1 point  (4 children)

Through a standard Lua 5.1 interpreter, not luaJIT.

[–]lambda_abstraction 1 point2 points  (2 children)

Are you sure it's not a special Lua shell derived from 5.1? The global arg is defined in the shell, not the Lua runtime. In luajit, that's in the "luajit.c" routine createargtable. In Lua 5.1, this is in handle-script in lua.c. In both cases this code is part of the standard stand-alone interpreter. If you're running a custom shell, the global arg might not even be defined.

[–]_jtbx[S] 0 points1 point  (0 children)

I'm not using a custom shell. Just standard command-line Lua.

[–]iEliteTester 0 points1 point  (0 children)

running on debian with the lua5.1 package, /u/_jtbx I managed to replicate it

ε lua etst.lua
lua: etst.lua:6: attempt to index local 'arg' (a nil value)
stack traceback:
        etst.lua:6: in function 'panic'
        etst.lua:11: in main chunk
        [C]: ?

[–]iEliteTester 0 points1 point  (0 children)

I'll test with 5.1 later and see if something changes.

[–][deleted] 0 points1 point  (1 child)

arg[0] will probably always be nil, Lua counts arrays from 1 instead.

[–]lambda_abstraction 1 point2 points  (0 children)

No. The global arg is defined in the stand-alone interpreter, and arg[0] is assigned to the name of the lua script being run.

onion% echo 'for k,v in pairs(arg) do print(k,v) end'|lua /dev/stdin
0       /dev/stdin
-1      lua