Skip to content

Groovy CLIBuilder In Practice

Ok, it doesn’t get much easier than this to build a CLI:

class ContentUpdaterMain {

static void main(args) {

def cli = new CliBuilder(usage: 'java -jar contentupdater.jar -su[dh] "update name"')
cli.h(longOpt: 'help', 'usage information')
cli.u(longOpt: 'update', 'update the provided table', args: 1)
cli.s(longOpt: 'stage', 'stage the provided table', args: 1)
cli.d(longOpt: 'debug', 'run the process with debugging enabled')
def opt = cli.parse(args)
if(!opt) return

if(opt.h) cli.usage()

if( opt.d ) {
//turn on debug
}

if(opt.u) {
//do updates
}

if( opt.s ) {
// run against staging db
}
}
}

This produces the following usage output:

usage: java -jar contentupdater.jar -su[dh] "update name"
-d,--debug run the process with debugging enabled
-h,--help usage information
-s,--stage stage the provided table
-u,--update update the provided table

And all that remains is to validate the input and invoke the processes!

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

6 Comments

  1. You may make it a tiny bit groovier with using “with()”:

    cli.with {
    h longOpt: ‘…’, ‘…’
    u longOpt: ‘…’, ‘…’
    }

    Friday, July 24, 2009 at 3:37 pm | Permalink
  2. Jeff wrote:

    Thanks for the pointer Guillaume. By the way I’ve been reading about Gaelyk recently. I think it’s a very timely addition! I’m looking forward to writing about it here in the future.

    Sunday, July 26, 2009 at 2:36 pm | Permalink
  3. David Karr wrote:

    How do you allow for options that can appear more than once? This means that you have to be able to iterate through the provided options in some way.

    I’d have to say that the CliBuilder doc is less than informative.

    Friday, August 28, 2009 at 5:47 pm | Permalink
  4. Jeff wrote:

    Each option is of type org.apache.commons.cli.Option. This class contains an arg property, which allows you to set the maximum number of args a given property can accept. Take a look here:
    http://commons.apache.org/cli/api-1.2/org/apache/commons/cli/Option.html

    Monday, August 31, 2009 at 6:45 pm | Permalink
  5. Steve wrote:

    I have yet to find a way to handle the -foo=bar option. Does the CLIBuilder do this? If it does, where can I find an example of this syntax? right now I have written it myself using a for loop and making it in the format of -f bar…but I am sure there is a better way to do this.

    Wednesday, June 30, 2010 at 10:25 pm | Permalink
  6. Jeff wrote:

    Hey Steve,
    I think what you’re looking for is the args property.
    Check out the examples here: http://groovy.codehaus.org/gapi/groovy/util/CliBuilder.html

    In particular look at the example provided emulating the ant command line:

    cli.logfile(args:1, argName:’file’, ‘use given file for log’)
    cli.D(args:2, valueSeparator:’=', argName:’property=value’,
    ‘use value for given property’)

    Thursday, July 1, 2010 at 12:31 am | Permalink

One Trackback/Pingback

  1. REVERT TO CONSOLE › The Product of Scorn on Thursday, March 25, 2010 at 6:43 pm

    [...] are three problems, I chose the middle one to start with. I wrote it in groovy and used the very groovy clibuilder M. Laforge recommended in his comment. I mavenized it to make it easier to [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*