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. main(int argc, char **argv)参数解读

    main(int argc, char **argv)参数解读 编译生成了test.exe ,然后在控制台下相应的目录下输入:test  1  2  3 4 argc就是一个输入了多少个参数,包括te ...

  2. C#高级编程笔记 Day 4, 2016年9月 12日(接口)

    1.定义和实现接口:接口名称通常上以字母 I 开头 例子:定义IBankAccount接口 namespace Test.YinXi{ public interface IBankAccount{ v ...

  3. MapReduce的核心资料索引 [转]

    转自http://prinx.blog.163.com/blog/static/190115275201211128513868/和http://www.cnblogs.com/jie46583173 ...

  4. linux 配置ssh免密码登陆本机

    1.安装 sudo apt-get install ssh 2.配置无密码登录 ssh-keygen -t rsa 遇到停顿按回车即可 进入/home/zeze/.ssh目录(隐藏目录,在winSCP ...

  5. JavaScript刷新页面n种方法

    window.location.href 属性 window.location.href=window.location.href;//刷新当前页面 asp.net 或 asp 利用此功能刷新页面 R ...

  6. git 入门操作

    从已有的GIT仓库获取最新代码 git clone url 建立本地仓库,并提交至git-hub 生成ssh的公钥私钥对:ssh-keygen 必须把这两个文件放到当前用户目录的“.ssh”目录下才能 ...

  7. 与你相遇好幸运,Postman设置Header不生效问题解决

    POstMan安装地址 安装完 , Postman 设置Header的Referer不生效 , 提示 Restricted Header (use postman interceptor) 时 , 要 ...

  8. LockSupport

    LockSupport是高级线程同步类的基础,用来block和释放线程.这里要区别notify和wait的点在于这里可以先unpark,再park.(有点类似于unpark等于-1,park等于+1. ...

  9. ORA-01102 报错解决方法

    Problem Explanation:  ====================   A database is started in EXCLUSIVE mode by default.  Th ...

  10. Leetcode: Surrounded regions

    Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...