diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/common/CLICompiler.java b/compiler/cli/src/org/jetbrains/kotlin/cli/common/CLICompiler.java
index 4adedc24355..cd96566013c 100644
--- a/compiler/cli/src/org/jetbrains/kotlin/cli/common/CLICompiler.java
+++ b/compiler/cli/src/org/jetbrains/kotlin/cli/common/CLICompiler.java
@@ -65,20 +65,19 @@ public abstract class CLICompiler {
}
@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 {
) {
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;
}
diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/common/Usage.java b/compiler/cli/src/org/jetbrains/kotlin/cli/common/Usage.java
index a648b196994..ee9ebe973ac 100644
--- a/compiler/cli/src/org/jetbrains/kotlin/cli/common/Usage.java
+++ b/compiler/cli/src/org/jetbrains/kotlin/cli/common/Usage.java
@@ -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() + " ");
- 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.");
}
diff --git a/compiler/testData/cli/jvm/wrongArgument.out b/compiler/testData/cli/jvm/wrongArgument.out
index f7bdd44574a..fda43c1d896 100644
--- a/compiler/testData/cli/jvm/wrongArgument.out
+++ b/compiler/testData/cli/jvm/wrongArgument.out
@@ -1,28 +1,3 @@
-Invalid argument: -wrong-argument
-Usage: kotlinc-jvm
-where possible options include:
- -d Destination for generated class files
- -classpath (-cp) Paths where to find user class files
- -include-runtime Include Kotlin runtime in to resulting .jar
- -jdk-home 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 to the module file to compile
- -script Evaluate the script file
- -script-templates
- Script definition template classes
- -kotlin-home Path to Kotlin compiler home directory, used for runtime libraries discovery
- -module-name Module name
- -jvm-target 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 Provide source compatibility with specified language version
- -api-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::=
- Pass an option to a plugin
-INTERNAL_ERROR
\ No newline at end of file
+error: invalid argument: -wrong-argument
+info: use -help for more information
+COMPILATION_ERROR
\ No newline at end of file