bin/moonc: add support for argparse, remove support for alt-getopt

This commit is contained in:
RyanSquared 2017-05-30 00:14:20 -05:00 committed by leaf
parent dfef936a0c
commit dd9661db93

View File

@ -1,50 +1,38 @@
#!/usr/bin/env lua #!/usr/bin/env lua
local alt_getopt = require "alt_getopt" local argparse = require "argparse"
local lfs = require "lfs" local lfs = require "lfs"
local opts, ind = alt_getopt.get_opts(arg, "lvhwt:o:pTXb", { local parser = argparse()
print = "p", tree = "T", version = "v", help = "h", lint = "l"
})
local read_stdin = arg[1] == "--" parser:flag("-l --lint", "Perform a lint on the file instead of compiling")
parser:flag("-v --version", "Print version")
parser:flag("-w --watch", "Watch file/directory for updates")
parser:mutex(
parser:flag("-t --output-to", "Specify where to place compiled files"),
parser:flag("-o", "Write output to file"):args(1),
parser:flag("-p", "Write output to standard output"),
parser:flag("-T", "Write parse tree instead of code (to stdout)"),
parser:flag("-b", "Write parse and compile time instead of code(to stdout)"),
parser:flag("-X", "Write line rewrite map instead of code (to stdout)")
)
parser:flag("-",
"Read from standard in, print to standard out (Must be only argument)")
local help = [[Usage: %s [options] files/directories... local read_stdin = arg[1] == "--" -- luacheck: ignore 113
-h Print this message if not read_stdin then
-w Watch file/directory parser:argument("file/directory"):args("+")
-t path Specify where to place compiled files end
-o file Write output to file
-p Write output to standard out
-T Write parse tree instead of code (to stdout)
-X Write line rewrite map instead of code (to stdout)
-l Perform lint on the file instead of compiling
-b Dump parse and compile time (doesn't write output)
-v Print version
-- Read from standard in, print to standard out local opts = parser:parse()
(Must be first and only argument)
]]
if opts.v then if opts.version then
local v = require "moonscript.version" local v = require "moonscript.version"
v.print_version() v.print_version()
os.exit() os.exit()
end end
function print_help(err)
local help_msg = help:format(arg[0])
if err then
io.stderr:write("Error: ".. err .. "\n")
io.stderr:write(help_msg .. "\n")
os.exit(1)
else
print(help_msg)
os.exit(0)
end
end
function log_msg(...) function log_msg(...)
if not opts.p then if not opts.p then
io.stderr:write(table.concat({...}, " ") .. "\n") io.stderr:write(table.concat({...}, " ") .. "\n")
@ -53,10 +41,7 @@ end
local moonc = require("moonscript.cmd.moonc") local moonc = require("moonscript.cmd.moonc")
local util = require "moonscript.util" local util = require "moonscript.util"
local mkdir = moonc.mkdir
local normalize_dir = moonc.normalize_dir local normalize_dir = moonc.normalize_dir
local parse_dir = moonc.parse_dir
local parse_file = moonc.parse_file
local compile_and_write = moonc.compile_and_write local compile_and_write = moonc.compile_and_write
local path_to_target = moonc.path_to_target local path_to_target = moonc.path_to_target
@ -102,23 +87,19 @@ local function get_files(fname, files)
for _, sub_fname in ipairs(scan_directory(fname)) do for _, sub_fname in ipairs(scan_directory(fname)) do
table.insert(files, { table.insert(files, {
sub_fname, sub_fname,
path_to_target(sub_fname, opts.t, fname) path_to_target(sub_fname, opts.output_to, fname)
}) })
end end
else else
table.insert(files, { table.insert(files, {
fname, fname,
path_to_target(fname, opts.t) path_to_target(fname, opts.output_to)
}) })
end end
return files return files
end end
if opts.h then
print_help()
end
if read_stdin then if read_stdin then
local parse = require "moonscript.parse" local parse = require "moonscript.parse"
local compile = require "moonscript.compile" local compile = require "moonscript.compile"
@ -137,14 +118,7 @@ if read_stdin then
os.exit() os.exit()
end end
local inputs = {} local inputs = opts["file/directory"]
for i = ind, #arg do
table.insert(inputs, arg[i])
end
if #inputs == 0 then
print_help("No files specified")
end
local files = {} local files = {}
for _, input in ipairs(inputs) do for _, input in ipairs(inputs) do
@ -155,10 +129,6 @@ files = remove_dups(files, function(f)
return f[2] return f[2]
end) end)
if opts.o and #files > 1 then
print_help("-o can not be used with multiple input files")
end
-- returns an iterator that returns files that have been updated -- returns an iterator that returns files that have been updated
local function create_watcher(files) local function create_watcher(files)
local watchers = require("moonscript.cmd.watchers") local watchers = require("moonscript.cmd.watchers")
@ -170,10 +140,10 @@ local function create_watcher(files)
return watchers.SleepWatcher(files):each_update() return watchers.SleepWatcher(files):each_update()
end end
if opts.w then if opts.watch then
-- build function to check for lint or compile in watch -- build function to check for lint or compile in watch
local handle_file local handle_file
if opts.l then if opts.lint then
local lint = require "moonscript.cmd.lint" local lint = require "moonscript.cmd.lint"
handle_file = lint.lint_file handle_file = lint.lint_file
else else
@ -199,7 +169,7 @@ if opts.w then
end end
local success, err = handle_file(fname, target) local success, err = handle_file(fname, target)
if opts.l then if opts.lint then
if success then if success then
io.stderr:write(success .. "\n\n") io.stderr:write(success .. "\n\n")
elseif err then elseif err then
@ -218,7 +188,7 @@ if opts.w then
end end
io.stderr:write("\nQuitting...\n") io.stderr:write("\nQuitting...\n")
elseif opts.l then elseif opts.lint then
local has_linted_with_error; local has_linted_with_error;
local lint = require "moonscript.cmd.lint" local lint = require "moonscript.cmd.lint"
for _, tuple in pairs(files) do for _, tuple in pairs(files) do