- 浏览: 2832214 次
- 性别:
- 来自: 武汉
文章分类
- 全部博客 (1173)
- 名言警句 (5)
- 心情随笔 (50)
- 数据库 (57)
- Java基础 (241)
- J2EE框架 (91)
- 数据结构 (12)
- 程序设计 (21)
- WEB技术 (128)
- 网络日志 (12)
- IT资讯 (247)
- linux (64)
- solaris (2)
- 其它 (143)
- WebService (4)
- 日语学习 (2)
- 机器人 (5)
- Android (5)
- cgywin (3)
- Game (1)
- DWR (1)
- spring (8)
- canvas (1)
- Guava (3)
- Modbus (5)
- 测试 (6)
- mongodb (9)
- Quartz (2)
- Cron (1)
- windows (2)
- 持续集成 (1)
- bootstrap (3)
- 结对编程 (1)
- nodejs (1)
- Netty (1)
- 安全 (3)
- webstorm (2)
- sparkline (1)
- Job (1)
- git (3)
- Maven (3)
- knockout (5)
- jquery (1)
- bower (1)
- docker (1)
- confluence (4)
- wiki (1)
- GoogleMap (1)
- jekyll (10)
- ruby (2)
- npm (3)
- browserify (1)
- gulp (3)
- openwrt (1)
- discuz (3)
- 输入法 (1)
- JPA (1)
- eclipse (2)
- IntelliJ (1)
- css (1)
- 虚拟机 (1)
- 操作系统 (1)
- azkaban (2)
- scrum (1)
最新评论
-
pangxiea_:
你好, 想请问一下 Linux下 这么使用rxtxcomm 在 ...
使用Java进行串口通信 -
abababudei:
请教一下,这个您是怎么解决的:/dev/ttyS2enteri ...
Java应用程序的MODBUS通讯 -
xuniverse:
hannibal005 写道楼主,我问下 request.se ...
用javascript与java进行RSA加密与解密 -
atxkm:
找了一下午,终于找到了
gulp 拷贝文件时如何移除文件目录结构 -
kalogen:
gtczr 写道非常感谢,经过我自己的修改,已经完美实现。发出 ...
用javascript与java进行RSA加密与解密
Args4J 是一个用来出来命令行的工具.
在实际的项目中用到命令行的并不是很常见,但当真正使用到时,特别是在程序启动时配置一下参数的时候就很有用了,如果参数很多的话,一个一个解析命令行还是比较麻烦的.这时使用Args4J就相当好办了. 在本文中我们来看看Args4J的使用,当需要时能提供一个解决方案.
Args4J使用一个被称为Option类的类来保存输入的参数,让后根据该类来应用参数,每个参数可以对应一个类中的属性,该属性用Annotation注释,在Annotation中给出该参数 的选项, 还可以配置其他有用的信息.该Annotation就是 Option 注解: 该注解的doc如下:
Marks a field/setter that receives a command line switch value.
This annotation can be placed on a field of type T or the method of the form void methodName(T value). Its access modified can be anything, but if it's not public, your application needs to run in a security context that allows args4j to access the field/method (see AccessibleObject.setAccessible(boolean).
The behavior of the annotation differs depending on T --- the type of the field or the parameter of the method.
Boolean Switch
When T is boolean , it represents a boolean option that takes the form of "-OPT". When this option is set, the property will be set to true.
String Switch
When T is String, it represents an option that takes one operand. The value of the operand is set to the property.
Enum Switch
When T is derived from Enum, it represents an option that takes an operand, which must be one of the enum constant. The comparion between the operand and the enum constant name is done in a case insensitive fashion.
For example, the following definition will represent command line options like "-coin penny" or "-coin DIME" but things like "-coin" or "-coin abc" are errors.
enum Coin { PENNY,NICKEL,DIME,QUARTER }
class Option {
@Option(name="-coin")
public Coin coin;
}
File Switch
When T is a File, it represents an option that takes a file/directory name as an operand.
该注解有5各域 其中name是必须的,其他四个是可选的.如下所示,关于该注解的详细Doc请查看 其docs.
Required Element Summary
=======================
String
name
Name of the option, such as "-foo" or "-bar".
Optional Element Summary
Class<? extends OptionHandler>
handler
Specify the OptionHandler that processes the command line arguments.
String
metaVar
When the option takes an operand, the usage screen will show something like this:
boolean required
Specify that the option is mandatory.
String
usage
Help string used to display the usage screen.
当命令行设定后 其使用方式和java 命令里面的参数使用方式一样 如:java -cp ./calssPath/.......
下面通过一个例子来解释:
/* * Created on 2006-2-21 * @author icerain */ package test.args4j; import org.kohsuke.args4j.Argument; import org.kohsuke.args4j.CmdLineException; import org.kohsuke.args4j.CmdLineParser; import org.kohsuke.args4j.ExampleMode; import org.kohsuke.args4j.Option; import org.kohsuke.args4j.spi.BooleanOptionHandler; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class TestArgs4J { // 利用Option注解来定义一个boolean 命令行参数 其参数name为 -re ,required指定该参数是必须的 @Option(name = "-re", usage = "recursively run something", required = true) private boolean recursive; //利用Option注解定义一个File 命令行参数, name为-o, 输入时候知道该file的路径就可以了 //metaVar 用来设定显示 使用方式时候的输出,这个输出为-o OUTPUT : output to this file //如果不指定该属性 则使用默认的代替 为-o FILE : output to this file @Option(name = "-o", usage = "output to this file", metaVar = "OUTPUT") private File out = new File("."); //If 'usage' value is empty, the option will not be displayed // in the usage screen. //注意该处没有指定 usage 属性 或者指定usage 但是其值为空的 如usage = "",这样当使用 //parser.printExample(ExampleMode.ALL) 请注意下面第92行的输出 @Option(name = "-str", required = true) //@Option(name = "-str", usage = "测试", required = true) // 该行 -str参数有用 // no usage private String str = "(default value)"; // 整数参数 @Option(name = "-n", usage = "repeat <n> times\nusage can have new lines in it and also it can be verrry long") private int num = -1; // using 'handler=...' allows you to specify a custom OptionHandler // implementation class. This allows you to bind a standard Java type // with a non-standard option syntax //指定一个特定的handler @Option(name = "-custom", handler=BooleanOptionHandler.class,usage="boolean value for checking the custom handler") private boolean data; // receives other command line parameters than options @Argument private List<String> arguments = new ArrayList<String>(); public static void main(String[] args) throws IOException { new TestArgs4J().doMain(args); } public void doMain(String[] args) throws IOException { //Creates a new command line owner that parses arguments/options and set them into the given object. CmdLineParser parser = new CmdLineParser(this); try { // parse the arguments. parser.parseArgument(args); // you can parse additional arguments if you want. // parser.parseArgument("more","args"); // after parsing arguments, you should check // if enough arguments are given. if (arguments.isEmpty()) throw new CmdLineException("No argument is given"); } catch (CmdLineException e) { // if there's a problem in the command line, // you'll get this exception. this will report // an error message. System.err.println(e.getMessage()); //打印出错消息 System.err.println("java SampleMain [options...] arguments..."); // print the list of available options parser.printUsage(System.err); // 打印参数的用法 System.err.println(); System.err.println("测试!!!!!"); // print option sample. This is useful some time System.err.println(" Example: java SampleMain" + parser.printExample(ExampleMode.ALL)); // 注意 在Option中如果没有指定 usage 属性, System.err.println("/n 2........."); //则这两行程序不会输出该参数的使用的 System.err.println(" 2 Example2: java SampleMain" //注意 在Option中如果没有指定 usage 属性, + parser.printExample(ExampleMode.REQUIRED)); //则这两行程序不会输出该参数的使用的 return; } // this will redirect the output to the specified output System.out.println(out); if (recursive) System.out.println("-r flag is set"); if (data) System.out.println("-custom flag is set"); System.out.println("-str was " + str); if (num >= 0) System.out.println("-n was " + num); // access non-option arguments System.out.println("other arguments are:"); for (String s : arguments) System.out.println(s); } }
当不使用命令行时候 输入信息如下:
Option "-re" is required
java SampleMain [options...] arguments...
-custom : boolean value for checking the custom handler
-n N : repeat <n> times
usage can have new lines in it and also it can be verrrrrrr
rrrrrrrrrrry long
-o OUTPUT : output to this file
-re : recursively run something
测试!!!!!
Example: java SampleMain -custom -n N -o OUTPUT -re
// 注意该处没有 -str的出现
/n 2.........
2 Example2: java SampleMain -re
// 注意该处没有 -str的出现
当使用 -re 为命令行输入时,输出如下:// 后为作者加的注释 不是输出
Option "-str" is required
//也要指定-str该参数
java SampleMain [options...] arguments...
-custom : boolean value for checking the custom handler
-n N : repeat <n> times
usage can have new lines in it and also it can be verrrrrrr
rrrrrrrrrrry long
-o OUTPUT : output to this file
-re : recursively run something
测试!!!!!
Example: java SampleMain -custom -n N -o OUTPUT -re
/n 2.........
2 Example2: java SampleMain -re
当使用-re -str some 为命令行输入时,结果如下:这是由于的73 行的判断引起的
No argument is given
java SampleMain [options...] arguments...
-custom : boolean value for checking the custom handler
-n N : repeat <n> times
usage can have new lines in it and also it can be verrrrrrr
rrrrrrrrrrry long
-o OUTPUT : output to this file
-re : recursively run something
测试!!!!!
Example: java SampleMain -custom -n N -o OUTPUT -re
/n 2.........
2 Example2: java SampleMain -re
使用-custom -n 2 -re -str some otherstring 为命令行输入时的 结果如下:
. // file
-r flag is set
-custom flag is set
-str was some
-n was 2
other arguments are:
otherstring
当使用-custom -n 2 -re -str some -o log.txt otherstring 时候的输出如下:
log.txt
-r flag is set
-custom flag is set
-str was some
-n was 2
other arguments are:
otherstring
当使用一个不存在的参数时候 会有Exception的 例如:-custom -ee 2 -re -str some -o log.txt otherstring
其中-ee参数不存在 结果如下:
"-ee" is not a valid option
java SampleMain [options...] arguments...
-custom : boolean value for checking the custom handler
-n N : repeat <n> times
......
由以上的实例可以看出 args4J 取得命令行的输入参数,然后根据保存参数的类中的属性类型比较
并转换为适当的值, 然后我们可以使用这些属性了,这样就免去了自己判断args 的麻烦了,
当默认的Handler不满足你的要求时 可以自己扩展Handler实现,关于这点请参考Args4J的测试用例.
发表评论
-
高级Java程序员值得拥有的10本书
2015-05-04 07:24 769Java是时下最流行的编程语言之一。市面上也出现了适合初学者 ... -
深入理解java异常处理机制
2015-01-30 09:30 12831. 引子 try…catch…fi ... -
java 运行时参数设置
2015-01-07 09:13 825JVM的运行时参数: -Xms为执行单元内存的1/4, ... -
每个Java开发者都应该知道的5个JDK工具
2014-12-29 12:37 1106JDK是Java语言的软件开 ... -
使用双重锁判定可以大幅降低锁的征用
2014-12-29 12:30 710class ObjInstance { //单例 pri ... -
MAVEN Scope使用说明
2014-11-24 09:40 718在Maven的依赖管理中,经常会用到依赖的scope设置。这 ... -
Spring4 quartz job xml configuration
2014-11-11 09:46 14061. 定义job details public ... -
Add items into list in one line using guava
2014-11-10 10:54 696//@formatter:off fina ... -
配置动态读取(变化)文件 in Spring
2014-11-10 08:51 12951. 从环境变量中读取路径: <bean id=&q ... -
JAVA实现AES加密与解密
2014-11-04 15:34 619package com.eifesun.monitor.up ... -
Netty4.x分析
2014-07-31 11:06 1423官网定义: netty是一个异步、事件驱动的网络应用框架,用 ... -
Ways to sort lists of objects in Java based on multiple fields
2014-07-21 17:19 7381. the first way: Sorting wit ... -
how to parse a String to BigDecimal
2014-07-21 10:08 890private BigDecimal parsePrice( ... -
order list using google guava
2014-07-21 09:08 855Predicate<String> filter ... -
Java 读文件操作
2014-07-08 14:09 8591. only use java core, no exte ... -
怎样使Java 中测试按一定顺序执行
2014-03-10 11:27 1271@FixMethodOrder(MethodSorters. ... -
如何实现在当类初始化时,自动调动某个方法
2014-02-14 14:44 925有两种思路, 1. 将这个类实现为thread类 (or ... -
持续集成JenkinsAPI常见用法
2014-02-10 13:54 43jenkins(持续集成开源工具)提供了丰富的api接口,基 ... -
Sonar 安装与使用
2014-01-13 10:49 1698Sonar 是一个用于代码质量管理的开放平台。通过插件机制, ... -
源代码管理分析工具 Source Navigator的安装与使用
2014-01-13 09:51 1845Source-Navigator是原来redhat开发的一个 ...
相关推荐
args4j-2.0.17.zip,args4j-2.0.17.zip,args4j-2.0.17.zip
标签:args4j.jar.zip,args4j,jar.zip包下载,依赖包
args4j-2.33.jar 很有用的一个资源,自己找了很久才找到的
标签:args4j-2.0.2.jar.zip,args4j,2.0.2,jar.zip包下载,依赖包
标签:args4j-2.0.1.jar.zip,args4j,2.0.1,jar.zip包下载,依赖包
标签:args4j-2.0.3.jar.zip,args4j,2.0.3,jar.zip包下载,依赖包
标签:args4j-2.0.11-javadoc.jar.zip,args4j,2.0.11,javadoc,jar.zip包下载,依赖包
标签:args4j-tools-2.0.9.jar.zip,args4j,tools,2.0.9,jar.zip包下载,依赖包
标签:args4j-2.0.16-sources.jar.zip,args4j,2.0.16,sources,jar.zip包下载,依赖包
标签:args4j-2.0.9-javadoc.jar.zip,args4j,2.0.9,javadoc,jar.zip包下载,依赖包
标签:args4j-tools-2.0.15-javadoc.jar.zip,args4j,tools,2.0.15,javadoc,jar.zip包下载,依赖包
标签:args4j-2.0.4.jar.zip,args4j,2.0.4,jar.zip包下载,依赖包
args4j args4j是一个小型Java类库,可轻松解析CUI应用程序中的命令行选项/参数。 在[ ]上查看更多信息。 我为什么要使用它?...使用Starter和Args4J的 [Kohsuke的博客:以JDK 5.0样式解析命令行选项]( ) 法语
NULL 博文链接:https://rensanning.iteye.com/blog/2161201
args4j-helpers.zip,用于简化args4j解析的args4j帮助程序
标签:args4j-2.0.7.jar.zip,args4j,2.0.7,jar.zip包下载,依赖包
标签:args4j-2.0.13.jar.zip,args4j,2.0.13,jar.zip包下载,依赖包
标签:args4j-2.0.14.jar.zip,args4j,2.0.14,jar.zip包下载,依赖包
标签:args4j-2.0.16.jar.zip,args4j,2.0.16,jar.zip包下载,依赖包
标签:args4j-2.0.9.jar.zip,args4j,2.0.9,jar.zip包下载,依赖包