熟悉Linux命令的都知道几乎所有程序都会提供一些命令行选项。而命令行选项有两种风格:以“-”开头的单个字符的POSIX风格;以“--”后接选项关键字的GNU风格。
假定我们的程序需要以下选项:
引用
Options:
-t,--text use given information(String)
-b display current time(boolean)
-s,--size use given size(Integer)
-f,--file use given file(File)
-D <property=value> use value for given property(property=value)
(1)
Apache的Commons-CLI
版本:commons-cli-1.2.jar
支持三种CLI选项解析:
- BasicParser:直接返回参数数组值
- PosixParser:解析参数及值(-s10)
- GnuParser:解析参数及值(--size=10)
对于动态参数:
-Dkey=value
需要代码设置参数,返回类型需要转换。
args = new String[]{"-t", "rensanning", "-f", "c:/aa.txt", "-b", "-s10", "-Dkey1=value1", "-Dkey2=value2" };
try {
// create Options object
Options options = new Options();
options.addOption(new Option("t", "text", true, "use given information(String)"));
options.addOption(new Option("b", false, "display current time(boolean)"));
options.addOption(new Option("s", "size", true, "use given size(Integer)"));
options.addOption(new Option("f", "file", true, "use given file(File)"));
@SuppressWarnings("static-access")
Option property = OptionBuilder.withArgName("property=value")
.hasArgs(2)
.withValueSeparator()
.withDescription("use value for given property(property=value)")
.create("D");
property.setRequired(true);
options.addOption(property);
// print usage
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp( "AntOptsCommonsCLI", options );
System.out.println();
// create the command line parser
CommandLineParser parser = new PosixParser();
CommandLine cmd = parser.parse(options, args);
// check the options have been set correctly
System.out.println(cmd.getOptionValue("t"));
System.out.println(cmd.getOptionValue("f"));
if (cmd.hasOption("b")) {
System.out.println(new Date());
}
System.out.println(cmd.getOptionValue( "s" ));
System.out.println(cmd.getOptionProperties("D").getProperty("key1") );
System.out.println(cmd.getOptionProperties("D").getProperty("key2") );
} catch (Exception ex) {
System.out.println( "Unexpected exception:" + ex.getMessage() );
}
(2)
Args4J
版本:args4j-2.0.29.jar
基于注解。
args = new String[] {"-t", "rensanning", "-f", "c:/aa.txt", "-b", "-s", "10", "-D", "key1=value1" , "-D", "key2=value2"};
try {
Args4jOptions options = new Args4jOptions();
CmdLineParser parser = new CmdLineParser(options);
// print usage
parser.printUsage(System.out);
System.out.println();
parser.parseArgument(args);
// check the options have been set correctly
System.out.println(options.getText());
System.out.println(options.getFile().getName());
if(options.isBol()) {
System.out.println(new Date());
}
System.out.println(options.getSize());
System.out.println(options.getProperties().get("key1"));
System.out.println(options.getProperties().get("key2"));
} catch (Exception ex) {
System.out.println("Unexpected exception:" + ex.getMessage());
}
@Option(name = "-t", aliases = "-text", usage = "use given information(String)")
private String text;
@Option(name = "-b", usage = "display current time(boolean)")
private boolean bol = false;
@Option(name = "-s", aliases = "-size", usage = "use given size(Integer)")
private int size = 0;
@Option(name = "-f", aliases = { "-file" }, metaVar = "<file>", usage = "use given file(File)")
private File file;
private Map<String, String> properties = new HashMap<String, String>();
@Option(name = "-D", metaVar = "<property>=<value>", usage = "use value for given property(property=value)")
public void setProperty(final String property) {
String[] arr = property.split("=");
properties.put(arr[0], arr[1]);
}
(3)
JCommander
版本:jcommander-1.45.jar
基于注解、TestNG作者开发。
args = new String[] {"-t", "rensanning", "-f", "c:/aa.txt", "-b", "-s", "10", "-D", "key1=value1" , "-D", "key2=value2"};
try {
JCmdrOptions options = new JCmdrOptions();
JCommander jcmdr = new JCommander(options, args);
// print usage
jcmdr.setProgramName("AntOptsCommonsCLI");
jcmdr.usage();
// check the options have been set correctly
System.out.println(options.getText());
System.out.println(options.getFile().getName());
if(options.isBol()) {
System.out.println(new Date());
}
System.out.println(options.getSize());
System.out.println(options.getDynamicParams().get("key1"));
System.out.println(options.getDynamicParams().get("key2"));
} catch (Exception ex) {
System.out.println("Unexpected exception:" + ex.getMessage());
}
@Parameter(names = { "-t", "-text" }, description = "use given information(String)")
private String text;
@Parameter(names = { "-b" }, description = "display current time(boolean)")
private boolean bol = false;
@Parameter(names = { "-s", "-size" }, description = "use given size(Integer)")
private int size = 0;
@Parameter(names = { "-f", "-file" }, description = "use given file(File)")
private File file;
@DynamicParameter(names = "-D", description = "use value for given property(property=value)")
public Map<String, String> dynamicParams = new HashMap<String, String>();
分享到:
相关推荐
spring-framework & commons-logging spring-framework & commons-logging spring-framework & commons-logging spring-framework & commons-logging spring-framework & commons-logging spring-framework & ...
标签:cli、commons、jar包、java、API文档、中英对照版; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请...
标签:commons、cli、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。
最新的commons-cli,解析命令行参数,程序员必备,不要问我是谁,我叫雷锋,积分太多了,改了一下积分
标签:cli、commons、jar包、java、中文文档; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。
ALevin环境配置所需的jar包——commons-cli-1,5,0 适合人群: 对虚拟网络嵌入算法感兴趣的人 能学到什么: 可以帮你快速的将ALevin基础运行环境配置好,为你节省时间进行进一步的学习 阅读建议: 由于ALevin的配置...
commons-cli-1.4.jar,commons-configuration-1.0.jar,commons-lang-2.3.jar,commons-logging-1.1.1.jar
commons-cli-1.2.jar
这是微信企业账户转账必用的一个JAR包,用户企业账户号给指定openid转账,通过微信直接转账到openid用户的零钱包中
标签:cli、commons、jar包、java、API文档、中文版; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心...
标签:commons、cli、中英对照文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用...
commons-cli包,进行命令行参数解析的工具类,java工具类。可以直接引用到项目中,简单又方便。
commons-cli的jar包 Commons CLI 是一个用来处理命令行参数的 Java 工具包。
commons-cli-1.0.jar Apache Commons CLI library为用户提供了一个解释命令行的API.它在解释命令行时主要有三个状态,即:定义、解释和询问交互
commons-cli-1.5.0.jar
java连接池;java连接池jar;commons-pool-1.2.jar;commons-pool-1.3.jar+commons-pool.jar;java连接池jar包java连接池;java连接池jar;commons-pool-1.2.jar;commons-pool-1.3.jar+commons-pool.jar;java连接池jar包...
两个jar包,commons-beanutils-1.9.1.jar 和 commons-logging-1.1.3.jar
commons-cli命令模式基本架构-自用
jar包大小:342KB log4j-1.2.6.jar jar包大小:135KB commons-validator-1.3.1.jar jar包大小:93KB commons-transaction-1.2.jar jar包大小:141KB commons-scxml-0.6.jar jar包大小:254KB commons-primitives-1.0.jar ...