http://commons.apache.org/proper/commons-cli/usage.html

Apache Commons CLI用于解析命令行选项,也可以输出详细的选项说明信息。

Commons CLI 支持多种选项类型:

  • POSIX like options (ie. tar -zxvf foo.tar.gz)
  • GNU like long options (ie. du --human-readable --max-depth=1)
  • Java like properties (ie. java -Djava.awt.headless=true -Djava.net.useSystemProxies=true Foo)
  • Short options with value attached (ie. gcc -O2 foo.c)
  • long options with single hyphen (ie. ant -projecthelp)

Commons CLI处理过程可以分成三个阶段

  • 定义阶段,Options类。
  • 解析阶段,使用CommandLineParser类讲String[] args解析为CommandLine
  • 查询阶段,调用CommandLine对象的方法,查询和提取特定选项。

命令行选型定义

Options options = new Options();
options.addOption("h", "help", false, "Print this usage information");
options.addOption("v", "verbose", false, "Print out VERBOSE information" );
options.addOption("p", false, "no error if existing, make parent directories as needed."); options.addOption(OptionBuilder.withArgName("file")
.hasArg()
.withDescription("search for buildfile towards the root of the filesystem and use it")
.create("O"));
options.addOption(OptionBuilder.withLongOpt("block-size")
.withDescription("use SIZE-byte blocks")
.withValueSeparator('=')
.hasArg()
.create() );

解析

CommandLineParser parser = new BasicParser( );
CommandLineParser parser = new PosixParser();
CommandLine commandLine = parser.parse( options, args );

选型查询和提取

  if( commandLine.hasOption('f') ) {
file = commandLine.getOptionValue('f');
} String[] str = commandLine.getArgs();

显示帮助信息

HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("xxcmd [-lkjs] PATH", "", options , "");
formatter.printHelp( "ant", options );
formatter.printHelp( "ant", options, true ); //generate a usage statment as well as the help information

Java命令行解析:apache commons-cli的更多相关文章

  1. Apache Commons CLI命令行启动

    今天又看了下Hangout的源码,一般来说一个开源项目有好几种启动方式--比如可以从命令行启动,也可以从web端启动.今天就看看如何设计命令行启动... Apache Commons CLI Apac ...

  2. Apache Commons CLI官方文档翻译 —— 快速构建命令行启动模式

    昨天通过几个小程序以及Hangout源码学习了CLI的基本使用,今天就来尝试翻译一下CLI的官方使用手册. 下面将会通过几个部分简单的介绍CLI在应用中的使用场景. 昨天已经联系过几个基本的命令行参数 ...

  3. 使用 Apache Commons CLI 解析命令行参数示例

    很好的输入参数解析方法 ,转载记录下 转载在: https://www.cnblogs.com/onmyway20xx/p/7346709.html Apache Commons CLI 简介 Apa ...

  4. Apache Commons CLI 开发命令行工具示例

    概念说明Apache Commons CLI 简介 虽然各种人机交互技术飞速发展,但最传统的命令行模式依然被广泛应用于各个领域:从编译代码到系统管理,命令行因其简洁高效而备受宠爱.各种工具和系统都 提 ...

  5. 使用 Apache Commons CLI 开发命令行工具示例

    Apache Commons CLI 简介 Apache Commons CLI 是 Apache 下面的一个解析命令行输入的工具包,该工具包还提供了自动生成输出帮助文档的功能. Apache Com ...

  6. maven命令行创建web项目报错:java.lang.NoClassDefFoundError: org/apache/commons/lang/StringUtils

    早上一上班就想新建一个web项目玩玩,没想到一敲命令创建就失败了,真是出师不利.各种折腾无果,当然我也可以用eclipse直接创建的,就是不甘心被这破问题给耍了.刚刚才发现问题原因,这个结果我也是醉了 ...

  7. Java命令行参数解析

    参考  http://blog.csdn.net/mldxs/article/details/36204079 http://rensanning.iteye.com/blog/2161201 imp ...

  8. [转] Java 命令行交互-JCommander

    [From] https://github.com/Sayi/sayi.github.com/issues/32 我喜欢简单,什么是简单?正如若干字符组成的命令行. 有时候我们用Java开发了一个小工 ...

  9. Apache Commons CLI 简介

    CLI 命令代码实现 命令行程序处理流程相对比较简单,主要流程为设定命令行参数 -> 解析输入参数 -> 使用输入的数据进行逻辑处理CLI 定义阶段 每一条命令行都必须定义一组参数,它们被 ...

随机推荐

  1. js parseInt 显示0

    parseInt 有第二个参数, 就是进制参数 parseInt("08", 10);  //表示这个数字是十进制的就不会出错了.

  2. Android 全局获取 Context 与使用 Intent 传递对象

    =====================全局获取 Context======================== Android 开发中很多地方需要用到 Context,比如弹出 Toast.启动活 ...

  3. php 批量去空格和注释

    原理:php自带函数去注释和空格  => php_strip_whitespace如题,新建文件 re_note.php,将文件放入你要批量去除注释和空格的根目录.然后运行就行了 代码如下: & ...

  4. C#基本工具代码

    1.下载Xlsx public static void TryToDisplayGeneratedFileXlsx(string writeFilePath, string fileName) { H ...

  5. lua高阶用法 OO的实现

    //Lua的类的实现,可以派生,可重写方法 local _class={} function class(super) local class_type={} class_type.ctor=fals ...

  6. MySQL 5.7 学习:安全相关特性

    背景: 继上次介绍 初识 MySQL 5.6 新功能.参数完之后,刚好MySQL 5.7又GA了,在官方测试里看到,MySQL5.7在功能.性能.可用性.安全和监控上又提升了很高.现在看看和MySQL ...

  7. [转]TextView maxWidth maxLength maxEms 区别

    maxWidth=”80dp” 限制TextView最大宽度.必须与layout_width=”wrap_content”搭配使用,当指定layout_width为其他值时,maxWidth会失效. ...

  8. epoll示例

    书到用时方恨少,一切尽在不言中 #include <iostream> #include <sys/socket.h> #include <sys/epoll.h> ...

  9. 前端 js 发送验证码

    1. 代码如下: <html> <head> <meta charset="utf-8"> <title></title> ...

  10. jquery 控件赋值

    input 文本框赋值$("#id").attr("赋值"); input 文本框清空$("#id").attr("") ...