Java利用Picocli開發(fā)一個簡化命令行工具
1、簡述
Picocli 是一個強大、易用且功能豐富的 Java 庫,用于開發(fā)命令行工具。它支持多級子命令、類型安全的參數(shù)解析和自動生成幫助信息,同時具有較低的學習曲線,是現(xiàn)代 CLI 應用開發(fā)的理想選擇。
2、為什么選擇 Picocli
簡單易用:通過注解定義參數(shù),減少手動解析邏輯。
強大功能:支持多種參數(shù)類型、子命令和多線程執(zhí)行。
自動化:內(nèi)置幫助信息和自動補全功能。
活躍社區(qū):有詳細的文檔和大量實踐案例。
Picocli 的核心功能:
- 命令行參數(shù)解析:支持選項、位置參數(shù)、標志。
- 多級子命令:輕松實現(xiàn)復雜的命令行結(jié)構(gòu)。
- 自動生成幫助信息:無需手動編寫幫助文檔。
- 多線程支持:通過 @CommandLine.Command 中的 ExecutionStrategy 輕松實現(xiàn)并發(fā)任務。
3、實踐樣例
以下是一個實踐案例,展示如何使用 Picocli 開發(fā) CLI 工具,構(gòu)建一個簡單的文件操作命令行工具:
3.1 添加依賴
在 Maven 項目中添加 Picocli 的依賴:
<dependency> <groupId>info.picocli</groupId> <artifactId>picocli</artifactId> <version>4.7.4</version> </dependency>
3.2 構(gòu)建一個基本命令
下面創(chuàng)建一個簡單的命令,支持對文件進行讀取和統(tǒng)計操作。
import picocli.CommandLine; import picocli.CommandLine.Command; import picocli.CommandLine.Option; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.concurrent.Callable; @Command(name = "filetool", version = "FileTool 1.0", description = "一個用于文件操作的命令行工具", mixinStandardHelpOptions = true) public class FileTool implements Callable<Integer> { @Option(names = {"-p", "--path"}, description = "文件路徑", required = true) private String filePath; @Option(names = {"-c", "--count-lines"}, description = "統(tǒng)計文件行數(shù)") private boolean countLines; @Option(names = {"-r", "--read-content"}, description = "讀取并輸出文件內(nèi)容") private boolean readContent; @Override public Integer call() throws Exception { if (countLines) { long lineCount = Files.lines(Paths.get(filePath)).count(); System.out.println("文件行數(shù): " + lineCount); } if (readContent) { String content = new String(Files.readAllBytes(Paths.get(filePath))); System.out.println("文件內(nèi)容:\n" + content); } return 0; } public static void main(String[] args) { int exitCode = new CommandLine(new FileTool()).execute(args); System.exit(exitCode); } }
3.3 使用說明
編譯并運行程序后,可以通過以下方式使用該工具:
查看幫助信息:
java -jar filetool.jar --help
輸出示例:
用法: filetool [-hV] [-c] [-r] -p=<filePath>
一個用于文件操作的命令行工具
-c, --count-lines 統(tǒng)計文件行數(shù)
-h, --help 顯示幫助信息
-p, --path=<filePath> 文件路徑
-r, --read-content 讀取并輸出文件內(nèi)容
-V, --version 打印版本信息
統(tǒng)計文件行數(shù):
java -jar filetool.jar -p sample.txt -c
輸出示例:
文件行數(shù): 10
讀取文件內(nèi)容:
java -jar filetool.jar -p sample.txt -r
輸出示例:
文件內(nèi)容:
Hello World!
This is a test file.
4、高級功能示例
Picocli 支持子命令,可以用于實現(xiàn)復雜的 CLI 工具。以下是一個示例,構(gòu)建一個多功能工具,包含子命令 read 和 count:
@Command(name = "filetool", description = "文件工具", mixinStandardHelpOptions = true, subcommands = { FileReadCommand.class, FileCountCommand.class }) public class FileTool { public static void main(String[] args) { int exitCode = new CommandLine(new FileTool()).execute(args); System.exit(exitCode); } } @Command(name = "read", description = "讀取文件內(nèi)容") class FileReadCommand implements Callable<Integer> { @Option(names = {"-p", "--path"}, description = "文件路徑", required = true) private String filePath; @Override public Integer call() throws Exception { String content = new String(Files.readAllBytes(Paths.get(filePath))); System.out.println("文件內(nèi)容:\n" + content); return 0; } } @Command(name = "count", description = "統(tǒng)計文件行數(shù)") class FileCountCommand implements Callable<Integer> { @Option(names = {"-p", "--path"}, description = "文件路徑", required = true) private String filePath; @Override public Integer call() throws Exception { long lineCount = Files.lines(Paths.get(filePath)).count(); System.out.println("文件行數(shù): " + lineCount); return 0; } }
運行示例:
查看幫助信息:
java -jar filetool.jar --help
使用子命令 read:
java -jar filetool.jar read -p sample.txt
使用子命令 count:
java -jar filetool.jar count -p sample.txt
5、總結(jié)
Picocli 是一個現(xiàn)代化的 Java 命令行工具開發(fā)庫,通過簡單的注解和直觀的 API 提供強大的功能。無論是實現(xiàn)單一命令,還是構(gòu)建多級子命令的復雜工具,Picocli 都能快速滿足需求。
推薦的使用場景:
開發(fā)運維工具:如文件管理工具、數(shù)據(jù)處理工具。
數(shù)據(jù)分析腳本:支持多線程處理。
多子命令的框架型 CLI 工具。
到此這篇關(guān)于Java利用Picocli開發(fā)一個簡化命令行工具的文章就介紹到這了,更多相關(guān)Java Picocli簡化命令行內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java基于logback?MessageConverter實現(xiàn)日志脫敏方案分析
本文介紹了一種日志脫敏方案,即基于logbackMessageConverter和正則匹配的方法,該方法的優(yōu)點是侵入性低,工作量少,只需修改xml配置文件,適用于老項目,感興趣的朋友跟隨小編一起看看吧2024-10-10Spring boot實現(xiàn)一個簡單的ioc(2)
這篇文章主要為大家詳細介紹了Spring boot實現(xiàn)一個簡單ioc的第二篇,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-04-04maven打生產(chǎn)環(huán)境可執(zhí)行包的實現(xiàn)
本文主要介紹了maven打生產(chǎn)環(huán)境可執(zhí)行包的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2025-01-01解決springSecurity 使用默認登陸界面登錄后無法跳轉(zhuǎn)問題
這篇文章主要介紹了解決springSecurity 使用默認登陸界面登錄后無法跳轉(zhuǎn)問題,項目環(huán)境springboot下使用springSecurity 版本2.7.8,本文通過實例代碼給大家介紹的非常詳細,需要的朋友參考下吧2023-12-12