[英]Java Command line argument parsing issue using Apache commons library
I am using Apache commons basic/gnu parser to parse command line options as shown below. 我正在使用Apache commons basic / gnu解析器来解析命令行选项,如下所示。
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.GnuParser;
CommandLineParser parser = new GnuParser();
CommandLine cmd = parser.parse(options, args);
System.out.println(cmd.getOptionValue("iplist"));
I am invoking program using below mention list of parameters. 我正在使用下面提到的参数列表来调用程序。
java -jar myjar.jar --iplist 160.1.1.1,3009 160.1.1.1,3003 160.1.1.1,3004
Out put i am getting is just first IP address, how can i get all three IP addresses with port which are passed as an argument to --iplist variable? 我得到的输出只是第一个IP地址,我如何获得带有端口的所有三个IP地址作为参数传递给--iplist变量?
Here are the options i am using. 这是我正在使用的选项。
options.addOption("h", "help", false, "show help.");
options.addOption("iplst","iplist", true, "Provide name of server where program can listen IP,PORT");
CommandLineParser parser = new GnuParser();
CommandLine cmd = null;
try {
cmd = parser.parse(options, args);
if (cmd.hasOption("h"))
help();
if (cmd.hasOption("iplist")) {
System.out.println( "Using cli argument --server=" + cmd.getOptionValue("iplistr"));
//Code here
}
You can use OptionBuilder like: 您可以像这样使用OptionBuilder :
Option iplist = OptionBuilder
.withArgs() // option has unlimited argument
.withDescription("Provide name of server where program can listen IP,PORT")
.withLongOption("iplist") // means start with --
.create()
Also look at: 还要看:
https://commons.apache.org/proper/commons-cli/usage.html https://commons.apache.org/proper/commons-cli/usage.html
http://apache-commons.680414.n4.nabble.com/cli-Example-using-of-option-with-two-mandatory-arguments-td3321524.html http://apache-commons.680414.n4.nabble.com/cli-Example-using-of-option-with-two-mandatory-arguments-td3321524.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.