今天又看了下Hangout的源碼,一般來說一個開源項目有好幾種啟動方式——比如可以從命令行啟動,也可以從web端啟動。今天就看看如何設計命令行啟動...
Apache Commons CLI是開源的命令行解析工具,它可以幫助開發者快速構建啟動命令,並且幫助你組織命令的參數、以及輸出列表等。
CLI分為三個過程:
Options options = new Options();
Option opt = new Option("h", "help", false, "Print help");
opt.setRequired(false);
options.addOption(opt);
opt = new Option("c", "configFile", true, "Name server config properties file");
opt.setRequired(false);
options.addOption(opt);
opt = new Option("p", "printConfigItem", false, "Print all config item");
opt.setRequired(false);
options.addOption(opt);
其中Option的參數:
通過解析器解析參數
CommandLine commandLine = null;
CommandLineParser parser = new PosixParser();
try {
commandLine = parser.parse(options, args);
}catch(Exception e){
//TODO xxx
}
根據commandLine查詢參數,提供服務
HelpFormatter hf = new HelpFormatter();
hf.setWidth(110);
if (commandLine.hasOption('h')) {
// 打印使用幫助
hf.printHelp("testApp", options, true);
}
package hangout.study;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;
public class CLITest {
public static void main(String[] args) {
String[] arg = { "-h", "-c", "config.xml" };
testOptions(arg);
}
public static void testOptions(String[] args) {
Options options = new Options();
Option opt = new Option("h", "help", false, "Print help");
opt.setRequired(false);
options.addOption(opt);
opt = new Option("c", "configFile", true, "Name server config properties file");
opt.setRequired(false);
options.addOption(opt);
opt = new Option("p", "printConfigItem", false, "Print all config item");
opt.setRequired(false);
options.addOption(opt);
HelpFormatter hf = new HelpFormatter();
hf.setWidth(110);
CommandLine commandLine = null;
CommandLineParser parser = new PosixParser();
try {
commandLine = parser.parse(options, args);
if (commandLine.hasOption('h')) {
// 打印使用幫助
hf.printHelp("testApp", options, true);
}
// 打印opts的名稱和值
System.out.println("--------------------------------------");
Option[] opts = commandLine.getOptions();
if (opts != null) {
for (Option opt1 : opts) {
String name = opt1.getLongOpt();
String value = commandLine.getOptionValue(name);
System.out.println(name + "=>" + value);
}
}
}
catch (ParseException e) {
hf.printHelp("testApp", options, true);
}
}
}
usage: testApp [-c <arg>] [-h] [-p]
-c,--configFile <arg> Name server config properties file
-h,--help Print help
-p,--printConfigItem Print all config item
--------------------------------------
help=>null
configFile=>config.xml
package hangout.study;
import org.apache.commons.cli.BasicParser;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
public class HangoutMainTest {
/**
* 解析Hangout參數
*
* @param args
* @return
* @throws ParseException
*/
private static CommandLine parseArg(String[] args) throws ParseException {
//定義階段
Options options = new Options();
options.addOption("h", false, "usage help");
options.addOption("help", false, "usage help");
options.addOption("f", true, "configuration file");
options.addOption("l", true, "log file");
options.addOption("w", true, "filter worker number");
options.addOption("v", false, "print info log");
options.addOption("vv", false, "print debug log");
options.addOption("vvvv", false, "print trace log");
//解析階段
CommandLineParser paraer = new BasicParser();
CommandLine cmdLine = paraer.parse(options, args);
//詢問階段
if (cmdLine.hasOption("help") || cmdLine.hasOption("h")) {
/*usage(); //這裡作者自定義了幫助信息,其實可以使用helpFormat直接輸出的*/
HelpFormatter hf = new HelpFormatter();
hf.setWidth(110);
hf.printHelp("testApp", options, true);
System.exit(-1);
}
// TODO need process invalid arguments
if (!cmdLine.hasOption("f")) {
throw new IllegalArgumentException("Required -f argument to specify config file");
}
return cmdLine;
}
public static void main(String[] args) throws Exception {
String[] arg = {"-h","xx.file"};//輸入參數
CommandLine cmdLine = parseArg(arg);//解析參數
System.out.println(cmdLine.getOptionValue("f"));//拿到重要參數
//TODO
}
}
1 Apache Commons CLI 下載地址
2 Apache Commons CLI 官方指南
3 IBM 開發者文檔 http://www.linuxidc.com/Linux/2016-06/132607.htm
4 Commons CLI 使用詳解 http://www.linuxidc.com/Linux/2016-06/132591.htm