Required Option with and without arguments in Apache Common CLI

1k Views Asked by At

I need to have the same option with and without argument. Example,

CLIParser -d 2 abc.txt

In above case d=2

CLIParser -d abc.txt

In above case d=1

I tried using .optionalArg(true) and .numberOfArgs(1) but nothing seems to work. Is this doable?

1

There are 1 best solutions below

0
THe_strOX On

This is working fine for me.

Option.builder("d").hasArg().optionalArg(true).build();

You need both:

hasArg = true and optionalArg = true

If there is no arg then:

CommandLine.getOptionValue("d");

will return null. You can change that to 1 as your requirement.