Apache-common包之common-cli筆記

來源:互聯網
上載者:User

  一. common-cli是命令列工具包:包括三個階段:

1. 定義命令列選項

2. 解析命令列選項

3.解釋命令列選項

二. 定義階段:

Options類是Option類的集合

解析階段:

CommandLineParser類為命令列解析類,解析返回CommandLine類

解釋階段:

查詢CommandLine根據不同組合進行不同分支處理

三. 例子:

// create the command line parserCommandLineParser parser = new PosixParser();// create the OptionsOptions 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" )                                .withValueSeparator( '=' )                                .hasArg()                                .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 arguments    CommandLine line = parser.parse( options, args );    // validate that block-size has been set    if( line.hasOption( "block-size" ) ) {        // print the value of block-size        System.out.println( line.getOptionValue( "block-size" ) );    }}catch( ParseException exp ) {    System.out.println( "Unexpected exception:" + exp.getMessage() );}

四. 自動產生協助聲明:

// automatically generate the help statement
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp( "ant", options );

列印出來資訊:

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 the                        filesystem 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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.