Apache Commons CLI 簡介
Apache Commons CLI 是 Apache 下面的一個解析命令行輸入的工具包,該工具包還提供了自動生成輸出幫助文檔的功能。
Apache Commons CLI 支持多種輸入參數格式,主要支持的格式有以下幾種:
-
POSIX(Portable Operating System Interface of Unix)中的參數形式,例如 tar -zxvf foo.tar.gz
-
GNU 中的長參數形式,例如 du --human-readable --max-depth=1
-
Java 命令中的參數形式,例如 java -Djava.net.useSystemProxies=true Foo
-
短杠參數帶參數值的參數形式,例如 gcc -O2 foo.c
-
長杠參數不帶參數值的形式,例如 ant – projecthelp
CLI 命令代碼實現命令行程序處理流程相對比較簡單,主要流程為設定命令行參數 -> 解析輸入參數 -> 使用輸入的數據進行邏輯處理
CLI 定義階段
每一條命令行都必須定義一組參數,它們被用來定義應用程序的接口。Apache Commons CLI 使用 Options 這個類來定義和設置參數,它是所有 Option 實例的容器。在 CLI 中,目前有兩種方式來創建 Options,一種是通過構造函數,這是最普通也是最為大家所熟知的一種方式;另外一種方法是通過 Options 中定義的工廠方式來實現。
CLI 定義階段的目標結果就是創建 Options 實例。
// 創建 Options 對象 Options options = new Options(); // 添加 -h 參數 options.addOption("h", false, "Lists short help"); // 添加 -t 參數 options.addOption("t", true, "Sets the HTTP communication protocol for CIM connection");
其中 addOption() 方法有三個參數,第一個參數設定這個 option 的單字符名字,第二個參數指明這個 option 是否需要輸入數值,第三個參數是對這個 option 的簡要描述。在這個代碼片段中,第一個參數只是列出幫助文件,不需要用戶輸入任何值,而第二個參數則是需要用戶輸入 HTTP 的通信協議,所以這兩個 option 的第二個參數分別為 false 和 true
CLI 解析階段
在解析階段中,通過命令行傳入應用程序的文本來進行處理。處理過程將根據在解析器的實現過程中定義的規則來進行。在 CommandLineParser 類中定義的 parse 方法將用 CLI 定義階段中產生的 Options 實例和一組字符串作為輸入,並返回解析后生成的 CommandLine。
CLI 解析階段的目標結果就是創建 CommandLine 實例。
CommandLineParser parser = new PosixParser(); CommandLine cmd = parser.parse(options, args); if(cmd.hasOption("h")) { // 這里顯示簡短的幫助信息
}
CLI 詢問階段
在詢問階段中,應用程序通過查詢 CommandLine,並通過其中的布爾參數和提供給應用程序的參數值來決定需要執行哪些程序分支。這個階段在用戶的代碼中實現,CommandLine 中的訪問方法為用戶代碼提供了 CLI 的詢問能力。
CLI 詢問階段的目標結果就是將所有通過命令行以及處理參數過程中得到的文本信息傳遞給用戶的代碼。
commandLine = parser.parse(options, args); if (commandLine.hasOption('h')) { //打印使用幫助
hf.printHelp("testApp", options, true); }
完整的代碼示例:
import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLineParser; import org.apache.commons.cli.HelpFormatter; import org.apache.commons.cli.Option; import org.apache.commons.cli.Options; import org.apache.commons.cli.ParseException; import org.apache.commons.cli.PosixParser; /** * DateTime: 2015年1月1日 下午5:07:31 * */
public class Test { public static void main(String[] args) { String[] arg = { "-h", "-c", "config.xml" }; testOptions(arg); } public static void testOptions(String[] args) { Options options = new Options(); Option opt = new Option("h", "help", false, "Print help"); opt.setRequired(false); options.addOption(opt); opt = new Option("c", "configFile", true, "Name server config properties file"); opt.setRequired(false); options.addOption(opt); opt = new Option("p", "printConfigItem", false, "Print all config item"); opt.setRequired(false); options.addOption(opt); HelpFormatter hf = new HelpFormatter(); hf.setWidth(110); CommandLine commandLine = null; CommandLineParser parser = new PosixParser(); try { commandLine = parser.parse(options, args); if (commandLine.hasOption('h')) { // 打印使用幫助
hf.printHelp("testApp", options, true); } // 打印opts的名稱和值
System.out.println("--------------------------------------"); Option[] opts = commandLine.getOptions(); if (opts != null) { for (Option opt1 : opts) { String name = opt1.getLongOpt(); String value = commandLine.getOptionValue(name); System.out.println(name + "=>" + value); } } } catch (ParseException e) { hf.printHelp("testApp", options, true); } } }