CLI: support false and true values for boolean -X arguments

#KT-48417 Fixed
This commit is contained in:
Alexander Udalov
2021-08-26 03:38:43 +02:00
parent a37642e5af
commit fbfbb0c72c
10 changed files with 56 additions and 1 deletions
@@ -54,6 +54,8 @@ data class ArgumentParseErrors(
var argumentWithoutValue: String? = null,
var booleanArgumentWithValue: String? = null,
val argfileErrors: MutableList<String> = SmartList(),
// Reports from internal arguments parsers
@@ -159,7 +161,16 @@ private fun <A : CommonToolArguments> parsePreprocessedCommandLineArguments(args
val (property, argument) = argumentField
val value: Any = when {
argumentField.property.returnType.classifier == Boolean::class -> true
argumentField.property.returnType.classifier == Boolean::class -> {
if (arg.startsWith(argument.value + "=")) {
// Can't use toBooleanStrict yet because this part of the compiler is used in Gradle and needs API version 1.4.
when (arg.substring(argument.value.length + 1)) {
"true" -> true
"false" -> false
else -> true.also { errors.booleanArgumentWithValue = arg }
}
} else true
}
arg.startsWith(argument.value + "=") -> {
arg.substring(argument.value.length + 1)
}
@@ -213,6 +224,9 @@ fun validateArguments(errors: ArgumentParseErrors?): String? {
if (errors.argumentWithoutValue != null) {
return "No value passed for argument ${errors.argumentWithoutValue}"
}
errors.booleanArgumentWithValue?.let { arg ->
return "No value expected for boolean argument ${arg.substringBefore('=')}. Please remove the value: $arg"
}
if (errors.unknownArgs.isNotEmpty()) {
return "Invalid argument: ${errors.unknownArgs.first()}"
}
@@ -0,0 +1,4 @@
$TESTDATA_DIR$/extraBooleanArgumentEqualsFalse.kt
-d
$TEMP_DIR$
-Xallow-kotlin-package=false
@@ -0,0 +1,3 @@
package kotlin
class X
@@ -0,0 +1,4 @@
compiler/testData/cli/jvm/extraBooleanArgumentEqualsFalse.kt:1:1: error: only the Kotlin standard library is allowed to use the 'kotlin' package
package kotlin
^
COMPILATION_ERROR
@@ -0,0 +1,4 @@
$TESTDATA_DIR$/extraBooleanArgumentEqualsFalse.kt
-d
$TEMP_DIR$
-Xallow-kotlin-package=nonsense
@@ -0,0 +1,3 @@
error: no value expected for boolean argument -Xallow-kotlin-package. Please remove the value: -Xallow-kotlin-package=nonsense
info: use -help for more information
COMPILATION_ERROR
@@ -0,0 +1,4 @@
$TESTDATA_DIR$/extraBooleanArgumentEqualsTrue.kt
-d
$TEMP_DIR$
-Xallow-kotlin-package=true
@@ -0,0 +1,3 @@
package kotlin
class X
@@ -0,0 +1 @@
OK
@@ -281,6 +281,21 @@ public class CliTestGenerated extends AbstractCliTest {
runTest("compiler/testData/cli/jvm/extraArgumentPassedInObsoleteForm.args");
}
@TestMetadata("extraBooleanArgumentEqualsFalse.args")
public void testExtraBooleanArgumentEqualsFalse() throws Exception {
runTest("compiler/testData/cli/jvm/extraBooleanArgumentEqualsFalse.args");
}
@TestMetadata("extraBooleanArgumentEqualsNonsense.args")
public void testExtraBooleanArgumentEqualsNonsense() throws Exception {
runTest("compiler/testData/cli/jvm/extraBooleanArgumentEqualsNonsense.args");
}
@TestMetadata("extraBooleanArgumentEqualsTrue.args")
public void testExtraBooleanArgumentEqualsTrue() throws Exception {
runTest("compiler/testData/cli/jvm/extraBooleanArgumentEqualsTrue.args");
}
@TestMetadata("extraHelp.args")
public void testExtraHelp() throws Exception {
runTest("compiler/testData/cli/jvm/extraHelp.args");