You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Flavio Pompermaier <po...@okkam.it> on 2020/11/06 09:31:08 UTC

Apache Commons CLI default value of Option

Hi to all,
I was trying to use your library to parse the arguments passed from the
command line and I have 2 questions/1 observation: why the Option doesn't
have a default value but the default value is handled by the CommandLine?
Currently you have to use CommandLine.getValue(defaultValue) but this has
one big limitation to me, i.e. you can't print the default value in the
help (assuming to use HelpFormatter). Am I wrong?
I think that also the code would benefit from moving the default value in
the Option (since you better separate the concerns). for example:

public static Option getDatalinksJobIdOption() {
    return Option.builder("someOpt")
        .argName("my-opt")
        .type(String.class)
        .hasArg(true)
        .desc("Some help")
        -defaultValue("XX")
        .build();
  }

What do you think?

Best,
Flavio

Re: Apache Commons CLI default value of Option

Posted by Remko Popma <re...@gmail.com>.
May I recommend using the picocli <https://picocli.info/> library instead?
Picocli has more functionality than any other CLI library, has excellent
documentation, and is actively maintained.

Using picocli's annotation API, one way to accomplish your use case would
be like the example below.
(There is also a programmatic API.)

class MyApp implements Runnable /*Callable is fine too*/ {

  @Option(names = {"--my-opt"}, paramLabel = "my-opt", defaultValue = "XX",
       description = "Some help. Default value is ${DEFAULT-VALUE}.")
  String myOpt;

  public void run() {
    // business logic here
  }

  public static void main(String[] args) {
    System.exit(new CommandLine(new MyApp()).execute(args));
  }
}

On Fri, Nov 6, 2020 at 8:50 PM Flavio Pompermaier <po...@okkam.it>
wrote:

> Hi to all,
> I was trying to use your library to parse the arguments passed from the
> command line and I have 2 questions/1 observation: why the Option doesn't
> have a default value but the default value is handled by the CommandLine?
> Currently you have to use CommandLine.getValue(defaultValue) but this has
> one big limitation to me, i.e. you can't print the default value in the
> help (assuming to use HelpFormatter). Am I wrong?
> I think that also the code would benefit from moving the default value in
> the Option (since you better separate the concerns). for example:
>
> public static Option getDatalinksJobIdOption() {
>     return Option.builder("someOpt")
>         .argName("my-opt")
>         .type(String.class)
>         .hasArg(true)
>         .desc("Some help")
>         -defaultValue("XX")
>         .build();
>   }
>
> What do you think?
>
> Best,
> Flavio
>