Command Line parser and lack of subcommand and grouping?
Hi just take a look into jcommander which exactly supports the described scenario. You mentioned Commons CLI which is true in the releases 1.X but there exists a development for CLI2 which supports this as well, but unfortunately this release has never been published.
Another good solution would be https://picocli.info/
Args4j now supports subcommands (starting with version 2.0.23 or thereabouts).
picocli supports nested subcommands to arbitrary depth.
The main command defines global options, each following level of nested commands can add options that only apply to that level.
CommandLine commandLine = new CommandLine(new MainCommand())
.addSubcommand("cmd1", new ChildCommand1()) // 1st level
.addSubcommand("cmd2", new ChildCommand2())
.addSubcommand("cmd3", new CommandLine(new ChildCommand3()) // 2nd level
.addSubcommand("cmd3sub1", new GrandChild3Command1())
.addSubcommand("cmd3sub2", new GrandChild3Command2())
.addSubcommand("cmd3sub3", new CommandLine(new GrandChild3Command3()) // 3rd
.addSubcommand("cmd3sub3sub1", new GreatGrandChild3Command3_1())
.addSubcommand("cmd3sub3sub2", new GreatGrandChild3Command3_2())
// etc
)
);
You may also like its usage help with ANSI styles and colors.
Note that usage help lists the registered subcommands in addition to options and positional parameters.
The usage help is easily customized with annotations.
- annotation-based
- git-style subcommands
- nested sub-subcommands
- strongly typed option parameters
- strongly typed positional parameters
- customizable type conversion
- multi-value options
- intuitive model for how many arguments a field consumes
- fluent API
- POSIX-style clustered short options
- GNU style long options
- allows any option prefix
- ANSI colors in usage help
- customizable usage help
- single source file: include as source to keep your application a single jar