Do not print usage if wrong argument is passed, do not exit with INTERNAL_ERROR

This is a normal situation, not an internal error of the compiler, so
return COMPILATION_ERROR instead
This commit is contained in:
Alexander Udalov
2017-04-06 15:57:31 +03:00
parent ff846e787f
commit ce6676baa8
3 changed files with 22 additions and 41 deletions
@@ -65,20 +65,19 @@ public abstract class CLICompiler<A extends CommonCompilerArguments> {
}
@Nullable
private A parseArguments(@NotNull PrintStream errStream, @NotNull MessageRenderer messageRenderer, @NotNull String[] args) {
private A parseArguments(@NotNull MessageCollector messageCollector, @NotNull String[] args) {
try {
A arguments = createArguments();
parseArguments(args, arguments);
return arguments;
}
catch (IllegalArgumentException e) {
errStream.println(e.getMessage());
Usage.print(errStream, createArguments(), false);
throw e;
}
catch (Throwable t) {
errStream.println(messageRenderer.render(EXCEPTION, OutputMessageUtil.renderException(t), null));
messageCollector.report(EXCEPTION, OutputMessageUtil.renderException(t), null);
return null;
}
return null;
}
// Used in kotlin-maven-plugin (KotlinCompileMojoBase) and in kotlin-gradle-plugin (KotlinJvmOptionsImpl, KotlinJsOptionsImpl)
@@ -98,13 +97,20 @@ public abstract class CLICompiler<A extends CommonCompilerArguments> {
) {
K2JVMCompiler.Companion.resetInitStartTime();
A arguments = parseArguments(errStream, messageRenderer, args);
if (arguments == null) {
return INTERNAL_ERROR;
MessageCollector parseArgumentsCollector = new PrintingMessageCollector(errStream, messageRenderer, false);
A arguments;
try {
arguments = parseArguments(parseArgumentsCollector, args);
if (arguments == null) return INTERNAL_ERROR;
}
catch (IllegalArgumentException e) {
parseArgumentsCollector.report(ERROR, e.getMessage(), null);
parseArgumentsCollector.report(INFO, "Use -help for more information", null);
return COMPILATION_ERROR;
}
if (arguments.help || arguments.extraHelp) {
Usage.print(errStream, createArguments(), arguments.extraHelp);
Usage.print(errStream, arguments);
return OK;
}
@@ -29,19 +29,19 @@ class Usage {
// The magic number 29 corresponds to the similar padding width in javac and scalac command line compilers
private static final int OPTION_NAME_PADDING_WIDTH = 29;
public static void print(@NotNull PrintStream target, @NotNull CommonCompilerArguments arguments, boolean extraHelp) {
public static void print(@NotNull PrintStream target, @NotNull CommonCompilerArguments arguments) {
target.println("Usage: " + arguments.executableScriptFileName() + " <options> <source files>");
target.println("where " + (extraHelp ? "advanced" : "possible") + " options include:");
target.println("where " + (arguments.extraHelp ? "advanced" : "possible") + " options include:");
for (Class<?> clazz = arguments.getClass(); clazz != null; clazz = clazz.getSuperclass()) {
for (Field field : clazz.getDeclaredFields()) {
String usage = fieldUsage(field, extraHelp);
String usage = fieldUsage(field, arguments.extraHelp);
if (usage != null) {
target.println(usage);
}
}
}
if (extraHelp) {
if (arguments.extraHelp) {
target.println();
target.println("Advanced options are non-standard and may be changed or removed without any notice.");
}
+3 -28
View File
@@ -1,28 +1,3 @@
Invalid argument: -wrong-argument
Usage: kotlinc-jvm <options> <source files>
where possible options include:
-d <directory|jar> Destination for generated class files
-classpath (-cp) <path> Paths where to find user class files
-include-runtime Include Kotlin runtime in to resulting .jar
-jdk-home <path> Path to JDK home directory to include into classpath, if differs from default JAVA_HOME
-no-jdk Don't include Java runtime into classpath
-no-stdlib Don't include Kotlin runtime into classpath
-no-reflect Don't include Kotlin reflection implementation into classpath
-module <path> Path to the module file to compile
-script Evaluate the script file
-script-templates <fully qualified class name[,]>
Script definition template classes
-kotlin-home <path> Path to Kotlin compiler home directory, used for runtime libraries discovery
-module-name Module name
-jvm-target <version> Target version of the generated JVM bytecode (1.6 or 1.8), default is 1.6
-java-parameters Generate metadata for Java 1.8 reflection on method parameters
-language-version <version> Provide source compatibility with specified language version
-api-version <version> Allow to use declarations only from the specified version of bundled libraries
-nowarn Generate no warnings
-verbose Enable verbose logging output
-version Display compiler version
-help (-h) Print a synopsis of standard options
-X Print a synopsis of advanced options
-P plugin:<pluginId>:<optionName>=<value>
Pass an option to a plugin
INTERNAL_ERROR
error: invalid argument: -wrong-argument
info: use -help for more information
COMPILATION_ERROR