Apache Commons CLI提供了解析命令行参数的API。它也可以在命令行打印详细的参数信息。官网教程:http://commons.apache.org/proper/commons-cli/usage.html,Commons CLI的Javadoc:http://commons.apache.org/proper/commons-cli/javadocs/api-release/index.html。
Commons CLI提供了一下不同类型的参数形式
addOption(Option opt) addOption(String opt, boolean hasArg, String description) addOption(String opt, String description) addOption(String opt, String longOpt, boolean hasArg, String description)
Option filesOption = OptionBuilder.withArgName("args") .withLongOpt("files") .hasArgs(2) .withValueSeparator(',') .withDescription("file names") .create("f");
CommandLineParser parser = new PosixParser(); CommandLine cli = parser.parse(options, args);
if(cli.hasOption("h")){ HelpFormatter hf = new HelpFormatter(); hf.printHelp("Options", options); }
<dependency> <groupId>commons-cli</groupId> <artifactId>commons-cli</artifactId> <version>${cli-version}</version> </dependency>
BasicParser和DefaultParser只能解析基础的命令行参数,如:
public static void basicParseCLI(String[] args){ Options options = new Options(); options.addOption("h", "help", false, "print options' information"); options.addOption("d", "database", true, "name of a database"); options.addOption("t", true, "name of a table"); Option filesOption = OptionBuilder.withArgName("args") .withLongOpt("files") .hasArgs() .withDescription("file names") .create("f"); options.addOption(filesOption); // CommandLineParser parser = new DefaultParser(); CommandLineParser parser = new BasicParser(); try { CommandLine cli = parser.parse(options, args); if(cli.hasOption("h")){ HelpFormatter hf = new HelpFormatter(); hf.printHelp("Options", options); } else { String database = cli.getOptionValue("d"); System.out.println("database: " + database); String table = cli.getOptionValue("t"); System.out.println("table: " + table); String[] files = cli.getOptionValues("f"); System.out.println("files: " + Arrays.asList(files)); } } catch (Exception e){ e.printStackTrace(); } }
-d database -t table -files file1 file2 -database database -t table -files file1 file2
database: database table: table files: [file1, file2]
PosixParser可以解析POSIX形式的命令行参数和Java命令中参数形式,如:
public static void posixParseCLI(String[] args){ Options options = new Options(); options.addOption("h", "help", false, "print options' information"); options.addOption("d", "database", true, "name of a database"); options.addOption("t", true, "name of a table"); Option filesOption = OptionBuilder.withArgName("args") .withLongOpt("files") .hasArgs() .withDescription("file names") .create("f"); options.addOption(filesOption); // hasArgs()指定后跟参数值得个数 Option property = OptionBuilder.withArgName("property=name") .hasArgs() .withValueSeparator() .withDescription("use value for a property") .create("D"); options.addOption(property); CommandLineParser parser = new PosixParser(); try { CommandLine cli = parser.parse(options, args); if(cli.hasOption("h")){ HelpFormatter hf = new HelpFormatter(); hf.printHelp("Options", options); } else { String database = cli.getOptionValue("d"); System.out.println("database: " + database); String table = cli.getOptionValue("t"); System.out.println("table: " + table); String[] files = cli.getOptionValues("f"); System.out.println("files: " + Arrays.asList(files)); Properties properties = cli.getOptionProperties("D"); String ext = properties.getProperty("ext"); System.out.println("property ext = " + ext); } } catch (Exception e){ e.printStackTrace(); } }
-d database -ttable -files file1 file2 -Dext=java
database: database table: table files: [file1, file2] property ext = java
GnuParser可以解析长参数及Java命令中参数,如:opt=value。
public static void gnuParseCLI(String[] args){ Options options = new Options(); options.addOption("h", "help", false, "print options' information"); options.addOption("d", "database", true, "name of a database"); options.addOption("t", true, "name of a table"); // withValueSeparator(char sep)指定参数值之间的分隔符 Option filesOption = OptionBuilder.withArgName("args") .withLongOpt("files") .hasArgs() .withValueSeparator(',') .withDescription("file names") .create("f"); options.addOption(filesOption); Option property = OptionBuilder.withArgName("property=name") .hasArgs() .withValueSeparator() .withDescription("use value for a property") .create("D"); options.addOption(property); CommandLineParser parser = new GnuParser(); try { CommandLine cli = parser.parse(options, args); if(cli.hasOption("h")){ HelpFormatter hf = new HelpFormatter(); hf.printHelp("Options", options); } else { String database = cli.getOptionValue("database"); System.out.println("database: " + database); String table = cli.getOptionValue("t"); System.out.println("table: " + table); String[] files = cli.getOptionValues("f"); System.out.println("files: " + Arrays.asList(files)); Properties properties = cli.getOptionProperties("D"); String ext = properties.getProperty("ext"); String dir = properties.getProperty("dir"); System.out.println("property ext: " + ext + "\tdir:" + dir); } } catch (Exception e){ e.printStackTrace(); } }
--database=database -t table --files=file1,file2 -Dext=java -Ddir=dir
database: database table: table files: [file1, file2] property ext: java dir:dir