-
I'm stuck with my project and would appreciate some help. Currently, I see two ways of doing this:
If you could help me figure out 2 or provide me with a simpler alternative option I'd be very happy. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
For custom shebang programs, you don't probably don't need some of the fancier things local function execute(path, contents)
-- Shift arg left one, so replacing { [0] = "shebang-program", "program", "arg1" }
-- with { [-1] = "shebang-program", [0] = "program", "arg1" }.
local new_arg = {}
local i = #arg while arg[i] do new_arg[i - 1] = arg[i] i = i - 1 end
-- Create a new environment and require instance.
-- You could probably overwrite _ENV.{arg,require,package}, rather than creating a new env,
-- but this feels cleaner.
local env = setmetatable({ arg = new_arg, shell = shell, multishell = multishell } , {__index = _G})
env.require, env.package = require "cc.require".make(env, fs.getDir(path))
-- Load the file and run it.
local func, err = load(contents, "@/" .. path, nil, env)
if not func then error(err, 0) end
return func(table.unpack(new_arg))
end
local path = arg[1]
local handle = fs.open(path, "r")
local contents = handle.readAll()
handle.close()
execute(path, contents:gsub("[hH]ello", "hi")) That said, if you're trying to instrument the program, you might want a custom |
Beta Was this translation helpful? Give feedback.
For custom shebang programs, you don't probably don't need some of the fancier things
shell
does (like fancy error reporting), so can probably get away with something like this: