Skip to content

Latest commit

 

History

History
118 lines (91 loc) · 3.95 KB

README.md

File metadata and controls

118 lines (91 loc) · 3.95 KB

jbock-compiler jbock

jbock is a command line parser, which uses well-known annotation names, similar to those used by JCommander and picocli.

jbock is an annotation processor. It does not rely on runtime reflection, but generates java source code instead.

Quick start

Create a Java interface type, and add the @Command annotation to make the annotation processor aware of it.

In the command interface, each abstract (i.e. non-default) method represents a command line option or parameter. The method must have "getter signature". It must either be annotated with @Option or @Parameter.

Besides String, some other return types are possible. See known types.

The return types boolean, List<?> and Optional<?> (including OptionalInt and such) have special semantics. They are used to declare flags, repeable and optional options and parameters, respectively.

Tip

Only the exact type java.util.List has special semantics. An abstract method returning ArrayList<String>, for example, does not represent a repeatable option or catch-all parameter.

An example:

@Command
interface DeleteCommand {

  /**
   * The return type of this method is &quot;optionalish&quot;,
   * so this is an optional option.
   * Using int or Integer instead would make it a required option.
   */
  @Option(names = {"-v", "--verbosity"})
  OptionalInt verbosity();

  /**
   * A required positional parameter.
   * It captures the first positional parameter.
   * The index attribute can be omitted, since it is
   * clear from the method's position in the source file.
   */
  @Parameter(index = 0)
  Path path();

  /**
   * The return type of this method is &quot;optionalish&quot;,
   * so this is an optional positional parameter.
   * It captures the second positional parameter, if available.
   * The index attribute can be omitted.
   */
  @Parameter(index = 1)
  Optional<Path> anotherPath();

  /**
   * The return type of this parameter is java.util.List,
   * so this is the catch-all parameter.
   * It captures all but the first two positional parameters.
   * The index attribute {@code index = 2} is omitted here.
   */
  @Parameter
  List<Path> morePaths();
  
  /**
   * The return type is boolean, so this is a mode flag.
   */
  @Option(names = "--dry-run")
  boolean dryRun();
  
  /**
   * Charset is not one of the &quot;known types&quot;,
   * so we use a default method to convert manually.
   * See below.
   */
  @Option(names = "--charset")
  Optional<String> charsetString();
  
  /**
   * This default method converts from String to Charset.
   */
  default Charset charset() {
      return Charset.forName(charsetString());
  }
}

The generated class is called DeleteCommandParser. We can use it in our main method:

public static void main(String[] args) {
  DeleteCommand command = DeleteCommandParser.parseOrExit(args);
  // alternatively:
  // Either<ParsingFailed, DeleteCommand> either = DeleteCommandParser.parse(List.of(args));
  // more cool stuff...
}

Known types

Some types are converted from String automatically. All of these are listed in StandardConverters.java.

Sample projects

Alternatives