Picocli – Easiness for CLI arguments in Java

Advertisements

CLI (Command Line Interface) is the way how a program gets input when is executed from the command line or console.

It’s usual that the applications require to some additional parameters or arguments to be initialized. That entrypoint for Java (also Scala and Kotlin) applications is the famous public static main (String [] args) method, on it the arguments and options of the applications are indicated as an array of String elements, those arguments will be later interpreted by the application logic.

How to handle those argument in a really functional way (indexed, named parameters, optional parameters, default values, and more) following standards of UNIX, or POSIX will require a considerable effort. However, that’s why we have and can use Picocli.

Picocli, defines itself as the easiest way to create a rich command line application for JVM (and non JVM) environments. Let’s see a little bit more of this utility.

Getting started

Lets start by creating a simple example class, receiving three arguments and perform a math operation.

Java

The application then is called from command line with:

Shell

We can see the dedicated effort to get, validate, and cast the arguments/parameters from the String array to the expected data that our application requires. And these are just basic validations, and don’t provide a rich and friendly interaction. So, let’s transform that code but using picocli at this time.

Java

We can see that picocli is in charge of getting the parameters by us, validating the data (see the enum for example) and casting them to the expected datatype. Now, if we want to call the application we have many different ways to do it.

Shell

For this operation we can use the commands -o , --operator , that value is optional, so if not indicated it will use a default value. In addition to that, the option -o can be indicated before or after any other arguments.

Advertisements

Another advantage provided by Picocli is that our class has a description field in the Command annotation, also the annotations Option y Parameter have it. Therefore it can generate automatically the documentation and help section accessible from command line for our application (without any extra effort) . So, the user can use the help command to understand how our application works and what it requires.

Options and Parameters

An @Option is an argument that must have one or more names. The names are case-sensitive, but that can be customized if required.

Java

In addition to that, the options can be “interactive“, in other words, if the option is not indicated during the application call, Picocli will request it in the command line before starting your application logic. This is very useful for passwords.

Java

Any argument that is not a subcommand or an @Option is then considered as a “parameter”. The parameters are interpreted in posicional order, with indexed from 0 to N.

Java

Also, a range of indexes can be indicated, and it will be interpret and map as an array in our code.

Java

Supported Data types

There is a big number of datatype supported out of the box, for most of the common uses we can face or required. They are validated by Picocli, so the don’t have to take care how to transform the String argument to our expected datatype value.

  • Any primitives Java type (boolean, short, char, int, double, float, long) and its wrappers.
  • Any enum
  • StringStringBuilderCharSequence
  • java.math.BigDecimaljava.math.BigInteger
  • java.nio.Charset
  • java.io.File
  • java.net.InetAddress
  • java.util.regex.Pattern
  • java.util.Date (in format "yyyy-MM-dd")
  • java.net.URLjava.net.URI
  • java.util.UUID
  • java.lang.Class 
  • java.nio.file.Path 
  • java.time object of the package: 
    • DurationInstantLocalDateLocalDateTimeLocalTimeMonthDayOffsetDateTimeOffsetTimePeriodYearYearMonthZonedDateTimeZoneIdZoneOffset 
  • java.sql.Connection (with the url pattern-> jdbc:subprotocol:subname)
  • and more data types.

In addition to that, customized data type converts can be created if required. Se more here

Adding Picocli to my code

If maven or gradle is used, the corresponding dependency can be added to the project.

Also, the creators of Picocli have created the utility as a Single class framework. They allow people to get the Command class from this git repository, and copy it into your code.

References

Advertisements

Leave a Reply

Your email address will not be published. Required fields are marked *