Java命令行解析:apache commons-cli
http://commons.apache.org/proper/commons-cli/usage.html
Apache Commons CLI用于解析命令行选项,也可以输出详细的选项说明信息。
Commons CLI 支持多种选项类型:
- POSIX like options (ie.
tar -zxvf foo.tar.gz
) - GNU like long options (ie.
du --human-readable --max-depth=1
) - Java like properties (ie.
java -Djava.awt.headless=true -Djava.net.useSystemProxies=true Foo
) - Short options with value attached (ie.
gcc -O2 foo.c
) - long options with single hyphen (ie.
ant -projecthelp
)
Commons CLI处理过程可以分成三个阶段
- 定义阶段,Options类。
- 解析阶段,使用CommandLineParser类讲String[] args解析为CommandLine
- 查询阶段,调用CommandLine对象的方法,查询和提取特定选项。
命令行选型定义
Options options = new Options();
options.addOption("h", "help", false, "Print this usage information");
options.addOption("v", "verbose", false, "Print out VERBOSE information" );
options.addOption("p", false, "no error if existing, make parent directories as needed.");
options.addOption(OptionBuilder.withArgName("file")
.hasArg()
.withDescription("search for buildfile towards the root of the filesystem and use it")
.create("O"));
options.addOption(OptionBuilder.withLongOpt("block-size")
.withDescription("use SIZE-byte blocks")
.withValueSeparator('=')
.hasArg()
.create() );
解析
CommandLineParser parser = new BasicParser( );
CommandLineParser parser = new PosixParser();
CommandLine commandLine = parser.parse( options, args );
选型查询和提取
if( commandLine.hasOption('f') ) {
file = commandLine.getOptionValue('f');
}
String[] str = commandLine.getArgs();
显示帮助信息
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("xxcmd [-lkjs] PATH", "", options , "");
formatter.printHelp( "ant", options );
formatter.printHelp( "ant", options, true ); //generate a usage statment as well as the help information
Java命令行解析:apache commons-cli的更多相关文章
- Apache Commons CLI命令行启动
今天又看了下Hangout的源码,一般来说一个开源项目有好几种启动方式--比如可以从命令行启动,也可以从web端启动.今天就看看如何设计命令行启动... Apache Commons CLI Apac ...
- Apache Commons CLI官方文档翻译 —— 快速构建命令行启动模式
昨天通过几个小程序以及Hangout源码学习了CLI的基本使用,今天就来尝试翻译一下CLI的官方使用手册. 下面将会通过几个部分简单的介绍CLI在应用中的使用场景. 昨天已经联系过几个基本的命令行参数 ...
- 使用 Apache Commons CLI 解析命令行参数示例
很好的输入参数解析方法 ,转载记录下 转载在: https://www.cnblogs.com/onmyway20xx/p/7346709.html Apache Commons CLI 简介 Apa ...
- Apache Commons CLI 开发命令行工具示例
概念说明Apache Commons CLI 简介 虽然各种人机交互技术飞速发展,但最传统的命令行模式依然被广泛应用于各个领域:从编译代码到系统管理,命令行因其简洁高效而备受宠爱.各种工具和系统都 提 ...
- 使用 Apache Commons CLI 开发命令行工具示例
Apache Commons CLI 简介 Apache Commons CLI 是 Apache 下面的一个解析命令行输入的工具包,该工具包还提供了自动生成输出帮助文档的功能. Apache Com ...
- maven命令行创建web项目报错:java.lang.NoClassDefFoundError: org/apache/commons/lang/StringUtils
早上一上班就想新建一个web项目玩玩,没想到一敲命令创建就失败了,真是出师不利.各种折腾无果,当然我也可以用eclipse直接创建的,就是不甘心被这破问题给耍了.刚刚才发现问题原因,这个结果我也是醉了 ...
- Java命令行参数解析
参考 http://blog.csdn.net/mldxs/article/details/36204079 http://rensanning.iteye.com/blog/2161201 imp ...
- [转] Java 命令行交互-JCommander
[From] https://github.com/Sayi/sayi.github.com/issues/32 我喜欢简单,什么是简单?正如若干字符组成的命令行. 有时候我们用Java开发了一个小工 ...
- Apache Commons CLI 简介
CLI 命令代码实现 命令行程序处理流程相对比较简单,主要流程为设定命令行参数 -> 解析输入参数 -> 使用输入的数据进行逻辑处理CLI 定义阶段 每一条命令行都必须定义一组参数,它们被 ...
随机推荐
- 兼容IE8以下浏览器input表单属性placeholder不能智能提示功能
当前很多表单提示使用了表单属性placeholder,可这属性不兼容IE8以下的浏览器,我自己写了一个兼容处理js // 兼容IE8以下浏览器input不能智能提示功能 if(navigator.ap ...
- javascript基础06
javascript基础06 splice var del_arr = del.splice(0,2); //删除从指定位置deletePos开始的指定数量deleteCount的元素,数组形式返 ...
- linux常用命令-文件搜索命令-find
find [目录] [选项] 文件名或者正则表达式 -name 根据文件名搜索 -iname 搜索文件名的时候忽略大小写 例:find /etc -name init find /etc -i ...
- Maven 入门 (2)—— 创建Maven项目
http://blog.csdn.net/kakashi8841/article/details/17427043 读这篇文章之前请先确保你成功安装了maven,如果你还没安装成功,请先看:Maven ...
- linux c 笔记-4 工程项目阅读推荐
作者:周子涵链接:https://www.zhihu.com/question/27705862/answer/37738315来源:知乎著作权归作者所有,转载请联系作者获得授权. 转自网上不知道什么 ...
- poj 2503(字符串)
http://poj.org/problem?id=2503 题意:就是翻译,给你一个字典,然后再查找单词,找得到的就输出单词,找不到的输出eh,用Map水题一个,但这个题有点意思的就是输入的问题 # ...
- poj1703_Find them, Catch them_并查集
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42451 Accepted: ...
- 【学习笔记】ionic 学习之环境搭建
初学ionic ,后面会把学习的点滴和踩到坑全部记录下来 1.环境 安装node.js 官网地址:https://nodejs.org/en/ 下载安装包安装.自己记住自己的安装路径哦 安装完成后我们 ...
- node06-path
目录:node01-创建服务器 node02-util node03-events node04-buffer node05-fs node06-path node07-http node08-exp ...
- C++ 基础知识复习(六)
操作系统部分: 79. 操作系统的最小调度单位:线程. 线程thread,进程process.一个进程至少包含一个线程,主线程,main thread. 80. 资源的最小单位是:进程. 81. 进程 ...