Command line argument parsing in Java is one of those things that makes me scratch my head — "Hasn't someone gotten this right yet, for my definition of right?" When we needed a library for PXE, I played with Commons CLI (including the stillborn version 2), Martian's JSAP, John Lloyd's approach (complete with 2,000-line core class — Gah!), and whatever else Google had to offer. (Kohsuke's args4j and Sam's take weren't around at the time.) Commons CLI was good enough for initial work, but I wrote my own once I got frustrated enough with it. My requirements were:
- Multiple valid command line argument combinations per command with single help output.
- Match-based selection of which combination format applies.
- Nested logical (e.g., XOR or AND) relationships between arguments within a combination.
- Multiple named, ordered, non-flagged (i.e., no -x) arguments with multiplicities and good help output, like for cp with 1..n "from" files and a single "to" directory.
Maybe I'll package up my version, but a similarly picky person would probably find something objectionable in form or function and write their own anyway. (And, for that matter, it's a pleasant afternoon's diversion to do so.)











Comment from Cedric @ 2006-01-11T14:16:48Z # permalink
Comment from Paul @ 2006-01-11T17:47:13Z # permalink
Comment from Carlos @ 2008-07-25T14:59:19Z # permalink
I also got tired of commons-cli. It's a real pain in the neck after a while. I found that arg4j works great for me. It's hard to make it much simpler than that (http://carlosbinarystuff.blogspot.com/2008/05/parse-command-line-arguments-with.html).