Apps CLI
Introduction
In the context of Clean Architecture1, the Command Line Interface (CLI) can be one of several primary driver interfaces interacting directly with the application's use cases. Clean Architecture, stresses separation of concerns by organizing software into layers with distinct responsibilities and dependencies that flow inward. This structure ensures interfaces such as CLIs depend on policies and business rules, not vice versa.
In the Java ecosystem, there are several libraries and frameworks designed to help develop robust and efficient CLI applications. These frameworks simplify parsing command-line options, managing input/output, and providing an organized way to structure CLI-based applications. Here are some of the most popular Java frameworks and libraries used for CLI application development:
- Apache Commons CLI: Part of the Apache Commons project, this library provides an API for parsing command line options passed to programs. It is both flexible and easy to use, and it supports different types of options including single-dash and double-dash style options.
- JCommander: Developed by Cedric Beust, the creator of TestNG, JCommander is a small library that annotates Java classes with command line options to parse the command line arguments. It provides easy handling of typed variables, defaults, and help for the commands and is particularly useful for grabbing structured information from the command line arguments.
- picocli: A modern framework that leverages annotations and type converters to reduce the amount of boilerplate code needed to create complex command-line interfaces. Picocli supports sub-commands, colors, auto-completion, and is capable of generating detailed usage help messages in a variety of formats. It has the advantage of a very small footprint (a single file), making it easy to include in projects.
- Argparse4j: Inspired by Python's argparse module, Argparse4j aims to provide a command-line argument parser that understands how users prefer to write their commands in the console. It supports various argument types, repeated arguments, and more, and it can generate formatted help information.
- Spring Shell: For developers already using the Spring Framework, Spring Shell provides tools to create an interactive shell application. It uses a pluggable command-line parser and is integrated with Spring ASCII Art Generator for producing banners, bringing an aspect of customizability and visual appeal to CLI applications.
- Airline: A powerful annotation-based framework for building robust CLI applications, Airline helps you compartmentalize your command-line application by separating option parsing, command metadata, and business logic.
Our application already uses the Spring Framework, and it makes sense to use Spring Shell2 to keep the technologies consistent.
Clean architecture emphasizes separation of concerns by organizing software into layers with different responsibilities and dependencies that flow within. This structure ensures that interfaces such as the CLI depend on policies and business rules, and not the other way around.