jbock is a command line parser, which uses the same well-known annotation names as JCommander and picocli. It is an annotation processor which does not use runtime reflection, but generates a custom parser at compile time instead.
Create an abstract class, or alternatively a Java interface, and add the @Command annotation. In this so-called command class, each abstract method represents a command line option or argument. Every such method must have
- getter signature (doesn't return
void, takes no arguments) and - annotation (either
@Option,@Parameteror@VarargsParameter).
The types boolean, List and Optional (including OptionalInt) have special meaning. See example below.
@CommandabstractclassDeleteCommand{@Option(names ={"-v", "--verbosity"}, description ={"A non-required, named option. The return type is optionalish.", "Using int or Integer would make it required."}) abstractOptionalIntverbosity(); @Parameter( index = 0, description ={"A required positional parameter. Return type is not optionalish.", "Built-in converter is available for type Path."}) abstractPathpath(); @Parameter( index = 1, description = "An optional positional parameter.") abstractOptional<Path> anotherPath(); @VarargsParameter( description ={"A varargs parameter. There can only be one of these.", "Must return List."}) abstractList<Path> morePaths(); @Option(names = "--dry-run", description = "A nullary option, a.k.a. mode flag. Must return boolean.") abstractbooleandryRun(); @Option(names = "-h", description = "A repeatable option. Must return List.") abstractList<String> headers(); @Option(names = "--charset", description = "Named option with a custom converter", converter = CharsetConverter.class) abstractOptional<Charset> charset(); staticclassCharsetConverterextendsStringConverter<Charset>{@OverrideprotectedCharsetconvert(Stringtoken){returnCharset.forName(token)} } }The generated class is called *Parser.
publicstaticvoidmain(String[] args){DeleteCommandcommand = DeleteCommandParser.parseOrExit(args); // alternatively:// Either<ParsingFailed, DeleteCommand> either = DeleteCommandParser.parse(List.of(args)); }Some types don't need a custom converter. See StandardConverters.java.
The @SuperCommand annotation can be used to define a git-like subcommand structure. See javadoc.