GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - jbock-java/jbock: Reflectionless command line parser

BranchesTags
Open more actions menu

Latest commit

 

History

2,658 Commits
2,658 Commits

Folders and files

NameName
Last commit message
Last commit date
.github
.github
 
 
compiler
compiler
 
 
examples
examples
 
 
gradle/wrapper
gradle/wrapper
 
 
jbock
jbock
 
 
.gitignore
.gitignore
 
 
LICENSE
LICENSE
 
 
Makefile
Makefile
 
 
README.md
README.md
 
 
RELEASING.md
RELEASING.md
 
 
gradlew
gradlew
 
 
release
release
 
 
settings.gradle.kts
settings.gradle.kts
 
 

Repository files navigation

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

  • jbock-maven-example
  • jbock-gradle-example

Alternatives

  • Tim's list

About

Reflectionless command line parser

Topics

annotation-processingcommand-line-parserjava

Resources

Readme
MIT license

Contributing

Contributing
Activity
Custom properties

Stars

96 stars

Watchers

2 watching

Forks

9 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.