lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Thanks, I was thinking about using debug but thought I might be overlooking something cleaner…

On Jan 4, 2013 2:29 AM, "Paul K" <paulclinger@yahoo.com> wrote:
> is there a way for a piece of Lua code to detect if it has been called as a
> standalone script from command line, or loaded during require() or dofile()?

You can probably use something like this:

print(debug.getinfo(3, "S"), #{...}, select(1, ...))

If you put this info module foo.lua

- from require this outputs (you get module name as a parameter in ...):
table:... 1 foo

- from dofile this outputs:
table:... 0

- from command line this outputs:
nil 0

If you pass more parameters from the command line, then the number of
arguments is going to be non-zero, but getinfo will still return nil.

CLI = debug.getinfo(3, "S") == nil
DOF = not CLI and #{...} == 0
REQ = not CLI and not DOF

Paul.

On Thu, Jan 3, 2013 at 4:47 PM, František Fuka <fuka@fuxoft.cz> wrote:
> Hello,
>
> is there a way for a piece of Lua code to detect if it has been called as a
> standalone script from command line, or loaded during require() or dofile()?
>
> --
> -- Frantisek Fuka
> (yes, that IS my real name)
>
> -- My Google+ profile
> -- Personal Homepage
> -- Jabber/GoogleTalk ID: fuka@fuxoft.cz