The implementation strategy of processing complex command line parameters in the Java class library
The implementation strategy of processing complex command line parameters in the Java class library
Summary: In Java development, sometimes some complex command line parameters need to be processed.This article introduces some implementation strategies for processing complex command line parameters, including using Apache Commons Cli and using Getopt.In addition, the corresponding Java code example is provided.
1 Introduction
The command line parameters refer to the parameters or options of the specified command entered in the console. These parameters and options are used to configure and customize the behavior.When developing Java applications, it is necessary to analyze and process command line parameters passed to the program, especially when the parameters become complex or there are multiple options.
The Java class library provides a variety of implementation strategies for processing complex command line parameters, including the use of Apache Commons Cli and Getopt.The following two strategies and their corresponding Java code examples will be introduced.
2. Use Apache Commons Cli
Apache Commons Cli is an open source library that provides a simple and flexible interface to analyze the command line parameters.It supports short options (such as "-V") and long options (such as "--verbose"), and also supports the default value and help text of the parameters.
First, the dependence of Apache Commons Cli needs to be added to the project.In the Maven project, you can add the following code to the pom.xml file:
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.4</version>
</dependency>
Below is a sample 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();
Option input = new Option("i", "input", true, "input file path");
input.setRequired(true);
options.addOption(input);
Option output = new Option("o", "output", true, "output file path");
output.setRequired(true);
options.addOption(output);
CommandLineParser parser = new DefaultParser();
HelpFormatter formatter = new HelpFormatter();
CommandLine cmd;
try {
cmd = parser.parse(options, args);
} catch (ParseException e) {
System.out.println(e.getMessage());
formatter.printHelp("utility-name", options);
System.exit(1);
return;
}
String inputFilePath = cmd.getOptionValue("input");
String outputFilePath = cmd.getOptionValue("output");
System.out.println("Input file path: " + inputFilePath);
System.out.println("Output file path: " + outputFilePath);
}
}
In the above example, first create an Options object and add two options: input and output.Then, a CommandLineParser object and a HelpFormatter object was created.In the TRY-CATCH block, call the Parser.PARSE method to parse the command line parameters and obtain the value of the parameter through the CMD object.
This is an example of the command line running this example:
java CommandLineParserExample -i input.txt -o output.txt
The output result will be:
Input file path: input.txt
Output file path: output.txt
3. Use getopt
Getopt is a Java class library for handling command line parameters.It provides a simple API to analyze and handle command line options.
Before using Getopt, you need to download and add the JAR files of the Getopt library to the project.You can download the latest version of the Getopt library here: http://www.urbanophile.com/arenn/hacking/download.html
Here are a sample code that uses Getopt to analyze command line parameters:
import gnu.getopt.Getopt;
import gnu.getopt.LongOpt;
public class GetoptExample {
public static void main(String[] args) {
LongOpt[] longOptions = new LongOpt[2];
longOptions[0] = new LongOpt("input", LongOpt.REQUIRED_ARGUMENT, null, 'i');
longOptions[1] = new LongOpt("output", LongOpt.REQUIRED_ARGUMENT, null, 'o');
Getopt g = new Getopt("utility-name", args, "i:o:", longOptions);
int c;
String inputFilePath = null;
String outputFilePath = null;
while ((c = g.getopt()) != -1) {
switch (c) {
case 'i':
inputFilePath = g.getOptarg();
break;
case 'o':
outputFilePath = g.getOptarg();
break;
default:
break;
}
}
System.out.println("Input file path: " + inputFilePath);
System.out.println("Output file path: " + outputFilePath);
}
}
In the above examples, a Longopt array is first created to define the INPUT and Output options and its corresponding short options.Then, a Getopt object was created, and the ARGS array and the Longopt array passed to the program were created for initialization.In the While loop, use the getPT method to obtain the value of the command line option, and process it accordingly according to the different options.
This is an example of the command line running this example:
java GetoptExample -i input.txt -o output.txt
The output result will be:
Input file path: input.txt
Output file path: output.txt
in conclusion:
This article introduces the implementation strategy of processing complex command line parameters in the Java class library.By using Apache Commons Cli and Getopt, you can easily analyze and process complex command line parameters.It is hoped that the example code provided in this article will help readers processing command line parameters when developing Java applications.
references:
-Apache Commons Cli official document: https://commons.apache.org/proper/commons-cli/
-Getopt library official website: http://www.urbanophile.com/arenn/hacking/getOptopt.html
The above content is written for the author based on his own knowledge and understanding, for reference only.