Skip to content

Commit b9768c8

Browse files
committed
add support for moon -e to run code from command line
1 parent 3b134e0 commit b9768c8

File tree

2 files changed

+93
-41
lines changed

2 files changed

+93
-41
lines changed

bin/moon

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ local unpack = util.unpack
77
local argparser = argparse()({
88
name = "moon"
99
})
10-
argparser:argument("script")
10+
argparser:argument("script"):args("?")
1111
argparser:argument("args"):args("*")
12-
argparser:option("-c --coverage", "Collect and print code coverage")
13-
argparser:option("-d", "Disable stack trace rewriting")
14-
argparser:option("-v --version", "Print version information")
12+
argparser:flag("--coverage -c", "Collect and print code coverage")
13+
argparser:flag("-d", "Disable stack trace rewriting")
14+
argparser:option("--execute -e", "Execute MoonScript code string")
15+
argparser:flag("--version -v", "Print version information")
1516
local base = 0
1617
local _list_0 = arg
1718
for _index_0 = 1, #_list_0 do
@@ -48,29 +49,55 @@ run = function()
4849
require("moonscript.version").print_version()
4950
os.exit()
5051
end
51-
local script_fname = opts.script
5252
args = {
5353
unpack(arg, base + 1)
5454
}
5555
args[-1] = arg[0]
56-
args[0] = opts.script
5756
local moonscript_chunk, lua_parse_error
58-
local passed, err = pcall(function()
59-
moonscript_chunk, lua_parse_error = moonscript.loadfile(script_fname, {
60-
implicitly_return_root = false
61-
})
62-
end)
63-
if not (passed) then
64-
print_err(err)
65-
os.exit(1)
66-
end
67-
if not (moonscript_chunk) then
68-
if lua_parse_error then
69-
print_err(lua_parse_error)
70-
else
71-
print_err("Can't file file: " .. tostring(script_fname))
57+
if opts.execute then
58+
args[0] = "-e"
59+
local passed, err = pcall(function()
60+
moonscript_chunk, lua_parse_error = moonscript.loadstring(opts.execute, "=(command line)", {
61+
implicitly_return_root = false
62+
})
63+
end)
64+
if not (passed) then
65+
print_err(err)
66+
os.exit(1)
67+
end
68+
if not (moonscript_chunk) then
69+
if lua_parse_error then
70+
print_err(lua_parse_error)
71+
else
72+
print_err("Failed to compile: " .. tostring(opts.execute))
73+
end
74+
os.exit(1)
75+
end
76+
else
77+
local script_fname = opts.script
78+
if not (script_fname) then
79+
print_err("Usage: moon [options] script [args]")
80+
print_err("Use 'moon --help' for more information.")
81+
os.exit(1)
82+
end
83+
args[0] = script_fname
84+
local passed, err = pcall(function()
85+
moonscript_chunk, lua_parse_error = moonscript.loadfile(script_fname, {
86+
implicitly_return_root = false
87+
})
88+
end)
89+
if not (passed) then
90+
print_err(err)
91+
os.exit(1)
92+
end
93+
if not (moonscript_chunk) then
94+
if lua_parse_error then
95+
print_err(lua_parse_error)
96+
else
97+
print_err("Can't file file: " .. tostring(script_fname))
98+
end
99+
os.exit(1)
72100
end
73-
os.exit(1)
74101
end
75102
util.getfenv(moonscript_chunk).arg = args
76103
local run_chunk

bin/moon.moon

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ unpack = util.unpack
88

99
argparser = argparse! name: "moon"
1010

11-
argparser\argument "script"
11+
argparser\argument("script")\args "?"
1212
argparser\argument("args")\args "*"
13-
argparser\option "-c --coverage", "Collect and print code coverage"
14-
argparser\option "-d", "Disable stack trace rewriting"
15-
argparser\option "-v --version", "Print version information"
13+
argparser\flag "--coverage -c", "Collect and print code coverage"
14+
argparser\flag "-d", "Disable stack trace rewriting"
15+
argparser\option "--execute -e", "Execute MoonScript code string"
16+
argparser\flag "--version -v", "Print version information"
1617

1718
base = 0
1819
for flag in *arg
@@ -31,30 +32,54 @@ run = ->
3132
require("moonscript.version").print_version!
3233
os.exit!
3334

34-
script_fname = opts.script
35-
3635
args = {unpack arg, base + 1}
3736
args[-1] = arg[0]
38-
args[0] = opts.script
3937

4038
local moonscript_chunk, lua_parse_error
4139

42-
passed, err = pcall ->
43-
moonscript_chunk, lua_parse_error = moonscript.loadfile script_fname, {
44-
implicitly_return_root: false
45-
}
40+
if opts.execute
41+
args[0] = "-e"
4642

47-
unless passed
48-
print_err err
49-
os.exit 1
43+
passed, err = pcall ->
44+
moonscript_chunk, lua_parse_error = moonscript.loadstring opts.execute, "=(command line)", {
45+
implicitly_return_root: false
46+
}
5047

51-
unless moonscript_chunk
52-
if lua_parse_error
53-
print_err lua_parse_error
54-
else
55-
print_err "Can't file file: #{script_fname}"
48+
unless passed
49+
print_err err
50+
os.exit 1
5651

57-
os.exit 1
52+
unless moonscript_chunk
53+
if lua_parse_error
54+
print_err lua_parse_error
55+
else
56+
print_err "Failed to compile: #{opts.execute}"
57+
os.exit 1
58+
else
59+
script_fname = opts.script
60+
61+
unless script_fname
62+
print_err "Usage: moon [options] script [args]"
63+
print_err "Use 'moon --help' for more information."
64+
os.exit 1
65+
66+
args[0] = script_fname
67+
68+
passed, err = pcall ->
69+
moonscript_chunk, lua_parse_error = moonscript.loadfile script_fname, {
70+
implicitly_return_root: false
71+
}
72+
73+
unless passed
74+
print_err err
75+
os.exit 1
76+
77+
unless moonscript_chunk
78+
if lua_parse_error
79+
print_err lua_parse_error
80+
else
81+
print_err "Can't file file: #{script_fname}"
82+
os.exit 1
5883

5984
util.getfenv(moonscript_chunk).arg = args
6085

0 commit comments

Comments
 (0)