1
0
Fork 0
mirror of https://github.com/Reuh/candran.git synced 2025-10-27 17:59:30 +00:00

Use argparse to parse CLI args, separate preprocessor constants from options

This commit is contained in:
Étienne Fildadut 2021-06-17 19:45:53 +02:00
parent dd22f2de3d
commit e9ae8e21a3
8 changed files with 253 additions and 346 deletions

View file

@ -1,48 +1,63 @@
#!/usr/bin/env lua
local candran = require("candran")
local cmdline = require("candran.cmdline")
local parse = require("candran.can-parser.parser").parse
local pp = require("candran.can-parser.pp")
local util = require("candran.util")
local argparse = require("argparse")
local args = cmdline(arg)
-- Parse args --
if #arg < 1 or args.help or args.h then
print("Candran "..candran.VERSION.." compiler by Reuh")
print("Usage: "..arg[0].." [options] filenames...")
print("Use - instead of filenames to read from the standard input. The output file will be named stdin.lua by default.")
print("Compiler options:")
print(" dest=\"directory\" where compiled files should be written")
print(" out=\"name.lua\" output filename. By default, will use the same name as the input file with a .lua extension.")
print(" -print write to the standard output instead of creating files")
print(" -preprocess only run the preprocessor")
print(" -compile only run the compiler")
print(" -parse only parse the file and prints errors to stdout")
print(" -ast (for debugging purposes) only parse the files and dump the AST to stdout")
print(" -help or -h print this text")
print("Default options:")
for opt, val in pairs(candran.default) do
if type(val) == "string" then val = val:gsub("\n", "\\n") end
print((" %s=%q"):format(opt, tostring(val)))
end
return
end
local parser = argparse()
:name "canc"
:description("Candran "..candran.VERSION.." compiler by Reuh.")
:epilog "For more info, see https://github.com/Reuh/candran"
if arg[#arg] == "-" then
table.insert(args, io.stdin)
end
parser:argument("filename", "Candran files to compile. Use - to read from standard input; the output file will then be named stdin.lua by default.")
:args "+"
for _, file in ipairs(args) do
parser:group("Output options",
parser:option("-d --destination")
:description "Where compiled files should be written"
:argname "directory",
parser:option("-o --output")
:description "Output filename. (default: same name as the input file with a .lua extension)"
:argname "filename",
parser:flag("-p --print")
:description "Write to the standard output instead of creating files",
parser:flag("--preprocess")
:description "Only run the preprocessor",
parser:flag("--compile")
:description "Only run the compiler",
parser:flag("--parse")
:description "Only parse the file and prints syntax errors to stdout",
parser:flag("--ast")
:description"(for debugging purposes) Only parse the files and dump the AST to stdout"
)
util.cli.addCandranOptions(parser)
local args = parser:parse()
-- Compile --
for _, file in ipairs(args.filename) do
-- Read
local dest, input
if file == io.stdin then
dest = args.out or "stdin.lua"
if file == "-" then
dest = args.output or "stdin.lua"
input = io.read("*a")
args.chunkname = "stdin"
else
dest = args.out or (file:gsub("%.can$", "")..".lua")
dest = args.output or (file:gsub("%.can$", "")..".lua")
local inputFile, err = io.open(file, "r")
if not inputFile then
@ -69,8 +84,10 @@ for _, file in ipairs(args) do
end
-- Compile and output
if args.dest then
dest = args.dest .. "/" .. dest
local options = util.cli.makeCandranOptions(args)
if args.destination then
dest = args.destination .. "/" .. dest
end
if not args.print then
@ -79,7 +96,7 @@ for _, file in ipairs(args) do
local out = input
if args.preprocess then
local r, err = candran.preprocess(out, args)
local r, err = candran.preprocess(out, options)
if not r then
io.stderr:write("canc: "..err.."\n")
os.exit(1)
@ -87,7 +104,7 @@ for _, file in ipairs(args) do
out = r
end
if args.compile then
local r, err = candran.compile(out, args)
local r, err = candran.compile(out, options)
if not r then
io.stderr:write("canc: "..err.."\n")
os.exit(1)
@ -95,7 +112,7 @@ for _, file in ipairs(args) do
out = r
end
if args.compile == nil and args.preprocess == nil then
local r, err = candran.make(input, args)
local r, err = candran.make(input, options)
if not r then
io.stderr:write("canc: "..err.."\n")
os.exit(1)