Commons Cli 简明教程
Apache Commons CLI - GNU Parser
GNU 解析器用于解析类似 gnu 的传递参数。现在它已被弃用,并替换为 DefaultParser。
Example
CLITester.java
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
public class CLITester {
public static void main(String[] args) throws ParseException {
//Create GNU like options
Options gnuOptions = new Options();
gnuOptions.addOption("p", "print", false, "Print")
.addOption("g", "gui", false, "GUI")
.addOption("n", true, "Scale");
CommandLineParser gnuParser = new GnuParser();
CommandLine cmd = gnuParser.parse(gnuOptions, args);
if( cmd.hasOption("p") ) {
System.out.println("p option was used.");
}
if( cmd.hasOption("g") ) {
System.out.println("g option was used.");
}
if( cmd.hasOption("n") ) {
System.out.println("Value passed: " + cmd.getOptionValue("n"));
}
}
}