最近在用Java,写了一个动态从xml中加载命令行提示,支持多语言的模块
package com.common;
import org.apache.commons.cli.*;
import org.dom4j.io.SAXReader;
import java.io.*;
import java.util.Iterator;
import java.util.List;
/**
* Created by LonelyRain on 16/7/12.
*/
public class ParseCommand {
static Options optionList;
public static CommandLine ParseOption(String[] args) {
Options options = new Options();
CommandLine UserCommand = null;
try {
//根据args对比XML内的命令
UserCommand = ProgramOptionList.loading(options, args);
} catch (Exception error) {
System.err.println("Error:\r\n\t==>" + error.getMessage());
System.exit(0);
}
return UserCommand;
}
public static void help() throws Exception {
String ProgramName = "";
//创建解析器
InputStream inputFile = Thread.currentThread().getContextClassLoader().getResourceAsStream("xml/CommandList.xml");
SAXReader reader = new SAXReader();
org.dom4j.Document document = reader.read(inputFile);
//获取根
org.dom4j.Element root = document.getRootElement();
ProgramName = root.attributeValue("name");
HelpFormatter hf = new HelpFormatter();
hf.printHelp(ProgramName, optionList);
}
public static class ProgramOptionList {
static CommandLine CommandList;
public static CommandLine loading(Options defaultOptions, String[] args) throws Exception {
CommandLineParser CommandParser = new DefaultParser();
__loadConfigFile(defaultOptions, "en");
CommandLine UserCommand = CommandParser.parse(defaultOptions, args);
optionList = defaultOptions;
if (UserCommand.hasOption("l")) {
Options otherOptions = new Options();
__loadConfigFile(otherOptions, UserCommand.getOptionValue("l"));
optionList = otherOptions;
}
CommandList = UserCommand;
return CommandList;
}
private static void __loadConfigFile(Options options, String language) throws Exception {
//创建解析器
SAXReader reader = new SAXReader();
//读取文档
File inputFile = new File("/Users/SilverRat/开发/java/DomainSearcher/src/xml/CommandList.xml");
org.dom4j.Document document = reader.read(inputFile);
//获取根
org.dom4j.Element root = document.getRootElement();
//获取子节点
List<org.dom4j.Element> list = root.elements();
for (org.dom4j.Element e : list) {
String shortCMD = "";
String longCMD = "";
String describetion = "";
boolean with_args = false;
for (Iterator s = e.elementIterator(); s.hasNext(); ) {
org.dom4j.Element commandInfo = (org.dom4j.Element) s.next();
switch (commandInfo.getName()) {
case "short":
shortCMD = commandInfo.getStringValue();
break;
case "long":
longCMD = commandInfo.getStringValue();
break;
case "describetion":
if (commandInfo.attributeValue("lang").equals(language)) {
describetion = commandInfo.getStringValue();
}
break;
case "with_args":
with_args = Boolean.parseBoolean(commandInfo.getStringValue());
break;
}
}
options.addOption(shortCMD, longCMD, with_args, describetion);
}
}
}
}
上面的是解析模块
下面的是配置的xml文件
<?xml version="1.0" encoding="utf-8" ?>
<command_list name="DomainSearcher">
<command id="0">
<short>h</short>
<long>help</long>
<with_args>false</with_args>
<describetion lang="en">Help Information</describetion>
<describetion lang="zh">帮助信息</describetion>
</command>
<command id="1">
<short>t</short>
<long>target</long>
<with_args>true</with_args>
<describetion lang="en">Your Target Host/IP</describetion>
<describetion lang="zh">目标域名/IP</describetion>
</command>
<command id="2">
<short>d</short>
<long>dict</long>
<with_args>true</with_args>
<describetion lang="en">Your Dictionary</describetion>
<describetion lang="zh">加载字典路径</describetion>
</command>
<command id="3">
<short>l</short>
<long>language</long>
<with_args>true</with_args>
<describetion lang="en">选择程序语言(en / zh)</describetion>
<describetion lang="zh">Choose Display Language(en / zh)</describetion>
</command>
</command_list>
下面是运用例子
import ParseCommand.*;
public class DomainSearcher {
public static void main(String[] args) {
ParseCommand parsecommand = new ParseCommand();
parsecommand.Parse(args);
}
行效果: