Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Command Line parser and lack of subcommand and grouping?

Tags:

java

I know this question has been asked many times here and elsewhere before as I am searching for the answer. However, it still puzzles me why the command line parser library doesn't provide this common usage scenario, where I have a group of subcommands, each subcommand has its own set of required and optional arguments. Similar construct can be found in git/svn, though in their case, the subcommand command is a standalone program if I am not mistaken.

To summarize, what I am looking for is an easy way to:

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

In java's world, two often mentioned library are Apache Command CLI and JSAP. I don't see either of them have this in mind - though you could probably tweak and customize a lot to fit ... but the basic flow of defining a option, register ... then at the end, parse them all doesn't seen to consider the case with different subcommands, the validation and parser need to behave in different way.

Maybe the common wisdom here is that this is too application specific and should be left to the application itself to handle. One way I can think of is to define a BaseCommand class, and each subcommand extends it and register themself ... as a way to break it up for easy management. If any mature framework can do this, I'd appreciate any pointer here.

I could be wrong about my understanding of current parser capability though, any insight is much appreciated.

like image 539
Oliver Avatar asked Oct 12 '11 11:10

Oliver


1 Answers

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/

like image 150
khmarbaise Avatar answered Oct 01 '22 06:10

khmarbaise