Made main.nim a bit nicer with command-line options

This commit is contained in:
Mattia Giambirtone 2022-05-25 11:49:21 +02:00
parent a6d22f740d
commit 990b54fa3e
1 changed files with 49 additions and 3 deletions

View File

@ -2,7 +2,9 @@
import strformat
import strutils
import terminal
import parseopt
import os
# Thanks art <3
import jale/editor as ed
import jale/templates
@ -18,6 +20,7 @@ import frontend/parser as p
import frontend/compiler as c
import backend/vm as v
import util/serializer as s
import config
# Forward declarations
proc fillSymbolTable(tokenizer: Lexer)
@ -290,11 +293,54 @@ proc runFile(f: string) =
when isMainModule:
setControlCHook(proc () {.noconv.} = quit(0))
let args = commandLineParams()
if args.len() == 0:
var optParser = initOptParser(commandLineParams())
var file: string = ""
var fromString: bool = false
var interactive: bool = false
for kind, key, value in optParser.getopt():
case kind:
of cmdArgument:
file = key
of cmdLongOption:
case key:
of "help":
echo HELP_MESSAGE
quit()
of "version":
echo PEON_VERSION_STRING
quit()
of "string":
file = key
fromString = true
of "interactive":
interactive = true
else:
echo &"error: unkown option '{key}'"
quit()
of cmdShortOption:
case key:
of "h":
echo HELP_MESSAGE
quit()
of "v":
echo PEON_VERSION_STRING
quit()
of "s":
file = key
fromString = true
of "i":
interactive = true
else:
echo &"error: unkown option '{key}'"
quit()
else:
echo "usage: peon [options] [filename.pn]"
quit()
# TODO: Use interactive/fromString options
if file == "":
repl()
else:
runFile(args[0])
runFile(file)
proc fillSymbolTable(tokenizer: Lexer) =