views:

3179

answers:

9

Is there a good options parser framework/library for command-line programs written in Java? E.g. the equivalent of Python's optparse?

A: 

Take a look at the Commons CLI project, lots of good stuff in there.

Marc Novakowski
+1  A: 

Maybe these

  • JArgs command line option parsing suite for Java - this tiny project provides a convenient, compact, pre-packaged and comprehensively documented suite of command line option parsers for the use of Java programmers. Initially, parsing compatible with GNU-style 'getopt' is provided.

  • ritopt, The Ultimate Options Parser for Java - Although, several command line option standards have been preposed, ritopt follows the conventions prescribed in the opt package.

mepcotterell
+6  A: 

I've used JOpt and found it quite handy: http://jopt-simple.sourceforge.net/

The front page also provides a list of about 8 alternative libraries, check them out and pick the one that most suits your needs.

GaryF
+1  A: 

Yeap.

I think you're looking for something like this: http://commons.apache.org/cli

The Apache Commons CLI library provides an API for processing command line interfaces.

OscarRyz
+2  A: 

if you are familiar with gnu getopt, there is a java port at: http://www.urbanophile.com/arenn/hacking/download.htm. there appears to be a some classes that do this: http://docs.sun.com/source/816-5618-10/netscape/ldap/util/GetOpt.html, http://xml.apache.org/xalan-j/apidocs/org/apache/xalan/xsltc/cmdline/getopt/GetOpt.html

Ray Tayek
+3  A: 

You might find this meta-article of unhappiness interesting as a jumping off point:

http://furiouspurpose.blogspot.com/2008/07/command-line-parsing-libraries-for-java.html

Alex Miller
That guy is pretty useless "Commons CLIApparently 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..", but thanks for the link anyway.
James McMahon
+2  A: 

Someone pointed me to args4j lately which is annotation based. I really like it!

André
+5  A: 

Take a look at the more recent JCommander: http://beust.com/jcommander

(I created it, happy to answer questions or take feature requests)

Cedric Beust
I like your slogan: "Because life is too short to parse command line parameters"
Ed Brannin
Using it now and loving it! I would like to see automatic parsing of the - and -- for single char vs string arguments. i.e. "h" vs "-h" and "help" vs "--help"
NorthIsUp
Glad you like JCommander :-) I didn't want to add too much semantic to how the flags are treated, so you just need to add synonyms in the annotations you use:@Parameter(names = { "-h", "--help" })I thought it's a reasonable compromise.
Cedric Beust