[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Auto-detecting whether script is stand-alone or module?
- From: Paul K <paulclinger@...>
- Date: Thu, 3 Jan 2013 17:27:18 -0800
> 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