简体   繁体   English

使用Apache Commons库的Java命令行参数解析问题

[英]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.

相关问题 命令行解析使用apache commons cli - command line parsing using apache commons cli 异常:解析Java中的命令行参数时,org.apache.commons.cli.MissingOptionException - Exception: org.apache.commons.cli.MissingOptionException while parsing command line arguments in Java 在Java中使用Apache Commons Optimize()的问题 - Issue using apache commons optimize() in java 使用apache commons电子邮件库出现错误必须首先发出starttls命令。 请帮忙 - Using apache commons email library getting error must issue a starttls command first. Please help 使用commons-cli解析命令行时的同义词 - Synonyms when parsing command line using commons-cli 使用Apache Commons CLI检测缺少命令行参数/选项 - Using Apache Commons CLI to detect absence of command line arguments/options 不同的输出-在命令行中运行mathtext时以及使用apache-commons-exec从Java程序执行命令时 - Different output — when running mathtext in command line and when the command is executed from a java program using apache-commons-exec 如何使用 Java 中的 Apache Commons Math 库找到矩阵的逆矩阵? - How to find the inverse of a matrix using Apache Commons Math library in Java? Java:使用 InputStream 和 Apache Commons CSV 没有行号 - Java: Using InputStream and Apache Commons CSV without line numbers Apache.commons.cli命令行参数 - Apache.commons.cli Command Line Arguments
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM