参考  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命令行参数解析的更多相关文章

  1. JAVA 命令行参数解析,org.apache.commons.cli的使用

    maven依赖引入 <dependency> <groupId>commons-cli</groupId> <artifactId>commons-cl ...

  2. Python 中命令行参数解析工具 docopt 安装和应用

    什么是 docopt? 1.docopt 是一种 Python 编写的命令行执行脚本的交互语言. 它是一种语言! 它是一种语言! 它是一种语言! 2.使用这种语言可以在自己的脚本中,添加一些规则限制. ...

  3. python命令行参数解析模块argparse和docopt

    http://blog.csdn.net/pipisorry/article/details/53046471 还有其他两个模块实现这一功能,getopt(等同于C语言中的getopt())和弃用的o ...

  4. gflags命令行参数解析

    gflags库是google开源的命令行参数解析工具. 安装 官方没有提供二进制库,但是Debian/Ubuntu平台本身提供了二进制库,可以直接git clone https://github.co ...

  5. [Go] 命令行参数解析包(flag 包)使用详解

    Go 的 flag 包可以解析命令行的参数. 一.命令行语法 命令行语法主要有以下几种形式: cmd -flag       // 只支持bool类型 cmd -flag=xxx cmd -flag ...

  6. $命令行参数解析模块argparse的用法

    argparse是python内置的命令行参数解析模块,可以用来为程序配置功能丰富的命令行参数,方便使用,本文总结一下其基本用法. 测试脚本 把以下脚本存在argtest.py文件中: # codin ...

  7. Google开源命令行参数解析库gflags

    Google开源命令行参数解析库gflags http://blog.csdn.net/lming_08/article/details/25072899 CMDLINE的解析 http://blog ...

  8. PHP 命令行参数解析工具类

    <?php/** * 命令行参数解析工具类 * @author guolinchao * @email luoyecb@163.com */class CommandLine{ // store ...

  9. golang-flag - 命令行参数解析

    flag - 命令行参数解析 在写命令行程序(工具.server)时,对命令参数进行解析是常见的需求.各种语言一般都会提供解析命令行参数的方法或库,以方便程序员使用.如果命令行参数纯粹自己写代码解析, ...

随机推荐

  1. Linux中rz和sz命令用法详解

    http://www.111cn.net/sys/linux/67810.htm 在linux中rz 和 sz 命令允许开发板与主机通过串口进行传递文件了,下面我们就来简单的介绍一下rz 和 sz 命 ...

  2. mysqldump定时备份数据库

    mysql服务器ip:192.168.0.10 备份服务器ip:192.168.0.11 数据库名称:db_product 思路:在mysql服务器A上创建一个用户分配权限专门用于数据库备份,A服务器 ...

  3. JVM核心知识体系(转http://www.cnblogs.com/wxdlut/p/10670871.html)

    1.问题 1.如何理解类文件结构布局? 2.如何应用类加载器的工作原理进行将应用辗转腾挪? 3.热部署与热替换有何区别,如何隔离类冲突? 4.JVM如何管理内存,有何内存淘汰机制? 5.JVM执行引擎 ...

  4. EgretPaper学习笔记一 (安装环境,新建项目)

    参考: 官方Egret3D教程 白鹭引擎5.3.0正式发布,支持3D游戏开发 一 下载安装EgretPaper Egret3D 编辑器的开发代号为“Paper”,于2018年9月份开始小规模内测,并在 ...

  5. C# Aspose.Cells导出xlsx格式Excel,打开文件报“Excel 已完成文件级验证和修复。此工作簿的某些部分可能已被修复或丢弃”

    报错信息: 最近打开下载的 Excel,会报如下错误.(xls 格式不受影响) 解决方案: 下载代码(红色为新添代码) public void download() { string fileName ...

  6. Hibernate HQL一对多 在一方的查询

    首先说一句:是版本的问题! 在多对多或者多对一,从一中查找多中查询某些语句时容易出现 我写的hql为: from Department as d where d.employees.name='Tom ...

  7. nw.js---创建一个hello word的方法

    一.如果用nw.js 来开发桌面应用 首先到Nw.js中文网下载软件: https://nwjs.org.cn/download.html 下载下来进行解压就可以了,绿色的免安装的,整个目录结果是这样 ...

  8. 在elementUI中使用 el-autocomplete 实现远程搜索的下拉框

    参考;https://blog.csdn.net/qq_37746973/article/details/78402812?utm_source=blogxgwz1 https://blog.csdn ...

  9. axis2与cxf区别

    1.CXF支持 WS-Addressing,WS-Policy, WS-RM, WS-Security和WS-I Basic Profile.Axis2不支持WS-Policy,但是承诺在下面的版本支 ...

  10. hive 限制本地内存使用量

    hive.mapred.local.mem Mapper/Reducer 在本地模式的最大内存量,以字节为单位,0为不限制. hive-default.xml <property> < ...