Detect the use of short vs. long parameter

6 views
Skip to first unread message

Andres Almiray

unread,
Nov 18, 2021, 12:10:52 PM11/18/21
to picocli
Hi,

Is there a way to detect if the short version of a parameter (-V) vs. when the long one (--version) was used? Specifically for the picocli.CommandLine.IVersionProvider class.

My use case is to provide a detailed version description when one option is invoked and a much shorter description when the other options use invoked.

Thanks
Andres

picocli

unread,
Nov 26, 2021, 5:27:43 PM11/26/21
to picocli
Hey Andres,
Apologies, I missed the email notification for your message and I don't check the groups that often... 

Yes, this is possible.
Are you using a custom IVersionProvider?

One way is to get the "expanded args" from the ParseResult.
This is the list of Strings that were provided on the command line (after @argument files were read and tokenized).

For example:

class SpecInjectedVersionProvider implements CommandLine.IVersionProvider {
  @Spec CommandSpec spec;

  public String[] getVersion() {
    if (spec.commandLine().getParseResult().expandedArgs().contains("-V")) {
      return new String[] { "Short version for " + spec.qualifiedName(); }
    }
    return new String[] { "Long version for " + spec.qualifiedName() };
  }
}
Reply all
Reply to author
Forward
0 new messages