Java class library commonly used command line parameter analysis method
There are many commonly used command line parameter analysis methods in the Java library, which can help developers simplify the analysis and processing process of command line parameters.This article will introduce several of the common methods and provide relevant Java code examples.
1. Apache Commons CLI
Apache Commons Cli is a popular Java class library that provides a powerful command line parameter parsing function.It allows developers to define command line options, parameters and parameter values, and support various types of parameter verification and processing.
The following is an example code that uses Apache Commons Cli to resolve the command line parameters:
import org.apache.commons.cli.*;
public class CommandLineParserExample {
public static void main(String[] args) {
Options options = new Options();
options.addoption ("h", "help", false, "Display Help Information");
options.addoption ("v", "VERBOSE", FALSE, "Show Details");
options.addoption ("f", "file", true, "Specify file name");
CommandLineParser parser = new DefaultParser();
HelpFormatter formatter = new HelpFormatter();
try {
CommandLine cmd = parser.parse(options, args);
if (cmd.hasOption("h")) {
Formatter.printhelp ("Command Line Parameter Example", Options);
System.exit(0);
}
if (cmd.hasOption("v")) {
System.out.println ("Currently using -Verbose Options");
}
if (cmd.hasOption("f")) {
String fileName = cmd.getOptionValue("f");
System.out.println ("specified file name:" + FILENAME);
}
} catch (ParseException e) {
System.out.println(e.getMessage());
Formatter.printhelp ("Command Line Parameter Example", Options);
System.exit(1);
}
}
}
In the above code, we created three options with the `Options` class:` `-H` or`-Help` used to display help information, `-v` or`-vebose` used to display detailed information, `-F` or `-File` is used to specify the file name.Then, we use the command line parameter parser (`CommandLineParser`) and the help information formatter (` HelpFormatter`) for analysis and formatting.
2. JCommander
JCOMMANDER is a simple and powerful parameter parsing library, which can define command line parameters by annotation.It supports various parameter types and verification rules.
The following is an example code that uses JCOMMANDER to parse the command line parameters:
import com.beust.jcommander.*;
public class CommandLineParserExample {
@Parameter (names = {"-H", "-Help"}, help = true, design = "Display help information")
private boolean help;
@Parameter (names = {"-v", "--Verbose"}, description = "Display Details")
private boolean verbose;
@Parameter (names = {"-f", "--file"}, description = "specified file name")
private String fileName;
public static void main(String[] args) {
CommandLineParserExample example = new CommandLineParserExample();
JCommander commander = JCommander.newBuilder()
.addObject(example)
.build();
try {
commander.parse(args);
if (example.help) {
commander.usage();
System.exit(0);
}
if (example.verbose) {
System.out.println ("Currently using -Verbose Options");
}
if (example.fileName != null) {
System.out.println ("specified file name:" + Example.filename);
}
} catch (ParameterException e) {
System.out.println(e.getMessage());
commander.usage();
System.exit(1);
}
}
}
In the above code, we use the@Parameter` annotation to define the three command line parameters and use the `jcommander` to analyze it.By setting the `Help` attribute to` true`, we can automatically generate help information.
3. GetOpt
Getopt is a simple, lightweight command line parameter parsing library.It provides a basic command line option analysis function.
Here are a sample code that uses Getopt to analyze command line parameters:
import gnu.getopt.*;
public class CommandLineParserExample {
public static void main(String[] args) {
LongOpt[] longOptions = new LongOpt[]{
new LongOpt("help", LongOpt.NO_ARGUMENT, null, 'h'),
new LongOpt("verbose", LongOpt.NO_ARGUMENT, null, 'v'),
new LongOpt("file", LongOpt.REQUIRED_ARGUMENT, null, 'f')
};
Getopt Getopt = New Getopt
getopt.setOpterr(false);
int c;
String fileName = null;
while ((c = getopt.getopt()) != -1) {
switch (c) {
case 'h':
System.out.println ("Show help information");
System.exit(0);
break;
case 'v':
System.out.println ("Currently using -Verbose Options");
break;
case 'f':
fileName = getopt.getOptarg();
System.out.println ("specified file name:" + FILENAME);
break;
default:
System.out.println ("Unknown Options");
System.exit(1);
}
}
}
}
In the above code, we created three options with the `Longopt` class and parsed with the` Getopt` class.`Getopt.getopt ()` method returns the ASCII code value of the current option. We use the `switch` statement to handle accordingly according to the option.
Summarize:
This article introduces several command line parameters commonly used in the Java library: Apache Commons Cli, JCOMMANDER, and Getopt.By using these libraries, developers can easily analyze and handle command line parameters.Hope this article will help you!