Skip to content

lightningkite/kotliner-cli

Repository files navigation

Kotlin-er CLI

A different take on making command line programs, emphasizing development speed over customization and performance.

Essentially exposes Kotlin functions as command-line options.

Pros:

  • Extremely fast to set up
  • Uses Kotlin reflection to reduce documentation and argument definition overhead
  • Ensures at least a minimal documentation and error checking will exist in your CLI

Cons:

  • Slow runtime performance due to reflection
  • Not compatible with Kotlin Multiplatform due to reflection

Status

Auto-deployed to Maven Central after passing unit tests using GitHub Actions

release status snapshot status

Release on Sonatype Releases Release on Sonatype Snapshots

Download

dependencies {
  //...
  implementation("com.lightningkite:kotliner-cli:$kotlinerVersion")
}

Example

import com.lightningkite.kotlinercli.cli
import java.io.File

fun main(args: Array<String>) = cli(args, ::runServer, ::migrate, ::dump, ::deleteItems)

fun runServer(host: String = "0.0.0.0", port: Int = 8080) = println("Running the server at $host on port $port")
fun migrate(version: Int) = println("Upgrading the database")
fun dump(to: File) = println("Dumping to a file")
fun deleteItems(vararg ids: Int) = println("Deleting ${ids.joinToString()}")
$ myProgram --help
Available commands:
runServer [--host <String>] [--port <Int>]
migrate --version <Int>
dump --to <File>
deleteItems --ids <Int...>

$ myProgram runServer --help
runServer
--host <String> (optional)
--port <Int> (optional)

$ myProgram runServer
Running the server at 0.0.0.0 on port 8080

$ myProgram runServer 127.0.0.0 8080
Running the server at 127.0.0.0 on port 8080

$ myProgram runServer --port 8080
Running the server at 0.0.0.0 on port 8080

$ myProgram deleteItems 1 2 3
Deleting 1, 2, 3

Supported Features

  • Primitive Types
  • Types with a constructor that takes a String
  • Ordered Arguments
  • Named Arguments
  • Default Arguments
  • Variadic Arguments
  • Enum Arguments (case-insensitive)
  • Boolean Flags (i.e. using --flag for flag=true)
  • Description Annotation - add the annotation to provide more documentation about functions and parameters.
  • Interactive mode when no arguments are given (like supervisor)

Potential Features

  • Annotation to automatically register functions to expose to the CLI
    • Questionable security-wise - makes it easy to insert functionality with no obvious connection.
  • Compiler plugin to read KDoc for runtime documentation

Deliberately unsupported features

  • Object types without a string constructor - No good way to represent in the command line.
  • Expose all functions in the command line - would be a potential security problem