Skip to content

Commit 8aba862

Browse files
committed
CLI: use "--" to separate free arguments from compiler options
#KT-9370 Fixed
1 parent baef649 commit 8aba862

File tree

6 files changed

+36
-13
lines changed

6 files changed

+36
-13
lines changed

compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/CommonCompilerArguments.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ public abstract class CommonCompilerArguments implements Serializable {
109109

110110
public List<String> freeArgs = new SmartList<>();
111111

112+
public List<String> unknownArgs = new SmartList<>();
113+
112114
public List<String> unknownExtraFlags = new SmartList<>();
113115

114116
// Names of extra (-X...) arguments which have been passed in an obsolete form ("-Xaaa bbb", instead of "-Xaaa=bbb")

compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/argumentUtils.kt

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,9 @@ import java.util.*
2424
fun <A : CommonCompilerArguments> parseArguments(args: Array<String>, arguments: A, ignoreInvalidArguments: Boolean = false) {
2525
parseCommandLineArguments(args, arguments)
2626

27-
if (ignoreInvalidArguments) {
28-
arguments.freeArgs.removeAll { it.startsWith("-") }
29-
}
30-
31-
for (argument in arguments.freeArgs) {
32-
if (argument.startsWith("-")) {
33-
throw IllegalArgumentException("Invalid argument: " + argument)
34-
}
27+
val invalidArgument = arguments.unknownArgs.firstOrNull()?.takeUnless { ignoreInvalidArguments }
28+
if (invalidArgument != null) {
29+
throw IllegalArgumentException("Invalid argument: $invalidArgument")
3530
}
3631
}
3732

compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/parseCommandLineArguments.kt

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ val Argument.isAdvanced: Boolean
3030
get() = value.startsWith(ADVANCED_ARGUMENT_PREFIX) && value.length > ADVANCED_ARGUMENT_PREFIX.length
3131

3232
private val ADVANCED_ARGUMENT_PREFIX = "-X"
33+
private val FREE_ARGS_DELIMITER = "--"
3334

3435
// Parses arguments in the passed [result] object, or throws an [IllegalArgumentException] with the message to be displayed to the user
3536
fun <A : CommonCompilerArguments> parseCommandLineArguments(args: Array<String>, result: A) {
@@ -41,22 +42,32 @@ fun <A : CommonCompilerArguments> parseCommandLineArguments(args: Array<String>,
4142
}
4243

4344
val visitedArgs = mutableSetOf<String>()
45+
var freeArgsStarted = false
4446

4547
var i = 0
4648
while (i < args.size) {
4749
val arg = args[i++]
50+
51+
if (freeArgsStarted) {
52+
result.freeArgs.add(arg)
53+
continue
54+
}
55+
if (arg == FREE_ARGS_DELIMITER) {
56+
freeArgsStarted = true
57+
continue
58+
}
59+
4860
val argumentField = fields.firstOrNull { (_, argument) ->
4961
argument.value == arg ||
5062
argument.shortName.takeUnless(String::isEmpty) == arg ||
5163
(argument.isAdvanced && arg.startsWith(argument.value + "="))
5264
}
5365

5466
if (argumentField == null) {
55-
if (arg.startsWith(ADVANCED_ARGUMENT_PREFIX)) {
56-
result.unknownExtraFlags.add(arg)
57-
}
58-
else {
59-
result.freeArgs.add(arg)
67+
when {
68+
arg.startsWith(ADVANCED_ARGUMENT_PREFIX) -> result.unknownExtraFlags.add(arg)
69+
arg.startsWith("-") -> result.unknownArgs.add(arg)
70+
else -> result.freeArgs.add(arg)
6071
}
6172
continue
6273
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
OUT:
2+
hi
3+
-name
4+
Marty
5+
--
6+
there
7+
8+
Return code: 0
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
for (arg in args) {
2+
println(arg)
3+
}

compiler/tests/org/jetbrains/kotlin/integration/CompilerSmokeTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ public void testSimpleScript() throws Exception {
5757
runCompiler("script", "-script", "script.kts", "hi", "there");
5858
}
5959

60+
public void testScriptDashedArgs() throws Exception {
61+
runCompiler("script", "-script", "script.kts", "--", "hi", "-name", "Marty", "--", "there");
62+
}
63+
6064
public void testScriptWithClasspath() throws Exception {
6165
runCompiler("script", "-cp", new File("lib/javax.inject.jar").getAbsolutePath(), "-script", "script.kts");
6266
}

0 commit comments

Comments
 (0)