Fix property parsing bug in the cli (#596)

This commit is contained in:
Islon Scherer
2024-07-25 09:27:38 +02:00
committed by GitHub
parent bfc2d7abbf
commit e3133f604b
2 changed files with 14 additions and 4 deletions

View File

@@ -64,6 +64,16 @@ class BaseOptions : OptionGroup() {
throw CliException(message)
}
}
fun RawOption.associateProps():
OptionWithValues<Map<String, String>, Pair<String, String>, Pair<String, String>> {
return convert {
val parts = it.split("=")
if (parts.size <= 1) parts[0] to "true" else parts[0] to parts[1]
}
.multiple()
.toMap()
}
}
private val defaults = CliBaseOptions()
@@ -116,7 +126,7 @@ class BaseOptions : OptionGroup() {
metavar = "<name=value>",
help = "External property to set (repeatable)."
)
.associate()
.associateProps()
val noCache: Boolean by
option(names = arrayOf("--no-cache"), help = "Disable caching of packages")
@@ -214,7 +224,7 @@ class BaseOptions : OptionGroup() {
allowedModules = allowedModules.ifEmpty { null },
allowedResources = allowedResources.ifEmpty { null },
environmentVariables = envVars.ifEmpty { null },
externalProperties = properties.mapValues { it.value.ifBlank { "true" } }.ifEmpty { null },
externalProperties = properties.ifEmpty { null },
modulePath = modulePath.ifEmpty { null },
workingDir = workingDir,
settings = settings,

View File

@@ -45,10 +45,10 @@ class BaseCommandTest {
@Test
fun `external properties without value default to 'true'`() {
cmd.parse(arrayOf("-p", "flag1", "-p", "flag2", "-p", "FOO=bar"))
cmd.parse(arrayOf("-p", "flag1", "-p", "flag2=", "-p", "FOO=bar"))
val props = cmd.baseOptions.baseOptions(emptyList()).externalProperties
assertThat(props).isEqualTo(mapOf("flag1" to "true", "flag2" to "true", "FOO" to "bar"))
assertThat(props).isEqualTo(mapOf("flag1" to "true", "flag2" to "", "FOO" to "bar"))
}
@Test