当前位置: 首页 > news >正文

JCommander + AutoService打造带子命令的Java命令行应用

文章目录

    • 需求
    • Java命令行工具库
    • 依赖库
    • 定义各个子命令
    • 主类CLI
    • 测试一下
    • 参考文档

需求

最近想将自己的一个Java应用包装成命令行工具,看了几个库,最后选取了JCommander,结合AutoService库,实现了带子命令的工具,方便扩展新的子命令。

子命令放在同一包下,实现相同的接口,通过java.util.ServiceLocator加载。

Java命令行工具库

常用的几个库为:

  • JCommander
    项目地址: https://github.com/cbeust/jcommander
    Star: 1010 Fork: 227
    文档地址: http://jcommander.org/

  • picocli
    地址: https://github.com/remkop/picocli
    Star: 336, Fork: 32
    示例:https://github.com/kakawait/picocli-spring-boot-starter

  • Commons CLI
    地址: https://commons.apache.org/proper/commons-cli/
    地址: 来自apache common的开源项目
    更新: 最后一次更新是1.5-SNAPSHOT,是在2017年6月8日

  • Args4j
    项目地址: https://0github.com/kohsuke/args4j
    Star: 570 Fork: 151
    文档地址: http://args4j.kohsuke.org/sample.html
    活跃程度: 最后一次更新为2年之前

依赖库

        <!-- https://mvnrepository.com/artifact/com.google.auto.service/auto-service --><dependency><groupId>com.google.auto.service</groupId><artifactId>auto-service</artifactId><version>1.1.1</version></dependency><!-- https://mvnrepository.com/artifact/com.beust/jcommander --><dependency><groupId>com.beust</groupId><artifactId>jcommander</artifactId><version>1.82</version></dependency>

定义各个子命令

Create子命令:

@AutoService(Command.class)
@Parameters(commandNames = { CREATE_CMD },commandDescription = "create a new ebook."
)
@Getter
public class CreateCommand implements Command{@Parameter(names = { "--indexUrl", "-u" })public String indexUrl;@Overridepublic void execute() throws CommandException {System.out.println("create a new book from " + indexUrl);}
}

Fetch子命令:

@AutoService(Command.class)
@Parameters(commandNames = {FETCH_CMD},commandDescription = "fetch some articles from website."
)
@Getter
public class FetchCommand implements Command {@Parameter(names = {"--indexUrl", "-u"})public String indexUrl;@Overridepublic void execute() throws CommandException {System.out.println("fetch articles from " + indexUrl);}
}

都实现了接口Command:

public interface Command {default Collection<Command> commands() {return null;}void execute() throws CommandException;
}

注意,子命令支持嵌套的子命令。当然一般用不到。

主类CLI

public class CLI {static final String CREATE_CMD = "create";static final String FETCH_CMD = "fetch";@Parameter(names = { "-h", "--help" }, help = true)private boolean help;public void exec(String[] args) {final JCommander.Builder builder = JCommander.newBuilder().addObject(new CLI());       final JCommander jCommander = builder.build();ServiceLoader.load(Command.class).forEach(command -> CLI.registerCommand(jCommander, command));JCommander leafCommander = jCommander;try {jCommander.parse(args);final String rootVerb = jCommander.getParsedCommand();final JCommander rootCommander = jCommander.getCommands().get(rootVerb);if (rootCommander == null) {jCommander.usage();System.exit(1);}leafCommander = rootCommander;do {final String subVerb = leafCommander.getParsedCommand();final JCommander subCommander = leafCommander.getCommands().get(subVerb);if (subCommander != null)leafCommander = subCommander;elsebreak;} while (true);final Command command = (Command) leafCommander.getObjects().get(0);command.execute();} catch (final CommandException e) {System.err.printf("%1$s: %2$s. See '%1$s --help'.%n", leafCommander.getProgramName(), e.getMessage());System.exit(e.getStatus());} catch (final Exception e) {System.err.printf("%1$s: %2$s. See '%1$s --help'.%n", leafCommander.getProgramName(), e.getMessage());System.exit(1);}}private static final void registerCommand(final JCommander jCommander, final Command command) {jCommander.addCommand(command);final Parameters commandParameters = command.getClass().getAnnotation(Parameters.class);if (commandParameters == null || commandParameters.commandNames().length == 0)return;final JCommander subCommander = jCommander.getCommands().get(commandParameters.commandNames()[0]);final Collection<Command> subCommands = command.commands();if (subCommands != null)subCommands.forEach(subCommand -> CLI.registerCommand(subCommander, subCommand));}public static void main(final String[] args) {CLI cli = new CLI();cli.exec(args);}
}

原理很简单:ServiceLocator加载了所有Command的实现类,然后根据子命令调用相应的Command类。AutoService的好处就是不必自己去创建META-INF/services下的相关文件。

测试一下

    @Testpublic void testCreateCommand() {CLI cli = new CLI();String[] argv = { "create",  "-u", "http://www.sina.com.cn"};cli.exec(argv);}@Testpublic void testFetchCommand() {CLI cli = new CLI();String[] argv = { "fetch",  "-u", "http://www.csdn.cn"};cli.exec(argv);}

参考文档

  • https://gist.github.com/mkarg/9d9ca23e6da32b47c7fadaf10ae16ba6
  • https://pedrorijo.com/blog/java-service-loader/
http://www.lryc.cn/news/109582.html

相关文章:

  • pycharm运行pytest无法实时输出信息
  • Mac 卸载 IntelliJ IDEA 方法
  • 数据安全能力框架模型-详细解读(三)
  • vscode启动leiningen项目
  • Qt事件的传递顺序
  • 基于facenet+faiss开发构建人脸识别系统
  • 数据分析的心脏:获取数据的好工具
  • 【万字长文】SpringBoot整合Atomikos实现多数据源分布式事务(提供Gitee源码)
  • js中什么是宏任务、微任务?宏任务、微任务有哪些?又是怎么执行的?
  • Word中如何断开表格中线段
  • 大数据指标体系-笔记
  • Arthas协助MQ消费性能优化
  • 【Linux】【docker】安装sonarQube免费社区版9.9
  • C/C++实现librosa音频处理库melspectrogram和mfcc
  • 浪潮服务器硬盘指示灯显示黄色的服务器数据恢复案例
  • 宋浩概率论笔记(三)随机向量/二维随机变量
  • 附件展示 点击下载
  • HotSpot虚拟机之Class文件及字节码指令
  • 关于盐雾试验
  • windows美化任务栏,不使用软件
  • 24考研数据结构-并查集
  • Redis 和 Mysql 如何保证数据一致性
  • WSL1升级为WSL2
  • 力扣 1049. 最后一块石头的重量 II
  • 【广州华锐视点】葡萄种植VR虚拟仿真实训平台
  • PBR材质理解整理
  • 从c++的角度来看ffmpeg 的架构
  • Ubuntu安装JDK与IntelliJ IDEA
  • 【雕爷学编程】Arduino动手做(182)---DRV8833双路电机驱动模块2
  • 一个完整的http请求响应过程