Command line parser and lack of subcommand and grouping?

I know this question has been asked many times here and elsewhere when I am looking for an answer. However, it still puzzles me why the command line analyzer library does not provide this common use case, where I have a group of subcommands, each subcommand has its own set of mandatory and optional arguments. A similar constructor can be found in git / svn, although in this case the subcommand command is a standalone program if I am not mistaken.

To summarize, I am looking for an easy way:

top_command subcmd_A [ command A options ....] top_command subcmd_B [ command B options ....] ... 

In the java world, two commonly mentioned libraries are Apache Command CLI and JSAP. I don’t see any of them having this in mind - although you could probably tweak and configure a lot to fit ... but the main flow of defining the option, register ... then, at the end, analyze them all , t, in order to consider the case with different subcommands, validation and parser must behave differently.

Maybe the general wisdom here is that it is too specific for the application and should be left to the application itself. One way I can think of is to define a BaseCommand class, and each subcommand extends it and registers itself ... as a way to break it down for ease of management. If any mature framework can do this, I would appreciate any pointer here.

I could be mistaken in my understanding of the current ability of the parser, although any understanding is much appreciated.

Oliver

+7
source share
4 answers

Hi, just take a look at jcommander , which accurately supports the described scenario. You mentioned Commons CLI, which is true in versions 1.X, but there is a development for CLI2 that also supports this, but unfortunately this release has never been published.

+6
source

Args4j now supports subcommands (starting with version 2.0.23 or so).

+2
source

picocli supports nested subcommands to arbitrary depth.

The main command defines global parameters; each next level of nested commands can add parameters that apply only to this 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 use with ANSI styles and colors.

Please note that the usage help lists registered subcommands in addition to parameters and positional parameters.

enter image description here

Help for use is easily customizable using annotations.

enter image description here

  • based annotation
  • git-line subcommands
  • nested subcommands
  • strongly typed parameter parameters
  • strongly typed positional parameters
  • custom type conversion
  • multi-valued parameters
  • intuitive model for the number of arguments the field consumes
  • Free API
  • Brief POSIX-style cluster options
  • Advanced GNU Style Options
  • allows any option prefix
  • ANSI color to use
  • custom usage help
  • single source file: include as source so that your application is a single bank
+1
source

Take a look at cli-parsec . It includes (among other things) exactly what I think you need :-) https://github.com/dr1fter/cli-parsec

It has an arbitrary nesting of subcommands (subcommands can have subcommands, etc.). Each auxiliary command can define parameters. Here's a simple example (two subcommands with separate options): https://github.com/dr1fter/cli-parsec/wiki/Example

Excerpt from Maven:

 <dependency> <groupId>de.dr1fter</groupId> <artifactId>de.dr1fter.cli-parsec> <version>0.2.1</version> </dependency> 
0
source

All Articles