Programming language: Java
Tags: CLI     Projects    
Latest version: v3.4.008

jbock alternatives and similar libraries

Based on the "CLI" category

Do you think we are missing an alternative of jbock or a related project?

Add another 'CLI' Library


core annotations

jbock is a simple but expressive command line parser that uses the same annotation names as airline and picocli. The following example shows how to declare your command line arguments:

abstract class MyArguments {

   * A "param" is a positional parameter.
   * The number 1 is arbitrary as long as there is only one param.
  abstract Path path();

   * This javadoc will show up when "--help" is passed.
   * Alternatively you can define the help text in a resource bundle.
  @Option(value = "verbosity", mnemonic = 'v')
  abstract OptionalInt verbosity();

jbock must be configured as an annotation processor. The presence of the annotated class above will trigger a round of code generation at compile time. The resulting class MyArguments_Parser can be used as follows:

String[] args = { "-v", "2", "file.txt" }; // command line parameters
MyArguments my = new MyArguments_Parser().parseOrExit(args);

// make sure this works as expected...
assertEquals(OptionalInt.of(2), my.verbosity());
assertEquals(Paths.get("file.txt"), my.path());

Please note that path is a required param, while verbosity is an optional option. This is determined from the methods' return type, using the following "matching" rules (upper left corner applies for both parameters):

Parameter type matching

No mapper defined Mapper defined Nocollectordefined <!-- No mapper, no collector--> boolean | Boolean flag* X required Optional<X> optional OptionalInt etc. optional List<X> repeatable <!-- Mapper, no collector--> R required Optional<R> optional OptionalInt (if R == Integer) optional OptionalLong (if R == Long) optional OptionalDouble (if R == Double) optional List<R> repeatable Collectordefined repeatable

* : does not apply to positional parameters

where X is one of the "auto types", and R is the return type of the mapper.