Clikt

Kotlin的多平台命令行接口解析。(Multiplatform command line interface parsing for Kotlin)

Github stars Tracking Chart







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)

Main metrics

Overview
Name With Ownerajalt/clikt
Primary LanguageKotlin
Program languageKotlin (Language Count: 3)
PlatformLinux, Mac, Windows
License:Apache License 2.0
所有者活动
Created At2018-04-10 18:04:56
Pushed At2025-04-11 16:12:27
Last Commit At2025-04-11 18:12:27
Release Count45
Last Release Name5.0.3 (Posted on )
First Release Name1.0.0 (Posted on )
用户参与
Stargazers Count2.7k
Watchers Count17
Fork Count126
Commits Count869
Has Issues Enabled
Issues Count334
Issue Open Count18
Pull Requests Count212
Pull Requests Open Count0
Pull Requests Close Count26
项目设置
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private

Clikt (pronounced "clicked") is a Kotlin library that makes writing
command line interfaces simple and intuitive. It's the "Command Line
Interface for Kotlin".

It is designed to make the process of writing command line tools effortless
while supporting a wide variety of use cases and allowing advanced
customization when needed.

Clikt has:

  • arbitrary nesting of commands
  • composable, type safe parameter values
  • support for a wide variety of command line interface styles

What does it look like? Here's a complete example of a simple Clikt
program:

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() {
        for (i in 1..count) {
            echo("Hello $name!")
        }
    }
}

fun main(args: Array<String>) = Hello().main(args)

And here's what it looks like when run:

 $ ./hello --count=3
 Your name: John
 Hello John!
 Hello John!
 Hello John!

The help page is generated for you:

$ ./hello --help
Usage: hello [OPTIONS]

Options:
  --count INT  Number of greetings
  --name TEXT  The person to greet
  -h, --help   Show this message and exit

Errors are also taken care of:

$ ./hello --whoops
Usage: hello [OPTIONS]

Error: no such option: "--whoops".

Documentation

The full documentation can be found on the website.

There are also a number of sample applications. You can run
them with the included runsample script.

Installation

Clikt is distributed through
Maven Central,
Jcenter and
Jitpack.

dependencies {
   implementation("com.github.ajalt:clikt:2.4.0")
}

License

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.

No more projects by this owner are included. Recommend one?