Args4J 使用指南

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 SwitchWhen 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 SwitchWhen T is String, it represents an option that takes one operand. The value of the operand is set to the property.Enum SwitchWhen 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 SwitchWhen T is a File, it represents an option that takes a file/directory name as an operand.该注解有5各域 其中name是必须的,其他四个是可选的.如下所示,关于该注解的详细Doc请查看 其docs. Required Element Summary======================= Stringname 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. StringmetaVar When the option takes an operand, the usage screen will show something like this: boolean required Specify that the option is mandatory. Stringusage Help string used to display the usage screen. 当命令行设定后 其使用方式和java 命令里面的参数使用方式一样 如:java -cp ./calssPath/.......下面通过一个例子来解释: Java代码 收藏代码 /* * 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 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 arguments = new ArrayList(); 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 requiredjava SampleMain [options...] arguments...-custom : boolean value for checking the custom handler-n N : repeat timesusage can have new lines in it and also it can be verrrrrrrrrrrrrrrrrry 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 timesusage can have new lines in it and also it can be verrrrrrrrrrrrrrrrrry 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 givenjava SampleMain [options...] arguments...-custom : boolean value for checking the custom handler-n N : repeat timesusage can have new lines in it and also it can be verrrrrrrrrrrrrrrrrry 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 2other 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 2other arguments are:otherstring当使用一个不存在的参数时候 会有Exception的 例如:-custom -ee 2 -re -str some -o log.txt otherstring其中-ee参数不存在 结果如下:"-ee" is not a valid optionjava SampleMain [options...] arguments...-custom : boolean value for checking the custom handler-n N : repeat times......由以上的实例可以看出 args4J 取得命令行的输入参数,然后根据保存参数的类中的属性类型比较 并转换为适当的值, 然后我们可以使用这些属性了,这样就免去了自己判断args 的麻烦了,当默认的Handler不满足你的要求时 可以自己扩展Handler实现,关于这点请参考Args4J的测试用例.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值