summaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/HobgoblinCli.java62
1 files changed, 62 insertions, 0 deletions
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/HobgoblinCli.java b/src/main/java/org/zwobble/hobgoblin/compiler/HobgoblinCli.java
new file mode 100644
index 0000000..a4992e4
--- /dev/null
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/HobgoblinCli.java
@@ -0,0 +1,62 @@
+package org.zwobble.hobgoblin.compiler;
+
+import net.sourceforge.argparse4j.ArgumentParsers;
+import net.sourceforge.argparse4j.ext.java7.PathArgumentType;
+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 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.<Path>get("source");
+
+ switch (args.<Command>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,
+ }
+}