Friday, August 01, 2008

Groovy CliBuilder and Long Opts

After having trouble (ie, could not get to work) with longopts passed to a groovy script that was using CliBuilder to parse the arguments, I discovered a crucial tidbit on the Apache site:

Create the Parser

We now need to create a Parser. This will parse the command line arguments, using the rules specified by the Options and return an instance of CommandLine . This time we will use a GnuParser which is able to handle options that are more than one character long.

GnuParser is part of apache commons cli: org.apache.commons.cli.GnuParser
CliBuilder has a parser field, so all you need to do is pass an instance as a named argument to the CliBuilder constructor like so:

def cli = new CliBuilder(usage: USAGE, parser: new GnuParser())

No comments: