成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

怎么基于Java編寫一個CLI工具?

開發 前端
總的來說,Apache Commons CLI是一個非常強大的工具,可以幫助你輕松地處理命令行參數。無論你的應用程序需要處理多少個參數,或者這些參數的類型是什么, Commons CLI都可以提供幫助。

CLI

CLI,全稱為命令行界面(Command Line Interface),是一種用于通過鍵盤輸入指令與操作系統進行交互的軟件機制。這種界面是在圖形用戶界面得到普及之前使用最為廣泛的用戶界面,并且, 即使在當前圖形用戶界面廣泛使用的環境下,CLI仍然有其獨特的優勢和廣泛的應用。

對于CLI,它的一個重要特性就是效率。用戶可以在一條文本命令中對多個文件執行操作,而不需要在各個窗口之間切換,節省了大量時間。此外,如果你已經熟悉了這些命令,那么你可以非常快速地瀏覽系統并與之交互。

構建CLI的工具很多,今天主要基于Java語言來實現,其中Apache Commons CLI框架提供了這樣的便利。今天結合之前學習的graalVM提供的native-image工具,來生成一個exe類型的可執行文件,由于graalVM的跨平臺性,我們還能生成各個平臺的CLI命令來輔助完成更多的工作。

Apache Commons CLI是一個用于編寫命令行界面的Java庫。它提供了一個靈活的框架,可以很容易地定義和解析命令行參數。這個庫的主要優點是它可以處理各種類型的參數,包括選項、位置參數、可選參數等。

構成

下面以native-image為例,通過在終端輸入native-image --help可以看到以下信息

_> native-image --help

