Popularity
2.2
Growing
Activity
9.4
Declining
34
2
1

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

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

README

core annotations

jbock is a command line parser that uses the same annotation names as airline and picocli. It does not make any use of Java's reflection capabilities, but generates java source code instead, which can be easily read and debugged.

The jbock way to declare your command line interface is via a custom model class, the so called "command" class. In this class, each option and param corresponds to one annotated abstract method.

@Command
abstract class MyArguments {

  /**
   * A "param" is a positional parameter.
   * The number 1 determines its position relative to the other params. 
   * In this case it is irrelevant, as there is only one param.
   */
  @Param(1)
  abstract Path path();

  /**
   * An "option" is a named parameter (or flag).
   * The name is passed with one or two leading dashes. In this case,
   * ["--verbosity=1"], ["--verbosity", "1"], ["-v", "1"] and ["-v1"] are all valid.
   * Note: This javadoc will show up when "--help" is passed.
   * Alternatively the help text can be taken from a resource bundle.
   */
  @Option(value = "verbosity", mnemonic = 'v')
  abstract OptionalInt verbosity();
}

jbock must be configured as an annotation processor. The presence of a command class 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);

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

In the example above, path is a required param, while verbosity is an optional option. We call this property skew. There are four different skews: required, optional, repeatable and flag. Each option and param has a skew which is determined by its type, according to the following rules.

Skew rules

These are the rules for options and params that define neither a custom mapper nor collector:

param/option type Skew
boolean or Boolean flag (only applies to options)
Optional<A> optional
Optional{Int|Long|Double} optional
List<A> repeatable
any other required

where A is one of the auto types.

If a custom mapper is defined, but no collector, then the skew is determined by comparing the mapper return type and the param/option type:

Mapper return type param/option type Skew
M Optional<M> optional
Integer OptionalInt optional
Long OptionalLong optional
Double OptionalDouble optional
M List<M> repeatable
M M (exact match, or via boxing) required

If a custom collector is defined, then the skew is always repeatable.