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. 记一个视频播放器插件 video.js

    最近在看扣丁学堂上面的一些视频, 突然对他用的视频播放器有点兴趣, 他也是采用的 ts切片播放, 如果使用传统的video标签是无法实现的 他使用的插件叫做 video.js 官网地址 官网提供的播放 ...

  2. “更新时间”字段的:ON UPDATE CURRENT_TIMESTAMP 含义

    "更新时间"字段的:ON UPDATE CURRENT_TIMESTAMP 含义: 表示在数据库数据有更新的时候UPDATE_TIME的时间会自动更新(如果数据库数据值没有变化的话 ...

  3. vue-cli 创建的项目,在 nginx 上配置启用浏览器缓存

    nginx 配置,关键参数: server { listen 80; server_name xxx.xxx.xxx.xxx; charset utf-8; root /home/xxx/dist/; ...

  4. bzoj 2739 最远点——分治处理决策单调性

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2739 分治处理决策单调性的思想就是先找到一个询问,枚举所有可能的转移找到它的决策点,那么这个 ...

  5. Linux lsattr命令详解

    Linux lsattr命令 Linux lsattr命令用于显示文件属性. 用chattr执行改变文件或目录的属性,可执行lsattr指令查询其属性 用法: lsattr [-adlRvV][文件或 ...

  6. ajaxFileupload 多文件上传

    ajaxFileupload 多文件上传 修改前的代码: var oldElement = jQuery('#' + fileElementId); var newElement = jQuery(o ...

  7. AXI_LITE源码学习笔记

    AXI_LITE源码学习笔记 1. axi_awready信号的产生 准备接收写地址信号 // Implement axi_awready generation // axi_awready is a ...

  8. 阅读 RAM-Based Shift Register(ALTSHIFT_TAPS) IP Core User Guide

    阅读 RAM-Based Shift Register(ALTSHIFT_TAPS) IP Core User Guide 说明:本文档自带测试工程: DE_ALTSHIFT_TAPS.zip 1.支 ...

  9. imp导入备份时报:无法转换为环境字符集句柄

    imp导入备份时报:无法转换为环境字符集句柄 ... 在imp 还原数据库的时候出现问题如下: 这个问题是 你用 expdp导出的 却用客户端的 imp 导入:换成impdp导入即可. 当用impdp ...

  10. Mysql 基准测试mysqlslap,sysbench

     基准测试基本步骤 设计尽可能简单,明确测试方式哪一种,使用什么样的数据 例如:反映实际情况,用生产环境的数据的备份,可以通过日志,进行访问量的回放 如果测试性能,可以用测试工具生成数据. 执行一次和 ...