Maven:
<dependency>
<groupId>com.github.ajalt</groupId>
<artifactId>clikt</artifactId>
<version>3.2.0</version>
</dependency>
Gradle:
groovy
compile 'com.github.ajalt:clikt:3.2.0'
import com.github.ajalt.clikt.core.CliktCommand;
import com.github.ajalt.clikt.parameters.arguments.PairArgument;
import com.github.ajalt.clikt.parameters.arguments.argument;
import com.github.ajalt.clikt.parameters.options.Option;
import com.github.ajalt.clikt.parameters.options.flag;
import com.github.ajalt.clikt.parameters.options.option;
public class HelloCommand extends CliktCommand {
private final Option<String> name by option(help = "Your name")
private final Option<Int> age by option(help = "Your age")
private final Option<Boolean> verbose by option(help = "Enable verbose output").flag(default = false)
private final Argument<String, Int> pair1 by argument()
.pair()
.validate { require(it.second >= it.first) { "Second must be >= first" } }
private final Argument<String, String> pair2 by argument()
.pair()
.validate { require(it.first.length < it.second.length) { "First length must be < Second length" } }
override fun run() {
echo("Hello, ${name ?: "Anonymous"}!")
if (verbose) echo("Your age is ${age ?: "unknown"}.")
echo("Pair1: ${pair1.first} ${pair1.second}")
echo("Pair2: ${pair2.first} ${pair2.second}")
}
}
public class Main {
public static void main(String[] args) {
HelloCommand().main(args)
}
}
java Main --name=John --age=30 arg1 arg2
Hello, John!
Your age is 30.
Pair1: arg1 arg2
Pair2: arg1 arg2