CLI frameworks for Java: Picocli, JCommander, and Spring Shell

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

CLI frameworks for Java: building powerful console applications

Java remains one of the most popular languages for creating server-side applications, but console utilities are also regularly written in it. However, manually parsing command-line arguments via args[] quickly turns into chaos when options, flags, and nested commands appear. Specialized CLI frameworks come to the rescue. In this article, we will look at three leading tools: Picocli, JCommander, and Spring Shell. You will learn their features, see code examples, and be able to choose the right one for your project.



1. Picocli — modern and performant

Picocli (pronounced "pike-cli") is a lightweight framework with minimal dependencies. It supports annotations, shell auto-completion, nested commands, and even help generation in man-page format. Picocli is ideal for creating both simple utilities and complex multi-level CLIs.



Example of a basic calculator with Picocli:

import picocli.CommandLine;import picocli.CommandLine.Command;import picocli.CommandLine.Option;import picocli.CommandLine.Parameters;

@Command(name = "calc", mixinStandardHelpOptions = true, version = "1.0", description = "Simple calculator")public class Calculator implements Runnable {

@Parameters(index = "0", description = "First number") private double a;

@Parameters(index = "1", description = "Second number") private double b;

@Option(names = {"-o", "--operation"}, defaultValue = "+", description = "Operation: +, -, *, /") private String operation;

@Override public void run() { double result = switch (operation) { case "+" -> a + b; case "-" -> a - b; case "*" -> a * b; case "/" -> a / b; default -> throw new IllegalArgumentException("Unknown operation: " + operation); }; System.out.printf("Result: %.2f%n", result); }

public static void main(String[] args) { int exitCode = new CommandLine(new Calculator()).execute(args); System.exit(exitCode); }}

Run: java Calculator 10 5 -o "*" will output Result: 50.00. Picocli will automatically generate help with --help and version with --version.



2. JCommander — time-tested

JCommander is one of the oldest libraries for argument parsing in Java. It uses annotations but provides more manual control over parsing. JCommander is often chosen for projects that require flexibility and integration with existing code.



Example of a currency converter with JCommander:

import com.beust.jcommander.JCommander;import com.beust.jcommander.Parameter;import com.beust.jcommander.Parameters;

@Parameters(commandDescription = "Currency converter")public class CurrencyConverter {

@Parameter(names = {"--amount", "-a"}, description = "Amount", required = true) private Double amount;

@Parameter(names = {"--from", "-f"}, description = "From currency (USD, EUR)", required = true) private String from;

@Parameter(names = {"--to", "-t"}, description = "To currency", required = true) private String to;

@Parameter(names = {"--help", "-h"}, help = true, description = "Show help") private boolean help;

public void run() { if (help) { JCommander jc = new JCommander(this); jc.usage(); return; } // Simplified conversion logic double rate = switch (from.toUpperCase() + "_" + to.toUpperCase()) { case "USD_EUR" -> 0.92; case "EUR_USD" -> 1.09; default -> 1.0; }; System.out.printf("%.2f %s = %.2f %s%n", amount, from.toUpperCase(), amoun

Recommendations