jbock icon indicating copy to clipboard operation
jbock copied to clipboard

Dynamic and/or hidden parameters?

Open DrDaleks opened this issue 2 years ago • 2 comments

I have a use case where I'd like to add/remove an option based on the presence/absence of a module on the classpath. I'm assuming this is impossible to do this at compile-time, but alternatively, hiding an option from the usage printout should be possible at runtime.

One way I imagined this could be possible by calling StandardErrorHandler's printUsageDocumentation using a different CommandModel with fewer elements, but cloning or modifying the command model is currently not possible afaik.

DrDaleks avatar Apr 15 '23 10:04 DrDaleks

Thanks for pointing this out. I've added a new method CommandModel.toBuilder() in 5.15.

h908714124 avatar Apr 15 '23 18:04 h908714124

I hope something like this will work:

parser.parse(List.of(args)).ifLeftOrElse(parsingFailed -> {
    CommandModel model = parsingFailed.commandModel();
    List<Option> options = model.options().stream().filter(this::isInClasspath).toList();
    StandardErrorHandler.builder().build().printUsageDocumentation(model.toBuilder().withOptions(options).build());
}, this::runCommand);

h908714124 avatar Apr 15 '23 18:04 h908714124