moonscript/bin/moon

116 lines
2.8 KiB
Plaintext
Raw Normal View History

2017-10-16 23:19:08 +00:00
#!/usr/bin/env lua
local argparse = require("argparse")
2015-06-09 04:46:28 +00:00
local moonscript = require("moonscript.base")
local util = require("moonscript.util")
local errors = require("moonscript.errors")
2013-01-03 01:55:43 +00:00
local unpack = util.unpack
local argparser = argparse()({
name = "moon"
2015-06-09 04:46:28 +00:00
})
argparser:argument("script")
argparser:argument("args"):args("*")
argparser:option("-c --coverage", "Collect and print code coverage")
argparser:option("-d", "Disable stack trace rewriting")
argparser:option("-v --version", "Print version information")
local base = 0
local _list_0 = arg
for _index_0 = 1, #_list_0 do
local flag = _list_0[_index_0]
base = base + 1
if flag:sub(1, 1) ~= "-" then
break
end
end
local args = {
unpack(arg, 1, base)
}
local opts = argparser:parse(args)
2015-06-09 04:46:28 +00:00
local print_err
print_err = function(...)
local msg = table.concat((function(...)
local _accum_0 = { }
local _len_0 = 1
local _list_1 = {
2015-06-09 04:46:28 +00:00
...
}
for _index_0 = 1, #_list_1 do
local v = _list_1[_index_0]
2015-06-09 04:46:28 +00:00
_accum_0[_len_0] = tostring(v)
_len_0 = _len_0 + 1
end
return _accum_0
end)(...), "\t")
return io.stderr:write(msg .. "\n")
2011-06-20 01:47:47 +00:00
end
2015-06-09 04:46:28 +00:00
local run
run = function()
if opts.version then
2015-06-09 04:46:28 +00:00
require("moonscript.version").print_version()
os.exit()
end
local script_fname = opts.script
args = {
unpack(arg, base + 1)
2015-06-09 04:46:28 +00:00
}
args[-1] = arg[0]
args[0] = opts.script
2015-06-09 04:46:28 +00:00
local moonscript_chunk, lua_parse_error
local passed, err = pcall(function()
moonscript_chunk, lua_parse_error = moonscript.loadfile(script_fname, {
implicitly_return_root = false
})
end)
if not (passed) then
print_err(err)
os.exit(1)
end
if not (moonscript_chunk) then
if lua_parse_error then
print_err(lua_parse_error)
else
print_err("Can't file file: " .. tostring(script_fname))
end
os.exit(1)
end
util.getfenv(moonscript_chunk).arg = args
2015-06-09 04:46:28 +00:00
local run_chunk
run_chunk = function()
moonscript.insert_loader()
moonscript_chunk(unpack(args))
2015-06-09 04:46:28 +00:00
return moonscript.remove_loader()
end
if opts.d then
return run_chunk()
end
local err, trace, cov
if opts.coverage then
2015-06-09 04:46:28 +00:00
print("starting coverage")
local coverage = require("moonscript.cmd.coverage")
cov = coverage.CodeCoverage()
cov:start()
end
xpcall(run_chunk, function(_err)
err = _err
trace = debug.traceback("", 2)
end)
if err then
local truncated = errors.truncate_traceback(util.trim(trace))
local rewritten = errors.rewrite_traceback(truncated, err)
if rewritten then
return print_err(rewritten)
else
return print_err(table.concat({
err,
util.trim(trace)
}, "\n"))
end
else
if cov then
cov:stop()
return cov:print_results()
end
end
2011-07-04 16:02:17 +00:00
end
2015-06-09 04:46:28 +00:00
return run()
2017-10-16 23:19:08 +00:00
-- vim: set filetype=lua: