27

For writing command line applications in Java, what is the best library for parsing and managing arguments and paramenters?

Note: this question is an updated version of this closed question on Stack Overflow.

3
  • At first I was unsure if this question was allowed here but it is. However I still doubt its value here when you're linking to a very well answered question on SO.
    – user416
    Commented Jan 20, 2015 at 19:58
  • 1
    The reason I moved the question here is because the question is still relevant, but the question on SO has been closed due to not being appropriate. This is the appropriate home, where the question can remain a 'alive'.
    – Ivar
    Commented Jan 20, 2015 at 23:15
  • Closed questions remain available meta.stackexchange.com/questions/19231/…
    – user416
    Commented Jan 21, 2015 at 8:44

3 Answers 3

14

picocli is different from other Java CLI libraries:

  • It is designed to be included in source form. This lets users run picocli-based applications without requiring picocli as an external dependency.
  • Generates polished and easily tailored usage help, using ANSI colors when the underlying platform supports it.
  • Autocompletion for your Java command line applications on supported platforms

Example usage help message:

enter image description here

Quick overview:

  • Effortless command line parsing - just annotate fields
  • Strongly typed everything - command line options as well as positional parameters
  • full support for both GNU style and POSIX clustered short options (so it handles <command> -xvfInputFile as well as <command> -x -v -f InputFile)
  • An arity model that allows a minimum, maximum and variable number of parameters, e.g, "1..*", "3..5"
  • Subcommands
  • Works with Java 5 and higher
  • Well-structured user manual

Usage help is the face of your application, so be creative and have fun!

enter image description here

Update:

Picocli is also actively maintained. Since the original post, many new features were added, for example:

  • programmatic API as well as annotations API
  • Dependency Injection container integration
  • JLine integration: delegate to AutoComplete for your command’s Completer implementation
  • interface methods can be annotated with @Option or @Parameters (picocli creates a synthetic implementation that returns the matched options)
  • class methods can be annotated with @Option or @Parameters (so you can validate options and positional parameters)
  • support for @Command methods for extra compact code
  • internationalization with resource bundles

Disclaimer: I am the author.

1
  • 2
    I have added option management with picocli to a project and it was very easy to integrate with my project. Seems nice and has more advance features that I intend to add later.
    – aled
    Commented Sep 14, 2018 at 16:42
8

I recommend JOpt Simple. It 'attempts to honor the command line option syntaxes of POSIX getopt() and GNU getopt_long().' It has community traction and notably is the command line parsing lib of choice for the OpenJDK itself.

For comparison, here's a relatively up to date (as of Jan 2015) list of related libraries that serve the same purpose.

3
  • 1
    I just tried the Commons CLI library, found it extremely frustrating to use, switched to JCommander and have no regrets! Something as simple as accepting unknown options was ridiculously complicated to achieve with Commons CLI. It's a one-liner on the other hand with JCommander. Commented May 23, 2017 at 12:55
  • Please take a look at picocli. It’s easy to use and offers a lot. Commented Sep 8, 2018 at 3:18
  • jopt simple link is dead. this one: github.com/jopt-simple/jopt-simple seems to exist, but hasn't been updated in a while.
    – Tom
    Commented Mar 7, 2021 at 19:51
2

My choice goes to JCommander.

As of Oct 2016, have to start a new project and finally decided to do arguments the smart way. Just compared Jopt-simple, JCommander, args4j.

Choice reason: only JCommander and args4j have annotations, only JOpt-simple and JCommander have a great tutorial.

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.