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

时间:2016-04-26 00:43:49

标签: java command-line-arguments apache-commons-cli

我使用Apache commons basic / gnu解析器来解析命令行选项,如下所示。

features.selectAll("path")
      .data(topojson.feature(geodata,geodata.objects.collection).features) 
        .enter()
        .append("path")
        .attr("d",path)
        .style("fill", function(d){
                   //get the value of the checked
                   var value = d3.select('input[name="dataset"]:checked').node().value;
                   if(value =="sum_vendors"){
                      return vendorcolor(d.properties.sum_vendors);
                   } else {
                      return color(d.properties.sum_clients); 
                   }
       })
        .style("stroke", "#837E7C")
        .on('mouseover', tip.show)
        .on('mouseout', tip.hide)

我使用下面提到的参数列表来调用程序。

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"));

我得到的输出只是第一个IP地址,我如何获得带有端口的所有三个IP地址作为参数传递给--iplist变量?

以下是我正在使用的选项。

 java -jar myjar.jar --iplist 160.1.1.1,3009 160.1.1.1,3003 160.1.1.1,3004

1 个答案:

答案 0 :(得分:1)

您可以使用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()

另见:

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

相关问题