jbock is a command line parser, similar to JCommander and picocli. It is an annotation processor that generates custom parsing code at compile time. jbock requires Java 11.
Basic example
A command line interface is defined as an abstract
class
which has a @Command
annotation.
In this class, each abstract
method defines a named option or a positional parameter.
As an annotation processor, jbock has access to the method's return type before erasure.
It uses this full type information to determine multiplicity.
@Command(name = "rm", description = "Coffee time!")
abstract class DeleteCommand {
/* Path, not Optional<Path>:
* This positional parameter is required (multiplicity = 1).
*/
@Parameter(index = 0,
description = "A positional parameter.")
abstract Path path();
/* OptionalInt, not int or Integer:
* This named option is optional (multiplicity = 0..1).
*/
@Option(names = {"-v", "--verbosity"},
description = "A named option.")
abstract OptionalInt verbosity();
}
See here for the code this generates: DeleteCommandParser.java
The generated parser is usually used in a main
method:
public static void main(String[] args) {
DeleteCommand command = new DeleteCommandParser().parseOrExit(args);
// ...
}
In addition to parseOrExit
, there is also a highly configurable parse
method with no side effects.
Please see the wiki for details.