0

I am trying to parse command line arguments as

Options options = new Options();
        options.addOption("c", "count", false, "number of message to be generated");
        options.addOption("s", "size", false, "size of each messages in bytes");
        options.addOption("t", "threads", false, "number of threads");
        options.addOption("r", "is random", false, "is random");
        CommandLine cli = new DefaultParser().parse(options, args);

        int count = Integer.parseInt(cli.getOptionValue("c", "20000000"));
//        int count = Integer.parseInt(cli.getOptionValue("c", "100"));
        int recordSize = Integer.parseInt(cli.getOptionValue("s", "512"));
        int threads = Integer.parseInt(cli.getOptionValue("t","4"));
        boolean isRandom = Boolean.valueOf(cli.getOptionValue("r", "true"));
        System.out.println(" threads "+threads);
        System.out.println(" count "+count);

and i run it in eclipse with

t 6 c 7

but i always get

threads 4
count 20000000

what am i missing?

2
  • same result. still does not work Commented May 26, 2016 at 16:58
  • You passed in false for all of the options you are adding. That parameter is whether the option takes an argument, which for all but the random one, I'd say would be true. Commented May 26, 2016 at 17:19

1 Answer 1

1

You should use true for addOption method when the option takes an argument. From the Javadoc:

  • @param hasArg flag signally if an argument is required after this option
    options.addOption("c", "count", true, "number of message to be generated");
    options.addOption("s", "size", true, "size of each messages in bytes");
    options.addOption("t", "threads", true, "number of threads");
    options.addOption("r", "is random", false, "is random");

Yes, and a leading - is required for short option specification (e.g. -t 4) and a leading -- is required for long option specification (e.g. --threads 4).

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

2 Comments

thanks! that did it. after that i still had to specify as -t=6
When I specified in the IDE the program arguments as -t 6 -c 200 it seems to work.

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.