From 1e1641d81938c48b5f01f6ab71fe4b8868a5889d Mon Sep 17 00:00:00 2001 From: gegy1000 Date: Tue, 18 Jun 2019 21:37:15 +0200 Subject: Use jopt for cli parsing --- src/main/java/cuchaz/enigma/Main.java | 105 +++++++++++++++++++++++++--------- 1 file changed, 77 insertions(+), 28 deletions(-) (limited to 'src/main/java/cuchaz/enigma/Main.java') diff --git a/src/main/java/cuchaz/enigma/Main.java b/src/main/java/cuchaz/enigma/Main.java index 76a3fff..a642840 100644 --- a/src/main/java/cuchaz/enigma/Main.java +++ b/src/main/java/cuchaz/enigma/Main.java @@ -12,48 +12,97 @@ package cuchaz.enigma; import cuchaz.enigma.gui.Gui; +import cuchaz.enigma.gui.GuiController; import cuchaz.enigma.translation.mapping.serde.MappingFormat; +import joptsimple.*; -import java.io.File; +import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.Paths; public class Main { - public static void main(String[] args) throws Exception { - Gui gui = new Gui(); + public static void main(String[] args) throws IOException { + OptionParser parser = new OptionParser(); - // parse command-line args - if (args.length >= 1) { - gui.getController().openJar(getFile(args[0]).toPath()); - } - if (args.length >= 2) { - Path mappingsFile = getFile(args[1]).toPath(); - if (Files.isDirectory(mappingsFile)) { - gui.getController().openMappings(MappingFormat.ENIGMA_DIRECTORY, mappingsFile); - } else { - gui.getController().openMappings(MappingFormat.ENIGMA_FILE, mappingsFile); + OptionSpec jar = parser.accepts("jar", "Jar file to open at startup") + .withRequiredArg() + .withValuesConvertedBy(PathConverter.INSTANCE); + + OptionSpec mappings = parser.accepts("mappings", "Mappings file to open at startup") + .withRequiredArg() + .withValuesConvertedBy(PathConverter.INSTANCE); + + OptionSpec profile = parser.accepts("profile", "Profile json to apply at startup") + .withRequiredArg() + .withValuesConvertedBy(PathConverter.INSTANCE); + + parser.accepts("help", "Displays help information"); + + try { + OptionSet options = parser.parse(args); + + if (options.has("help")) { + parser.printHelpOn(System.out); + return; + } + + Gui gui = new Gui(); + GuiController controller = gui.getController(); + + if (options.has(jar)) { + Path jarPath = options.valueOf(jar); + controller.openJar(jarPath); } - } - // DEBUG - //gui.getController().openDeclaration(new ClassEntry("none/byp")); + if (options.has(mappings)) { + Path mappingsPath = options.valueOf(mappings); + if (Files.isDirectory(mappingsPath)) { + controller.openMappings(MappingFormat.ENIGMA_DIRECTORY, mappingsPath); + } else { + controller.openMappings(MappingFormat.ENIGMA_FILE, mappingsPath); + } + } + } catch (OptionException e) { + System.out.println("Invalid arguments: " + e.getMessage()); + System.out.println(); + parser.printHelpOn(System.out); + } } - private static File getFile(String path) { - // expand ~ to the home dir - if (path.startsWith("~")) { - // get the home dir - File dirHome = new File(System.getProperty("user.home")); - - // is the path just ~/ or is it ~user/ ? - if (path.startsWith("~/")) { - return new File(dirHome, path.substring(2)); - } else { - return new File(dirHome.getParentFile(), path.substring(1)); + private static class PathConverter implements ValueConverter { + static final ValueConverter INSTANCE = new PathConverter(); + + PathConverter() { + } + + @Override + public Path convert(String path) { + // expand ~ to the home dir + if (path.startsWith("~")) { + // get the home dir + Path dirHome = Paths.get(System.getProperty("user.home")); + + // is the path just ~/ or is it ~user/ ? + if (path.startsWith("~/")) { + return dirHome.resolve(path.substring(2)); + } else { + return dirHome.getParent().resolve(path.substring(1)); + } } + + return Paths.get(path); } - return new File(path); + @Override + public Class valueType() { + return Path.class; + } + + @Override + public String valuePattern() { + return "path"; + } } } -- cgit v1.2.3