1. Options

private Options options = new Options();

2. option

(1) way1

launcher.options.addOption("h", "help", false, "show help.");

(2) way2

launcher.options.addOption(optFile);

3. command line parser

CommandLineParser cmdParser = new DefaultParser();

4. exec method

     private void help() {
        // This prints out some help
         HelpFormatter formater = new HelpFormatter();
         formater.printHelp(this.getClass().getSimpleName(), options);
         System.exit(0);
     }

5. test

java --SNAPSHOT.jar;C:\Users\xiaobin\.m2\repository\commons-cli\commons-cli\1.4\commons-cli-1.4.jar my.csdn9.ConsoleLauncher --help

ConsoleLauncher.java

package my.csdn9;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

public class ConsoleLauncher {
    // Step 1: otions
    private Options options = new Options();

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ConsoleLauncher launcher = new ConsoleLauncher();

        // Step 2: add option
        Option optFile = Option.builder( "file" )
                .longOpt( "logfile" )
                 .hasArg(false)
                 .desc(  "use given file for log" )
                 .build();

        // Way I: Direct definition
        launcher.options.addOption("h", "help", false, "show help.");
        // Way II: Use the option class to define
        launcher.options.addOption(optFile);
        // Step 3: cmd parser
        CommandLineParser cmdParser = new DefaultParser();
        try {
            CommandLine cmd = cmdParser.parse(launcher.options, args);
            if (cmd.hasOption("h")) {
                launcher.help();
            }
            if (cmd.hasOption("file")) {
                 System.out.println("----file----");
            }
        } catch (ParseException e) {
            // TODO Auto-generated catch block
             e.printStackTrace();
        }

    }

     // Step 4: exec method
     private void help() {
        // This prints out some help
         HelpFormatter formater = new HelpFormatter();
         formater.printHelp(this.getClass().getSimpleName(), options);
         System.exit(0);
     }

}

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.tdtc</groupId>
  <artifactId>cliDemo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>cliDemo</name>
  <dependencies>
      <dependency>
          <groupId>commons-cli</groupId>
          <artifactId>commons-cli</artifactId>
          <version>1.4</version>
      </dependency>
  </dependencies>
</project>

Reference:

1. java command line interfaces - part 1: apache commons

2. Option.Builder

Apache CLI Demo的更多相关文章

  1. 【java】之 apache commons-codec 与Apache Digest demo实例,支持md5 sha1 base64 hmac urlencode

    使用commons-codec 进行加密的一些操作 package com.jiepu.ApacheDigest; import java.io.FileInputStream; import org ...

  2. C# Apache Thrift Demo

    转载至 https://headsigned.com/posts/csharp-apache-thrift-demo/ This demo application shows how to imple ...

  3. 遇见CUBA CLI

    原文:Meet CLI for CUBA Platform 翻译:CUBA China CUBA-Platform 官网 : https://www.cuba-platform.com CUBA Ch ...

  4. Lucene入门-安装和运行Demo程序

    Lucene版本:7.1 一.下载安装包 https://lucene.apache.org/core/downloads.html 二.安装 把4个必备jar包和路径添加到CLASSPATH \lu ...

  5. How-to: Do Real-Time Log Analytics with Apache Kafka, Cloudera Search, and Hue

    Cloudera recently announced formal support for Apache Kafka. This simple use case illustrates how to ...

  6. atitit.跨语言执行cmd cli api的原理及兼容性设计草案

    atitit.跨语言执行cmd cli api的原理及兼容性设计草案 1. 标准输入,标准输出,标准错误与重新定向1 2. 常见问题2 2.1. 执行bat文件2 2.2. 执行bat文件  /c   ...

  7. Lucene-安装和运行Demo程序

    Lucene是什么 Lucene是一款高性能.可扩展的信息检索工具库.- Lucene In Action Lucene版本:7.1 一.下载安装包 https://lucene.apache.org ...

  8. Apache Poi实现excel解析

    一.说明 1.本文通过使用 poi 工具解析 excel 表格数据,实现导入导出 2.excel目前有两种格式 2003版本的 excel.xls 与 2007版本的 excel.xlsx ,注意两种 ...

  9. Dubbo源码本地运行demo遇到的问题

    从github上拉下来的Dubbo源码,运行Dubbo项目的demo工程,报如下错误(Dubbo版本为2.7.6): Exception in thread "main" java ...

随机推荐

  1. MySQL 出现 Host is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts'

    MySQL 出现 Host is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts' 一大 ...

  2. 20 约束 异常处理 MD5 日志

    三十九, 一.类的约束 1.抛出异常    NotImplementedError 2.抽象方法 含有抽象方法的类是抽象类 抽象类中的方法全是抽象方法的是接口 抽象类不能创建对象 二.异常处理 处理完 ...

  3. day01 计算机的基础知识

    1.编程语言: 人与计算机沟通的表达方式. 2.编程: 程序员用计算机能理解的表达方式(编程语言)把程序员想让计算机实现的功能写到文件里,这些文件称之为程序. 3.计算机硬件组成: 控制器:是计算机的 ...

  4. zookeeper 学习资料

    zookeeper 学习资料 学习资料 网址 Zookeeper 教程(菜鸟教程) https://www.w3cschool.cn/zookeeper/

  5. 打造mac上最好用的Terminal

    出处:https://blog.csdn.net/kebing1011/article/details/46934533?utm_source=blogxgwz0

  6. Xshell配置SSH免密码登录

    思路: 私钥存放于客户端,id_rsa 将客户端公钥存放于要远程控制服务器上:将客户在公钥id_rsa.pub内容追加到 /root/.ssh/authorized_keys 使用密钥认证分3步: 1 ...

  7. 代码问题: 【MatConvNet库编译】

    问题1: 老版本的MatConvNet在编译对cuDNN支持的时候,cuDNN的版本是2或者4比较好,官网有明确的描述. 比如我编译用了cuDNN 6.0的版本,在编译 nnconv_cudnn.cu ...

  8. python基本知识点

    1.基本数据类型 1.1int 字符串转换为数字,比如 a = “123” print(type(a) , a) b = int(a) print(type(b),b) num = “b” v = i ...

  9. 【Zabbix3.0】之入门到精通

    https://www.cnblogs.com/clsn/p/7885990.html 饿了么技术债 http://server.51cto.com/sOS-555999.htm

  10. 【idea】之取消@param注释参数错误提示

    改为