This is a tool for processing commands. For example, the main method input string[] needs parsing. You can define the rules of the parameters in advance, and then you can invoke the CLI to resolve them.
Download Address: http://commons.apache.org/downloads/download_cli.cgi
The following is a demo code for generating command-line comments using the CLI:
Package demo;
Import Java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStreamReader;
Import Org.apache.commons.cli.BasicParser;
Import Org.apache.commons.cli.CommandLine;
Import Org.apache.commons.cli.HelpFormatter;
Import org.apache.commons.cli.Options;
Import org.apache.commons.cli.ParseException;
public class Optionstip ... {
public static void Main (string[] args) ... {
BufferedReader in=new BufferedReader (New InputStreamReader (system.in));
Try ... {
args = In.readline (). Split ("");
Options opt = new options ();
Opt.addoption ("H", false, "Print Help for this application");
Opt.addoption ("U", true, "the username to use");
Opt.addoption ("DSN", True, "the data source to use");
Basicparser parser = new Basicparser ();
CommandLine cl = Parser.parse (opt, args);
if (cl.hasoption (' h ')) ... {
Helpformatter f = new Helpformatter ();
F.printhelp ("Optionstip", opt);
else ... {
System.out.println (Cl.getoptionvalue ("U"));
System.out.println (Cl.getoptionvalue ("DSN"));
}
catch (ParseException e) ... {
E.printstacktrace ();
catch (IOException e) ... {
E.printstacktrace ();
}
}
}
After the appeal code is run, enter-h on the console, displaying the following information:
usage: OptionsTip
-dsn <arg> The data source to use
-h Print help for this application
-u <arg> The username to use