Clikt
Clikt(读作 "clicked")是一个多平台的 Kotlin 库,它使编写命令行界面变得简单直观。它是 "Kotlin的命令行接口"。
它的设计目的是让编写命令行工具的过程变得毫不费力,同时支持各种用例,并允许在需要时进行高级定制。
Clikt 具有
- 命令的任意嵌套
- 可组合、类型安全的参数值
- 支持多种命令行界面风格。
- 适用于 JVM、NodeJS 和原生 Linux、Windows 和 MacOS 的多平台包。
它是什么样子的呢?下面是一个简单的 Clikt 程序的完整例子。
class Hello : CliktCommand() { val count: Int by option(help="Number of greetings").int().default(1) val name: String by option(help="The person to greet").prompt("Your name") override fun run() { repeat(count) { echo("Hello $name!") } } } fun main(args: Array<String>) = Hello().main(args)
这是它运行时的样子:
$ ./hello --count=3 Your name: John Hello John! Hello John! Hello John!
帮助页面将为您生成:
$ ./hello --help Usage: hello [OPTIONS] Options: --count INT Number of greetings --name TEXT The person to greet -h, --help Show this message and exit
错误也会得到处理:
$ ./hello --whoops Usage: hello [OPTIONS] Error: no such option: "--whoops".
文档
完整的文件可以在 网站 上找到。
此外,还有一些示例应用程序。您可以使用包含的 runsample
脚本 运行它们。
安装方法
Clikt 是通过 Maven Central 发布的。
dependencies { implementation("com.github.ajalt:clikt:2.8.0") }
多平台
对于多平台项目,使用 "com.github.ajalt:clikt-multiplatform:$cliktVersion "代替。你需要使用 Gradle 6 或更新的版本。
Clikt 支持以下目标:jvm、mingwX64、linuxX64、macosX64 和 js(对于 NodeJS 和 Browsers)。关于每个目标所支持的功能的更多信息,请参见文档。
Snapshots
也可提供快照构建。你需要添加 Sonatype 快照存储库。
repositories { maven { url = uri("https://oss.sonatype.org/content/repositories/snapshots/") } }
许可
Copyright 2018-2020 AJ Alt Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
(The first version translated by vz on 2020.08.16)