diff --git a/kotlin-native/klib/src/main/kotlin/org/jetbrains/kotlin/cli/klib/KlibToolArguments.kt b/kotlin-native/klib/src/main/kotlin/org/jetbrains/kotlin/cli/klib/KlibToolArguments.kt new file mode 100644 index 00000000000..577d073a03f --- /dev/null +++ b/kotlin-native/klib/src/main/kotlin/org/jetbrains/kotlin/cli/klib/KlibToolArguments.kt @@ -0,0 +1,16 @@ +/* + * Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package org.jetbrains.kotlin.cli.klib + +import org.jetbrains.kotlin.library.KotlinIrSignatureVersion + +internal class KlibToolArguments( + val commandName: String, + val libraryNameOrPath: String, + val repository: String?, + val printSignatures: Boolean, + val signatureVersion: KotlinIrSignatureVersion?, +) diff --git a/kotlin-native/klib/src/main/kotlin/org/jetbrains/kotlin/cli/klib/KlibToolArgumentsParser.kt b/kotlin-native/klib/src/main/kotlin/org/jetbrains/kotlin/cli/klib/KlibToolArgumentsParser.kt new file mode 100644 index 00000000000..6da0521a7ff --- /dev/null +++ b/kotlin-native/klib/src/main/kotlin/org/jetbrains/kotlin/cli/klib/KlibToolArgumentsParser.kt @@ -0,0 +1,113 @@ +/* + * Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package org.jetbrains.kotlin.cli.klib + +import org.jetbrains.kotlin.library.KotlinIrSignatureVersion +import kotlin.system.exitProcess + +internal class KlibToolArgumentsParser { + fun parseArguments(rawArgs: Array): KlibToolArguments { + if (rawArgs.size < 2) { + printUsage() + exitProcess(0) + } + + val extraArgs: Map> = parseOptions(rawArgs.drop(2).toTypedArray()) + .entries + .mapNotNull { (option, values) -> + val knownOption = ExtraOption.parseOrNull(option) + if (knownOption == null) { + logWarning("Unrecognized command-line argument: $option") + return@mapNotNull null + } + knownOption to values + }.toMap() + + val signatureVersion = extraArgs[ExtraOption.SIGNATURE_VERSION]?.last()?.let { rawSignatureVersion -> + rawSignatureVersion.toIntOrNull()?.let(::KotlinIrSignatureVersion) + ?: logError("Invalid signature version: $rawSignatureVersion") + } + + if (signatureVersion != null && signatureVersion !in KotlinIrSignatureVersion.CURRENTLY_SUPPORTED_VERSIONS) + logError("Unsupported signature version: ${signatureVersion.number}") + + + return KlibToolArguments( + commandName = rawArgs[0], + libraryNameOrPath = rawArgs[1], + repository = extraArgs[ExtraOption.REPOSITORY]?.last(), + printSignatures = extraArgs[ExtraOption.PRINT_SIGNATURES]?.last()?.toBoolean() == true, + signatureVersion, + ) + } + + private fun parseOptions(args: Array): Map> { + val options = mutableMapOf>() + for (index in args.indices step 2) { + val key = args[index] + if (key[0] != '-') { + logError("Expected a flag with initial dash: $key") + } + if (index + 1 == args.size) { + logError("Expected an value after $key") + } + val value = listOf(args[index + 1]) + options[key]?.addAll(value) ?: options.put(key, value.toMutableList()) + } + return options + } + + private fun printUsage() { + println( + """ + Usage: klib [