Martian Software, Inc. logo

JSAP v2.1: the Java Simple Argument Parser

Last Updated August 4, 2006

Why do we need another command line parser?

Put simply, I got tired of writing simple command line parsers for every java utility program I wrote. I would usually just throw together something simple, requiring a certain number of arguments and deciding that args[0] would always mean, say, an input file, and args[1] would be an output file. It was fine for simpler utilities with just a few parameters, but it didn't handle switches - and if I wanted anything other than a String from the command line, I had to write that parsing into the program, too. What annoyed me was that none of this had anything to do with the actual task for which the utility was intended.

I found several parsers on the Internet, all of which handled switches, but none of which had the versatility I wanted in terms of return types and configuration files. Hence JSAP - the Java Simple Argument Parser.

So what does JSAP actually do, then?

JSAP not only syntactically validates your program's command line arguments, but it converts those arguments into objects you specify. If you tell JSAP that one of your parameters is an Integer, for example, and the user does not provide a String that can be converted to an Integer when invoking the program, JSAP will throw a ParseException when you have it parse the command line. If no exception is thrown, you are guaranteed an Integer when you request that parameter's value from your program. There's a pretty big (and growing) list of return types suppored by JSAP, including Integers, Floats, Dates, URLs, and even java.awt.Colors; you can also add your own in a matter of minutes.

JSAP provides not only for default values for its parameters, but for an entire chain of defaults. With a couple extra lines of code, JSAP will, for example:

  1. Parse the command line.
  2. For any omitted parameters, look for values in ~/.yourProgramName.conf
  3. For any still omitted parameters, look for values in /etc/yourProgramName.conf
  4. For any still omitted parameters, use the default values (if any) specified by the developer.

How do I get it?

By going to JSAP's SourceForge page.