Ryan's Java world!

          something about Java and opensource!

          BlogJava 首頁 新隨筆 聯(lián)系 聚合 管理
            51 Posts :: 25 Stories :: 59 Comments :: 0 Trackbacks

          Args4J 使用指南




          Args4J
          是一個(gè)用來出來命令行的工具.

          在實(shí)際的項(xiàng)目中用到命令行的并不是很常見,但當(dāng)真正使用到時(shí),特別是在程序啟動時(shí)配置一下參數(shù)的時(shí)候就很有用了,如果參數(shù)很多的話,一個(gè)一個(gè)解析命令行還是比較麻煩的.這時(shí)使用Args4J就相當(dāng)好辦了. 在本文中我們來看看Args4J的使用,當(dāng)需要時(shí)能提供一個(gè)解決方案.

          Args4J使用一個(gè)被稱為Option類的類來保存輸入的參數(shù),讓后根據(jù)該類來應(yīng)用參數(shù),每個(gè)參數(shù)可以對應(yīng)一個(gè)類中的屬性,該屬性用Annotation注釋,在Annotation中給出該參數(shù) 的選項(xiàng), 還可以配置其他有用的信息.該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是必須的,其他四個(gè)是可選的.如下所示,關(guān)于該注解的詳細(xì)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.
          ?StringmetaVar
          ??????????When the option takes an operand, the usage screen will show something like this:
          ?booleanrequired
          ??????????Specify that the option is mandatory.
          ?Stringusage
          ??????????Help string used to display the usage screen.

           

          當(dāng)命令行設(shè)定后 其使用方式和java 命令里面的參數(shù)使用方式一樣 如:java -cp ./calssPath/.......

          下面通過一個(gè)例子來解釋:

          001?/*
          002??*?Created?on?2006-2-21
          003??*?@author?icerain
          004??*/
          005?package?test.args4j;
          006?
          007?import?org.kohsuke.args4j.Argument;
          008?import?org.kohsuke.args4j.CmdLineException;
          009?import?org.kohsuke.args4j.CmdLineParser;
          010?import?org.kohsuke.args4j.ExampleMode;
          011?import?org.kohsuke.args4j.Option;
          012?import?org.kohsuke.args4j.spi.BooleanOptionHandler;
          013?
          014?import?java.io.File;
          015?import?java.io.IOException;
          016?import?java.util.ArrayList;
          017?import?java.util.List;
          018?
          019?public?class?TestArgs4J?{
          020???//?利用Option注解來定義一個(gè)boolean?命令行參數(shù)?其參數(shù)name為?-re?,required指定該參數(shù)是必須的
          021???@Option(name?=?"-re",?usage?=?"recursively?run?something",?required?=?true)
          022???private?boolean?recursive;
          023?
          024???//利用Option注解定義一個(gè)File?命令行參數(shù),?name為-o,?輸入時(shí)候知道該file的路徑就可以了
          025???//metaVar?用來設(shè)定顯示?使用方式時(shí)候的輸出,這個(gè)輸出為-o?OUTPUT?:?output?to?this?file
          026???//如果不指定該屬性?則使用默認(rèn)的代替?為-o?FILE?:?output?to?this?file
          027???@Option(name?=?"-o",?usage?=?"output?to?this?file",?metaVar?=?"OUTPUT")
          028???private?File?out?=?new?File(".");
          029?
          030???//If?'usage'?value?is?empty,?the?option?will?not?be?displayed
          031?????//?in?the?usage?screen.
          032???//注意該處沒有指定?usage?屬性?或者指定usage?但是其值為空的?如usage?=?"",這樣當(dāng)使用
          033???//parser.printExample(ExampleMode.ALL)?請注意下面第92行的輸出
          034???@Option(name?=?"-str",?required?=?true)
          035???//@Option(name?=?"-str",?usage?=?"測試",?required?=?true)??//?該行?-str參數(shù)有用
          036???//?no?usage
          037???
          038???private?String?str?=?"(default?value)";
          039?
          040???//?整數(shù)參數(shù)
          041???@Option(name?=?"-n",?usage?=?"repeat?<n>?times\nusage?can?have?new?lines?in?it?and?also?it?can?be?verrry?long")
          042???private?int?num?=?-1;
          043?
          044???//?using?'handler=...'?allows?you?to?specify?a?custom?OptionHandler
          045???//?implementation?class.?This?allows?you?to?bind?a?standard?Java?type
          046???//?with?a?non-standard?option?syntax
          047???//指定一個(gè)特定的handler?
          048???@Option(name?=?"-custom",?handler=BooleanOptionHandler.class,usage="boolean?value?for?checking?the?custom?handler")
          049???private?boolean?data;
          050?
          051???//?receives?other?command?line?parameters?than?options
          052???@Argument
          053???private?List<String>?arguments?=?new?ArrayList<String>();
          054?
          055???public?static?void?main(String[]?args)?throws?IOException?{
          056?????new?TestArgs4J().doMain(args);
          057???}
          058?
          059???public?void?doMain(String[]?args)?throws?IOException?{
          060?????//Creates?a?new?command?line?owner?that?parses?arguments/options

          ?????????????and?set?them?into?the?given?object.

          061?????CmdLineParser?parser?=?new?CmdLineParser(this);
          062?
          063?????try?{
          064???????//?parse?the?arguments.
          065???????parser.parseArgument(args);
          066?
          067???????//?you?can?parse?additional?arguments?if?you?want.
          068???????//?parser.parseArgument("more","args");
          069?
          070???????//?after?parsing?arguments,?you?should?check
          071???????//?if?enough?arguments?are?given.
          072???????if?(arguments.isEmpty())
          073?????????throw?new?CmdLineException("No?argument?is?given");
          074?
          075?????}?catch?(CmdLineException?e)?{
          076???????//?if?there's?a?problem?in?the?command?line,
          077???????//?you'll?get?this?exception.?this?will?report
          078???????//?an?error?message.
          079???????System.err.println(e.getMessage());??//打印出錯(cuò)消息
          080???????System.err.println("java?SampleMain?[options...]?arguments...");
          081???????//?print?the?list?of?available?options
          082???????parser.printUsage(System.err);??//?打印參數(shù)的用法
          083???????System.err.println();
          084?
          085???????System.err.println("測試!!!!!");
          086???????//?print?option?sample.?This?is?useful?some?time
          087???????System.err.println("??Example:?java?SampleMain"
          088???????????+?parser.printExample(ExampleMode.ALL));???????

          // 注意 在Option中如果沒有指定 usage 屬性,

          089???????System.err.println("/n?2.........");???????????????

          //
          則這兩行程序不會輸出該參數(shù)的使用的
          090?
          091???????System.err.println("?2?Example2:?java?SampleMain"?

          //
          注意 在Option中如果沒有指定 usage 屬性,
          092???????????+?parser.printExample(ExampleMode.REQUIRED));??

          //
          則這兩行程序不會輸出該參數(shù)的使用的
          093???????return;
          094?????}
          095?
          096?????//?this?will?redirect?the?output?to?the?specified?output
          097?????System.out.println(out);
          098?
          099?????if?(recursive)
          100???????System.out.println("-r?flag?is?set");
          101?
          102?????if?(data)
          103???????System.out.println("-custom?flag?is?set");
          104?
          105?????System.out.println("-str?was?"?+?str);
          106?
          107?????if?(num?>=?0)
          108???????System.out.println("-n?was?"?+?num);
          109?
          110?????//?access?non-option?arguments
          111?????System.out.println("other?arguments?are:");
          112?????for?(String?s?:?arguments)
          113???????System.out.println(s);
          114???}
          115?
          116?}

          當(dāng)不使用命令行時(shí)候 輸入信息如下:

          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的出現(xiàn)

          /n 2.........

          2 Example2: java SampleMain -re

          ?// 注意該處沒有 -str的出現(xiàn)

          當(dāng)使用 -re 為命令行輸入時(shí),輸出如下:// 后為作者加的注釋 不是輸出

          Option "-str" is required?

          ?//也要指定-str該參數(shù)

          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

          當(dāng)使用-re -str some 為命令行輸入時(shí),結(jié)果如下:這是由于的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 為命令行輸入時(shí)的 結(jié)果如下:

          .??? // file

          -r flag is set

          -custom flag is set

          -str was some

          -n was 2

          other arguments are:

          otherstring

          當(dāng)使用-custom -n 2 -re -str some -o log.txt otherstring 時(shí)候的輸出如下:

          log.txt

          -r flag is set

          -custom flag is set

          -str was some

          -n was 2

          other arguments are:

          otherstring

          當(dāng)使用一個(gè)不存在的參數(shù)時(shí)候 會有Exception的 例如:-custom -ee 2 -re -str some -o log.txt otherstring

          其中-ee參數(shù)不存在 結(jié)果如下:

          "-ee" is not a valid option

          java SampleMain [options...] arguments...

          -custom : boolean value for checking the custom handler

          -n N : repeat <n> times

          ......

          由以上的實(shí)例可以看出 args4J 取得命令行的輸入?yún)?shù),然后根據(jù)保存參數(shù)的類中的屬性類型比較
          ?
          并轉(zhuǎn)換為適當(dāng)?shù)闹? 然后我們可以使用這些屬性了,這樣就免去了自己判斷args 的麻煩了,

          當(dāng)默認(rèn)的Handler不滿足你的要求時(shí) 可以自己擴(kuò)展Handler實(shí)現(xiàn),關(guān)于這點(diǎn)請參考Args4J的測試用例,

          From : java 愛好者? by : icess

          posted on 2006-04-12 14:27 冰雨 閱讀(2797) 評論(0)  編輯  收藏 所屬分類: OpenSource

          JSF中文技術(shù)文摘
          主站蜘蛛池模板: 颍上县| 文昌市| 龙门县| 淳化县| 湘潭县| 金沙县| 辽宁省| 文登市| 平度市| 沙洋县| 益阳市| 伊川县| 泉州市| 太湖县| 乌鲁木齐市| 安庆市| 青龙| 渑池县| 敦化市| 革吉县| 咸丰县| 家居| 龙州县| 宁安市| 团风县| 绥阳县| 河西区| 京山县| 呈贡县| 泽普县| 佛冈县| 玛纳斯县| 余姚市| 长沙县| 濉溪县| 永济市| 崇文区| 云南省| 常熟市| 大安市| 都安|