CLI is a subclass of Jakarta commons. If you only have one or two parameters to process, it is a bit redundant to use. However, if you need to capture the setting parameters of most applications from the command line, so using CLI is just right.
Before using CLI, you need to create an options object. This object is equivalent to a container, and there is also an option object. Each option object is relative to a parameter in the command line.
Options opts = new options ();
By using this options, you can use the addoption () method to define the command line parameters acceptable to your application. Each time you call this method for an option, see the following example:
Opts. addoption ("H", false, "Print help for this application ");
Opts. addoption ("u", true, "the username to use ");
Opts. addoption ("DSN", true, "the data source to use ");
Of course, you can also create an option pair separately and add it using the addoption () method. As follows:
Option op = New Option ("H", false, "Print help for this application ");
Once you define the class parameters, create a commandlineparser and analyze the groups that have been passed to the main method.
Basicparser parser = new basicparser ();
CommandLine Cl = parser. parse (OPTs, argS );
After all the parameters are parsed, you can begin to check the returned command line. In these command lines, the parameters and values provided by the user have been carefully checked by the syntax analysis program.
If (Cl. hasoption ('H ')){
Helpformatter HF = new helpformatter ();
HF. printhelp ("optionstip", opts );
} Else {
System. Out. println (Cl. getoptionvalue ("U "));
System. Out. println (Cl. getoptionvalue ("DSN "));
}
As you can see, you can use the helprormatter class to automatically generate usage information for your program.
Let's take a look at all the code:
Package com. Founder. Common;
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 ){
Try {
Options opts = new options ();
Opts. addoption ("H", false, "Print help for this application ");
Opts. addoption ("u", true, "the username to use ");
Opts. addoption ("DSN", true, "the data source to use ");
Basicparser parser = new basicparser ();
CommandLine Cl = parser. parse (OPTs, argS );
If (Cl. hasoption ('H ')){
Helpformatter HF = new helpformatter ();
HF. printhelp ("optionstip", opts );
} Else {
System. Out. println (Cl. getoptionvalue ("U "));
System. Out. println (Cl. getoptionvalue ("DSN "));
}
} Catch (parseexception PE ){
PE. printstacktrace ();
}
}
}
Note: Don't forget to add the commons-cli-1.0.jar to your classpath when using this program
Running result:
E:/javaworkspace/collection/src> JAVA com. Founder. Common. optionstip-H
Usage: optionstip
-DSN the data source to use
-H print help for this application
-U the username to use
E:/javaworkspace/collection/src> JAVA com. Founder. Common. optionstip-u Eric-DSN founder
Eric
Founder
Reposted from
Http://www.blogjava.net/rain1102/archive/2008/04/16/193521.html