The Apache commons CLI Library provides an API for parsing command line options passed to programs. It's also able to print help messages detailing the options available for a command line tool.
Commons CLI supports different types of options:
- POSIX like options (ie.
tar -zxvf foo.tar.gz
)
- GNU like Long Options (ie.
du --human-readable --max-depth=1
)
- Java like properties (ie.
java -Djava.awt.headless=true -Djava.net.useSystemProxies=true Foo
)
- Short options with value attached (ie.
gcc -O2 foo.c
)
- Long Options with single hyphen (ie.
ant -projecthelp
)
CLI is the command line interface, that is, the command line interface, which is used to access and parse the command line for Java programs.
Parameters provide a unified interface.
It mainly processes
After compiling *. Class in pure Java, it will pass. The following command runs the class with main
Java class name
To compress a jar package, run the following command (with the main method)
Java-jar package name. Jar
To run the command in eclipse, you must use run as/runconfigurations/arguments to configure command line parameters.
The parameter configuration is the same as the Common commands, such as horizontal bars, parameter names, spaces, and parameter values.
-Parameter name parameter value
Then, Java obtains the command line parameters based on string [] ARGs in the main method.
By using commons-CLI, you can easily access parameters without repeating string [] ARGs
Package test. common. CLI; </P> <p> Import Org. apache. commons. CLI. commandLine; <br/> Import Org. apache. commons. CLI. commandlineparser; <br/> Import Org. apache. commons. CLI. options; <br/> Import Org. apache. commons. CLI. parseexception; <br/> Import Org. apache. commons. CLI. posixparser; </P> <p>/* <br/> enter the-C code-t time parameter. <br/> running result: <br/> Code <br/> null <br/> */<br/> public class clitest <br/> {<br/>/** <br/> * @ Param ARGs <br/> * @ throws parseexception <br/> */<br/> Public static void main (string [] ARGs) throws parseexception <br/>{</P> <p> options Options = new options (); <br/> options. addoption ("T", false, "display current time"); // The parameter is unavailable <br/> options. addoption ("C", true, "Country Code"); // The parameter is available </P> <p> commandlineparser parser = new posixparser (); <br/> CommandLine cmd = parser. parse (options, argS); </P> <p> If (CMD. hasoption ("C") <br/>{< br/> string countrycode = cmd. getoptionvalue ("C"); <br/> system. out. println (countrycode); <br/>}</P> <p> If (CMD. hasoption ("T") <br/>{< br/> string countrycode = cmd. getoptionvalue ("T"); <br/> system. out. println (countrycode); <br/>}< br/>}
Automatically generate a help statement:
// Automatically generate the help statement <br/> helpformatter formatter = new helpformatter (); <br/> formatter. printhelp ("ant", options );
Printed information:
Usage: ant <br/>-D <property = value> use value for given property <br/>-buildfile <File> use given buildfile <br/>-Debug print debugging information <br />-Emacs produce logging information without adornments <br/>-file <File> Search for buildfile towards the root of the <br/> filesystem and use it <br/>-help print this message <br/>-listener <classname> Add an instance of class as a project listener <br/>-logger <classname> the class which it to perform logging <br/ >-projecthelp print project help information <br/>-Quiet be extra quiet <br/>-verbose be extra verbose <br/>-version print the version information and exit </P> <p>
Reference URL:
Http://commons.apache.org/cli/
Http://phl.javaeye.com/blog/730737
Http://blog.csdn.net/socoolfj/archive/2005/12/01/541429.aspx