📜  Apache Commons CLI-使用示例

📅  最后修改于: 2020-11-18 08:02:20             🧑  作者: Mango


Apache Commons CLI提供了HelpFormatter类来打印命令行参数的用法指南。参见示例。

CLITester.java

import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

public class CLITester {
   public static void main(String[] args) throws ParseException {

      Options options = new Options();
      options.addOption("p", "print", false, "Send print request to printer.")
         .addOption("g", "gui", false, "Show GUI Application")
         .addOption("n", true, "No. of copies to print");

      HelpFormatter formatter = new HelpFormatter();
      formatter.printHelp("CLITester", options);
   } 
}

输出

运行文件并查看结果。

java CLITester
usage: CLITester
 -g,--gui     Show GUI Application
 -n      No. of copies to print
 -p,--print   Send print request to printer.