1. Brief description
Picocli is a powerful, easy to use and feature-rich Java library for developing command-line tools. It supports multi-level subcommands, type-safe parameter analysis and automatic generation of help information, and has a low learning curve, making it ideal for modern CLI application development.
2. Why choose Picocli
Simple and easy to use: define parameters through annotations to reduce manual parsing logic.
Powerful features: Supports multiple parameter types, subcommands and multi-threaded execution.
Automation: Built-in help information and automatic completion functions.
Active Community: There are detailed documentation and extensive practical cases.
The core features of Picocli:
- Command line parameter analysis: Supports options, position parameters, and flags.
- Multi-level subcommands: Easily implement complex command-line structures.
- Automatically generate help information: no need to write a help document manually.
- Multithreading support: Easily implement concurrent tasks with ExecutionStrategy in @.
3. Practical examples
Here is a practical example showing how to use Picocli to develop CLI tools to build a simple file manipulation command line tool:
3.1 Add dependencies
Add Picocli dependencies in your Maven project:
<dependency> <groupId></groupId> <artifactId>picocli</artifactId> <version>4.7.4</version> </dependency>
3.2 Building a basic command
The following creates a simple command that supports reading and statistical operations on files.
import ; import ; import ; import ; import ; import ; import ; @Command(name = "filetool", version = "FileTool 1.0", description = "A command line tool for file operations", mixinStandardHelpOptions = true) public class FileTool implements Callable<Integer> { @Option(names = {"-p", "--path"}, description = "File Path", required = true) private String filePath; @Option(names = {"-c", "--count-lines"}, description = "Count the number of file lines") private boolean countLines; @Option(names = {"-r", "--read-content"}, description = "Read and output file content") private boolean readContent; @Override public Integer call() throws Exception { if (countLines) { long lineCount = ((filePath)).count(); ("File Lines: " + lineCount); } if (readContent) { String content = new String(((filePath))); ("File content:\n" + content); } return 0; } public static void main(String[] args) { int exitCode = new CommandLine(new FileTool()).execute(args); (exitCode); } }
3.3 Instructions for use
After compiling and running the program, you can use the tool in the following ways:
View Help Information:
java -jar --help
Output example:
Usage: filetool [-hV] [-c] [-r] -p=<filePath>
A command line tool for file operations
-c, --count-lines Statistics the number of file lines
-h, --help Show help information
-p, --path=<filePath> file path
-r, --read-content Read and output file content
-V, --version Print version information
Statistics the number of file lines:
java -jar -p -c
Output example:
Number of file lines: 10
Read file content:
java -jar -p -r
Output example:
File content:
Hello World!
This is a test file.
4. Advanced function examples
Picocli supports subcommands and can be used to implement complex CLI tools. Here is an example of building a multi-function tool that contains subcommands read and count:
@Command(name = "filetool", description = "File Tool", mixinStandardHelpOptions = true, subcommands = { , }) public class FileTool { public static void main(String[] args) { int exitCode = new CommandLine(new FileTool()).execute(args); (exitCode); } } @Command(name = "read", description = "Read file content") class FileReadCommand implements Callable<Integer> { @Option(names = {"-p", "--path"}, description = "File Path", required = true) private String filePath; @Override public Integer call() throws Exception { String content = new String(((filePath))); ("File content:\n" + content); return 0; } } @Command(name = "count", description = "Count the number of file lines") class FileCountCommand implements Callable<Integer> { @Option(names = {"-p", "--path"}, description = "File Path", required = true) private String filePath; @Override public Integer call() throws Exception { long lineCount = ((filePath)).count(); ("File Lines: " + lineCount); return 0; } }
Running example:
View Help Information:
java -jar --help
Use the subcommand to read:
java -jar read -p
Use the subcommand count:
java -jar count -p
5. Summary
Picocli is a modern Java command line tool development library that provides powerful features through simple annotations and intuitive APIs. Whether it is implementing a single command or building complex tools for multi-level subcommands, Picocli can quickly meet the needs.
Recommended usage scenarios:
Development, operation and maintenance tools: such as file management tools and data processing tools.
Data analysis script: Supports multi-threading.
Framework CLI tool for multi-subcommittees.
This is the end of this article about Java using Picocli to develop a simplified command line tool. For more related content on simplified Java Picocli, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!