RocketMQ源码分析准备知识之CommonsCli
RocketMQ4.7.0版本使用了
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.2</version>
</dependency>
即采用了apache的命令行工具包
比如:org.apache.rocketmq.namesrv.NamesrvStartup类中
入门例子
import org.apache.commons.cli.*;
/**
* 以前写过一些命令行程序,在需要带参数的时候都是自己来判断args,导致程序光解析* args都占了好大一堆,而且解析代码也不美观。
* 偶然间发现了apache公共库中的cli库,在这里分享给大家。
* commons-cli中把解释参数分为三种状态,分别是定义、解释和询问交互。
*/
public class CLI001 {
public static void main(String[] args) throws ParseException {
//定义
Options options = new Options();
options.addOption("h",false,"list help");//false代表不强制有
options.addOption("t",true,"set time on system"); //true 代表
//解析
CommandLineParser parser = new PosixParser();
CommandLine cmd = parser.parse(options,args);
//查询交互
// 在引用此jar包,你的程序应当写在这里(根据模块不同编写不同逻辑),
// 从这里启动即根据参数做不同逻辑
if (cmd.hasOption("h")){
String formatstr = "CLI cli test";
HelpFormatter hf = new HelpFormatter();
hf.printHelp(formatstr, "", options, "");
return;
}
if (cmd.hasOption("t")){
System.out.printf("system time has setted %s \n",cmd.getOptionValue("t"));
return;
}
System.out.println("error");
}
}
测试类:
public class Cl002 {
public static void main(String[] args) throws ParseException {
Long now = System.currentTimeMillis();
// String argss[]={"-t " + now};
String argss[]={"-h"};
CLI001.main(argss);
}
}
RocketMQ的例子
import org.apache.commons.cli.*;
import java.util.Properties;
public class ServerUtil {
public static Options buildCommandlineOptions(final Options options) {
Option opt = new Option("h", "help", false, "Print help");
opt.setRequired(false);
options.addOption(opt);
opt =
new Option("n", "namesrvAddr", true,
"Name server address list, eg: 192.168.0.1:9876;192.168.0.2:9876");
opt.setRequired(false);
options.addOption(opt);
return options;
}
public static CommandLine parseCmdLine(final String appName, String[] args, Options options,
CommandLineParser parser) {
HelpFormatter hf = new HelpFormatter();
hf.setWidth(110);
CommandLine commandLine = null;
try {
commandLine = parser.parse(options, args);
if (commandLine.hasOption('h')) {
hf.printHelp(appName, options, true);
System.exit(0);
}
} catch (ParseException e) {
hf.printHelp(appName, options, true);
System.exit(1);
}
return commandLine;
}
public static void printCommandLineHelp(final String appName, final Options options) {
HelpFormatter hf = new HelpFormatter();
hf.setWidth(110);
hf.printHelp(appName, options, true);
}
public static Properties commandLine2Properties(final CommandLine commandLine) {
Properties properties = new Properties();
Option[] opts = commandLine.getOptions();
if (opts != null) {
for (Option opt : opts) {
String name = opt.getLongOpt();
String value = commandLine.getOptionValue(name);
if (value != null) {
properties.setProperty(name, value);
}
}
}
return properties;
}
public static void main(String[] args) {
Options options = buildCommandlineOptions(new Options());
printCommandLineHelp("allen's appName",options);
// String argss[]={"h"};
String argss[]={"-n 152.168.1.88:9876"};
CommandLineParser parser = new PosixParser();
// CommandLine cmd = parser.parse(options,args);
CommandLine commandLine = parseCmdLine("ALLEN'S App", argss, options, parser);
System.out.println(commandLine);
// {namesrvAddr= 152.168.1.88:9876}
System.out.println(commandLine2Properties(commandLine));
}
}