Skip to content
The jbock command line parser
Java Shell
Branch: master
Clone or download
Latest commit e558cbf Jan 28, 2020
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github/workflows add github workflow Nov 13, 2019
annotations
core refactoring Jan 28, 2020
examples remove unused file Jan 22, 2020
gradle/wrapper update gradle Jan 17, 2020
.gitignore use gradle Apr 29, 2019
LICENSE Create LICENSE Sep 28, 2018
README.md update documentation Jan 21, 2020
RELEASING.md merge release scripts Nov 30, 2019
SPAGHETTI.md update documentation Jan 21, 2020
gradlew undo accidential renaming Dec 23, 2019
release
settings.gradle disable gradle module release metadata Nov 29, 2019

README.md

core annotations

jbock is a simple yet flexible command line parser that uses the same annotation names as airline and picocli. Options and params are defined as abstract methods:

@Command
abstract class MyArguments {

  /**
   * A "param" is a positional parameter.
   * The number 1 is arbitrary as long as there is only one param.
   */
  @Param(1)
  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);

// 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 so-called parameter skew is determined by the parameter type, as described in the following section.

Skew rules

These are the rules if neither mapper nor collector are defined, like in the introductory example:

Parameter type Skew
boolean or Boolean flag (only applies to options)
X (exact match) required
Optional<X> optional
List<X> repeatable
Optional{Int|Long|Double} optional

where X is one of the auto types.

If an explicit mapper is defined, but no collector, then these rules apply:

Mapper return type Parameter type Skew
R R (exact match) required
R Optional<R> optional
R List<R> repeatable
Integer OptionalInt optional
Long OptionalLong optional
Double OptionalDouble optional

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

You can’t perform that action at this time.