2

We develop a Java Cli application, that has sub-commands with similar options. It's like svn:

svn commit -m messsage --username ARG --password ARG
svn checkout -r HEAD --username ARG --password ARG

I mean that each subcommand has special arguments, and we have also global arguments, and we have some arguments that are relvant for some subcommands, but not for all.

Which command line arguments parser supports that requirements?

thank you.

1
  • 1
    Please consider editing your question to add more details. Removing the the svn example would help since it appears your question has no relevance to SVN and it only confuse things. Consider adding a rel world example of what your command line should look like.
    – rrirower
    Commented Jan 2, 2017 at 17:33

2 Answers 2

2

picocli supports nested subcommands to arbitrary depth.

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.

enter image description here

The usage help is easily customized with annotations.

enter image description here

  • 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
  • allows any option prefix
  • fluent API
  • POSIX-style clustered short options
  • GNU style long options
  • ANSI colors in usage help
  • customizable usage help
  • single source file: include as source to keep your application a single jar
1

Which command line arguments parser supports these requirements?

From the limited information in your question the following appear to match your needs.


JOpt Simple

JOpt Simple is a Java library for parsing command line options, such as those you might pass to an invocation of javac.

In the interest of striving for simplicity, as closely as possible JOpt Simple attempts to honor the command line option syntaxes of POSIX getopt() and GNU getopt_long(). It also aims to make option parser configuration and retrieval of options and their arguments simple and expressive, without being overly clever.

The JOpt Simple web page also lists a number of other Java Commandline Parsers which may also be suitable if JOpt Simple is not sophisticated enough for you:

Here are some libraries that perform the same duties as JOpt Simple:

JArgs
Jakarta Commons CLI
TE-Code has a command line parsing library.
argparser
Java port of GNU getopt
Args4J
JSAP
CLAJR
CmdLn
JewelCli
JCommando
parse-cmd
JCommander
plume-lib Options

The website contains links for the above if you want to investigate them further.

Source JOpt Simple


Apache Commons CLI

The Apache Commons CLI library provides an API for parsing command line options passed to programs. It's also able to print help messages detailing the options available for a command line tool.

Commons CLI supports different types of options:

  • POSIX like options (ie. tar -zxvf foo.tar.gz)
  • GNU like long options (ie. du --human-readable --max-depth=1)
  • Java like properties (ie. java -Djava.awt.headless=true -Djava.net.useSystemProxies=true Foo)
  • Short options with value attached (ie. gcc -O2 foo.c)
  • long options with single hyphen (ie. ant -projecthelp)

A typical help message displayed by Commons CLI looks like this:

usage: ls
 -A,--almost-all          do not list implied . and ..
 -a,--all                 do not hide entries starting with .
 -B,--ignore-backups      do not list implied entried ending with ~
 -b,--escape              print octal escapes for nongraphic characters
    --block-size <SIZE>   use SIZE-byte blocks
 -c                       with -lt: sort by, and show, ctime (time of last
                          modification of file status information) with
                          -l:show ctime and sort by name otherwise: sort
                          by ctime
 -C                       list entries by columns

Source Apache Commons CLI

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.