帮助示例( Help Example)

优质
小牛编辑
140浏览
2023-12-01

Apache Commons CLI提供HelpFormatter类来打印与命令行参数相关的帮助。 查看示例。

例子 (Example)

CLITester.java

import java.io.PrintWriter;
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();
      final PrintWriter writer = new PrintWriter(System.out);
      formatter.printUsage(writer,80,"CLITester", options);
      writer.flush();    
   } 
}

输出 (Output)

运行该文件并查看结果。

java CLITester
usage: CLITester [-g] [-n <arg>] [-p]