I just released JCommander 1.20. The main new feature is “parameter delegates”:

Parameter delegates

If you are writing many different tools in the same project, you will probably find that most of these tools can share configurations. While you can use inheritance with your objects to avoid repeating this code, the restriction to single inheritance of implementation might limit your flexibility. To address this problem, JCommander supports parameter delegates.

When JCommander encounters an object annotated with @ParameterDelegate in one of your objects, it acts as if this object had been added as a description object itself:

class Delegate {
  @Parameter(names = "-port")
  public int port;
}

class MainParams {
  @Parameter(names = "-v")
  public boolean verbose;

  @ParametersDelegate
  public Delegate delegate = new Delegate();
}

The example above specifies a delegate parameter Delegate which is then referenced in MainParams. You only need to add a MainParams object to your JCommander configuration in order to use the delegate:

MainParams p = new MainParams();
new JCommander(p).parse("-v", "-port", "1234");
Assert.assertTrue(p.isVerbose);
Assert.assertEquals(p.delegate.port, 1234);

Change log for 1.20

  • Added: Support for delegating parameter definitions to child classes (rodionmoiseev)
  • Added: @Parameter(commandNames) so that command names can be specified with annotations
  • Added: Support for enums (Adrian Muraru)
  • Fixed: Throw if an unknown option is found
  • Fixed: Main parameters are now validated as well (Connor Mullen)