On this page

Picocli

1 Introduction

Picocli is a command line parser that supports usage help with ANSI colors, autocomplete and nested subcommands. It has an annotations API to create command line applications with almost no code, and a programmatic API for dynamic uses like creating Domain Specific Languages.

From the project Readme page:

How it works: annotate your class and picocli initializes it from the command line arguments, converting the input to strongly typed data. Supports git-like subcommands (and nested sub-subcommands), any option prefix style, POSIX-style grouped short options, password options, custom type converters and more. Parser tracing facilitates troubleshooting.

It distinguishes between named options and positional parameters and allows both to be strongly typed. Multi-valued fields can specify an exact number of parameters or a range (e.g., 0..*, 1..2). Supports Map options like -Dkey1=val1 -Dkey2=val2, where both key and value can be strongly typed.

It generates polished and easily tailored usage help and version help, using ANSI colors where possible. Picocli-based command line applications can have TAB autocompletion, interactively showing users what options and subcommands are available. Picocli can generate completion scripts for bash and zsh, and offers an API to easily create a JLine Completer for your application.

Micronaut features dedicated support for defining picocli Command instances. Micronaut applications built with picocli can be deployed with or without the presence of an HTTP server.

Combining picocli with Micronaut makes it easy to provide a rich, well-documented command line interface for your Microservices.

2 Release History

For this project, you can find a list of releases (with release notes) here:

3 Create Micronaut Picocli app

You can create a Micronaut command line interface application using the Micronaut CLI:

Example 1. Using the CLI
$ mn create-cli-app my-app
launch

4 Setting up Picocli

To add support for Picocli to an existing project, you should first add the picocli dependency and the Micronaut picocli configuration to your build configuration.

implementation("io.micronaut.picocli:micronaut-picocli")

The picocli-codegen module includes an annotation processor that can build a model from the picocli annotations at compile time rather than at runtime. Enabling this annotation processor in your project is optional, but recommended.

annotationProcessor("info.picocli:picocli-codegen")
Note
The picocli-codegen annotation processor is incompatible with the Kotlin KSP compiler plugin. Using it in a Kotlin project requires the Kotlin Kapt compiler plugin instead.

Configuring picocli

Picocli does not require configuration. See other sections of the manual for configuring the services and resources to inject.

5 Generating a Picocli Project

To create a project with picocli support using the Micronaut CLI, use the create-cli-app command. This will add the dependencies for the picocli feature, and set the applicationType of the generated project to cli, so the create-command command is available to generate additional commands.

The main class of the project is set to the *Command class (based on the project name - e.g., hello-world will generate HelloWorldCommand):

$ mn create-cli-app my-cli-app

The generated command looks like this:

my.cli.app.MyCliAppCommand.java generated by create-cli-app

Running the Application

Now you can build the project and start the application. Generate an executable Jar. When you run this JAR, it executes the MyCliAppCommand command.

With Gradle:

$ ./gradlew shadowJar
$ java -jar build/libs/my-cli-app-0.1-all.jar -v

With Maven Package:

$ ./mvnw package
$ java -jar target/my-cli-app-0.1.jar -v

6 Picocli Quick Start

Creating a Picocli Command with @Command

This section will show a quick example that provides a command line interface to a HTTP client that communicates with the GitHub API.

When creating this example project with the Micronaut CLI, use the create-cli-app command, and add the --features=http-client flag:

$ mn create-cli-app example.git-star --features http-client

This will add the io.micronaut:micronaut-http-client dependency to the build. You can also manually add to your build:

implementation("io.micronaut.picocli:micronaut-http-client")

An Example HTTP Client

To create a picocli Command you create a class with fields annotated with @Option or @Parameters to capture the values of the command line options or positional parameters, respectively.

For example the following is a picocli @Command that wraps around the GitHub API:

Example picocli command with injected HTTP client

The usage help message generated for this command looks like this:

picocli example

7 Subcommands

If your service has a lot of functionality, a common pattern is to have subcommands to control different areas of the service. To allow Micronaut to inject services and resources correctly into the subcommands, make sure to obtain subcommand instances from the ApplicationContext, instead of instantiating them directly.

The easiest way to do this is to declare the subcommands on the top-level command, like this:

A top-level command with subcommands

8 Customizing Picocli

Occasionally you may want to set parser options or otherwise customize picocli behavior. This can easily be done via the setter methods on the picocli CommandLine object, but the PicocliRunner does not expose that object.

In such cases, you may want to invoke picocli directly instead of using the PicocliRunner. The code below demonstrates how to do this:

Example of customizing the picocli parser before invoking a command

9 Repository

You can find the source code of this project in this repository: