I've recently had the need to do some command-line argument parsing in Java, and so I took the opportunity to quickly evaluate some of the command-line parsing libraries that are available for Java. After looking briefly, the following options seemed to be relatively well-known:
- Commons CLI
Of the options, I spent the most time with args4j; it seemed to be relatively modern, it was available in a Maven repository, it was written at least in part by Kohsuke Kawaguchi, whose Hudson project seems to have turned a lot of heads.
Most java.net projects have pretty weak websites and documentation, I've noticed. The args4j project is no exception. It has a sample page, a blog post and some javadoc, and if you piece all three of those together, you'll basically have enough information to get started, after which you'll occasionally have to dive into the code.
I was able to make this work for me, but it lacks most of the bells and whistles that I had hoped adopting a third-party library designed for this purpose would provide. Args4j will not easily:
- Limit the number of arguments
- Restrict arguments to matching arbitrary rules like regexp pattern-matching
- Display great help for the available options (it has usage, but it seems to leave out information like whether or not an option is required)
JewelCLI has a pretty spartan (Maven-built) site, but seems to have good functionality and clear, if simple, documentation by example. As far as I can tell, it isn't available in any Maven repositories, which is too bad because it seems to be built with Maven and, at first glance, I'd say it looks slightly more capable than args4j, and it seems to have a full test suite, although it doesn't look like anything's changed much recently.
It's another anotation-based implementation. It uses an interface instead of a class, which has pros and cons. It's vaguely fun because it allows them to implement some simple things under the hood like interrogatory methods for optional parameters. That said, args4j's class approach gives you the flexibility of putting the behavior inside the same class as the data, if you choose to accept that option.
jArgs seemed like it might be reasonable, but the last release was in 2005, the site is sometimes unresponsive, preventing you from looking into it in more detail, the implementation seems like more work to set up - programmatic configuration instead of class-based or interface-based reflection/generation. Ultimately, if I can't get to the project consistently, I tend to take that as an omen.
Apparently the oldest of the options here, and doesn't get a lot of respect from commenters, so I didn't really look at this very closely..
I ended up using Args4j for now, but I think I'd use Jewel CLI next time, and if it works out as well as it seems at a distance, probably stick with that.
This is also a good time to point out that Java's console programming model is pretty limited. Key bindings? Clear screen? Move around? Nope, sorry. There seem to be third-party tools to get you there, but the core console functionality is basically suited to log output and the occasional thin piece of input.
Taylor Gautier looked into some of the before, if you're looking for further comparisons.