further minor refactorings so we can easily reuse the class and command line argument handling in other command line tools like kdoc

This commit is contained in:
James Strachan
2012-03-08 09:19:15 +00:00
parent bfce06cbb0
commit 0c66d9db27
@@ -75,13 +75,16 @@ public class KotlinCompiler {
public boolean transformNamesToJava; public boolean transformNamesToJava;
} }
private static void usage(PrintStream target) { public static void main(String... args) {
target.println("Usage: KotlinCompiler [-output <outputDir>|-jar <jarFileName>] [-stdlib <path to runtime.jar>] [-src <filename or dirname>|-module <module file>] [-includeRuntime]"); 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 { try {
int rc = new KotlinCompiler().exec(args); int rc = compiler.exec(args);
if (rc != 0) { if (rc != 0) {
System.err.println("exec() finished with " + rc + " return code"); System.err.println("exec() finished with " + rc + " return code");
System.exit(rc); System.exit(rc);
@@ -97,25 +100,22 @@ public class KotlinCompiler {
} }
public int exec(PrintStream errStream, String... args) { public int exec(PrintStream errStream, String... args) {
System.setProperty("java.awt.headless", "true"); Arguments arguments = createArguments();
Arguments arguments = new Arguments(); if (!parseArguments(errStream, arguments, args)) {
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));
return 1; return 1;
} }
return exec(errStream, arguments);
}
/**
* Executes the compiler on the parsed arguments
*/
public int exec(PrintStream errStream, Arguments arguments) {
if (arguments.help) { if (arguments.help) {
usage(errStream); usage(errStream);
return 0; return 0;
} }
System.setProperty("java.awt.headless", "true");
FileNameTransformer fileNameTransformer = arguments.transformNamesToJava FileNameTransformer fileNameTransformer = arguments.transformNamesToJava
? ANY_EXTENSION_TO_JAVA ? 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 <outputDir>|-jar <jarFileName>] [-stdlib <path to runtime.jar>] [-src <filename or dirname>|-module <module file>] [-includeRuntime]");
}
/**
* Allow derived classes to add additional command line arguments
*/
protected Arguments createArguments() {
return new Arguments();
}
/** /**
* Strategy method to configure the environment, allowing compiler * Strategy method to configure the environment, allowing compiler
* based tools to customise their own plugins * based tools to customise their own plugins