Apache Commons CLI
Posted 熊猫太郎
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Apache Commons CLI相关的知识,希望对你有一定的参考价值。
Apache Commons CLI 简介
Apache Commons CLI 是 Apache 下面的一个解析命令行输入的工具包,该工具包还提供了自动生成输出帮助文档的功能。 类似工具包args4j, TE-Code command line parsing, CLAJR (Command-Line Arguments with Java Reflection), JArgs, JSAP (Java Simple Argument Processor), and 其他。
Apache Commons CLI 支持多种输入参数格式,主要支持的格式有以下几种:
-
POSIX(Portable Operating System Interface of Unix)中的参数形式,例如 tar -zxvf foo.tar.gz
-
GNU 中的长参数形式,例如 du --human-readable --max-depth=1
-
Java 命令中的参数形式,例如 java -Djava.net.useSystemProxies=true Foo
-
短杠参数带参数值的参数形式,例如 gcc -O2 foo.c
-
长杠参数不带参数值的形式,例如 ant – projecthelp
通过示例说一下cli处理posix和gnu的命令行参数,参考链接
import java.io.IOException; import java.io.OutputStream; import java.io.PrintWriter; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLineParser; import org.apache.commons.cli.GnuParser; import org.apache.commons.cli.HelpFormatter; import org.apache.commons.cli.Options; import org.apache.commons.cli.ParseException; import org.apache.commons.cli.PosixParser; /** * Main example demonstrating Apache Commons CLI. Apache Commons CLI and more * details on it are available at http://commons.apache.org/cli/. * * @author Dustin */ public class MainCliExample { private static Options options = new Options(); /** * Apply Apache Commons CLI PosixParser to command-line arguments. * * @param commandLineArguments Command-line arguments to be processed with * Posix-style parser. */ public static void usePosixParser(final String[] commandLineArguments) { final CommandLineParser cmdLinePosixParser = new PosixParser(); final Options posixOptions = constructPosixOptions(); CommandLine commandLine; try { commandLine = cmdLinePosixParser.parse(posixOptions, commandLineArguments); if ( commandLine.hasOption("display") ) { System.out.println("You want a display!"); } } catch (ParseException parseException) // checked exception { System.err.println( "Encountered exception while parsing using PosixParser:\n" + parseException.getMessage() ); } } /** * Apply Apache Commons CLI GnuParser to command-line arguments. * * @param commandLineArguments Command-line arguments to be processed with * Gnu-style parser. */ public static void useGnuParser(final String[] commandLineArguments) { final CommandLineParser cmdLineGnuParser = new GnuParser(); final Options gnuOptions = constructGnuOptions(); CommandLine commandLine; try { commandLine = cmdLineGnuParser.parse(gnuOptions, commandLineArguments); if ( commandLine.hasOption("p") ) { System.out.println("You want to print (p chosen)!"); } if ( commandLine.hasOption("print") ) { System.out.println("You want to print (print chosen)!"); } if ( commandLine.hasOption(‘g‘) ) { System.out.println("You want a GUI!"); } if ( commandLine.hasOption("n") ) { System.out.println( "You selected the number " + commandLine.getOptionValue("n")); } } catch (ParseException parseException) // checked exception { System.err.println( "Encountered exception while parsing using GnuParser:\n" + parseException.getMessage() ); } } /** * Construct and provide Posix-compatible Options. * * @return Options expected from command-line of Posix form. */ public static Options constructPosixOptions() { final Options posixOptions = new Options(); posixOptions.addOption("display", false, "Display the state."); return posixOptions; } /** * Construct and provide GNU-compatible Options. * * @return Options expected from command-line of GNU form. */ public static Options constructGnuOptions() { final Options gnuOptions = new Options(); gnuOptions.addOption("p", "print", false, "Option for printing") .addOption("g", "gui", false, "HMI option") .addOption("n", true, "Number of copies"); return gnuOptions; } /** * Display command-line arguments without processing them in any further way. * * @param commandLineArguments Command-line arguments to be displayed. */ public static void displayProvidedCommandLineArguments( final String[] commandLineArguments, final OutputStream out) { final StringBuffer buffer = new StringBuffer(); for ( final String argument : commandLineArguments ) { buffer.append(argument).append(" "); } try { out.write((buffer.toString() + "\n").getBytes()); } catch (IOException ioEx) { System.err.println( "WARNING: Exception encountered trying to write to OutputStream:\n" + ioEx.getMessage() ); System.out.println(buffer.toString()); } } /** * Display example application header. * * @out OutputStream to which header should be written. */ public static void displayHeader(final OutputStream out) { final String header = "[Apache Commons CLI Example from Dustin‘s Software Development " + "Cogitations and Speculations Blog]\n"; try { out.write(header.getBytes()); } catch (IOException ioEx) { System.out.println(header); } } /** * Write the provided number of blank lines to the provided OutputStream. * * @param numberBlankLines Number of blank lines to write. * @param out OutputStream to which to write the blank lines. */ public static void displayBlankLines( final int numberBlankLines, final OutputStream out) { try { for (int i=0; i<numberBlankLines; ++i) { out.write("\n".getBytes()); } } catch (IOException ioEx) { for (int i=0; i<numberBlankLines; ++i) { System.out.println(); } } } /** * Print usage information to provided OutputStream. * * @param applicationName Name of application to list in usage. * @param options Command-line options to be part of usage. * @param out OutputStream to which to write the usage information. */ public static void printUsage( final String applicationName, final Options options, final OutputStream out) { final PrintWriter writer = new PrintWriter(out); final HelpFormatter usageFormatter = new HelpFormatter(); usageFormatter.printUsage(writer, 80, applicationName, options); writer.flush(); } /** * Write "help" to the provided OutputStream. */ public static void printHelp( final Options options, final int printedRowWidth, final String header, final String footer, final int spacesBeforeOption, final int spacesBeforeOptionDescription, final boolean displayUsage, final OutputStream out) { final String commandLineSyntax = "java -cp ApacheCommonsCLI.jar"; final PrintWriter writer = new PrintWriter(out); final HelpFormatter helpFormatter = new HelpFormatter(); helpFormatter.printHelp( writer, printedRowWidth, commandLineSyntax, header, options, spacesBeforeOption, spacesBeforeOptionDescription, footer, displayUsage); writer.flush(); } /** * Main executable method used to demonstrate Apache Commons CLI. * * @param commandLineArguments Commmand-line arguments. */ public static void main(final String[] commandLineArguments) { final String applicationName = "MainCliExample"; displayBlankLines(1, System.out); displayHeader(System.out); displayBlankLines(2, System.out); if (commandLineArguments.length < 1) { System.out.println("-- USAGE --"); printUsage(applicationName + " (Posix)", constructPosixOptions(), System.out); displayBlankLines(1, System.out); printUsage(applicationName + " (Gnu)", constructGnuOptions(), System.out); displayBlankLines(4, System.out); System.out.println("-- HELP --"); printHelp( constructPosixOptions(), 80, "POSIX HELP", "End of POSIX Help", 3, 5, true, System.out); displayBlankLines(1, System.out); printHelp( constructGnuOptions(), 80, "GNU HELP", "End of GNU Help", 5, 3, true, System.out); } displayProvidedCommandLineArguments(commandLineArguments, System.out); usePosixParser(commandLineArguments); //useGnuParser(commandLineArguments); } }
以上是关于Apache Commons CLI的主要内容,如果未能解决你的问题,请参考以下文章
Hadoop NoSuchMethodError apache.commons.cli
Apache Commons CLI 中的 DefaultParser