在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):Kotlin/kotlinx-cli开源软件地址(OpenSource Url):https://github.com/Kotlin/kotlinx-cli开源编程语言(OpenSource Language):Kotlin 100.0%开源软件介绍(OpenSource Introduction):kotlinx-cliPure Kotlin implementation of a generic command-line parser.
Using in your projects
The library is published to Maven Central repository. Gradle
repositories {
mavenCentral()
} In Kotlin multiplatform projects, add the following dependency to a source set (it may be a common or platform specific source set): kotlin {
sourceSets {
commonMain {
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-cli:0.3.4")
}
}
}
}
If Gradle is used to build a project, turning on endorsed libraries in Kotlin/Native is possible with kotlin {
linuxX64("linux") {
compilations["main"].enableEndorsedLibs = true
}
}
MavenIn Kotlin projects, add the following dependency to the <dependency>
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>kotlinx-cli-jvm</artifactId>
<version>0.3.4</version>
</dependency> Command line entitiesThere are 2 base entity: option and argument. Option - command line entity started with some prefix (-/--) and can have value as next entity in command line string. Argument - command line entity which role is connected only with its position. Command line entities can be several types:
Custom types can be created. Exampleimport kotlinx.cli.*
fun produce(result: List<Double>, format: String, outputFileName: String?) {
outputFileName.let {
// Print to file.
...
} ?: run {
// Print to stdout.
...
}
}
fun readFrom(inputFileName: String): String {
...
}
fun calculate(inputData: String, eps: Double, debug: Boolean = false): List<Double> {
...
}
enum class Format {
HTML,
CSV,
PDF
}
fun main(args: Array<String>) {
val parser = ArgParser("example")
val input by parser.option(ArgType.String, shortName = "i", description = "Input file").required()
val output by parser.option(ArgType.String, shortName = "o", description = "Output file name")
val format by parser.option(ArgType.Choice<Format>(), shortName = "f",
description = "Format for output file").default(Format.CSV).multiple()
val stringFormat by parser.option(ArgType.Choice(listOf("html", "csv", "pdf"), { it }), shortName = "sf",
description = "Format as string for output file").default("csv").multiple()
val debug by parser.option(ArgType.Boolean, shortName = "d", description = "Turn on debug mode").default(false)
val eps by parser.option(ArgType.Double, description = "Observational error").default(0.01)
parser.parse(args)
val inputData = readFrom(input)
val result = calculate(inputData, eps, debug)
format.forEach {
produce(result, it, output)
}
} It's also possible to use arguments in current example. ...
val input by parser.argument(ArgType.String, description = "Input file")
val output by parser.argument(ArgType.String, description = "Output file name").optional() Auto-generated help message for this example is
SubcommandsIf application has rich command line interface and executes different actions with different arguments, subcommands can be useful. @file:OptIn(ExperimentalCli::class)
import kotlinx.cli.*
fun main(args: Array<String>) {
val parser = ArgParser("example")
val output by parser.option(ArgType.String, "output", "o", "Output file")
class Summary: Subcommand("summary", "Calculate summary") {
val invert by option(ArgType.Boolean, "invert", "i", "Invert results").default(false)
val addendums by argument(ArgType.Int, "addendums", description = "Addendums").vararg()
var result: Int = 0
override fun execute() {
result = addendums.sum()
result = if (invert!!) -1 * result else result
}
}
class Multiply: Subcommand("mul", "Multiply") {
val numbers by argument(ArgType.Int, description = "Addendums").vararg()
var result: Int = 0
override fun execute() {
result = numbers.reduce{ acc, it -> acc * it }
}
}
val summary = Summary()
val multiple = Multiply()
parser.subcommands(summary, multiple)
parser.parse(args)
} Then help information will be available for each subcommand separately. In case of
In case of
The boolean property @file:OptIn(ExperimentalCli::class)
import kotlinx.cli.*
fun main(args: Array<String>) {
val parser = ArgParser("example", strictSubcommandOptionsOrder = true)
val output by parser.option(ArgType.String, "output", "o", "Output file")
class Multiply: Subcommand("mul", "Multiply") {
val numbers by argument(ArgType.Int, description = "Addendums").vararg()
var result: Int = 0
override fun execute() {
result = numbers.reduce{ acc, it -> acc * it }
}
}
val multiple = Multiply()
parser.subcommands(summary, multiple)
parser.parse(args)
}
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论