Main Site

What is JCommando?

JCommando is a Java argument parser for command-line parameters.  I developed JCommando for another project in which I needed a comprehensive way to parse command-line arguments.  First, I went it alone, parsing the arguments myself; cascading 'if' statments looking for options like '-R' and '--recurse', for example.  However, soon this became painful as some options were required and some optional, some were compatible with others and some were exclusive of others, etc.

Searching for a solution on the web, I found the Apache Jakarta project's port of GNU GetOpt, in the Jakarta Commons.  This seemed better, it allowed me to define "option groups".  However, I quickly came to the end of it's capabilities, and my code was becoming very verbose defining all of the options and option groups.  And it didn't seem to provide a mechanism for defining dependencies between options -- if you use A, then you also must use B.  Maybe there is a way, but whatever it may be, it is not obvious.

JCommando is the solution.  While originally designed just to cover my needs, JCommando took on a life of it's own and has evolved into the most comprehensive argument parser for Java available.  JCommando uses an "Inversion of Control" (IOC) pattern whereby your class is called back as each parameter is parsed, allowing you to set state or perform actions based on the command-line arguments.

There are various other projects available, including Jakarta commons CLI, and one called JSAP.  JSAP is an interesting alternative, but in my opinion leads to code that makes defining even simple options verbose, and complex options combinations impossible.  I think you'll find 10 minutes initial investment in JCommando over the alternatives will lead to hours saved in the long run.  The amount of code that JCommando requires you to write over and above the XML description file is extremely minimal -- far less than any other solution.  Enabling you to to keep your code clean and simple, and get on with the task of handling options, not parsing them.

Feature Highlights:     

  • XML specification of command-line option syntax
  • Option dependency support
  • Complex option groupings possible (And, Or, Xor, Not)
  • Range and type validation support
  • Automatic usage generation
  • Ant task or command-line execution for parser generation
Check out our tutorial for the quickest "get off the ground" experience.