diff --git a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/CommonCompilerArguments.java b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/CommonCompilerArguments.java index cee6029fad8..7f4aebb69c5 100644 --- a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/CommonCompilerArguments.java +++ b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/CommonCompilerArguments.java @@ -109,6 +109,8 @@ public abstract class CommonCompilerArguments implements Serializable { public List freeArgs = new SmartList<>(); + public List unknownArgs = new SmartList<>(); + public List unknownExtraFlags = new SmartList<>(); // Names of extra (-X...) arguments which have been passed in an obsolete form ("-Xaaa bbb", instead of "-Xaaa=bbb") diff --git a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/argumentUtils.kt b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/argumentUtils.kt index 829df083d99..2325c0824c1 100644 --- a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/argumentUtils.kt +++ b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/argumentUtils.kt @@ -24,14 +24,9 @@ import java.util.* fun parseArguments(args: Array, arguments: A, ignoreInvalidArguments: Boolean = false) { parseCommandLineArguments(args, arguments) - if (ignoreInvalidArguments) { - arguments.freeArgs.removeAll { it.startsWith("-") } - } - - for (argument in arguments.freeArgs) { - if (argument.startsWith("-")) { - throw IllegalArgumentException("Invalid argument: " + argument) - } + val invalidArgument = arguments.unknownArgs.firstOrNull()?.takeUnless { ignoreInvalidArguments } + if (invalidArgument != null) { + throw IllegalArgumentException("Invalid argument: $invalidArgument") } } diff --git a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/parseCommandLineArguments.kt b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/parseCommandLineArguments.kt index 7cdf188f19e..d10cfed91d6 100644 --- a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/parseCommandLineArguments.kt +++ b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/parseCommandLineArguments.kt @@ -30,6 +30,7 @@ val Argument.isAdvanced: Boolean get() = value.startsWith(ADVANCED_ARGUMENT_PREFIX) && value.length > ADVANCED_ARGUMENT_PREFIX.length private val ADVANCED_ARGUMENT_PREFIX = "-X" +private val FREE_ARGS_DELIMITER = "--" // Parses arguments in the passed [result] object, or throws an [IllegalArgumentException] with the message to be displayed to the user fun parseCommandLineArguments(args: Array, result: A) { @@ -41,10 +42,21 @@ fun parseCommandLineArguments(args: Array, } val visitedArgs = mutableSetOf() + var freeArgsStarted = false var i = 0 while (i < args.size) { val arg = args[i++] + + if (freeArgsStarted) { + result.freeArgs.add(arg) + continue + } + if (arg == FREE_ARGS_DELIMITER) { + freeArgsStarted = true + continue + } + val argumentField = fields.firstOrNull { (_, argument) -> argument.value == arg || argument.shortName.takeUnless(String::isEmpty) == arg || @@ -52,11 +64,10 @@ fun parseCommandLineArguments(args: Array, } if (argumentField == null) { - if (arg.startsWith(ADVANCED_ARGUMENT_PREFIX)) { - result.unknownExtraFlags.add(arg) - } - else { - result.freeArgs.add(arg) + when { + arg.startsWith(ADVANCED_ARGUMENT_PREFIX) -> result.unknownExtraFlags.add(arg) + arg.startsWith("-") -> result.unknownArgs.add(arg) + else -> result.freeArgs.add(arg) } continue } diff --git a/compiler/testData/integration/smoke/scriptDashedArgs/script.expected b/compiler/testData/integration/smoke/scriptDashedArgs/script.expected new file mode 100644 index 00000000000..86c60cc9c31 --- /dev/null +++ b/compiler/testData/integration/smoke/scriptDashedArgs/script.expected @@ -0,0 +1,8 @@ +OUT: +hi +-name +Marty +-- +there + +Return code: 0 diff --git a/compiler/testData/integration/smoke/scriptDashedArgs/script.kts b/compiler/testData/integration/smoke/scriptDashedArgs/script.kts new file mode 100644 index 00000000000..9ebde070775 --- /dev/null +++ b/compiler/testData/integration/smoke/scriptDashedArgs/script.kts @@ -0,0 +1,3 @@ +for (arg in args) { + println(arg) +} diff --git a/compiler/tests/org/jetbrains/kotlin/integration/CompilerSmokeTest.java b/compiler/tests/org/jetbrains/kotlin/integration/CompilerSmokeTest.java index c59a2dbf926..77609afe98b 100644 --- a/compiler/tests/org/jetbrains/kotlin/integration/CompilerSmokeTest.java +++ b/compiler/tests/org/jetbrains/kotlin/integration/CompilerSmokeTest.java @@ -57,6 +57,10 @@ public class CompilerSmokeTest extends CompilerSmokeTestBase { runCompiler("script", "-script", "script.kts", "hi", "there"); } + public void testScriptDashedArgs() throws Exception { + runCompiler("script", "-script", "script.kts", "--", "hi", "-name", "Marty", "--", "there"); + } + public void testScriptWithClasspath() throws Exception { runCompiler("script", "-cp", new File("lib/javax.inject.jar").getAbsolutePath(), "-script", "script.kts"); }