GraalVM Native Image (https://www.graalvm.org/native-image/)

This tool can ahead-of-time compile Java code to native executables.

Usage: native-image [options] class [imagename] [options]
           (to build an image for a class)
   or  native-image [options] -jar jarfile [imagename] [options]
           (to build an image for a jar file)
   or  native-image [options] -m <module>[/<mainclass>] [options]
       native-image [options] --module <module>[/<mainclass>] [options]
           (to build an image for a module)

where options include:

    @argument files       one or more argument files containing options
    -cp <class search path of directories and zip/jar files>
    -classpath <class search path of directories and zip/jar files>
    --class-path <class search path of directories and zip/jar files>
                          A ; separated list of directories, JAR archives,
                          and ZIP archives to search for class files.
    -p <module path>
    --module-path <module path>...
                          A ; separated list of directories, each directory
                          is a directory of modules.

一個合格的CLI基本都會提供help選項來展示,這個CLI的語法、選項以及功能描述。從上面的輸出可以看到help主要包括:

  1. 介紹:主要對命令的功能的描述,包括官網、版本以及一些內在系數等
  2. 用法:包括命令語法格式、配置項、參數等信息
  3. 參數說明:具體配置項參數的說明,以及具體的功能描述

Common-CLI

  • 定義階段:在Java代碼中定義Option參數,定義參數、是否需要輸入值、簡單的描述等
  • 解析階段:應用程序傳入參數后,CLI進行解析
  • 詢問階段:通過查詢CommandLine詢問進入到哪個程序分支中

定義階段

主要是借助Option類提供的API來構建各種選項以及參數信息,下面是對應API的描述:

返回值

方法名

說明

Options

addOption(Option opt)

添加一個選項實例

Options

addOption(String opt, boolean hasArg, String description)

添加一個只包含短名稱的選項

Options

addOption(String opt, String description)

添加一個只包含短名稱的選項

Options

addOption(String opt, String longOpt, boolean hasArg, String description)

添加一個包含短名稱和長名稱的選項

Options

addOptionGroup(OptionGroup group)

添加一個選項組

List

getMatchingOptions(String opt)

獲得匹配選項的長名稱集合

Option

getOption(String opt)

通過長名稱或短名稱獲得選項

OptionGroup

getOptionGroup(Option opt)

獲得選項所在的選項組

Collection

getOptions()

獲得一個只讀的選項集合

List

getRequiredOptions()

獲得必須的選項集合

boolean

hasLongOption(String opt)

判斷是否存在選項

boolean

hasOption(String opt)

判斷是否存在選項

boolean

hasShortOption(String opt)

判斷是否存在選項

解析階段

主要對輸入參數的解析,也就是main方法的參數,默認提供下面3中語法解析的支持:

  • DefaultParser:提供了基礎的解析功能,能解析簡單的命令行參數。(比如:java -Djava.awt.headless=true -Djava.net.useSystemProxies=true Foo)
  • PosixParser:提供了解析POSIX形式參數的功能。(比如:tar -zxvf foo.tar.gz)
  • GnuParser:提供了解析長參數及Java命令中參數的功能。(比如:du --human-readable --max-depth=1)

詢問階段

基于上一步解析后,會將參數解析成CommandLine,結合Option中的配置,需要我們完成各種配置、參數匹配后的業務處理流程,類型下面這樣:

if( commandLine.hasOption("help") ){
          helper.printHelp("calendar [options] \n\nwhere options include:", null, options, null, false);
          return;
      }

      if( commandLine.hasOption("version") ){
          printResult("1.0.0");
          return;
      }

解析的過程有時候會比較些復雜,示例中是針對單一選項的分支,當多個選項混合使用時,比如tar -zxvf xxx.tar.gz這樣的,當然前提是我們定義的CLI支持這種風格。

示例

下面通過一個簡單的示例看下如何構建一個CLI的工具:該示例的作用是按指定格式輸出當前日期:

clendar -o yyyy-MM-dd
  • 定義配置項
private static Options initOptions() {
        Options options = new Options();

        options.addOption(Option.builder("H")
                .longOpt("help")
                .desc("show help information").build());

        options.addOption(Option.builder("V")
                .longOpt("version")
                .desc("show help information").build());

        options.addOption(Option.builder("O")
                .longOpt("out")
                .hasArg(true)
                .argName("fmt") // 只是定義
                .required(false)
                .desc("configure the date output format").build());

        return options;
    }
  • 解析參數
private static CommandLine parseArguments(Options options, String[] args){
        CommandLineParser parser = new DefaultParser();

        try {
            return parser.parse(options, args);
        } catch (ParseException e) {
            System.err.println(e.getMessage());
        }
        return null;
    }
  • 詢問階段
private static void handleCommand(Options options, CommandLine commandLine) {
        if(ArrayUtils.isEmpty(commandLine.getOptions()) ){
            printResult("Please specify options for calendar building or use --help for more info.");
            return;
        }

        if( commandLine.hasOption("help") ){
            helper.printHelp("calendar [options] \n\nwhere options include:", null, options, null, false);
            return;
        }

        if( commandLine.hasOption("version") ){
            printResult("1.0.0");
            return;
        }

        if( commandLine.hasOption("out") ){
            String fmt = commandLine.getOptionValue("out");
            if(StringUtils.isEmpty(fmt)){
                fmt = "yyyy-MM-dd HH:mm:ss";
            }
            printResult(DateFormatUtils.format(new Date(), fmt));
            return;
        }

        // calendar: 'x' is not a git command. See 'calendar --help'.
        helper.printHelp(String.format("calendar: '%s' is not a calendar command. See 'calendar --help'.", Arrays.toString(commandLine.getArgs())), options, false);
    }

定義程序入口:

public static void main(String[] args) {
        // 定義階段
        Options options = initOptions();
        // 解析階段
        CommandLine commandLine = parseArguments(options, args);
        // 詢問階段
        handleCommand(options, commandLine);
    }

打包

這里我們引入maven-assembly-plugin插件,主要幫助在打包時將依賴包一并寫入jar文件,同時將入口文件定義到manifest:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>3.3.0</version>
    <executions>
        <execution>
            <id>package-jar-with-dependencies</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>${main-class}</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <!-- bin,jar-with-dependencies,src,project -->
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
        </execution>
    </executions>
</plugin>

可以直接通過maven插件或者下的命令,將上面的代碼打包成jar文件

mvn clean package

測試jar

如果安裝上面的配置,最終會在項目target目錄輸出一個以jar-with-dependencies為后綴的jar文件,通過下面的命令可以測試cli命令

java -jar ./target/calendar-jar-with-dependencies.jar -h

這樣的CLI可不是我們想要的,一來需要依賴JRE的運行環境,同時調用極其不方便。

生成exe

如果你看過之前的文章,關于GraalVM的使用,按照文檔下載并配置好運行環境后,可以通過下面的命令對上一步的jar文件進一步處理

native-image -jar [jar] -o [name]

native-image -jar ./target/calendar-jar-with-dependencies.jar -o calendar

通過上面的命令會生成一個calendar.exe文件,這樣將其加入到環境變量后,則可以在windows平臺終端上使用了

對于不喜歡直接使用命令的,當然這里也可以使用插件exec-maven-plugin,在maven生命周期package階段,自動執行上面的命令,這樣整個過程只需要執行mvn clean package即可

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>3.1.0</version>
    <executions>
        <execution>
            <id>native-image-app</id>
            <phase>package</phase>
            <goals>
                <goal>exec</goal>
            </goals>
            <configuration>
                <environmentVariables>
                </environmentVariables>
                <!-- native-image -jar ./target/tool-jar-with-dependencies.jar -o tool -->
                <executable>native-image</executable>
                <arguments>
                    <argument>-jar</argument>
                    <argument>${project.basedir}/target/${project.build.finalName}-jar-with-dependencies.jar</argument>
                    <argument>-o</argument>
                    <argument>${project.build.finalName}</argument>
                </arguments>
            </configuration>
        </execution>
    </executions>
</plugin>

測試exe

在終端執行下面的命令接口看到預期的結果:

calendar.exe -O yyyy-MM-dd

總結

總的來說,Apache Commons CLI是一個非常強大的工具,可以幫助你輕松地處理命令行參數。無論你的應用程序需要處理多少個參數,或者這些參數的類型是什么, Commons CLI都可以提供幫助。

責任編輯:武曉燕 來源: Java技術指北
相關推薦

2024-02-26 10:22:53

2021-11-15 11:03:09

接口壓測工具

2009-07-07 11:38:54

jsp oracle

2025-01-08 11:00:00

AIshell開源

2017-05-03 13:37:05

Linuxweb性能監測

2015-09-01 14:09:22

2024-04-16 08:09:36

JavapulsarAPI

2024-06-10 23:07:05

2023-04-08 10:04:45

2025-04-09 03:00:00

2023-12-12 08:08:17

插件PRPulsar

2015-08-19 09:29:35

Git協議編寫

2016-01-06 09:57:23

編寫PHP擴展

2013-09-10 09:54:50

2021-04-13 06:35:13

Elixir語言編程語言軟件開發

2021-04-07 13:38:27

Django項目視圖

2020-10-12 10:58:15

IDEA插件監聽

2022-05-23 08:05:14

benchstat工具Go

2017-09-07 19:21:20

Java語言Iodine

2022-09-30 09:33:55

ViteCLI
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 亚洲欧美精品 | 一区二区三区欧美大片 | 精品久久久久久久久久久久久 | 日韩a级片 | 免费国产视频在线观看 | 日韩欧美三级电影 | 欧美激情综合 | 亚洲欧美一区二区三区视频 | a在线观看 | 日韩精品一区二区三区视频播放 | 日本精品视频在线观看 | 亚洲 中文 欧美 日韩 在线观看 | 日韩一区欧美一区 | 国产成人免费视频网站高清观看视频 | 日本成人在线观看网站 | 欧美精品一区在线发布 | 亚洲精品一区中文字幕乱码 | 国产一级片av | 狠狠色网| 亚洲国产69 | 欧美色综合 | 中国黄色在线视频 | 91精品久久久久久久久中文字幕 | 欧美成人一区二区三区 | 亚洲一区二区三区在线视频 | 91精品国产一区二区三区蜜臀 | 久久久久亚洲精品 | 久久免费精品 | 午夜精品久久久久99蜜 | 亚洲乱码一区二区 | 天天操夜夜骑 | 美女一区 | 亚洲综合五月天婷婷 | 国精产品一区二区三区 | 欧美精品福利 | 国产视频一区在线 | 9999视频| 91精品国产一区二区三区 | 成人久久久 | 色五月激情五月 | 久久99这里只有精品 |