Permalink
Browse files

app.lua returns the full command line, plus the Lua executable used; …

…this allows us to use run.lua with arbitrary Lua flags
  • Loading branch information...
1 parent 5c636f0 commit 6f00e1cfee3ec8ad3742b537e8da2ee5be0f7256 @stevedonovan committed May 12, 2012
Showing with 60 additions and 33 deletions.
  1. +30 −28 examples/which.lua
  2. +21 −0 lua/pl/app.lua
  3. +8 −4 run.lua
  4. +1 −1 tests/test-fenv.lua
View
@@ -1,28 +1,30 @@
--- a simple implementation of the which command. This looks for
--- the given file on the path. On windows, it will assume an extension
--- of .exe if no extension is given.
-local List = require 'pl.List'
-local path = require 'pl.path'
-
-local pathl = List.split(os.getenv 'PATH',path.dirsep)
-
-function which (file)
- local res = pathl:map(path.join,file)
- res = res:filter(path.exists)
- if res then return res[1] end
-end
-
-local file = arg[1] or arg[-1] -- i.e. location of lua executable
-local try
-
-if not file then return print 'must provide a filename' end
-
-if path.extension(file) == '' and path.is_windows then
- try = which(file..'.exe')
-else
- try = which(file)
-end
-
-if try then print(try) else print 'cannot find on path' end
-
-
+-- a simple implementation of the which command. This looks for
+-- the given file on the path. On windows, it will assume an extension
+-- of .exe if no extension is given.
+local List = require 'pl.List'
+local path = require 'pl.path'
+local app = require 'pl.app'
+
+local pathl = List.split(os.getenv 'PATH',path.dirsep)
+
+function which (file)
+ local res = pathl:map(path.join,file)
+ res = res:filter(path.exists)
+ if res then return res[1] end
+end
+
+local _,lua = app.lua()
+local file = arg[1] or lua -- i.e. location of lua executable
+local try
+
+if not file then return print 'must provide a filename' end
+
+if path.extension(file) == '' and path.is_windows then
+ try = which(file..'.exe')
+else
+ try = which(file)
+end
+
+if try then print(try) else print 'cannot find on path' end
+
+
View
@@ -75,6 +75,27 @@ function app.platform()
end
end
+--- return the full command-line used to invoke this script
+-- any extra flags occupy slots, so that 'lua -lpl' gives us {[-2]='lua',[-1]='-lpl')
+-- @return command-line
+-- @return name of Lua program used
+function app.lua ()
+ local args = _G.arg or error "not in a main program"
+ local imin = 0
+ for i in pairs(args) do
+ if i < imin then imin = i end
+ end
+ local cmd, append = {}, table.insert
+ for i = imin,-1 do
+ local a = args[i]
+ if a:match '%s' then
+ a = '"'..a..'"'
+ end
+ append(cmd,a)
+ end
+ return table.concat(cmd,' '),args[imin]
+end
+
--- parse command-line arguments into flags and parameters.
-- Understands GNU-style command-line flags; short (-f) and long (--flag).
-- These may be given a value with either '=' or ':' (-k:2,--alpha=3.2,-n2);
View
12 run.lua
@@ -2,12 +2,16 @@
require 'pl'
local lfs = require 'lfs'
--- get the Lua executable used to invoke this script
-cmd = arg[-1]
-if cmd:find '%s' then
- cmd = '"'..cmd..'"'
+local function quote_if_needed (s)
+ if s:match '%s' then
+ s = '"'..s..'"'
+ end
+ return s
end
+-- get the Lua command-line used to invoke this script
+local cmd = app.lua()
+
function do_lua_files ()
for _,f in ipairs(dir.getfiles('.','*.lua')) do
print(cmd..' '..f)
View
@@ -2,7 +2,7 @@
-- these are global side-effects of pl.utils
local utils = require 'pl.utils'
local asserteq = require 'pl.test'.asserteq
-local lua = arg[-1]
+local _,lua = require 'pl.app'. lua()
-- utils.execute is a compromise between 5.1 and 5.2 for os.execute changes
-- can we call Lua ?

0 comments on commit 6f00e1c

Please sign in to comment.