今天在看seata源码的时候,看到server的的启动能接收命令行参数,以前也经常用命令行启动的方式,但是今天才真正的去了解这么一个大神器:
JCommander
JCommander是一个用于解析命令行参数的Java框架,支持解析所有基本的数据类型,也支持将命令行解析成用户自定义的类型,只需要写一个转变函数。
首先我们来看下seata的源码:
@Parameter(names = "--help", help = true)
private boolean help;
@Parameter(names = {"--host", "-h"}, description = "The ip to register to registry center.", order = 1)
private String host;
@Parameter(names = {"--port", "-p"}, description = "The port to listen.", order = 2)
private int port = SERVER_DEFAULT_PORT;
@Parameter(names = {"--storeMode", "-m"}, description = "log store mode : file, db", order = 3)
private String storeMode;
@Parameter(names = {"--serverNode", "-n"}, description = "server node id, such as 1, 2, 3. default is 1", order = 4)
private int serverNode = SERVER_DEFAULT_NODE;
@Parameter(names = {"--seataEnv", "-e"}, description = "The name used for multi-configuration isolation.",
order = 5)
private String seataEnv;
这里非常明显的一个注解就是@Parameter,这个就是定义参数的核心内容了,所以如果我们想获取命令行参数就需要用到他咯