CliktKotlin 命令行接口
Clikt 是一個 Kotlin 庫,Kotlin 的命令行接口,能夠使編寫命令行接口變得簡潔直觀,同時支持各種各樣的用例,并允許在需要時進行高級定制。
Clikt 的特點:
- 可以任意嵌套命令
- 可組合,類型安全的參數(shù)值
- 支持多種命令行接口風(fēng)格
- JVM,NodeJS和本機Linux,Windows和MacOS的多平臺軟件包
示例代碼:
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)
運行結(jié)果:
$ ./hello --count=3 Your name: John Hello John! Hello John! Hello John!
自動生成幫助參數(shù):
$ ./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".
評論
圖片
表情
