package org.zwobble.hobgoblin.compiler; import net.sourceforge.argparse4j.ArgumentParsers; import net.sourceforge.argparse4j.inf.ArgumentParserException; import net.sourceforge.argparse4j.inf.Namespace; import org.zwobble.hobgoblin.compiler.errors.SourceError; import org.zwobble.hobgoblin.compiler.sources.NullSource; import org.zwobble.hobgoblin.compiler.util.argparse4j.PathArgumentType; import java.io.IOException; import java.nio.file.Path; public class HobgoblinCli { public static void main(String[] rawArgs) throws IOException, InterruptedException { var args = parseArgs(rawArgs); try { compile(args); } catch (SourceError error) { if (!(error.source() instanceof NullSource)) { System.err.println(error.source().describe()); } System.err.println(error.getMessage()); System.exit(2); } } private static void compile(Namespace args) throws IOException, InterruptedException { var sourcePath = args.get("source"); switch (args.get("command")) { case COMPILE -> { HobgoblinCompiler.compile(sourcePath); } } } private static Namespace parseArgs(String[] args) { var parser = ArgumentParsers.newFor("hobgoblin").build() .defaultHelp(true); var subparsers = parser.addSubparsers(); var compileSubParser = subparsers.addParser("compile"); compileSubParser.setDefault("command", Command.COMPILE); compileSubParser .addArgument("source") .type(new PathArgumentType()) .required(true); try { return parser.parseArgs(args); } catch (ArgumentParserException e) { parser.handleError(e); System.exit(1); throw new RuntimeException(e); } } private enum Command { COMPILE, } }