日韩成人免费在线_国产成人一二_精品国产免费人成电影在线观..._日本一区二区三区久久久久久久久不

當前位置:首頁 > 科技  > 軟件

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

來源: 責編: 時間:2023-12-11 09:26:18 214觀看
導讀CLICLI,全稱為命令行界面(Command Line Interface),是一種用于通過鍵盤輸入指令與操作系統(tǒng)進行交互的軟件機制。這種界面是在圖形用戶界面得到普及之前使用最為廣泛的用戶界面,并且, 即使在當前圖形用戶界面廣泛使用的環(huán)境

CLI

CLI,全稱為命令行界面(Command Line Interface),是一種用于通過鍵盤輸入指令與操作系統(tǒng)進行交互的軟件機制。這種界面是在圖形用戶界面得到普及之前使用最為廣泛的用戶界面,并且, 即使在當前圖形用戶界面廣泛使用的環(huán)境下,CLI仍然有其獨特的優(yōu)勢和廣泛的應用。4mn28資訊網(wǎng)——每日最新資訊28at.com

對于CLI,它的一個重要特性就是效率。用戶可以在一條文本命令中對多個文件執(zhí)行操作,而不需要在各個窗口之間切換,節(jié)省了大量時間。此外,如果你已經(jīng)熟悉了這些命令,那么你可以非常快速地瀏覽系統(tǒng)并與之交互。4mn28資訊網(wǎng)——每日最新資訊28at.com

構建CLI的工具很多,今天主要基于Java語言來實現(xiàn),其中Apache Commons CLI框架提供了這樣的便利。今天結合之前學習的graalVM提供的native-image工具,來生成一個exe類型的可執(zhí)行文件,由于graalVM的跨平臺性,我們還能生成各個平臺的CLI命令來輔助完成更多的工作。4mn28資訊網(wǎng)——每日最新資訊28at.com

Apache Commons CLI是一個用于編寫命令行界面的Java庫。它提供了一個靈活的框架,可以很容易地定義和解析命令行參數(shù)。這個庫的主要優(yōu)點是它可以處理各種類型的參數(shù),包括選項、位置參數(shù)、可選參數(shù)等。4mn28資訊網(wǎng)——每日最新資訊28at.com

構成

下面以native-image為例,通過在終端輸入native-image --help可以看到以下信息4mn28資訊網(wǎng)——每日最新資訊28at.com

_> native-image --helpGraalVM 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主要包括:4mn28資訊網(wǎng)——每日最新資訊28at.com

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

Common-CLI

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

定義階段

主要是借助Option類提供的API來構建各種選項以及參數(shù)信息,下面是對應API的描述:4mn28資訊網(wǎng)——每日最新資訊28at.com

返回值4mn28資訊網(wǎng)——每日最新資訊28at.com

方法名4mn28資訊網(wǎng)——每日最新資訊28at.com

說明4mn28資訊網(wǎng)——每日最新資訊28at.com

Options4mn28資訊網(wǎng)——每日最新資訊28at.com

addOption(Option opt)4mn28資訊網(wǎng)——每日最新資訊28at.com

添加一個選項實例4mn28資訊網(wǎng)——每日最新資訊28at.com

Options4mn28資訊網(wǎng)——每日最新資訊28at.com

addOption(String opt, boolean hasArg, String description)4mn28資訊網(wǎng)——每日最新資訊28at.com

添加一個只包含短名稱的選項4mn28資訊網(wǎng)——每日最新資訊28at.com

Options4mn28資訊網(wǎng)——每日最新資訊28at.com

addOption(String opt, String description)4mn28資訊網(wǎng)——每日最新資訊28at.com

添加一個只包含短名稱的選項4mn28資訊網(wǎng)——每日最新資訊28at.com

Options4mn28資訊網(wǎng)——每日最新資訊28at.com

addOption(String opt, String longOpt, boolean hasArg, String description)4mn28資訊網(wǎng)——每日最新資訊28at.com

添加一個包含短名稱和長名稱的選項4mn28資訊網(wǎng)——每日最新資訊28at.com

Options4mn28資訊網(wǎng)——每日最新資訊28at.com

addOptionGroup(OptionGroup group)4mn28資訊網(wǎng)——每日最新資訊28at.com

添加一個選項組4mn28資訊網(wǎng)——每日最新資訊28at.com

List4mn28資訊網(wǎng)——每日最新資訊28at.com

getMatchingOptions(String opt)4mn28資訊網(wǎng)——每日最新資訊28at.com

獲得匹配選項的長名稱集合4mn28資訊網(wǎng)——每日最新資訊28at.com

Option4mn28資訊網(wǎng)——每日最新資訊28at.com

getOption(String opt)4mn28資訊網(wǎng)——每日最新資訊28at.com

通過長名稱或短名稱獲得選項4mn28資訊網(wǎng)——每日最新資訊28at.com

OptionGroup4mn28資訊網(wǎng)——每日最新資訊28at.com

getOptionGroup(Option opt)4mn28資訊網(wǎng)——每日最新資訊28at.com

獲得選項所在的選項組4mn28資訊網(wǎng)——每日最新資訊28at.com

Collection4mn28資訊網(wǎng)——每日最新資訊28at.com

