📜  Apache Commons CLI-Posix Parser

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


Posix解析器用于解析Posix,就像传递的参数一样。现在已弃用,并由DefaultParser代替。

CLITester.java

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;

public class CLITester {
   public static void main(String[] args) throws ParseException {
   
      //Create posix like options
      Options posixOptions = new Options();
      posixOptions.addOption("D", false, "Display");
      posixOptions.addOption("A", false, "Act");

      CommandLineParser posixParser = new PosixParser();

      CommandLine cmd = posixParser.parse(posixOptions, args);

      if( cmd.hasOption("D") ) {
         System.out.println("D option was used.");
      }

      if( cmd.hasOption("A") ) {
         System.out.println("A option was used.");
      }
   } 
}

输出

在传递-D -A作为选项的同时运行文件,并查看结果。

java CLITester -D -A
D option was used.
A option was used.

在传递–D作为选项的同时运行文件并查看结果。

java CLITester --D
D option was used.