博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Apache-common包之common-cli笔记
阅读量:4118 次
发布时间:2019-05-25

本文共 2686 字,大约阅读时间需要 8 分钟。

  一. 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 
use value for given property-buildfile
use given buildfile-debug print debugging information-emacs produce logging information without adornments-file
search for buildfile towards the root of the filesystem and use it-help print this message-listener
add an instance of class as a project listener-logger
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

转载地址:http://qgdpi.baihongyu.com/

你可能感兴趣的文章
最完整PHP.INI中文版
查看>>
借助开源项目,学习软件开发
查看>>
Ubuntu系统下安装并配置Hadoop-2.2.0集群
查看>>
HBase完全分布式安装过程详解
查看>>
Zookeeper集群环境安装过程详解
查看>>
SUSE Linux 64位环境下编译hadoop2.2.0源码
查看>>
轻量级持久存储系统MemcacheDB 详细介绍安装使用
查看>>
命名实体识别NER
查看>>
【linux】利用patch和diff命令制作文件补丁
查看>>
C语言访问redis(hiredis)
查看>>
Redis集群规范
查看>>
redis cluster搭建
查看>>
如何用javac 和java 编译运行整个Java工程
查看>>
自己动手编译、运行Java程序
查看>>
memcachedb的C语言客户端libmemcached
查看>>
memcached的java语言客户端和访问集群
查看>>
redis的java客户端jedis
查看>>
命令行下Java的编译及运行(1)
查看>>
命令行下Java的编译及运行(2)
查看>>
javac,java,classpath的各种问题
查看>>