Java命令行参数解析
参考 http://blog.csdn.net/mldxs/article/details/36204079
http://rensanning.iteye.com/blog/2161201
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.BasicParser;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.CommandLine; public static void main(String[] args) throws Exception {
// Create a Parser
CommandLineParser parser = new BasicParser( );
Options options = new Options( );
options.addOption("h", "help", false, "Print this usage information");
options.addOption("v", "verbose", false, "Print out VERBOSE information" );
options.addOption("f", "file", true, "File to save program output to");
// Parse the program arguments
CommandLine commandLine = parser.parse( options, args );
// Set the appropriate variables based on supplied options
boolean verbose = false;
String file = ""; if( commandLine.hasOption('h') ) {
System.out.println( "Help Message")
System.exit(0);
}
if( commandLine.hasOption('v') ) {
verbose = true;
}
if( commandLine.hasOption('f') ) {
file = commandLine.getOptionValue('f');
}
}
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.2</version>
</dependency> 熟悉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作者开发。
Java命令行参数解析的更多相关文章
- JAVA 命令行参数解析,org.apache.commons.cli的使用
maven依赖引入 <dependency> <groupId>commons-cli</groupId> <artifactId>commons-cl ...
- Python 中命令行参数解析工具 docopt 安装和应用
什么是 docopt? 1.docopt 是一种 Python 编写的命令行执行脚本的交互语言. 它是一种语言! 它是一种语言! 它是一种语言! 2.使用这种语言可以在自己的脚本中,添加一些规则限制. ...
- python命令行参数解析模块argparse和docopt
http://blog.csdn.net/pipisorry/article/details/53046471 还有其他两个模块实现这一功能,getopt(等同于C语言中的getopt())和弃用的o ...
- gflags命令行参数解析
gflags库是google开源的命令行参数解析工具. 安装 官方没有提供二进制库,但是Debian/Ubuntu平台本身提供了二进制库,可以直接git clone https://github.co ...
- [Go] 命令行参数解析包(flag 包)使用详解
Go 的 flag 包可以解析命令行的参数. 一.命令行语法 命令行语法主要有以下几种形式: cmd -flag // 只支持bool类型 cmd -flag=xxx cmd -flag ...
- $命令行参数解析模块argparse的用法
argparse是python内置的命令行参数解析模块,可以用来为程序配置功能丰富的命令行参数,方便使用,本文总结一下其基本用法. 测试脚本 把以下脚本存在argtest.py文件中: # codin ...
- Google开源命令行参数解析库gflags
Google开源命令行参数解析库gflags http://blog.csdn.net/lming_08/article/details/25072899 CMDLINE的解析 http://blog ...
- PHP 命令行参数解析工具类
<?php/** * 命令行参数解析工具类 * @author guolinchao * @email luoyecb@163.com */class CommandLine{ // store ...
- golang-flag - 命令行参数解析
flag - 命令行参数解析 在写命令行程序(工具.server)时,对命令参数进行解析是常见的需求.各种语言一般都会提供解析命令行参数的方法或库,以方便程序员使用.如果命令行参数纯粹自己写代码解析, ...
随机推荐
- 【资料下载区】【iCore系列及其它模块相关文档】更新日期2017/07/24
iCore系列双核心板原理图下载区 iCore双核心板原理图下载(注释版)iCore1s双核心板原理图下载iCore2双核心板原理图下载iCore3双核心板原理图下载iCore4双核心板原理图下载 i ...
- 【GMT43智能液晶模块】基于HAL库的SDRAM和LCD驱动例程(MDK工程&CubeMX工程)
说明: 1.该工程基于HAL库实现动态存储器SDRAM驱动以及液晶控制器LCD驱动. 2.工程通过STM32CubeMX(Version 4.22.0)配置生成,可直接打开进行配置. 3.KEIL M ...
- 【iCore1S 双核心板_ARM】例程十六:USB_MSC实验——虚拟U盘
实验步骤: 1.将SD卡插在SD卡槽中. 2.将跳线冒跳至USB_Device,将USB_Device通过Micor USB线与USB主机(电脑)相连. 3.烧写程序,我的电脑中将出现一个磁盘. 实验 ...
- Tomcat中的backlog参数
在linux 2.2以前,backlog大小包括了半连接状态和全连接状态两种队列大小.linux 2.2以后,分离为两个backlog来分别限制半连接SYN_RCVD状态的未完成连接队列大小跟全连接E ...
- WebSphere MQ中的CCSID
CCSID是一个字符集的标识.作为unicode标准通过定义一个字符集内每个字符要对应那个数字值的方式定义了一个字符集.这说明CCSID就是一个定义字符集顺序的标识数码罢了.IBM的字符标识架构在文档 ...
- SpringBoot系统列 2 - 配置文件,多环境配置(dev,qa,online)
实现项目的多环境配置的方法有很多,比如通过在Pom.xml中配置profiles(最常见) 然后在Install项目打War包的时候,根据需求打不同环境的包,如图: 这种配置多环境的方法在SSM框架中 ...
- yii2的数据库读写分离配置
简介 数据库读写分离是在网站遇到性能瓶颈的时候最先考虑优化的步骤,那么yii2是如何做数据库读写分离的呢?本节教程来给大家普及一下yii2的数据库读写分离配置. 两个服务器的数据同步是读写分离的前提条 ...
- HAWQ集成Yarn HA作为资源管理服务
一.第一步当然是配置YARN HA,这在使用ambari管理时很简单,这里不在赘述. 二.建立HAWQ的专用资源队列queue 不要手工编辑scheduler设置,最方便的当然是使用queue man ...
- 利用opencv进行移动物体检测
进行运动物体检测就是将动态的前景从静态的背景中分离出来.将当前画面与假设是静态背景进行比较发现有明显的变化的区域,就可以认为该区域出现移动的物体.在实际情况中由于光照阴影等因素干扰比较大,通过像素直接 ...
- javascript:变量的作用域
window.onload = function (){ // 1) 在if或else代码块中声明的变量,在代码块的外面也可以取到 if(1==1){ var b = 12; }else{ var b ...