19

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.

  • 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. – Jan Doggen Jan 20 '15 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 Jan 20 '15 at 23:15
  • Closed questions remain available meta.stackexchange.com/questions/19231/… – Jan Doggen Jan 21 '15 at 8:44
10

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.

  • 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 Sep 14 '18 at 16:42
6

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.

  • 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. – Adil Hussain May 23 '17 at 12:55
  • Please take a look at picocli. It’s easy to use and offers a lot. – Remko Popma Sep 8 '18 at 3:18
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, privacy policy and cookie policy

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