From ba5f68aa1f6e909dad92dac0fdbf6cf808fbfe76 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Tue, 26 May 2026 19:47:05 +0100 Subject: Add CLI --- makefile | 4 ++ pom.xml | 36 +++++++++++++ .../zwobble/hobgoblin/compiler/HobgoblinCli.java | 62 ++++++++++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 makefile create mode 100644 src/main/java/org/zwobble/hobgoblin/compiler/HobgoblinCli.java diff --git a/makefile b/makefile new file mode 100644 index 0000000..66b9e08 --- /dev/null +++ b/makefile @@ -0,0 +1,4 @@ +.PHONY: package + +package: + mvn package -Dmaven.test.skip=true diff --git a/pom.xml b/pom.xml index c9af6b7..60dcf8a 100644 --- a/pom.xml +++ b/pom.xml @@ -55,6 +55,11 @@ + + net.sourceforge.argparse4j + argparse4j + 0.9.0 + org.zwobble.source-text source-text @@ -80,6 +85,37 @@ + + org.apache.maven.plugins + maven-shade-plugin + 3.4.1 + + false + + + *:* + + META-INF/* + + + + + + + org.zwobble.hobgoblin.compiler.HobgoblinCli + + + + + + + package + + shade + + + + org.sonatype.central central-publishing-maven-plugin 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.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, + } +} -- cgit v1.2.3