It is 2019, time to do better than Commons CLI... :-)
Buy or build?
Many small utility-like applications probably roll their own command line parsing to avoid the additional external dependency.
picocli may be interesting. It is designed to be included as source as a simpler alternative to shading jars into an uberjar.
Another feature you may like is its colorized usage help.
Parser features:
- Annotation based: parsing is one line of code
- Strongly typed everything - command line options as well as positional parameters
- POSIX clustered short options (
<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 (can be nested to arbitrary depth)
- Works with Java 5 and higher
The usage help message is easy to customize with annotations (without programming). For example:
(source)
I couldn't resist adding one more screenshot to show what usage help messages are possible. Usage help is the face of your application, so be creative and have fun!
Disclaimer: I created picocli. Feedback or questions very welcome.