0

I am using Apache commons basic/gnu parser to parse command line options as shown below.

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?

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
       }
4
  • 1
    have you tried java -jar myjar.jar --iplist "160.1.1.1,3009 160.1.1.1,3003 160.1.1.1,3004" ? Commented Apr 26, 2016 at 2:05
  • Please also show how you define the options Commented Apr 26, 2016 at 9:28
  • @centic I just added the code for options. Commented Apr 26, 2016 at 17:18
  • @ymonad double quote solution is working but i can't force user to pass arguments in quotes. Commented Apr 26, 2016 at 17:36

1 Answer 1

1

You can use OptionBuilder like:

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

http://apache-commons.680414.n4.nabble.com/cli-Example-using-of-option-with-two-mandatory-arguments-td3321524.html

Sign up to request clarification or add additional context in comments.

3 Comments

withArgs method is deprecated Option server = Option.builder("r").argName("ipl").longOpt("iplist") .hasArg().desc("Name of server where decoder or collector can listen IP,PORT") .required().build();
Or it is possible if i give values of iplist two times and get value out of it . java -jar myjar.jar --iplist 160.1.1.1,3009 --iplist 160.1.1.1,3010 ?
I am not sure but I guess it is a valid usage so you can try. But be careful about hasArg() and hasArgs(). Use second for multiple arguments.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.