From d0ba54e306d3e2020ced917e8294a87bb7982024 Mon Sep 17 00:00:00 2001 From: Alexey Solovyev Date: Mon, 25 Jan 2021 20:29:20 -0800 Subject: [PATCH] spark-logo/MainCLI: use picocli --- .../java/org/sparklogo/project/MainCLI.java | 97 ++++++++++++++++--- 1 file changed, 84 insertions(+), 13 deletions(-) diff --git a/spark-logo/src/main/java/org/sparklogo/project/MainCLI.java b/spark-logo/src/main/java/org/sparklogo/project/MainCLI.java index a50dff1..bc0a22b 100644 --- a/spark-logo/src/main/java/org/sparklogo/project/MainCLI.java +++ b/spark-logo/src/main/java/org/sparklogo/project/MainCLI.java @@ -1,25 +1,96 @@ package org.sparklogo.project; import org.sparklogo.gui.Configuration; +import picocli.CommandLine; +import picocli.CommandLine.ArgGroup; +import picocli.CommandLine.Parameters; +import picocli.CommandLine.Command; +import picocli.CommandLine.Option; import java.io.File; +import java.util.concurrent.Callable; -public class MainCLI { - public static void main(String[] args) throws Exception { - if (args.length != 1) { - System.out.println("Usage: spark-logo path_to_project_file"); - return; +@Command(name = "spark-pl", version = "1.4.0", description = "A SPARK-PL command line tool") +public class MainCLI implements Callable { + @Option(names = {"-h", "--help"}, usageHelp = true, description = "display this help message") + @SuppressWarnings("unused") + boolean usageHelpRequested; + + @Option(names = {"-v", "--version"}, versionHelp = true, description = "display version info") + @SuppressWarnings("unused") + boolean versionInfoRequested; + + @Option(names = {"-c", "--compile"}, description = "compile output Java files") + boolean compileFlag; + + @Option(names = {"-o", "--output"}, description = "output directory") + File outputDirectory; + + @Option(names = "--name", description = "custom project name") + String projectName; + + @Option(names = "--lib", description = "path to SPARK/lib") + File customLibPath; + + @ArgGroup(exclusive = true, multiplicity = "1") + Exclusive groups; + + static class Exclusive { + @ArgGroup(exclusive = false, multiplicity = "1") + FileOptions fileOptions; + + @ArgGroup(exclusive = false, multiplicity = "1") + ProjectOptions projectOptions; + } + + static class ProjectOptions { + @Option(names = {"-p", "--project"}, required = true, description = "compile the SPARK-PL project file") + File projectFile; + } + + static class FileOptions { + @Parameters(arity = "1..*", description = "SPARK-PL files to compile") + File[] inputFiles; + } + + @Override + public Integer call() throws Exception { + Project project = getProject(); + File libPath = getLibPath(); + project.translate(libPath); + if (compileFlag) { + project.compile(libPath); } - Project project = new Project(); - Configuration config = new Configuration(); + return 0; + } - System.out.println("Reading: " + args[0]); - ProjectFile.readProjectFile(new File(args[0]), project); + private Project getProject() throws Exception { + final Project project = new Project(); + if (groups.projectOptions != null) { + ProjectFile.readProjectFile(groups.projectOptions.projectFile, project); + } + else { + for (File file : groups.fileOptions.inputFiles) { + project.addFile(file); + } + } + if (outputDirectory != null){ + project.setOutputDirectory(outputDirectory.getAbsoluteFile()); + } + if (projectName != null) { + project.setName(projectName); + } - System.out.println("Translating"); - project.translate(config.getLibPath()); + return project; + } + + private File getLibPath() { + return customLibPath != null ? + customLibPath : + new Configuration().getLibPath(); + } - System.out.println("Compiling"); - project.compile(config.getLibPath()); + public static void main(String[] args) { + System.exit(new CommandLine(new MainCLI()).execute(args)); } }