getOptions()4mn28資訊網(wǎng)——每日最新資訊28at.com

獲得一個只讀的選項集合4mn28資訊網(wǎng)——每日最新資訊28at.com

List4mn28資訊網(wǎng)——每日最新資訊28at.com

getRequiredOptions()4mn28資訊網(wǎng)——每日最新資訊28at.com

獲得必須的選項集合4mn28資訊網(wǎng)——每日最新資訊28at.com

boolean4mn28資訊網(wǎng)——每日最新資訊28at.com

hasLongOption(String opt)4mn28資訊網(wǎng)——每日最新資訊28at.com

判斷是否存在選項4mn28資訊網(wǎng)——每日最新資訊28at.com

boolean4mn28資訊網(wǎng)——每日最新資訊28at.com

hasOption(String opt)4mn28資訊網(wǎng)——每日最新資訊28at.com

判斷是否存在選項4mn28資訊網(wǎng)——每日最新資訊28at.com

boolean4mn28資訊網(wǎng)——每日最新資訊28at.com

hasShortOption(String opt)4mn28資訊網(wǎng)——每日最新資訊28at.com

判斷是否存在選項4mn28資訊網(wǎng)——每日最新資訊28at.com

解析階段

主要對輸入?yún)?shù)的解析,也就是main方法的參數(shù),默認提供下面3中語法解析的支持:4mn28資訊網(wǎng)——每日最新資訊28at.com

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

詢問階段

基于上一步解析后,會將參數(shù)解析成CommandLine,結合Option中的配置,需要我們完成各種配置、參數(shù)匹配后的業(yè)務處理流程,類型下面這樣:4mn28資訊網(wǎng)——每日最新資訊28at.com

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支持這種風格。4mn28資訊網(wǎng)——每日最新資訊28at.com

示例

下面通過一個簡單的示例看下如何構建一個CLI的工具:該示例的作用是按指定格式輸出當前日期:4mn28資訊網(wǎng)——每日最新資訊28at.com

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;    }
  • 解析參數(shù)
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);    }

定義程序入口:4mn28資訊網(wǎng)——每日最新資訊28at.com

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

打包

這里我們引入maven-assembly-plugin插件,主要幫助在打包時將依賴包一并寫入jar文件,同時將入口文件定義到manifest:4mn28資訊網(wǎng)——每日最新資訊28at.com

<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文件4mn28資訊網(wǎng)——每日最新資訊28at.com

mvn clean package

測試jar

如果安裝上面的配置,最終會在項目target目錄輸出一個以jar-with-dependencies為后綴的jar文件,通過下面的命令可以測試cli命令4mn28資訊網(wǎng)——每日最新資訊28at.com

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

這樣的CLI可不是我們想要的,一來需要依賴JRE的運行環(huán)境,同時調用極其不方便。4mn28資訊網(wǎng)——每日最新資訊28at.com

生成exe

如果你看過之前的文章,關于GraalVM的使用,按照文檔下載并配置好運行環(huán)境后,可以通過下面的命令對上一步的jar文件進一步處理4mn28資訊網(wǎng)——每日最新資訊28at.com

native-image -jar [jar] -o [name]4mn28資訊網(wǎng)——每日最新資訊28at.com

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

通過上面的命令會生成一個calendar.exe文件,這樣將其加入到環(huán)境變量后,則可以在windows平臺終端上使用了4mn28資訊網(wǎng)——每日最新資訊28at.com

對于不喜歡直接使用命令的,當然這里也可以使用插件exec-maven-plugin,在maven生命周期package階段,自動執(zhí)行上面的命令,這樣整個過程只需要執(zhí)行mvn clean package即可4mn28資訊網(wǎng)——每日最新資訊28at.com

<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

在終端執(zhí)行下面的命令接口看到預期的結果:4mn28資訊網(wǎng)——每日最新資訊28at.com

calendar.exe -O yyyy-MM-dd

總結

總的來說,Apache Commons CLI是一個非常強大的工具,可以幫助你輕松地處理命令行參數(shù)。無論你的應用程序需要處理多少個參數(shù),或者這些參數(shù)的類型是什么, Commons CLI都可以提供幫助。4mn28資訊網(wǎng)——每日最新資訊28at.com

本文鏈接:http://www.www897cc.com/showinfo-26-41684-0.html怎么基于Java編寫一個CLI工具?

聲明:本網(wǎng)頁內容旨在傳播知識,若有侵權等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: Go語言 字符串拼接方式與性能比較,分析過沒?

下一篇: Spring6提供的四種遠程接口調用神器!你知道那種?

標簽:
  • 熱門焦點
Top 主站蜘蛛池模板: 柞水县| 隆林| 双城市| 新和县| 平乐县| 武强县| 沛县| 甘洛县| 蒙城县| 全椒县| 勐海县| 大新县| 石台县| 十堰市| 花莲县| 慈溪市| 兴山县| 会同县| 潞西市| 宣武区| 伊金霍洛旗| 和顺县| 苍南县| 石林| 吕梁市| 池州市| 军事| 隆化县| 陵川县| 温宿县| 富顺县| 菏泽市| 乐安县| 临海市| 津南区| 隆安县| 承德市| 长寿区| 仙游县| 鞍山市| 信宜市|