From 0c66d9db2719f1d9f0ee9b9da4a50d8f7839caf6 Mon Sep 17 00:00:00 2001 From: James Strachan Date: Thu, 8 Mar 2012 09:19:15 +0000 Subject: [PATCH] further minor refactorings so we can easily reuse the class and command line argument handling in other command line tools like kdoc --- .../org/jetbrains/jet/cli/KotlinCompiler.java | 61 ++++++++++++++----- 1 file changed, 45 insertions(+), 16 deletions(-) diff --git a/compiler/cli/src/org/jetbrains/jet/cli/KotlinCompiler.java b/compiler/cli/src/org/jetbrains/jet/cli/KotlinCompiler.java index 6a5a0f0ce3d..4dade5298ab 100644 --- a/compiler/cli/src/org/jetbrains/jet/cli/KotlinCompiler.java +++ b/compiler/cli/src/org/jetbrains/jet/cli/KotlinCompiler.java @@ -75,13 +75,16 @@ public class KotlinCompiler { public boolean transformNamesToJava; } - private static void usage(PrintStream target) { - target.println("Usage: KotlinCompiler [-output |-jar ] [-stdlib ] [-src |-module ] [-includeRuntime]"); + public static void main(String... args) { + doMain(new KotlinCompiler(), args); } - public static void main(String... args) { + /** + * Useful main for derived command line tools + */ + public static void doMain(KotlinCompiler compiler, String[] args) { try { - int rc = new KotlinCompiler().exec(args); + int rc = compiler.exec(args); if (rc != 0) { System.err.println("exec() finished with " + rc + " return code"); System.exit(rc); @@ -97,25 +100,22 @@ public class KotlinCompiler { } public int exec(PrintStream errStream, String... args) { - System.setProperty("java.awt.headless", "true"); - Arguments arguments = new Arguments(); - try { - Args.parse(arguments, args); - } - catch (IllegalArgumentException e) { - usage(System.err); - return 1; - } - catch (Throwable t) { - // Always use tags - errStream.println(MessageRenderer.TAGS.renderException(t)); + Arguments arguments = createArguments(); + if (!parseArguments(errStream, arguments, args)) { return 1; } + return exec(errStream, arguments); + } + /** + * Executes the compiler on the parsed arguments + */ + public int exec(PrintStream errStream, Arguments arguments) { if (arguments.help) { usage(errStream); return 0; } + System.setProperty("java.awt.headless", "true"); FileNameTransformer fileNameTransformer = arguments.transformNamesToJava ? ANY_EXTENSION_TO_JAVA @@ -143,6 +143,35 @@ public class KotlinCompiler { } } + /** + * Returns true if the arguments can be parsed correctly + */ + protected boolean parseArguments(PrintStream errStream, Arguments arguments, String[] args) { + try { + Args.parse(arguments, args); + return true; + } + catch (IllegalArgumentException e) { + usage(System.err); + } + catch (Throwable t) { + // Always use tags + errStream.println(MessageRenderer.TAGS.renderException(t)); + } + return false; + } + + protected void usage(PrintStream target) { + target.println("Usage: KotlinCompiler [-output |-jar ] [-stdlib ] [-src |-module ] [-includeRuntime]"); + } + + /** + * Allow derived classes to add additional command line arguments + */ + protected Arguments createArguments() { + return new Arguments(); + } + /** * Strategy method to configure the environment, allowing compiler * based tools to customise their own plugins