CLI frameworks for Kotlin: building powerful console applications
Kotlin is a modern language that is great not only for Android or server-side development, but also for creating console (CLI) applications. To avoid writing argument parsing manually, there are specialized frameworks. In this article, we will cover three of the most popular solutions: Clikt, kotlinx-cli, and picocli. You will learn their features, see code examples, and be able to choose the right tool for your project.
Why do you need a CLI framework?
Creating command-line utilities is a task that quickly becomes complex: you need to handle flags, options, positional arguments, and output help text. Manual implementation leads to errors and code duplication. Frameworks offer ready-made mechanisms:
- Declarative description of the command interface
- Automatic help generation (--help)
- Input validation
- Support for subcommands (git style)
- Type safety
1. Clikt — the most "Kotlin-like" framework
Clikt (https://github.com/ajalt/clikt) is a pure Kotlin library that uses DSL and delegated properties. It does not depend on Java annotations and offers an intuitive API.
Installation
// build.gradle.ktsimplementation("com.github.ajalt.clikt:clikt:4.2.2")Example: a simple calculator
import com.github.ajalt.clikt.core.CliktCommandimport com.github.ajalt.clikt.parameters.arguments.argumentimport com.github.ajalt.clikt.parameters.options.optionimport com.github.ajalt.clikt.parameters.types.int
class Calc : CliktCommand() { val a: Int by argument() val b: Int by argument() val multiply by option("--mult", "-m", help = "Multiply instead of adding").flag()
override fun run() { val result = if (multiply) a * b else a + b echo("Result: $result") }}
fun main(args: Array<String>) = Calc().main(args)Run: kotlin calc 5 3 --mult → Result: 15.
Advantages of Clikt:
- Full integration with Kotlin (DSL, delegates)
- Powerful validation system (custom validators)
- Support for subcommands of any depth
- Automatic man page generation
- Active community and updates
2. kotlinx-cli — a lightweight solution from JetBrains
kotlinx-cli is an experimental library that is part of the Kotlin ecosystem. It is minimalistic and suitable for small utilities.
Installation
// build.gradle.ktsimplementation("org.jetbrains.kotlinx:kotlinx-cli:0.3.6")Example: a greeting
import kotlinx.cli.*
fun main(args: Array<String>) { val parser = ArgParser("greet") val name by parser.option(ArgType.String, shortName = "n", description = "Name") .default("world") val times by parser.option(ArgType.Int, shortName = "t", description = "Number of times") .default(1)
parser.parse(args)
repeat(times) { println("Hello, $name!") }}Run: kotlin greet -n Kotlin -t 2 → Hello, Kotlin! (twice).
Features of kotlinx-cli:
- Very simple API — minimal code
- Support for basic types (String, Int, Boolean)
- Not well suited for complex command hierarchies
- Experimental status (API may change)
- No built-in validation or hints
3. picocli — a powerful framework with Java roots
picocli is a popular framework that works with both Java and Kotlin. It uses annotations but has special support for Kotlin DSL.
Also in library
Axios for JavaScript (Node.js): Complete Guide with Examples
Prometheus Metrics in Kotlin: Integration with Micrometer and Ktor
Working with CSV in Go (Golang): A Complete Guide to encoding/csv
Swift Task Scheduler for iOS/macOS: EventKit and DispatchSource