Quantcast
Channel: How do I parse command line arguments in Java? - Stack Overflow
Viewing all articles
Browse latest Browse all 24

Answer by Remko Popma for How do I parse command line arguments in Java?

$
0
0

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.

Minimal usage help with ANSI colors

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:

Extended usage help message (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!

picocli demo

Disclaimer: I created picocli. Feedback or questions very welcome.


Viewing all articles
Browse latest Browse all 24

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>