Apache Commons 系列简介 之 CLI
原文链接:http://www.blogways.net/blog/2014/01/15/apache-commons-cli.html
Apache Commons
的主要目的就是,创建和维护一个可重用的java组件库集合。这样Apache社区的开发者,就可以使用相同的基础组件库来开发不同的Apache项目了。
而Apache Commons
的开发者们,将尽量减少这些组件对其他外部库的影响,来确保这些组件可以很容易地进行部署。另外,这些组件将尽可能地保证接口稳定,以便Apache用户(包括Apache项目)可以实现这些组件,而不必担心未来发生变化。
本文将介绍Commons系列中的CLI组件库。
一、Commons CLI 概述
Apache Commons CLI
库提供API,可以帮助程序去解析传递给程序的命令行参数。他也可以打印帮助信息,来说明可以运用于命令行的有效参数。
CLI库支持不同格式的选项:
- POSIX格式的选项(比如:
tar -zxvf foo.tar.gz
) - GNU格式的长参数选项(比如:
du --human-readable --max-depth=1
) - Java格式的属性(比如:
java -Djava.awt.headless=true -Djava.net.useSystemProxies=true Foo
) - 带值的单选项(比如:
gcc -O2 foo.c
) - 单
-
号的长参数选项(比如:ant -projecthelp
)
CLI库可以提供的帮助信息,类似如下:
usage: ls-A,--almost-all do not list implied . and ..-a,--all do not hide entries starting with .-B,--ignore-backups do not list implied entried ending with ~-b,--escape print octal escapes for nongraphic characters--block-size <SIZE> use SIZE-byte blocks-c with -lt: sort by, and show, ctime (time of lastmodification of file status information) with-l:show ctime and sort by name otherwise: sortby ctime-C list entries by columns
二、Commons CLI 下载
截止本文撰写时,CLI的最新发布版本为1.2
。
官方下载地址:
http://commons.apache.org/proper/commons-cli/download_cli.cgi
源码:
svn checkout http://svn.apache.org/repos/asf/commons/proper/cli/trunk/ commons-cli
在Maven工程中添加依赖:
<dependency><groupId>commons-cli</groupId><artifactId>commons-cli</artifactId><version>1.2</version>
</dependency>
三、使用场景
3.1 布尔选项
布尔选项是命令行最常见的选项,作为开关使用,不带参数。如果命令行中存在该选项,那么选项值就为true
,否则其值为false
。
举例,如果程序需要布尔选项-t
,代码如下:
// create Options object
Options options = new Options();// add t option
options.addOption("t", false, "display current time");
如上,必须创建Options
选项,然后为其添加Option
对象。
上例中,addOption
方法有三个参数:第一个参数类型为String
,给出参数的名字;第二个参数类型为boolean
,用来标记该选项是否需要参数,在上面例子中,布尔选项不需要参数,所以设置为false
;第三个参数是选项的描述信息,该描述信息在打印命令行帮助信息时,会显示出来。
另外,addOption
还存在一个四个参数的调用方式:
addOption(String opt, String longOpt, boolean hasArg, String description)
在这里,多了一个长选项参数,在后面的例子中,我们可以看到具体的使用。
3.2 解析命令行参数
CommandLineParser
提供的方法parse
,就是用来解析命令行中的参数。接口CommandLineParser
存在多种实现类,比如BasicParser
、PosixParser
和GnuParser
,可以根据实际需求选择使用.
其中,PosixParser
与GnuParser
,顾名思义,其区别在于,前者把形如-log
的选项作为三个选项(l
、o
和g
)处理,而后者作为一个选项处理。
具体代码,参考如下:
CommandLineParser parser = new GnuParser();
CommandLine cmd = parser.parse( options, args);
如果,我们要检查命令行中t
选项是否被列出,可以使用hasOption
方法。例如:
if(cmd.hasOption("t")) {// 存在t选项的处理
}
else {// 不存在t选项的处理
}
3.2 带参数选项
除了布尔选项外,还有些选项是需要参数的。比如c
选项需要参数,那么可以如下设置:
// add c option
options.addOption("c", true, "country code");
并且,可以通过getOptionValue
方法,获取命令行传入的参数:
// get c option value
String countryCode = cmd.getOptionValue("c");if(countryCode == null) {// print default date
}
else {// print date for country specified by countryCode
}
3.3 Ant命令行实例
在这里,我们使用一个被普遍使用的java应用程序Ant来解释如果使用CLI库的。
3.3.1 先看看Ant的命令帮助
ant [options] [target [target2 [target3] ...]]Options: -help print this message-projecthelp print project help information-version print the version information and exit-quiet be extra quiet-verbose be extra verbose-debug print debugging information-emacs produce logging information without adornments-logfile <file> use given file for log-logger <classname> the class which is to perform logging-listener <classname> add an instance of class as a project listener-buildfile <file> use given buildfile-D<property>=<value> use value for given property-find <file> search for buildfile towards the root of thefilesystem and use it
3.3.2 创建布尔选项
为了代码清晰,在这里我们使用Option
的构造方法来创建。
Option help = new Option( "help", "print this message" );
Option projecthelp = new Option( "projecthelp", "print project help information" );
Option version = new Option( "version", "print the version information and exit" );
Option quiet = new Option( "quiet", "be extra quiet" );
Option verbose = new Option( "verbose", "be extra verbose" );
Option debug = new Option( "debug", "print debugging information" );
Option emacs = new Option( "emacs","produce logging information without adornments" );
3.3.3 创建带参数的选项
我们使用OptionBuilder
来创建:
Option logfile = OptionBuilder.withArgName( "file" ).hasArg().withDescription( "use given file for log" ).create( "logfile" );Option logger = OptionBuilder.withArgName( "classname" ).hasArg().withDescription( "the class which it to perform "+ "logging" ).create( "logger" );Option listener = OptionBuilder.withArgName( "classname" ).hasArg().withDescription( "add an instance of class as "+ "a project listener" ).create( "listener"); Option buildfile = OptionBuilder.withArgName( "file" ).hasArg().withDescription( "use given buildfile" ).create( "buildfile");Option find = OptionBuilder.withArgName( "file" ).hasArg().withDescription( "search for buildfile towards the "+ "root of the filesystem and use it" ).create( "find" );
3.3.4 创建java属性选项
Option property = OptionBuilder.withArgName( "property=value" ).hasArgs(2).withValueSeparator().withDescription( "use value for given property" ).create( "D" );
3.3.5 创建Options
上面已经创建了每个选项,下面我们需要创建Options
,然后继续使用addOption
方法,向其中添加每个选项,代码如下:
Options options = new Options();options.addOption( help );
options.addOption( projecthelp );
options.addOption( version );
options.addOption( quiet );
options.addOption( verbose );
options.addOption( debug );
options.addOption( emacs );
options.addOption( logfile );
options.addOption( logger );
options.addOption( listener );
options.addOption( buildfile );
options.addOption( find );
options.addOption( property );
**说明:可以通过Option
的setRequired
方法来设置,选项是否为必输项,默认不是必输项。 **
3.3.6 解析命令行参数
我们需要创建一个CommandLineParser
,并用它根据之前设置的Options
来解析命令行参数,代码如下:
public static void main( String[] args ) {// create the parserCommandLineParser parser = new GnuParser();try {// parse the command line argumentsCommandLine line = parser.parse( options, args );}catch( ParseException exp ) {// oops, something went wrongSystem.err.println( "Parsing failed. Reason: " + exp.getMessage() );}
}
3.3.7 获取命令行参数
使用hasOption
方法来检查命令行是否传入选项,使用getOptionValue
来获取传入的参数值。
代码如下:
// has the buildfile argument been passed?
if( line.hasOption( "buildfile" ) ) {// initialise the member variablethis.buildfile = line.getOptionValue( "buildfile" );
}
3.3.8 设置程序用例/帮助信息
CLI库还可以根据Options
来自动显示程序的用例/帮助信息。代码如下:
// automatically generate the help statement
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp( "ant", options, true );
执行后生成下面信息:
usage: ant
-D <property=value> use value for given property
-buildfile <file> use given buildfile
-debug print debugging information
-emacs produce logging information without adornments
-file <file> search for buildfile towards the root of thefilesystem and use it
-help print this message
-listener <classname> add an instance of class as a project listener
-logger <classname> the class which it to perform logging
-projecthelp print project help information
-quiet be extra quiet
-verbose be extra verbose
-version print the version information and exit
3.4 再来一个ls
实例
下面是帮助信息:
Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuSUX nor --sort.-a, --all do not hide entries starting with .
-A, --almost-all do not list implied . and ..
-b, --escape print octal escapes for nongraphic characters--block-size=SIZE use SIZE-byte blocks
-B, --ignore-backups do not list implied entries ending with ~
-c with -lt: sort by, and show, ctime (time of lastmodification of file status information)with -l: show ctime and sort by nameotherwise: sort by ctime
-C list entries by columns
下面是代码:
// create the command line parser
CommandLineParser parser = new GnuParser();// create the Options
Options options = new Options();
options.addOption( "a", "all", false, "do not hide entries starting with ." );
options.addOption( "A", "almost-all", false, "do not list implied . and .." );
options.addOption( "b", "escape", false, "print octal escapes for nongraphic "+ "characters" );
options.addOption( OptionBuilder.withLongOpt( "block-size" ).withDescription( "use SIZE-byte blocks" ).hasArg().withArgName("SIZE").create() );
options.addOption( "B", "ignore-backups", false, "do not list implied entried "+ "ending with ~");
options.addOption( "c", false, "with -lt: sort by, and show, ctime (time of last " + "modification of file status information) with "+ "-l:show ctime and sort by name otherwise: sort "+ "by ctime" );
options.addOption( "C", false, "list entries by columns" );String[] args = new String[]{ "--block-size=10" };try {// parse the command line argumentsCommandLine line = parser.parse( options, args );// validate that block-size has been setif( line.hasOption( "block-size" ) ) {// print the value of block-sizeSystem.out.println( line.getOptionValue( "block-size" ) );}
}
catch( ParseException exp ) {System.out.println( "Unexpected exception:" + exp.getMessage() );
}
Apache Commons 系列简介 之 CLI相关推荐
- Apache Commons Daemon简介说明
转自: Apache Commons Daemon简介说明 下文笔者讲述Apache Commons Daemon的功能简介说明,如下所示 Apache Commons Daemon的功能 用于将一个 ...
- 高性能jdbc封装工具 Apache Commons DbUtils 1.6(转载)
转载自原文地址:http://gao-xianglong.iteye.com/blog/2166444 前言 关于Apache的DbUtils中间件或许了解的人并不多,大部分开发人员在生成环境中更多的 ...
- IO与文件读写---使用Apache commons io包提高读写效率
[一]Apache commons IO简介 首先贴一段Apache commons IO官网上的介绍,来对这个著名的开源包有一个基本的了解: Commons IO is a library of u ...
- JavaMail| Apache Commons Email介绍
Apache Commons Email简介 Apache Commons Email旨在提供一个用于发送电子邮件的API.它建立在JavaMail API之上,旨在简化它. 官网: http://c ...
- java Apache Commons jar包简介
一.Commons BeanUtils 说明:针对Bean的一个工具集.由于Bean往往是有一堆get和set组成,所以BeanUtils也是在此基础上进行一些包装. 二.Commons CLI 说明 ...
- (转)Apache Commons工具集简介
(转)Apache Commons工具集简介 Apache Commons包含了很多开源的工具,用于解决平时编程经常会遇到的问题,减少重复劳动.我选了一些比较常用的项目做简单介绍.文中用了很多网上现成 ...
- 使用 Apache Commons CLI 开发命令行工具
http://www.ibm.com/developerworks/cn/java/j-lo-commonscli/index.html 使用 Apache Commons CLI 开发命令行工具 杨 ...
- Java命令行界面(第1部分):Apache Commons CLI
尽管我通常使用Groovy编写要从命令行运行的JVM托管脚本,但是有时候我需要解析Java应用程序中的命令行参数,并且有很多库可供Java开发人员用来解析命令行参数. 在本文中,我将介绍这些Java命 ...
- 使用 Apache Commons CLI 解析命令行参数示例
很好的输入参数解析方法 ,转载记录下 转载在: https://www.cnblogs.com/onmyway20xx/p/7346709.html Apache Commons CLI 简介 Apa ...
最新文章
- python sort函数排序_Python中排序常用到的sort 、sorted和argsort函数
- IE下的一个安全BUG —— 可实时跟踪系统鼠标位置
- ubuntu 删除opencv4_ubuntu16.04 卸载重装Opencv
- Linux下查看软件安装与安装路径
- 第三章 进化算法之遗传算法及其应用
- dsoframer.ocx java_DSOFramer.ocx 控件使用
- 从「模拟」理解零知识证明:平行宇宙与时光倒流—— 探索零知识证明系列(二)
- 罗技G29方向盘Mac驱动
- java万年历报告_java万年历设计报告
- 支架预压弹性变形值计算_支架预压计算.doc
- 对12w条数据进行相关清理和数据迁移 (数据清理项目实战完整版)文章内有大量sql脚本
- 虚拟机VMware 15安装教程
- Struts 2拦截器
- 关于深度学习人工智能模型的探讨(一)(1)
- [Pandas] 数据形状df.shape
- livechart 只显示 y 值_数字显示调节仪XMZ-H9-01-001A-老友网
- 软件测试自学还是报班好?
- 搜索引擎优化——通向成功的十步
- 使用包含排斥原理求 1~120 之间的素数个数。
- 浅谈人生中的失败与成功